Notifier.php 6.9 KB

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