1
0

KnownUserService.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\KnownUser;
  8. class KnownUserService {
  9. /** @var KnownUserMapper */
  10. protected $mapper;
  11. /** @var array */
  12. protected $knownUsers = [];
  13. public function __construct(KnownUserMapper $mapper) {
  14. $this->mapper = $mapper;
  15. }
  16. /**
  17. * Delete all matches where the given user is the owner of the phonebook
  18. *
  19. * @param string $knownTo
  20. * @return int Number of deleted matches
  21. */
  22. public function deleteKnownTo(string $knownTo): int {
  23. return $this->mapper->deleteKnownTo($knownTo);
  24. }
  25. /**
  26. * Delete all matches where the given user is the one in the phonebook
  27. *
  28. * @param string $contactUserId
  29. * @return int Number of deleted matches
  30. */
  31. public function deleteByContactUserId(string $contactUserId): int {
  32. return $this->mapper->deleteKnownUser($contactUserId);
  33. }
  34. /**
  35. * Store a match because $knownTo has $contactUserId in their phonebook
  36. *
  37. * @param string $knownTo User id of the owner of the phonebook
  38. * @param string $contactUserId User id of the contact in the phonebook
  39. */
  40. public function storeIsKnownToUser(string $knownTo, string $contactUserId): void {
  41. $entity = new KnownUser();
  42. $entity->setKnownTo($knownTo);
  43. $entity->setKnownUser($contactUserId);
  44. $this->mapper->insert($entity);
  45. }
  46. /**
  47. * Check if $contactUserId is in the phonebook of $knownTo
  48. *
  49. * @param string $knownTo User id of the owner of the phonebook
  50. * @param string $contactUserId User id of the contact in the phonebook
  51. * @return bool
  52. */
  53. public function isKnownToUser(string $knownTo, string $contactUserId): bool {
  54. if ($knownTo === $contactUserId) {
  55. return true;
  56. }
  57. if (!isset($this->knownUsers[$knownTo])) {
  58. $entities = $this->mapper->getKnownUsers($knownTo);
  59. $this->knownUsers[$knownTo] = [];
  60. foreach ($entities as $entity) {
  61. $this->knownUsers[$knownTo][$entity->getKnownUser()] = true;
  62. }
  63. }
  64. return isset($this->knownUsers[$knownTo][$contactUserId]);
  65. }
  66. }