1
0

Notifier.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing\Notification;
  8. use OCP\Files\IRootFolder;
  9. use OCP\Files\NotFoundException;
  10. use OCP\IGroupManager;
  11. use OCP\IL10N;
  12. use OCP\IURLGenerator;
  13. use OCP\IUser;
  14. use OCP\IUserManager;
  15. use OCP\L10N\IFactory;
  16. use OCP\Notification\AlreadyProcessedException;
  17. use OCP\Notification\INotification;
  18. use OCP\Notification\INotifier;
  19. use OCP\Notification\UnknownNotificationException;
  20. use OCP\Share\Exceptions\ShareNotFound;
  21. use OCP\Share\IManager;
  22. use OCP\Share\IShare;
  23. class Notifier implements INotifier {
  24. public const INCOMING_USER_SHARE = 'incoming_user_share';
  25. public const INCOMING_GROUP_SHARE = 'incoming_group_share';
  26. public function __construct(
  27. protected IFactory $l10nFactory,
  28. private IManager $shareManager,
  29. private IRootFolder $rootFolder,
  30. protected IGroupManager $groupManager,
  31. protected IUserManager $userManager,
  32. protected IURLGenerator $url,
  33. ) {
  34. }
  35. /**
  36. * Identifier of the notifier, only use [a-z0-9_]
  37. *
  38. * @return string
  39. * @since 17.0.0
  40. */
  41. public function getID(): string {
  42. return 'files_sharing';
  43. }
  44. /**
  45. * Human readable name describing the notifier
  46. *
  47. * @return string
  48. * @since 17.0.0
  49. */
  50. public function getName(): string {
  51. return $this->l10nFactory->get('files_sharing')->t('File sharing');
  52. }
  53. /**
  54. * @param INotification $notification
  55. * @param string $languageCode The code of the language that should be used to prepare the notification
  56. * @return INotification
  57. * @throws UnknownNotificationException When the notification was not prepared by a notifier
  58. * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
  59. * @since 9.0.0
  60. */
  61. public function prepare(INotification $notification, string $languageCode): INotification {
  62. if ($notification->getApp() !== 'files_sharing' ||
  63. ($notification->getSubject() !== 'expiresTomorrow' &&
  64. $notification->getObjectType() !== 'share')) {
  65. throw new UnknownNotificationException('Unhandled app or subject');
  66. }
  67. $l = $this->l10nFactory->get('files_sharing', $languageCode);
  68. $attemptId = $notification->getObjectId();
  69. try {
  70. $share = $this->shareManager->getShareById($attemptId, $notification->getUser());
  71. } catch (ShareNotFound $e) {
  72. throw new AlreadyProcessedException();
  73. }
  74. try {
  75. $share->getNode();
  76. } catch (NotFoundException $e) {
  77. // Node is already deleted, so discard the notification
  78. throw new AlreadyProcessedException();
  79. }
  80. if ($notification->getSubject() === 'expiresTomorrow') {
  81. $notification = $this->parseShareExpiration($share, $notification, $l);
  82. } else {
  83. $notification = $this->parseShareInvitation($share, $notification, $l);
  84. }
  85. return $notification;
  86. }
  87. protected function parseShareExpiration(IShare $share, INotification $notification, IL10N $l): INotification {
  88. $node = $share->getNode();
  89. $userFolder = $this->rootFolder->getUserFolder($notification->getUser());
  90. $path = $userFolder->getRelativePath($node->getPath());
  91. $notification
  92. ->setParsedSubject($l->t('Share will expire tomorrow'))
  93. ->setRichMessage(
  94. $l->t('Your share of {node} will expire tomorrow'),
  95. [
  96. 'node' => [
  97. 'type' => 'file',
  98. 'id' => (string)$node->getId(),
  99. 'name' => $node->getName(),
  100. 'path' => (string)$path,
  101. ],
  102. ]
  103. );
  104. return $notification;
  105. }
  106. protected function parseShareInvitation(IShare $share, INotification $notification, IL10N $l): INotification {
  107. if ($share->getShareType() === IShare::TYPE_USER) {
  108. if ($share->getStatus() !== IShare::STATUS_PENDING) {
  109. throw new AlreadyProcessedException();
  110. }
  111. } elseif ($share->getShareType() === IShare::TYPE_GROUP) {
  112. if ($share->getStatus() !== IShare::STATUS_PENDING) {
  113. throw new AlreadyProcessedException();
  114. }
  115. } else {
  116. throw new UnknownNotificationException('Invalid share type');
  117. }
  118. switch ($notification->getSubject()) {
  119. case self::INCOMING_USER_SHARE:
  120. if ($share->getSharedWith() !== $notification->getUser()) {
  121. throw new AlreadyProcessedException();
  122. }
  123. $sharer = $this->userManager->get($share->getSharedBy());
  124. if (!$sharer instanceof IUser) {
  125. throw new \InvalidArgumentException('Temporary failure');
  126. }
  127. $subject = $l->t('You received {share} as a share by {user}');
  128. $subjectParameters = [
  129. 'share' => [
  130. 'type' => 'highlight',
  131. 'id' => $notification->getObjectId(),
  132. 'name' => $share->getTarget(),
  133. ],
  134. 'user' => [
  135. 'type' => 'user',
  136. 'id' => $sharer->getUID(),
  137. 'name' => $sharer->getDisplayName(),
  138. ],
  139. ];
  140. break;
  141. case self::INCOMING_GROUP_SHARE:
  142. $user = $this->userManager->get($notification->getUser());
  143. if (!$user instanceof IUser) {
  144. throw new AlreadyProcessedException();
  145. }
  146. $group = $this->groupManager->get($share->getSharedWith());
  147. if ($group === null || !$group->inGroup($user)) {
  148. throw new AlreadyProcessedException();
  149. }
  150. if ($share->getPermissions() === 0) {
  151. // Already rejected
  152. throw new AlreadyProcessedException();
  153. }
  154. $sharer = $this->userManager->get($share->getSharedBy());
  155. if (!$sharer instanceof IUser) {
  156. throw new \InvalidArgumentException('Temporary failure');
  157. }
  158. $subject = $l->t('You received {share} to group {group} as a share by {user}');
  159. $subjectParameters = [
  160. 'share' => [
  161. 'type' => 'highlight',
  162. 'id' => $notification->getObjectId(),
  163. 'name' => $share->getTarget(),
  164. ],
  165. 'group' => [
  166. 'type' => 'user-group',
  167. 'id' => $group->getGID(),
  168. 'name' => $group->getDisplayName(),
  169. ],
  170. 'user' => [
  171. 'type' => 'user',
  172. 'id' => $sharer->getUID(),
  173. 'name' => $sharer->getDisplayName(),
  174. ],
  175. ];
  176. break;
  177. default:
  178. throw new UnknownNotificationException('Invalid subject');
  179. }
  180. $notification->setRichSubject($subject, $subjectParameters)
  181. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  182. $acceptAction = $notification->createAction();
  183. $acceptAction->setParsedLabel($l->t('Accept'))
  184. ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.acceptShare', ['id' => $share->getId()]), 'POST')
  185. ->setPrimary(true);
  186. $notification->addParsedAction($acceptAction);
  187. $rejectAction = $notification->createAction();
  188. $rejectAction->setParsedLabel($l->t('Decline'))
  189. ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.deleteShare', ['id' => $share->getId()]), 'DELETE')
  190. ->setPrimary(false);
  191. $notification->addParsedAction($rejectAction);
  192. return $notification;
  193. }
  194. }