Manager.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Contacts\ContactsMenu;
  27. use Exception;
  28. use OCP\App\IAppManager;
  29. use OCP\Constants;
  30. use OCP\Contacts\ContactsMenu\IBulkProvider;
  31. use OCP\Contacts\ContactsMenu\IEntry;
  32. use OCP\Contacts\ContactsMenu\IProvider;
  33. use OCP\IConfig;
  34. use OCP\IUser;
  35. class Manager {
  36. public function __construct(
  37. private ContactsStore $store,
  38. private ActionProviderStore $actionProviderStore,
  39. private IAppManager $appManager,
  40. private IConfig $config,
  41. ) {
  42. }
  43. /**
  44. * @throws Exception
  45. */
  46. public function getEntries(IUser $user, ?string $filter): array {
  47. $maxAutocompleteResults = max(0, $this->config->getSystemValueInt('sharing.maxAutocompleteResults', Constants::SHARING_MAX_AUTOCOMPLETE_RESULTS_DEFAULT));
  48. $minSearchStringLength = $this->config->getSystemValueInt('sharing.minSearchStringLength');
  49. $topEntries = [];
  50. if (strlen($filter ?? '') >= $minSearchStringLength) {
  51. $entries = $this->store->getContacts($user, $filter, $maxAutocompleteResults);
  52. $sortedEntries = $this->sortEntries($entries);
  53. $topEntries = array_slice($sortedEntries, 0, $maxAutocompleteResults);
  54. $this->processEntries($topEntries, $user);
  55. }
  56. $contactsEnabled = $this->appManager->isEnabledForUser('contacts', $user);
  57. return [
  58. 'contacts' => $topEntries,
  59. 'contactsAppEnabled' => $contactsEnabled,
  60. ];
  61. }
  62. /**
  63. * @throws Exception
  64. */
  65. public function findOne(IUser $user, int $shareType, string $shareWith): ?IEntry {
  66. $entry = $this->store->findOne($user, $shareType, $shareWith);
  67. if ($entry) {
  68. $this->processEntries([$entry], $user);
  69. }
  70. return $entry;
  71. }
  72. /**
  73. * @param IEntry[] $entries
  74. * @return IEntry[]
  75. */
  76. private function sortEntries(array $entries): array {
  77. usort($entries, function (Entry $entryA, Entry $entryB) {
  78. $aStatusTimestamp = $entryA->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
  79. $bStatusTimestamp = $entryB->getProperty(Entry::PROPERTY_STATUS_MESSAGE_TIMESTAMP);
  80. if (!$aStatusTimestamp && !$bStatusTimestamp) {
  81. return strcasecmp($entryA->getFullName(), $entryB->getFullName());
  82. }
  83. if ($aStatusTimestamp === null) {
  84. return 1;
  85. }
  86. if ($bStatusTimestamp === null) {
  87. return -1;
  88. }
  89. return $bStatusTimestamp - $aStatusTimestamp;
  90. });
  91. return $entries;
  92. }
  93. /**
  94. * @param IEntry[] $entries
  95. * @throws Exception
  96. */
  97. private function processEntries(array $entries, IUser $user): void {
  98. $providers = $this->actionProviderStore->getProviders($user);
  99. foreach ($providers as $provider) {
  100. if ($provider instanceof IBulkProvider && !($provider instanceof IProvider)) {
  101. $provider->process($entries);
  102. } elseif ($provider instanceof IProvider && !($provider instanceof IBulkProvider)) {
  103. foreach ($entries as $entry) {
  104. $provider->process($entry);
  105. }
  106. }
  107. }
  108. }
  109. }