Base.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // Legacy from before ownCloud 8.2
  124. $path = $parameter;
  125. $id = $event->getObjectId();
  126. } else {
  127. throw new \InvalidArgumentException('Could not generate file parameter');
  128. }
  129. return [
  130. 'type' => 'file',
  131. 'id' => $id,
  132. 'name' => basename($path),
  133. 'path' => trim($path, '/'),
  134. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]),
  135. ];
  136. }
  137. /**
  138. * @param string $uid
  139. * @param string $overwriteDisplayName - overwrite display name, only if user is not local
  140. *
  141. * @return array
  142. */
  143. protected function getUser(string $uid, string $overwriteDisplayName = '') {
  144. // First try local user
  145. $displayName = $this->userManager->getDisplayName($uid);
  146. if ($displayName !== null) {
  147. return [
  148. 'type' => 'user',
  149. 'id' => $uid,
  150. 'name' => $displayName,
  151. ];
  152. }
  153. // Then a contact from the addressbook
  154. if ($this->cloudIdManager->isValidCloudId($uid)) {
  155. $cloudId = $this->cloudIdManager->resolveCloudId($uid);
  156. return [
  157. 'type' => 'user',
  158. 'id' => $cloudId->getUser(),
  159. 'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $this->getDisplayNameFromAddressBook($cloudId->getDisplayId())),
  160. 'server' => $cloudId->getRemote(),
  161. ];
  162. }
  163. // Fallback to empty dummy data
  164. return [
  165. 'type' => 'user',
  166. 'id' => $uid,
  167. 'name' => (($overwriteDisplayName !== '') ? $overwriteDisplayName : $uid),
  168. ];
  169. }
  170. protected function getDisplayNameFromAddressBook(string $search): string {
  171. if (isset($this->displayNames[$search])) {
  172. return $this->displayNames[$search];
  173. }
  174. $addressBookContacts = $this->contactsManager->search($search, ['CLOUD'], [
  175. 'limit' => 1,
  176. 'enumeration' => false,
  177. 'fullmatch' => false,
  178. 'strict_search' => true,
  179. ]);
  180. foreach ($addressBookContacts as $contact) {
  181. if (isset($contact['isLocalSystemBook'])) {
  182. continue;
  183. }
  184. if (isset($contact['CLOUD'])) {
  185. $cloudIds = $contact['CLOUD'];
  186. if (is_string($cloudIds)) {
  187. $cloudIds = [$cloudIds];
  188. }
  189. $lowerSearch = strtolower($search);
  190. foreach ($cloudIds as $cloudId) {
  191. if (strtolower($cloudId) === $lowerSearch) {
  192. $this->displayNames[$search] = $contact['FN'] . " ($cloudId)";
  193. return $this->displayNames[$search];
  194. }
  195. }
  196. }
  197. }
  198. return $search;
  199. }
  200. }