Notifier.php 6.9 KB

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