1
0

Notifier.php 9.4 KB

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