KnownUserService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Vincent Petry <vincent@nextcloud.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 OC\KnownUser;
  26. class KnownUserService {
  27. /** @var KnownUserMapper */
  28. protected $mapper;
  29. /** @var array */
  30. protected $knownUsers = [];
  31. public function __construct(KnownUserMapper $mapper) {
  32. $this->mapper = $mapper;
  33. }
  34. /**
  35. * Delete all matches where the given user is the owner of the phonebook
  36. *
  37. * @param string $knownTo
  38. * @return int Number of deleted matches
  39. */
  40. public function deleteKnownTo(string $knownTo): int {
  41. return $this->mapper->deleteKnownTo($knownTo);
  42. }
  43. /**
  44. * Delete all matches where the given user is the one in the phonebook
  45. *
  46. * @param string $contactUserId
  47. * @return int Number of deleted matches
  48. */
  49. public function deleteByContactUserId(string $contactUserId): int {
  50. return $this->mapper->deleteKnownUser($contactUserId);
  51. }
  52. /**
  53. * Store a match because $knownTo has $contactUserId in his phonebook
  54. *
  55. * @param string $knownTo User id of the owner of the phonebook
  56. * @param string $contactUserId User id of the contact in the phonebook
  57. */
  58. public function storeIsKnownToUser(string $knownTo, string $contactUserId): void {
  59. $entity = new KnownUser();
  60. $entity->setKnownTo($knownTo);
  61. $entity->setKnownUser($contactUserId);
  62. $this->mapper->insert($entity);
  63. }
  64. /**
  65. * Check if $contactUserId is in the phonebook of $knownTo
  66. *
  67. * @param string $knownTo User id of the owner of the phonebook
  68. * @param string $contactUserId User id of the contact in the phonebook
  69. * @return bool
  70. */
  71. public function isKnownToUser(string $knownTo, string $contactUserId): bool {
  72. if ($knownTo === $contactUserId) {
  73. return true;
  74. }
  75. if (!isset($this->knownUsers[$knownTo])) {
  76. $entities = $this->mapper->getKnownUsers($knownTo);
  77. $this->knownUsers[$knownTo] = [];
  78. foreach ($entities as $entity) {
  79. $this->knownUsers[$knownTo][$entity->getKnownUser()] = true;
  80. }
  81. }
  82. return isset($this->knownUsers[$knownTo][$contactUserId]);
  83. }
  84. }