You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.8 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Gary Kim <gary@garykim.dev>
  5. *
  6. * @author Gary Kim <gary@garykim.dev>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Talk\Model;
  25. use OCA\Talk\Room;
  26. use OCP\AppFramework\Db\DoesNotExistException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\IDBConnection;
  30. use OCP\IUser;
  31. use SensitiveParameter;
  32. /**
  33. * Class InvitationMapper
  34. *
  35. * @package OCA\Talk\Model
  36. *
  37. * @method Invitation mapRowToEntity(array $row)
  38. * @method Invitation findEntity(IQueryBuilder $query)
  39. * @method Invitation[] findEntities(IQueryBuilder $query)
  40. * @template-extends QBMapper<Invitation>
  41. */
  42. class InvitationMapper extends QBMapper {
  43. public function __construct(IDBConnection $db) {
  44. parent::__construct($db, 'talk_invitations', Invitation::class);
  45. }
  46. /**
  47. * @throws DoesNotExistException
  48. */
  49. public function getInvitationById(int $id): Invitation {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb->select('*')
  52. ->from($this->getTableName())
  53. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  54. return $this->findEntity($qb);
  55. }
  56. /**
  57. * @throws DoesNotExistException
  58. */
  59. public function getByRemoteAndAccessToken(
  60. string $remoteServerUrl,
  61. int $remoteAttendeeId,
  62. #[SensitiveParameter]
  63. string $accessToken,
  64. ): Invitation {
  65. $qb = $this->db->getQueryBuilder();
  66. $qb->select('*')
  67. ->from($this->getTableName())
  68. ->where($qb->expr()->eq('remote_server_url', $qb->createNamedParameter($remoteServerUrl)))
  69. ->andWhere($qb->expr()->eq('remote_attendee_id', $qb->createNamedParameter($remoteAttendeeId, IQueryBuilder::PARAM_INT)))
  70. ->andWhere($qb->expr()->eq('access_token', $qb->createNamedParameter($accessToken)));
  71. return $this->findEntity($qb);
  72. }
  73. /**
  74. * @param IUser $user
  75. * @return Invitation[]
  76. */
  77. public function getInvitationsForUser(IUser $user): array {
  78. $qb = $this->db->getQueryBuilder();
  79. $qb->select('*')
  80. ->from($this->getTableName())
  81. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($user->getUID())));
  82. return $this->findEntities($qb);
  83. }
  84. /**
  85. * @throws DoesNotExistException
  86. */
  87. public function getInvitationForUserByLocalRoom(Room $room, string $userId, bool $caseInsensitive = false): Invitation {
  88. $query = $this->db->getQueryBuilder();
  89. $query->select('*')
  90. ->from($this->getTableName())
  91. ->where($query->expr()->eq('local_room_id', $query->createNamedParameter($room->getId())));
  92. if ($caseInsensitive) {
  93. $query->andWhere($query->expr()->eq($query->func()->lower('user_id'), $query->createNamedParameter(strtolower($userId))));
  94. } else {
  95. $query->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
  96. }
  97. return $this->findEntity($query);
  98. }
  99. public function countInvitationsForLocalRoom(Room $room): int {
  100. $qb = $this->db->getQueryBuilder();
  101. $qb->select($qb->func()->count('*', 'num_invitations'))
  102. ->from($this->getTableName())
  103. ->where($qb->expr()->eq('local_room_id', $qb->createNamedParameter($room->getId())));
  104. $result = $qb->executeQuery();
  105. $row = $result->fetch();
  106. $result->closeCursor();
  107. return (int) ($row['num_invitations'] ?? 0);
  108. }
  109. }