Notifier.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\Notification;
  8. use OCA\Files\Db\TransferOwnershipMapper;
  9. use OCP\AppFramework\Db\DoesNotExistException;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\IURLGenerator;
  12. use OCP\IUser;
  13. use OCP\IUserManager;
  14. use OCP\L10N\IFactory;
  15. use OCP\Notification\IAction;
  16. use OCP\Notification\IDismissableNotifier;
  17. use OCP\Notification\IManager;
  18. use OCP\Notification\INotification;
  19. use OCP\Notification\INotifier;
  20. use OCP\Notification\UnknownNotificationException;
  21. class Notifier implements INotifier, IDismissableNotifier {
  22. /** @var IFactory */
  23. protected $l10nFactory;
  24. /** @var IURLGenerator */
  25. protected $urlGenerator;
  26. /** @var TransferOwnershipMapper */
  27. private $mapper;
  28. /** @var IManager */
  29. private $notificationManager;
  30. /** @var IUserManager */
  31. private $userManager;
  32. /** @var ITimeFactory */
  33. private $timeFactory;
  34. public function __construct(IFactory $l10nFactory,
  35. IURLGenerator $urlGenerator,
  36. TransferOwnershipMapper $mapper,
  37. IManager $notificationManager,
  38. IUserManager $userManager,
  39. ITimeFactory $timeFactory) {
  40. $this->l10nFactory = $l10nFactory;
  41. $this->urlGenerator = $urlGenerator;
  42. $this->mapper = $mapper;
  43. $this->notificationManager = $notificationManager;
  44. $this->userManager = $userManager;
  45. $this->timeFactory = $timeFactory;
  46. }
  47. public function getID(): string {
  48. return 'files';
  49. }
  50. public function getName(): string {
  51. return $this->l10nFactory->get('files')->t('Files');
  52. }
  53. /**
  54. * @param INotification $notification
  55. * @param string $languageCode The code of the language that should be used to prepare the notification
  56. * @return INotification
  57. * @throws UnknownNotificationException When the notification was not prepared by a notifier
  58. */
  59. public function prepare(INotification $notification, string $languageCode): INotification {
  60. if ($notification->getApp() !== 'files') {
  61. throw new UnknownNotificationException('Unhandled app');
  62. }
  63. return match($notification->getSubject()) {
  64. 'transferownershipRequest' => $this->handleTransferownershipRequest($notification, $languageCode),
  65. 'transferownershipRequestDenied' => $this->handleTransferOwnershipRequestDenied($notification, $languageCode),
  66. 'transferOwnershipFailedSource' => $this->handleTransferOwnershipFailedSource($notification, $languageCode),
  67. 'transferOwnershipFailedTarget' => $this->handleTransferOwnershipFailedTarget($notification, $languageCode),
  68. 'transferOwnershipDoneSource' => $this->handleTransferOwnershipDoneSource($notification, $languageCode),
  69. 'transferOwnershipDoneTarget' => $this->handleTransferOwnershipDoneTarget($notification, $languageCode),
  70. default => throw new UnknownNotificationException('Unhandled subject')
  71. };
  72. }
  73. public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification {
  74. $l = $this->l10nFactory->get('files', $languageCode);
  75. $id = $notification->getObjectId();
  76. $param = $notification->getSubjectParameters();
  77. $approveAction = $notification->createAction()
  78. ->setParsedLabel($l->t('Accept'))
  79. ->setPrimary(true)
  80. ->setLink(
  81. $this->urlGenerator->getAbsoluteURL(
  82. $this->urlGenerator->linkTo(
  83. '',
  84. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  85. )
  86. ),
  87. IAction::TYPE_POST
  88. );
  89. $disapproveAction = $notification->createAction()
  90. ->setParsedLabel($l->t('Reject'))
  91. ->setPrimary(false)
  92. ->setLink(
  93. $this->urlGenerator->getAbsoluteURL(
  94. $this->urlGenerator->linkTo(
  95. '',
  96. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  97. )
  98. ),
  99. IAction::TYPE_DELETE
  100. );
  101. $sourceUser = $this->getUser($param['sourceUser']);
  102. $notification->addParsedAction($approveAction)
  103. ->addParsedAction($disapproveAction)
  104. ->setRichSubject(
  105. $l->t('Incoming ownership transfer from {user}'),
  106. [
  107. 'user' => [
  108. 'type' => 'user',
  109. 'id' => $sourceUser->getUID(),
  110. 'name' => $sourceUser->getDisplayName(),
  111. ],
  112. ])
  113. ->setRichMessage(
  114. $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
  115. [
  116. 'path' => [
  117. 'type' => 'highlight',
  118. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  119. 'name' => $param['nodeName'],
  120. ]
  121. ]);
  122. return $notification;
  123. }
  124. public function handleTransferOwnershipRequestDenied(INotification $notification, string $languageCode): INotification {
  125. $l = $this->l10nFactory->get('files', $languageCode);
  126. $param = $notification->getSubjectParameters();
  127. $targetUser = $this->getUser($param['targetUser']);
  128. $notification->setRichSubject($l->t('Ownership transfer denied'))
  129. ->setRichMessage(
  130. $l->t('Your ownership transfer of {path} was denied by {user}.'),
  131. [
  132. 'path' => [
  133. 'type' => 'highlight',
  134. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  135. 'name' => $param['nodeName'],
  136. ],
  137. 'user' => [
  138. 'type' => 'user',
  139. 'id' => $targetUser->getUID(),
  140. 'name' => $targetUser->getDisplayName(),
  141. ],
  142. ]);
  143. return $notification;
  144. }
  145. public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
  146. $l = $this->l10nFactory->get('files', $languageCode);
  147. $param = $notification->getSubjectParameters();
  148. $targetUser = $this->getUser($param['targetUser']);
  149. $notification->setRichSubject($l->t('Ownership transfer failed'))
  150. ->setRichMessage(
  151. $l->t('Your ownership transfer of {path} to {user} failed.'),
  152. [
  153. 'path' => [
  154. 'type' => 'highlight',
  155. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  156. 'name' => $param['nodeName'],
  157. ],
  158. 'user' => [
  159. 'type' => 'user',
  160. 'id' => $targetUser->getUID(),
  161. 'name' => $targetUser->getDisplayName(),
  162. ],
  163. ]);
  164. return $notification;
  165. }
  166. public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
  167. $l = $this->l10nFactory->get('files', $languageCode);
  168. $param = $notification->getSubjectParameters();
  169. $sourceUser = $this->getUser($param['sourceUser']);
  170. $notification->setRichSubject($l->t('Ownership transfer failed'))
  171. ->setRichMessage(
  172. $l->t('The ownership transfer of {path} from {user} failed.'),
  173. [
  174. 'path' => [
  175. 'type' => 'highlight',
  176. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  177. 'name' => $param['nodeName'],
  178. ],
  179. 'user' => [
  180. 'type' => 'user',
  181. 'id' => $sourceUser->getUID(),
  182. 'name' => $sourceUser->getDisplayName(),
  183. ],
  184. ]);
  185. return $notification;
  186. }
  187. public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification {
  188. $l = $this->l10nFactory->get('files', $languageCode);
  189. $param = $notification->getSubjectParameters();
  190. $targetUser = $this->getUser($param['targetUser']);
  191. $notification->setRichSubject($l->t('Ownership transfer done'))
  192. ->setRichMessage(
  193. $l->t('Your ownership transfer of {path} to {user} has completed.'),
  194. [
  195. 'path' => [
  196. 'type' => 'highlight',
  197. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  198. 'name' => $param['nodeName'],
  199. ],
  200. 'user' => [
  201. 'type' => 'user',
  202. 'id' => $targetUser->getUID(),
  203. 'name' => $targetUser->getDisplayName(),
  204. ],
  205. ]);
  206. return $notification;
  207. }
  208. public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification {
  209. $l = $this->l10nFactory->get('files', $languageCode);
  210. $param = $notification->getSubjectParameters();
  211. $sourceUser = $this->getUser($param['sourceUser']);
  212. $notification->setRichSubject($l->t('Ownership transfer done'))
  213. ->setRichMessage(
  214. $l->t('The ownership transfer of {path} from {user} has completed.'),
  215. [
  216. 'path' => [
  217. 'type' => 'highlight',
  218. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  219. 'name' => $param['nodeName'],
  220. ],
  221. 'user' => [
  222. 'type' => 'user',
  223. 'id' => $sourceUser->getUID(),
  224. 'name' => $sourceUser->getDisplayName(),
  225. ],
  226. ]);
  227. return $notification;
  228. }
  229. public function dismissNotification(INotification $notification): void {
  230. if ($notification->getApp() !== 'files') {
  231. throw new UnknownNotificationException('Unhandled app');
  232. }
  233. // TODO: This should all be moved to a service that also the transferownershipController uses.
  234. try {
  235. $transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
  236. } catch (DoesNotExistException $e) {
  237. return;
  238. }
  239. $notification = $this->notificationManager->createNotification();
  240. $notification->setUser($transferOwnership->getSourceUser())
  241. ->setApp('files')
  242. ->setDateTime($this->timeFactory->getDateTime())
  243. ->setSubject('transferownershipRequestDenied', [
  244. 'sourceUser' => $transferOwnership->getSourceUser(),
  245. 'targetUser' => $transferOwnership->getTargetUser(),
  246. 'nodeName' => $transferOwnership->getNodeName()
  247. ])
  248. ->setObject('transfer', (string)$transferOwnership->getId());
  249. $this->notificationManager->notify($notification);
  250. $this->mapper->delete($transferOwnership);
  251. }
  252. protected function getUser(string $userId): IUser {
  253. $user = $this->userManager->get($userId);
  254. if ($user instanceof IUser) {
  255. return $user;
  256. }
  257. throw new \InvalidArgumentException('User not found');
  258. }
  259. }