RecentContactMapper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. $or = $qb->expr()->orX();
  52. if ($uid !== null) {
  53. $or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  54. }
  55. if ($email !== null) {
  56. $or->add($qb->expr()->eq('email', $qb->createNamedParameter($email)));
  57. }
  58. if ($cloudId !== null) {
  59. $or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId)));
  60. }
  61. $select = $qb
  62. ->select('*')
  63. ->from($this->getTableName())
  64. ->where($or)
  65. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
  66. return $this->findEntities($select);
  67. }
  68. public function findLastUpdatedForUserId(string $uid): ?int {
  69. $qb = $this->db->getQueryBuilder();
  70. $select = $qb
  71. ->select('last_contact')
  72. ->from($this->getTableName())
  73. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)))
  74. ->orderBy('last_contact', 'DESC')
  75. ->setMaxResults(1);
  76. $cursor = $select->executeQuery();
  77. $row = $cursor->fetch();
  78. if ($row === false) {
  79. return null;
  80. }
  81. return (int)$row['last_contact'];
  82. }
  83. public function cleanUp(int $olderThan): void {
  84. $qb = $this->db->getQueryBuilder();
  85. $delete = $qb
  86. ->delete($this->getTableName())
  87. ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
  88. $delete->executeStatement();
  89. }
  90. }