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.

143 lines
3.7 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  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. namespace OCA\ContactsInteraction\Db;
  24. use OCP\AppFramework\Db\DoesNotExistException;
  25. use OCP\AppFramework\Db\QBMapper;
  26. use OCP\IDBConnection;
  27. use OCP\IUser;
  28. /**
  29. * @template-extends QBMapper<RecentContact>
  30. */
  31. class RecentContactMapper extends QBMapper {
  32. public const TABLE_NAME = 'recent_contact';
  33. public function __construct(IDBConnection $db) {
  34. parent::__construct($db, self::TABLE_NAME);
  35. }
  36. /**
  37. * @return RecentContact[]
  38. */
  39. public function findAll(string $uid): array {
  40. $qb = $this->db->getQueryBuilder();
  41. $select = $qb
  42. ->select('*')
  43. ->from($this->getTableName())
  44. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  45. return $this->findEntities($select);
  46. }
  47. /**
  48. * @param string $uid
  49. * @param int $id
  50. *
  51. * @return RecentContact
  52. * @throws DoesNotExistException
  53. */
  54. public function find(string $uid, int $id): RecentContact {
  55. $qb = $this->db->getQueryBuilder();
  56. $select = $qb
  57. ->select('*')
  58. ->from($this->getTableName())
  59. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT)))
  60. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  61. return $this->findEntity($select);
  62. }
  63. /**
  64. * @param IUser $user
  65. * @param string|null $uid
  66. * @param string|null $email
  67. * @param string|null $cloudId
  68. *
  69. * @return RecentContact[]
  70. */
  71. public function findMatch(IUser $user,
  72. ?string $uid,
  73. ?string $email,
  74. ?string $cloudId): array {
  75. $qb = $this->db->getQueryBuilder();
  76. $or = $qb->expr()->orX();
  77. if ($uid !== null) {
  78. $or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  79. }
  80. if ($email !== null) {
  81. $or->add($qb->expr()->eq('email', $qb->createNamedParameter($email)));
  82. }
  83. if ($cloudId !== null) {
  84. $or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId)));
  85. }
  86. $select = $qb
  87. ->select('*')
  88. ->from($this->getTableName())
  89. ->where($or)
  90. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
  91. return $this->findEntities($select);
  92. }
  93. /**
  94. * @param string $uid
  95. * @return int|null
  96. */
  97. public function findLastUpdatedForUserId(string $uid):?int {
  98. $qb = $this->db->getQueryBuilder();
  99. $select = $qb
  100. ->select('last_contact')
  101. ->from($this->getTableName())
  102. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)))
  103. ->orderBy('last_contact', 'DESC')
  104. ->setMaxResults(1);
  105. $cursor = $select->execute();
  106. $row = $cursor->fetch();
  107. if ($row === false) {
  108. return null;
  109. }
  110. return (int)$row['last_contact'];
  111. }
  112. public function cleanUp(int $olderThan): void {
  113. $qb = $this->db->getQueryBuilder();
  114. $delete = $qb
  115. ->delete($this->getTableName())
  116. ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
  117. $delete->execute();
  118. }
  119. }