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.

RecentContactMapper.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * @param string $uid
  51. * @param int $id
  52. *
  53. * @return RecentContact
  54. * @throws DoesNotExistException
  55. */
  56. public function find(string $uid, int $id): RecentContact {
  57. $qb = $this->db->getQueryBuilder();
  58. $select = $qb
  59. ->select('*')
  60. ->from($this->getTableName())
  61. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT)))
  62. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  63. return $this->findEntity($select);
  64. }
  65. /**
  66. * @param IUser $user
  67. * @param string|null $uid
  68. * @param string|null $email
  69. * @param string|null $cloudId
  70. *
  71. * @return RecentContact[]
  72. */
  73. public function findMatch(IUser $user,
  74. ?string $uid,
  75. ?string $email,
  76. ?string $cloudId): array {
  77. $qb = $this->db->getQueryBuilder();
  78. $or = $qb->expr()->orX();
  79. if ($uid !== null) {
  80. $or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  81. }
  82. if ($email !== null) {
  83. $or->add($qb->expr()->eq('email', $qb->createNamedParameter($email)));
  84. }
  85. if ($cloudId !== null) {
  86. $or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId)));
  87. }
  88. $select = $qb
  89. ->select('*')
  90. ->from($this->getTableName())
  91. ->where($or)
  92. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
  93. return $this->findEntities($select);
  94. }
  95. /**
  96. * @param string $uid
  97. * @return int|null
  98. */
  99. public function findLastUpdatedForUserId(string $uid):?int {
  100. $qb = $this->db->getQueryBuilder();
  101. $select = $qb
  102. ->select('last_contact')
  103. ->from($this->getTableName())
  104. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)))
  105. ->orderBy('last_contact', 'DESC')
  106. ->setMaxResults(1);
  107. $cursor = $select->execute();
  108. $row = $cursor->fetch();
  109. if ($row === false) {
  110. return null;
  111. }
  112. return (int)$row['last_contact'];
  113. }
  114. public function cleanUp(int $olderThan): void {
  115. $qb = $this->db->getQueryBuilder();
  116. $delete = $qb
  117. ->delete($this->getTableName())
  118. ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
  119. $delete->execute();
  120. }
  121. }