1
0

Notifier.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. ->setRichMessage(
  122. $l->t('Your share of {node} will expire tomorrow'),
  123. [
  124. 'node' => [
  125. 'type' => 'file',
  126. 'id' => $node->getId(),
  127. 'name' => $node->getName(),
  128. 'path' => $path,
  129. ],
  130. ]
  131. );
  132. return $notification;
  133. }
  134. protected function parseShareInvitation(IShare $share, INotification $notification, IL10N $l): INotification {
  135. if ($share->getShareType() === IShare::TYPE_USER) {
  136. if ($share->getStatus() !== IShare::STATUS_PENDING) {
  137. throw new AlreadyProcessedException();
  138. }
  139. } elseif ($share->getShareType() === IShare::TYPE_GROUP) {
  140. if ($share->getStatus() !== IShare::STATUS_PENDING) {
  141. throw new AlreadyProcessedException();
  142. }
  143. } else {
  144. throw new \InvalidArgumentException('Invalid share type');
  145. }
  146. switch ($notification->getSubject()) {
  147. case self::INCOMING_USER_SHARE:
  148. if ($share->getSharedWith() !== $notification->getUser()) {
  149. throw new AlreadyProcessedException();
  150. }
  151. $sharer = $this->userManager->get($share->getSharedBy());
  152. if (!$sharer instanceof IUser) {
  153. throw new \InvalidArgumentException('Temporary failure');
  154. }
  155. $subject = $l->t('You received {share} as a share by {user}');
  156. $subjectParameters = [
  157. 'share' => [
  158. 'type' => 'highlight',
  159. 'id' => $notification->getObjectId(),
  160. 'name' => $share->getTarget(),
  161. ],
  162. 'user' => [
  163. 'type' => 'user',
  164. 'id' => $sharer->getUID(),
  165. 'name' => $sharer->getDisplayName(),
  166. ],
  167. ];
  168. break;
  169. case self::INCOMING_GROUP_SHARE:
  170. $user = $this->userManager->get($notification->getUser());
  171. if (!$user instanceof IUser) {
  172. throw new AlreadyProcessedException();
  173. }
  174. $group = $this->groupManager->get($share->getSharedWith());
  175. if ($group === null || !$group->inGroup($user)) {
  176. throw new AlreadyProcessedException();
  177. }
  178. if ($share->getPermissions() === 0) {
  179. // Already rejected
  180. throw new AlreadyProcessedException();
  181. }
  182. $sharer = $this->userManager->get($share->getSharedBy());
  183. if (!$sharer instanceof IUser) {
  184. throw new \InvalidArgumentException('Temporary failure');
  185. }
  186. $subject = $l->t('You received {share} to group {group} as a share by {user}');
  187. $subjectParameters = [
  188. 'share' => [
  189. 'type' => 'highlight',
  190. 'id' => $notification->getObjectId(),
  191. 'name' => $share->getTarget(),
  192. ],
  193. 'group' => [
  194. 'type' => 'user-group',
  195. 'id' => $group->getGID(),
  196. 'name' => $group->getDisplayName(),
  197. ],
  198. 'user' => [
  199. 'type' => 'user',
  200. 'id' => $sharer->getUID(),
  201. 'name' => $sharer->getDisplayName(),
  202. ],
  203. ];
  204. break;
  205. default:
  206. throw new \InvalidArgumentException('Invalid subject');
  207. }
  208. $notification->setRichSubject($subject, $subjectParameters)
  209. ->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
  210. $acceptAction = $notification->createAction();
  211. $acceptAction->setParsedLabel($l->t('Accept'))
  212. ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.acceptShare', ['id' => $share->getId()]), 'POST')
  213. ->setPrimary(true);
  214. $notification->addParsedAction($acceptAction);
  215. $rejectAction = $notification->createAction();
  216. $rejectAction->setParsedLabel($l->t('Reject'))
  217. ->setLink($this->url->linkToOCSRouteAbsolute('files_sharing.ShareAPI.deleteShare', ['id' => $share->getId()]), 'DELETE')
  218. ->setPrimary(false);
  219. $notification->addParsedAction($rejectAction);
  220. return $notification;
  221. }
  222. }