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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. class RecentContactMapper extends QBMapper {
  29. public const TABLE_NAME = 'recent_contact';
  30. public function __construct(IDBConnection $db) {
  31. parent::__construct($db, self::TABLE_NAME);
  32. }
  33. /**
  34. * @return RecentContact[]
  35. */
  36. public function findAll(string $uid): array {
  37. $qb = $this->db->getQueryBuilder();
  38. $select = $qb
  39. ->select('*')
  40. ->from($this->getTableName())
  41. ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  42. return $this->findEntities($select);
  43. }
  44. /**
  45. * @param string $uid
  46. * @param int $id
  47. *
  48. * @return RecentContact
  49. * @throws DoesNotExistException
  50. */
  51. public function find(string $uid, int $id): RecentContact {
  52. $qb = $this->db->getQueryBuilder();
  53. $select = $qb
  54. ->select('*')
  55. ->from($this->getTableName())
  56. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT)))
  57. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid)));
  58. return $this->findEntity($select);
  59. }
  60. /**
  61. * @param IUser $user
  62. * @param string|null $uid
  63. * @param string|null $email
  64. * @param string|null $cloudId
  65. *
  66. * @return RecentContact[]
  67. */
  68. public function findMatch(IUser $user,
  69. ?string $uid,
  70. ?string $email,
  71. ?string $cloudId): array {
  72. $qb = $this->db->getQueryBuilder();
  73. $or = $qb->expr()->orX();
  74. if ($uid !== null) {
  75. $or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid)));
  76. }
  77. if ($email !== null) {
  78. $or->add($qb->expr()->eq('email', $qb->createNamedParameter($email)));
  79. }
  80. if ($cloudId !== null) {
  81. $or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId)));
  82. }
  83. $select = $qb
  84. ->select('*')
  85. ->from($this->getTableName())
  86. ->where($or)
  87. ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID())));
  88. return $this->findEntities($select);
  89. }
  90. public function cleanUp(int $olderThan): void {
  91. $qb = $this->db->getQueryBuilder();
  92. $delete = $qb
  93. ->delete($this->getTableName())
  94. ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan)));
  95. $delete->execute();
  96. }
  97. }