Notifier.php 7.0 KB

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