Notifier.php 9.2 KB

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