Base.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files_Sharing\Activity\Providers;
  25. use OCP\Activity\IEvent;
  26. use OCP\Activity\IEventMerger;
  27. use OCP\Activity\IManager;
  28. use OCP\Activity\IProvider;
  29. use OCP\Contacts\IManager as IContactsManager;
  30. use OCP\Federation\ICloudIdManager;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUserManager;
  34. use OCP\L10N\IFactory;
  35. abstract class Base implements IProvider {
  36. /** @var IFactory */
  37. protected $languageFactory;
  38. /** @var IL10N */
  39. protected $l;
  40. /** @var IURLGenerator */
  41. protected $url;
  42. /** @var IManager */
  43. protected $activityManager;
  44. /** @var IUserManager */
  45. protected $userManager;
  46. /** @var IEventMerger */
  47. protected $eventMerger;
  48. /** @var IContactsManager */
  49. protected $contactsManager;
  50. /** @var ICloudIdManager */
  51. protected $cloudIdManager;
  52. /** @var array */
  53. protected $displayNames = [];
  54. public function __construct(IFactory $languageFactory,
  55. IURLGenerator $url,
  56. IManager $activityManager,
  57. IUserManager $userManager,
  58. ICloudIdManager $cloudIdManager,
  59. IContactsManager $contactsManager,
  60. IEventMerger $eventMerger) {
  61. $this->languageFactory = $languageFactory;
  62. $this->url = $url;
  63. $this->activityManager = $activityManager;
  64. $this->userManager = $userManager;
  65. $this->cloudIdManager = $cloudIdManager;
  66. $this->contactsManager = $contactsManager;
  67. $this->eventMerger = $eventMerger;
  68. }
  69. /**
  70. * @param string $language
  71. * @param IEvent $event
  72. * @param IEvent|null $previousEvent
  73. * @return IEvent
  74. * @throws \InvalidArgumentException
  75. * @since 11.0.0
  76. */
  77. public function parse($language, IEvent $event, ?IEvent $previousEvent = null) {
  78. if ($event->getApp() !== 'files_sharing') {
  79. throw new \InvalidArgumentException();
  80. }
  81. $this->l = $this->languageFactory->get('files_sharing', $language);
  82. if ($this->activityManager->isFormattingFilteredObject()) {
  83. try {
  84. return $this->parseShortVersion($event);
  85. } catch (\InvalidArgumentException $e) {
  86. // Ignore and simply use the long version...
  87. }
  88. }
  89. return $this->parseLongVersion($event, $previousEvent);
  90. }
  91. /**
  92. * @param IEvent $event
  93. * @return IEvent
  94. * @throws \InvalidArgumentException
  95. * @since 11.0.0
  96. */
  97. abstract protected function parseShortVersion(IEvent $event);
  98. /**
  99. * @param IEvent $event
  100. * @param IEvent|null $previousEvent
  101. * @return IEvent
  102. * @throws \InvalidArgumentException
  103. * @since 11.0.0
  104. */
  105. abstract protected function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null);
  106. /**
  107. * @throws \InvalidArgumentException
  108. */
  109. protected function setSubjects(IEvent $event, string $subject, array $parameters): void {
  110. $event->setRichSubject($subject, $parameters);
  111. }
  112. /**
  113. * @param array|string $parameter
  114. * @param IEvent|null $event
  115. * @return array
  116. * @throws \InvalidArgumentException
  117. */
  118. protected function getFile($parameter, ?IEvent $event = null) {
  119. if (is_array($parameter)) {
  120. $path = reset($parameter);
  121. $id = (string) key($parameter);
  122. } elseif ($event !== null) {
  123. $path = $parameter;
  124. $id = (string)$event->getObjectId();
  125. } else {
  126. throw new \InvalidArgumentException('Could not generate file parameter');
  127. }
  128. return [
  129. 'type' => 'file',
  130. 'id' => $id,
  131. 'name' => basename($path),
  132. 'path' => trim($path, '/'),
  133. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  134. ];
  135. }
  136. /**
  137. * @param string $uid
  138. * @param string $overwriteDisplayName - overwrite display name, only if user is not local
  139. *
  140. * @return array
  141. */
  142. protected function getUser(string $uid, string $overwriteDisplayName = '') {
  143. // First try local user
  144. $displayName = $this->userManager->getDisplayName($uid);
  145. if ($displayName !== null) {
  146. return [
  147. 'type' => 'user',
  148. 'id' => $uid,
  149. 'name' => $displayName,
  150. ];
  151. }
  152. // Then a contact from the addressbook
  153. if ($this->cloudIdManager->isValidCloudId($uid)) {
  154. $cloudId = $this->cloudIdManager->resolveCloudId($uid);
  155. return [
  156. 'type' => 'user',
  157. 'id' => $cloudId->getUser(),
  158. 'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $this->getDisplayNameFromAddressBook($cloudId->getDisplayId())),
  159. 'server' => $cloudId->getRemote(),
  160. ];
  161. }
  162. // Fallback to empty dummy data
  163. return [
  164. 'type' => 'user',
  165. 'id' => $uid,
  166. 'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $uid),
  167. ];
  168. }
  169. protected function getDisplayNameFromAddressBook(string $search): string {
  170. if (isset($this->displayNames[$search])) {
  171. return $this->displayNames[$search];
  172. }
  173. $addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
  174. 'limit' => 1,
  175. 'enumeration' => false,
  176. 'fullmatch' => false,
  177. 'strict_search' => true,
  178. ]);
  179. foreach ($addressBookContacts as $contact) {
  180. if (isset($contact['isLocalSystemBook'])) {
  181. continue;
  182. }
  183. if (isset($contact['CLOUD'])) {
  184. $cloudIds = $contact['CLOUD'];
  185. if (is_string($cloudIds)) {
  186. $cloudIds = [$cloudIds];
  187. }
  188. $lowerSearch = strtolower($search);
  189. foreach ($cloudIds as $cloudId) {
  190. if (strtolower($cloudId) === $lowerSearch) {
  191. $this->displayNames[$search] = $contact['FN'] . " ($cloudId)";
  192. return $this->displayNames[$search];
  193. }
  194. }
  195. }
  196. }
  197. return $search;
  198. }
  199. }