Manager.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Contacts\ContactsMenu;
  7. use Exception;
  8. use OCP\App\IAppManager;
  9. use OCP\Constants;
  10. use OCP\Contacts\ContactsMenu\IBulkProvider;
  11. use OCP\Contacts\ContactsMenu\IEntry;
  12. use OCP\Contacts\ContactsMenu\IProvider;
  13. use OCP\IConfig;
  14. use OCP\IUser;
  15. class Manager {
  16. public function __construct(
  17. private ContactsStore $store,
  18. private ActionProviderStore $actionProviderStore,
  19. private IAppManager $appManager,
  20. private IConfig $config,
  21. ) {
  22. }
  23. /**
  24. * @throws Exception
  25. */
  26. public function getEntries(IUser $user, ?string $filter): array {
  27. $maxAutocompleteResults = max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT));
  28. $minSearchStringLength = $this->config->getSystemValueInt('sharing.minSearchStringLength');
  29. $topEntries = [];
  30. if (strlen($filter ?? '') >= $minSearchStringLength) {
  31. $entries = $this->store->getContacts($user, $filter, $maxAutocompleteResults);
  32. $sortedEntries = $this->sortEntries($entries);
  33. $topEntries = array_slice($sortedEntries, 0, $maxAutocompleteResults);
  34. $this->processEntries($topEntries, $user);
  35. }
  36. $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user);
  37. return [
  38. 'contacts' => $topEntries,
  39. 'contactsAppEnabled' => $contactsEnabled,
  40. ];
  41. }
  42. /**
  43. * @throws Exception
  44. */
  45. public function findOne(IUser $user, int $shareType, string $shareWith): ?IEntry {
  46. $entry = $this->store->findOne($user, $shareType, $shareWith);
  47. if ($entry) {
  48. $this->processEntries([$entry], $user);
  49. }
  50. return $entry;
  51. }
  52. /**
  53. * @param IEntry[] $entries
  54. * @return IEntry[]
  55. */
  56. private function sortEntries(array $entries): array {
  57. usort($entries, function (Entry $entryA, Entry $entryB) {
  58. $aStatusTimestamp = $entryA->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
  59. $bStatusTimestamp = $entryB->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
  60. if (!$aStatusTimestamp && !$bStatusTimestamp) {
  61. return strcasecmp($entryA->getFullName(), $entryB->getFullName());
  62. }
  63. if ($aStatusTimestamp === null) {
  64. return 1;
  65. }
  66. if ($bStatusTimestamp === null) {
  67. return -1;
  68. }
  69. return $bStatusTimestamp - $aStatusTimestamp;
  70. });
  71. return $entries;
  72. }
  73. /**
  74. * @param IEntry[] $entries
  75. * @throws Exception
  76. */
  77. private function processEntries(array $entries, IUser $user): void {
  78. $providers = $this->actionProviderStore->getProviders($user);
  79. foreach ($providers as $provider) {
  80. if ($provider instanceof IBulkProvider && !($provider instanceof IProvider)) {
  81. $provider->process($entries);
  82. } elseif ($provider instanceof IProvider && !($provider instanceof IBulkProvider)) {
  83. foreach ($entries as $entry) {
  84. $provider->process($entry);
  85. }
  86. }
  87. }
  88. }
  89. }