1
0

RecentContactMapper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\ContactsInteraction\Db;
  26. use OCP\AppFramework\Db\DoesNotExistException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\IDBConnection;
  29. use OCP\IUser;
  30. /**
  31. * @template-extends QBMapper<RecentContact>
  32. */
  33. class RecentContactMapper extends QBMapper {
  34. public const TABLE_NAME = 'recent_contact';
  35. public function __construct(IDBConnection $db) {
  36. parent::__construct($db, self::TABLE_NAME);
  37. }
  38. /**
  39. * @return RecentContact[]
  40. */
  41. public function findAll(string $uid): array {
  42. $qb = $this->db->getQueryBuilder();
  43. $select = $qb
  44. ->select('*')
  45. ->from($this->getTableName())
  46. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  47. return $this->findEntities($select);
  48. }
  49. /**
  50. * @throws DoesNotExistException
  51. */
  52. public function find(string $uid, int $id): RecentContact {
  53. $qb = $this->db->getQueryBuilder();
  54. $select = $qb
  55. ->select('*')
  56. ->from($this->getTableName())
  57. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT)))
  58. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  59. return $this->findEntity($select);
  60. }
  61. /**
  62. * @return RecentContact[]
  63. */
  64. public function findMatch(IUser $user,
  65. ?string $uid,
  66. ?string $email,
  67. ?string $cloudId): array {
  68. $qb = $this->db->getQueryBuilder();
  69. $or = $qb->expr()->orX();
  70. if ($uid !== null) {
  71. $or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  72. }
  73. if ($email !== null) {
  74. $or->add($qb->expr()->eq('email', $qb->createNamedParameter($email)));
  75. }
  76. if ($cloudId !== null) {
  77. $or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId)));
  78. }
  79. $select = $qb
  80. ->select('*')
  81. ->from($this->getTableName())
  82. ->where($or)
  83. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
  84. return $this->findEntities($select);
  85. }
  86. public function findLastUpdatedForUserId(string $uid): ?int {
  87. $qb = $this->db->getQueryBuilder();
  88. $select = $qb
  89. ->select('last_contact')
  90. ->from($this->getTableName())
  91. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)))
  92. ->orderBy('last_contact', 'DESC')
  93. ->setMaxResults(1);
  94. $cursor = $select->executeQuery();
  95. $row = $cursor->fetch();
  96. if ($row === false) {
  97. return null;
  98. }
  99. return (int)$row['last_contact'];
  100. }
  101. public function cleanUp(int $olderThan): void {
  102. $qb = $this->db->getQueryBuilder();
  103. $delete = $qb
  104. ->delete($this->getTableName())
  105. ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
  106. $delete->executeStatement();
  107. }
  108. }