1
0

Notifier.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing;
  8. use OCP\Contacts\IManager;
  9. use OCP\Federation\ICloudId;
  10. use OCP\Federation\ICloudIdManager;
  11. use OCP\HintException;
  12. use OCP\IURLGenerator;
  13. use OCP\L10N\IFactory;
  14. use OCP\Notification\INotification;
  15. use OCP\Notification\INotifier;
  16. use OCP\Notification\UnknownNotificationException;
  17. class Notifier implements INotifier {
  18. /** @var IFactory */
  19. protected $factory;
  20. /** @var IManager */
  21. protected $contactsManager;
  22. /** @var IURLGenerator */
  23. protected $url;
  24. /** @var array */
  25. protected $federatedContacts;
  26. /** @var ICloudIdManager */
  27. protected $cloudIdManager;
  28. /**
  29. * @param IFactory $factory
  30. * @param IManager $contactsManager
  31. * @param IURLGenerator $url
  32. * @param ICloudIdManager $cloudIdManager
  33. */
  34. public function __construct(IFactory $factory, IManager $contactsManager, IURLGenerator $url, ICloudIdManager $cloudIdManager) {
  35. $this->factory = $factory;
  36. $this->contactsManager = $contactsManager;
  37. $this->url = $url;
  38. $this->cloudIdManager = $cloudIdManager;
  39. }
  40. /**
  41. * Identifier of the notifier, only use [a-z0-9_]
  42. *
  43. * @return string
  44. * @since 17.0.0
  45. */
  46. public function getID(): string {
  47. return 'federatedfilesharing';
  48. }
  49. /**
  50. * Human readable name describing the notifier
  51. *
  52. * @return string
  53. * @since 17.0.0
  54. */
  55. public function getName(): string {
  56. return $this->factory->get('federatedfilesharing')->t('Federated sharing');
  57. }
  58. /**
  59. * @param INotification $notification
  60. * @param string $languageCode The code of the language that should be used to prepare the notification
  61. * @return INotification
  62. * @throws UnknownNotificationException
  63. */
  64. public function prepare(INotification $notification, string $languageCode): INotification {
  65. if ($notification->getApp() !== 'files_sharing' || $notification->getObjectType() !== 'remote_share') {
  66. // Not my app => throw
  67. throw new UnknownNotificationException();
  68. }
  69. // Read the language from the notification
  70. $l = $this->factory->get('federatedfilesharing', $languageCode);
  71. switch ($notification->getSubject()) {
  72. // Deal with known subjects
  73. case 'remote_share':
  74. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  75. $params = $notification->getSubjectParameters();
  76. $displayName = (count($params) > 3) ? $params[3] : '';
  77. if ($params[0] !== $params[1] && $params[1] !== null) {
  78. $remoteInitiator = $this->createRemoteUser($params[0], $displayName);
  79. $remoteOwner = $this->createRemoteUser($params[1]);
  80. $params[3] = $remoteInitiator['name'] . '@' . $remoteInitiator['server'];
  81. $params[4] = $remoteOwner['name'] . '@' . $remoteOwner['server'];
  82. $notification->setRichSubject(
  83. $l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
  84. [
  85. 'share' => [
  86. 'type' => 'pending-federated-share',
  87. 'id' => $notification->getObjectId(),
  88. 'name' => $params[2],
  89. ],
  90. 'user' => $remoteInitiator,
  91. 'behalf' => $remoteOwner,
  92. ]
  93. );
  94. } else {
  95. $remoteOwner = $this->createRemoteUser($params[0], $displayName);
  96. $params[3] = $remoteOwner['name'] . '@' . $remoteOwner['server'];
  97. $notification->setRichSubject(
  98. $l->t('You received {share} as a remote share from {user}'),
  99. [
  100. 'share' => [
  101. 'type' => 'pending-federated-share',
  102. 'id' => $notification->getObjectId(),
  103. 'name' => $params[2],
  104. ],
  105. 'user' => $remoteOwner,
  106. ]
  107. );
  108. }
  109. // Deal with the actions for a known subject
  110. foreach ($notification->getActions() as $action) {
  111. switch ($action->getLabel()) {
  112. case 'accept':
  113. $action->setParsedLabel(
  114. $l->t('Accept')
  115. )
  116. ->setPrimary(true);
  117. break;
  118. case 'decline':
  119. $action->setParsedLabel(
  120. $l->t('Decline')
  121. );
  122. break;
  123. }
  124. $notification->addParsedAction($action);
  125. }
  126. return $notification;
  127. default:
  128. // Unknown subject => Unknown notification => throw
  129. throw new UnknownNotificationException();
  130. }
  131. }
  132. /**
  133. * @param string $cloudId
  134. * @param string $displayName - overwrite display name
  135. *
  136. * @return array
  137. */
  138. protected function createRemoteUser(string $cloudId, string $displayName = '') {
  139. try {
  140. $resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
  141. if ($displayName === '') {
  142. $displayName = $this->getDisplayName($resolvedId);
  143. }
  144. $user = $resolvedId->getUser();
  145. $server = $resolvedId->getRemote();
  146. } catch (HintException $e) {
  147. $user = $cloudId;
  148. $displayName = ($displayName !== '') ? $displayName : $cloudId;
  149. $server = '';
  150. }
  151. return [
  152. 'type' => 'user',
  153. 'id' => $user,
  154. 'name' => $displayName,
  155. 'server' => $server,
  156. ];
  157. }
  158. /**
  159. * Try to find the user in the contacts
  160. *
  161. * @param ICloudId $cloudId
  162. * @return string
  163. */
  164. protected function getDisplayName(ICloudId $cloudId): string {
  165. $server = $cloudId->getRemote();
  166. $user = $cloudId->getUser();
  167. if (str_starts_with($server, 'http://')) {
  168. $server = substr($server, strlen('http://'));
  169. } elseif (str_starts_with($server, 'https://')) {
  170. $server = substr($server, strlen('https://'));
  171. }
  172. try {
  173. // contains protocol in the ID
  174. return $this->getDisplayNameFromContact($cloudId->getId());
  175. } catch (\OutOfBoundsException $e) {
  176. }
  177. try {
  178. // does not include protocol, as stored in addressbooks
  179. return $this->getDisplayNameFromContact($cloudId->getDisplayId());
  180. } catch (\OutOfBoundsException $e) {
  181. }
  182. try {
  183. return $this->getDisplayNameFromContact($user . '@http://' . $server);
  184. } catch (\OutOfBoundsException $e) {
  185. }
  186. try {
  187. return $this->getDisplayNameFromContact($user . '@https://' . $server);
  188. } catch (\OutOfBoundsException $e) {
  189. }
  190. return $cloudId->getId();
  191. }
  192. /**
  193. * Try to find the user in the contacts
  194. *
  195. * @param string $federatedCloudId
  196. * @return string
  197. * @throws \OutOfBoundsException when there is no contact for the id
  198. */
  199. protected function getDisplayNameFromContact($federatedCloudId) {
  200. if (isset($this->federatedContacts[$federatedCloudId])) {
  201. if ($this->federatedContacts[$federatedCloudId] !== '') {
  202. return $this->federatedContacts[$federatedCloudId];
  203. } else {
  204. throw new \OutOfBoundsException('No contact found for federated cloud id');
  205. }
  206. }
  207. $addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD'], [
  208. 'limit' => 1,
  209. 'enumeration' => false,
  210. 'fullmatch' => false,
  211. 'strict_search' => true,
  212. ]);
  213. foreach ($addressBookEntries as $entry) {
  214. if (isset($entry['CLOUD'])) {
  215. foreach ($entry['CLOUD'] as $cloudID) {
  216. if ($cloudID === $federatedCloudId) {
  217. $this->federatedContacts[$federatedCloudId] = $entry['FN'];
  218. return $entry['FN'];
  219. }
  220. }
  221. }
  222. }
  223. $this->federatedContacts[$federatedCloudId] = '';
  224. throw new \OutOfBoundsException('No contact found for federated cloud id');
  225. }
  226. }