1
0

Base.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Files_Sharing\Activity\Providers;
  7. use OCP\Activity\Exceptions\UnknownActivityException;
  8. use OCP\Activity\IEvent;
  9. use OCP\Activity\IEventMerger;
  10. use OCP\Activity\IManager;
  11. use OCP\Activity\IProvider;
  12. use OCP\Contacts\IManager as IContactsManager;
  13. use OCP\Federation\ICloudIdManager;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserManager;
  17. use OCP\L10N\IFactory;
  18. abstract class Base implements IProvider {
  19. /** @var IL10N */
  20. protected $l;
  21. /** @var array */
  22. protected $displayNames = [];
  23. public function __construct(
  24. protected IFactory $languageFactory,
  25. protected IURLGenerator $url,
  26. protected IManager $activityManager,
  27. protected IUserManager $userManager,
  28. protected ICloudIdManager $cloudIdManager,
  29. protected IContactsManager $contactsManager,
  30. protected IEventMerger $eventMerger,
  31. ) {
  32. }
  33. /**
  34. * @param string $language
  35. * @param IEvent $event
  36. * @param IEvent|null $previousEvent
  37. * @return IEvent
  38. * @throws UnknownActivityException
  39. * @since 11.0.0
  40. */
  41. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  42. if ($event->getApp() !== 'files_sharing') {
  43. throw new UnknownActivityException();
  44. }
  45. $this->l = $this->languageFactory->get('files_sharing', $language);
  46. if ($this->activityManager->isFormattingFilteredObject()) {
  47. try {
  48. return $this->parseShortVersion($event);
  49. } catch (\InvalidArgumentException $e) {
  50. // Ignore and simply use the long version...
  51. }
  52. }
  53. return $this->parseLongVersion($event, $previousEvent);
  54. }
  55. /**
  56. * @param IEvent $event
  57. * @return IEvent
  58. * @throws \InvalidArgumentException
  59. * @since 11.0.0
  60. */
  61. abstract protected function parseShortVersion(IEvent $event);
  62. /**
  63. * @param IEvent $event
  64. * @param IEvent|null $previousEvent
  65. * @return IEvent
  66. * @throws \InvalidArgumentException
  67. * @since 11.0.0
  68. */
  69. abstract protected function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null);
  70. /**
  71. * @throws \InvalidArgumentException
  72. */
  73. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  74. $event->setRichSubject($subject, $parameters);
  75. }
  76. /**
  77. * @param array|string $parameter
  78. * @param IEvent|null $event
  79. * @return array
  80. * @throws \InvalidArgumentException
  81. */
  82. protected function getFile($parameter, ?IEvent $event = null) {
  83. if (is_array($parameter)) {
  84. $path = reset($parameter);
  85. $id = (string)key($parameter);
  86. } elseif ($event !== null) {
  87. $path = $parameter;
  88. $id = (string)$event->getObjectId();
  89. } else {
  90. throw new \InvalidArgumentException('Could not generate file parameter');
  91. }
  92. return [
  93. 'type' => 'file',
  94. 'id' => $id,
  95. 'name' => basename($path),
  96. 'path' => trim($path, '/'),
  97. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  98. ];
  99. }
  100. /**
  101. * @param string $uid
  102. * @param string $overwriteDisplayName - overwrite display name, only if user is not local
  103. *
  104. * @return array
  105. */
  106. protected function getUser(string $uid, string $overwriteDisplayName = '') {
  107. // First try local user
  108. $displayName = $this->userManager->getDisplayName($uid);
  109. if ($displayName !== null) {
  110. return [
  111. 'type' => 'user',
  112. 'id' => $uid,
  113. 'name' => $displayName,
  114. ];
  115. }
  116. // Then a contact from the addressbook
  117. if ($this->cloudIdManager->isValidCloudId($uid)) {
  118. $cloudId = $this->cloudIdManager->resolveCloudId($uid);
  119. return [
  120. 'type' => 'user',
  121. 'id' => $cloudId->getUser(),
  122. 'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $this->getDisplayNameFromAddressBook($cloudId->getDisplayId())),
  123. 'server' => $cloudId->getRemote(),
  124. ];
  125. }
  126. // Fallback to empty dummy data
  127. return [
  128. 'type' => 'user',
  129. 'id' => $uid,
  130. 'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $uid),
  131. ];
  132. }
  133. protected function getDisplayNameFromAddressBook(string $search): string {
  134. if (isset($this->displayNames[$search])) {
  135. return $this->displayNames[$search];
  136. }
  137. $addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
  138. 'limit' => 1,
  139. 'enumeration' => false,
  140. 'fullmatch' => false,
  141. 'strict_search' => true,
  142. ]);
  143. foreach ($addressBookContacts as $contact) {
  144. if (isset($contact['isLocalSystemBook'])) {
  145. continue;
  146. }
  147. if (isset($contact['CLOUD'])) {
  148. $cloudIds = $contact['CLOUD'];
  149. if (is_string($cloudIds)) {
  150. $cloudIds = [$cloudIds];
  151. }
  152. $lowerSearch = strtolower($search);
  153. foreach ($cloudIds as $cloudId) {
  154. if (strtolower($cloudId) === $lowerSearch) {
  155. $this->displayNames[$search] = $contact['FN'] . " ($cloudId)";
  156. return $this->displayNames[$search];
  157. }
  158. }
  159. }
  160. }
  161. return $search;
  162. }
  163. }