Notifier.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. * @copyright Copyright (c) 2019, Joas Schilling <coding@schilljs.com>
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Files_Sharing\Notification;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\IGroupManager;
  31. use OCP\IL10N;
  32. use OCP\IURLGenerator;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\L10N\IFactory;
  36. use OCP\Notification\AlreadyProcessedException;
  37. use OCP\Notification\INotification;
  38. use OCP\Notification\INotifier;
  39. use OCP\Share\Exceptions\ShareNotFound;
  40. use OCP\Share\IManager;
  41. use OCP\Share\IShare;
  42. class Notifier implements INotifier {
  43. public const INCOMING_USER_SHARE = 'incoming_user_share';
  44. public const INCOMING_GROUP_SHARE = 'incoming_group_share';
  45. /** @var IFactory */
  46. protected $l10nFactory;
  47. /** @var IManager */
  48. private $shareManager;
  49. /** @var IRootFolder */
  50. private $rootFolder;
  51. /** @var IGroupManager */
  52. protected $groupManager;
  53. /** @var IUserManager */
  54. protected $userManager;
  55. /** @var IURLGenerator */
  56. protected $url;
  57. public function __construct(IFactory $l10nFactory,
  58. IManager $shareManager,
  59. IRootFolder $rootFolder,
  60. IGroupManager $groupManager,
  61. IUserManager $userManager,
  62. IURLGenerator $url) {
  63. $this->l10nFactory = $l10nFactory;
  64. $this->shareManager = $shareManager;
  65. $this->rootFolder = $rootFolder;
  66. $this->groupManager = $groupManager;
  67. $this->userManager = $userManager;
  68. $this->url = $url;
  69. }
  70. /**
  71. * Identifier of the notifier, only use [a-z0-9_]
  72. *
  73. * @return string
  74. * @since 17.0.0
  75. */
  76. public function getID(): string {
  77. return 'files_sharing';
  78. }
  79. /**
  80. * Human readable name describing the notifier
  81. *
  82. * @return string
  83. * @since 17.0.0
  84. */
  85. public function getName(): string {
  86. return $this->l10nFactory->get('files_sharing')->t('File sharing');
  87. }
  88. /**
  89. * @param INotification $notification
  90. * @param string $languageCode The code of the language that should be used to prepare the notification
  91. * @return INotification
  92. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  93. * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
  94. * @since 9.0.0
  95. */
  96. public function prepare(INotification $notification, string $languageCode): INotification {
  97. if ($notification->getApp() !== 'files_sharing' ||
  98. ($notification->getSubject() !== 'expiresTomorrow' &&
  99. $notification->getObjectType() !== 'share')) {
  100. throw new \InvalidArgumentException('Unhandled app or subject');
  101. }
  102. $l = $this->l10nFactory->get('files_sharing', $languageCode);
  103. $attemptId = $notification->getObjectId();
  104. try {
  105. $share = $this->shareManager->getShareById($attemptId, $notification->getUser());
  106. } catch (ShareNotFound $e) {
  107. throw new AlreadyProcessedException();
  108. }
  109. try {
  110. $share->getNode();
  111. } catch (NotFoundException $e) {
  112. // Node is already deleted, so discard the notification
  113. throw new AlreadyProcessedException();
  114. }
  115. if ($notification->getSubject() === 'expiresTomorrow') {
  116. $notification = $this->parseShareExpiration($share, $notification, $l);
  117. } else {
  118. $notification = $this->parseShareInvitation($share, $notification, $l);
  119. }
  120. return $notification;
  121. }
  122. protected function parseShareExpiration(IShare $share, INotification $notification, IL10N $l): INotification {
  123. $node = $share->getNode();
  124. $userFolder = $this->rootFolder->getUserFolder($notification->getUser());
  125. $path = $userFolder->getRelativePath($node->getPath());
  126. $notification
  127. ->setParsedSubject($l->t('Share will expire tomorrow'))
  128. ->setRichMessage(
  129. $l->t('Your share of {node} will expire tomorrow'),
  130. [
  131. 'node' => [
  132. 'type' => 'file',
  133. 'id' => $node->getId(),
  134. 'name' => $node->getName(),
  135. 'path' => $path,
  136. ],
  137. ]
  138. );
  139. return $notification;
  140. }
  141. protected function parseShareInvitation(IShare $share, INotification $notification, IL10N $l): INotification {
  142. if ($share->getShareType() === IShare::TYPE_USER) {
  143. if ($share->getStatus() !== IShare::STATUS_PENDING) {
  144. throw new AlreadyProcessedException();
  145. }
  146. } elseif ($share->getShareType() === IShare::TYPE_GROUP) {
  147. if ($share->getStatus() !== IShare::STATUS_PENDING) {
  148. throw new AlreadyProcessedException();
  149. }
  150. } else {
  151. throw new \InvalidArgumentException('Invalid share type');
  152. }
  153. switch ($notification->getSubject()) {
  154. case self::INCOMING_USER_SHARE:
  155. if ($share->getSharedWith() !== $notification->getUser()) {
  156. throw new AlreadyProcessedException();
  157. }
  158. $sharer = $this->userManager->get($share->getSharedBy());
  159. if (!$sharer instanceof IUser) {
  160. throw new \InvalidArgumentException('Temporary failure');
  161. }
  162. $subject = $l->t('You received {share} as a share by {user}');
  163. $subjectParameters = [
  164. 'share' => [
  165. 'type' => 'highlight',
  166. 'id' => $notification->getObjectId(),
  167. 'name' => $share->getTarget(),
  168. ],
  169. 'user' => [
  170. 'type' => 'user',
  171. 'id' => $sharer->getUID(),
  172. 'name' => $sharer->getDisplayName(),
  173. ],
  174. ];
  175. break;
  176. case self::INCOMING_GROUP_SHARE:
  177. $user = $this->userManager->get($notification->getUser());
  178. if (!$user instanceof IUser) {
  179. throw new AlreadyProcessedException();
  180. }
  181. $group = $this->groupManager->get($share->getSharedWith());
  182. if ($group === null || !$group->inGroup($user)) {
  183. throw new AlreadyProcessedException();
  184. }
  185. if ($share->getPermissions() === 0) {
  186. // Already rejected
  187. throw new AlreadyProcessedException();
  188. }
  189. $sharer = $this->userManager->get($share->getSharedBy());
  190. if (!$sharer instanceof IUser) {
  191. throw new \InvalidArgumentException('Temporary failure');
  192. }
  193. $subject = $l->t('You received {share} to group {group} as a share by {user}');
  194. $subjectParameters = [
  195. 'share' => [
  196. 'type' => 'highlight',
  197. 'id' => $notification->getObjectId(),
  198. 'name' => $share->getTarget(),
  199. ],
  200. 'group' => [
  201. 'type' => 'user-group',
  202. 'id' => $group->getGID(),
  203. 'name' => $group->getDisplayName(),
  204. ],
  205. 'user' => [
  206. 'type' => 'user',
  207. 'id' => $sharer->getUID(),
  208. 'name' => $sharer->getDisplayName(),
  209. ],
  210. ];
  211. break;
  212. default:
  213. throw new \InvalidArgumentException('Invalid subject');
  214. }
  215. $notification->setRichSubject($subject, $subjectParameters)
  216. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  217. $acceptAction = $notification->createAction();
  218. $acceptAction->setParsedLabel($l->t('Accept'))
  219. ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.acceptShare', ['id' => $share->getId()]), 'POST')
  220. ->setPrimary(true);
  221. $notification->addParsedAction($acceptAction);
  222. $rejectAction = $notification->createAction();
  223. $rejectAction->setParsedLabel($l->t('Reject'))
  224. ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.deleteShare', ['id' => $share->getId()]), 'DELETE')
  225. ->setPrimary(false);
  226. $notification->addParsedAction($rejectAction);
  227. return $notification;
  228. }
  229. }