Base.php 5.3 KB

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