RecentContactMapper.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\ContactsInteraction\Db;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\QBMapper;
  10. use OCP\IDBConnection;
  11. use OCP\IUser;
  12. /**
  13. * @template-extends QBMapper<RecentContact>
  14. */
  15. class RecentContactMapper extends QBMapper {
  16. public const TABLE_NAME = 'recent_contact';
  17. public function __construct(IDBConnection $db) {
  18. parent::__construct($db, self::TABLE_NAME);
  19. }
  20. /**
  21. * @return RecentContact[]
  22. */
  23. public function findAll(string $uid): array {
  24. $qb = $this->db->getQueryBuilder();
  25. $select = $qb
  26. ->select('*')
  27. ->from($this->getTableName())
  28. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  29. return $this->findEntities($select);
  30. }
  31. /**
  32. * @throws DoesNotExistException
  33. */
  34. public function find(string $uid, int $id): RecentContact {
  35. $qb = $this->db->getQueryBuilder();
  36. $select = $qb
  37. ->select('*')
  38. ->from($this->getTableName())
  39. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT)))
  40. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  41. return $this->findEntity($select);
  42. }
  43. /**
  44. * @return RecentContact[]
  45. */
  46. public function findMatch(IUser $user,
  47. ?string $uid,
  48. ?string $email,
  49. ?string $cloudId): array {
  50. $qb = $this->db->getQueryBuilder();
  51. $additionalWheres = [];
  52. if ($uid !== null) {
  53. $additionalWheres[] = $qb->expr()->eq('uid', $qb->createNamedParameter($uid));
  54. }
  55. if ($email !== null) {
  56. $additionalWheres[] = $qb->expr()->eq('email', $qb->createNamedParameter($email));
  57. }
  58. if ($cloudId !== null) {
  59. $additionalWheres[] = $qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId));
  60. }
  61. $select = $qb
  62. ->select('*')
  63. ->from($this->getTableName())
  64. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
  65. if (!empty($additionalWheres)) {
  66. $select->andWhere($select->expr()->orX(...$additionalWheres));
  67. }
  68. return $this->findEntities($select);
  69. }
  70. public function findLastUpdatedForUserId(string $uid): ?int {
  71. $qb = $this->db->getQueryBuilder();
  72. $select = $qb
  73. ->select('last_contact')
  74. ->from($this->getTableName())
  75. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)))
  76. ->orderBy('last_contact', 'DESC')
  77. ->setMaxResults(1);
  78. $cursor = $select->executeQuery();
  79. $row = $cursor->fetch();
  80. if ($row === false) {
  81. return null;
  82. }
  83. return (int)$row['last_contact'];
  84. }
  85. public function cleanUp(int $olderThan): void {
  86. $qb = $this->db->getQueryBuilder();
  87. $delete = $qb
  88. ->delete($this->getTableName())
  89. ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
  90. $delete->executeStatement();
  91. }
  92. }