You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Notifier.php 7.9KB

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