* * @author Gary Kim * * @license GNU AGPL version 3 or any later version * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . * */ namespace OCA\Talk\Model; use OCA\Talk\Room; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\IUser; use SensitiveParameter; /** * Class InvitationMapper * * @package OCA\Talk\Model * * @method Invitation mapRowToEntity(array $row) * @method Invitation findEntity(IQueryBuilder $query) * @method Invitation[] findEntities(IQueryBuilder $query) * @template-extends QBMapper */ class InvitationMapper extends QBMapper { public function __construct(IDBConnection $db) { parent::__construct($db, 'talk_invitations', Invitation::class); } /** * @throws DoesNotExistException */ public function getInvitationById(int $id): Invitation { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); return $this->findEntity($qb); } /** * @throws DoesNotExistException */ public function getByRemoteAndAccessToken( string $remoteServerUrl, int $remoteAttendeeId, #[SensitiveParameter] string $accessToken, ): Invitation { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('remote_server_url', $qb->createNamedParameter($remoteServerUrl))) ->andWhere($qb->expr()->eq('remote_attendee_id', $qb->createNamedParameter($remoteAttendeeId, IQueryBuilder::PARAM_INT))) ->andWhere($qb->expr()->eq('access_token', $qb->createNamedParameter($accessToken))); return $this->findEntity($qb); } /** * @param IUser $user * @return Invitation[] */ public function getInvitationsForUser(IUser $user): array { $qb = $this->db->getQueryBuilder(); $qb->select('*') ->from($this->getTableName()) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID()))); return $this->findEntities($qb); } /** * @throws DoesNotExistException */ public function getInvitationForUserByLocalRoom(Room $room, string $userId): Invitation { $query = $this->db->getQueryBuilder(); $query->select('*') ->from($this->getTableName()) ->where($query->expr()->eq('user_id', $query->createNamedParameter($userId))) ->andWhere($query->expr()->eq('local_room_id', $query->createNamedParameter($room->getId()))); return $this->findEntity($query); } public function countInvitationsForLocalRoom(Room $room): int { $qb = $this->db->getQueryBuilder(); $qb->select($qb->func()->count('*', 'num_invitations')) ->from($this->getTableName()) ->where($qb->expr()->eq('local_room_id', $qb->createNamedParameter($room->getId()))); $result = $qb->executeQuery(); $row = $result->fetch(); $result->closeCursor(); return (int) ($row['num_invitations'] ?? 0); } }