1
0

Notifier.php 6.7 KB

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