Notifier.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if ($notification->getSubject() === 'transferownershipRequest') {
  63. return $this->handleTransferownershipRequest($notification, $languageCode);
  64. }
  65. if ($notification->getSubject() === 'transferOwnershipFailedSource') {
  66. return $this->handleTransferOwnershipFailedSource($notification, $languageCode);
  67. }
  68. if ($notification->getSubject() === 'transferOwnershipFailedTarget') {
  69. return $this->handleTransferOwnershipFailedTarget($notification, $languageCode);
  70. }
  71. if ($notification->getSubject() === 'transferOwnershipDoneSource') {
  72. return $this->handleTransferOwnershipDoneSource($notification, $languageCode);
  73. }
  74. if ($notification->getSubject() === 'transferOwnershipDoneTarget') {
  75. return $this->handleTransferOwnershipDoneTarget($notification, $languageCode);
  76. }
  77. throw new \InvalidArgumentException('Unhandled subject');
  78. }
  79. public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification {
  80. $l = $this->l10nFactory->get('files', $languageCode);
  81. $id = $notification->getObjectId();
  82. $param = $notification->getSubjectParameters();
  83. $approveAction = $notification->createAction()
  84. ->setParsedLabel($l->t('Accept'))
  85. ->setPrimary(true)
  86. ->setLink(
  87. $this->urlGenerator->getAbsoluteURL(
  88. $this->urlGenerator->linkTo(
  89. '',
  90. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  91. )
  92. ),
  93. IAction::TYPE_POST
  94. );
  95. $disapproveAction = $notification->createAction()
  96. ->setParsedLabel($l->t('Reject'))
  97. ->setPrimary(false)
  98. ->setLink(
  99. $this->urlGenerator->getAbsoluteURL(
  100. $this->urlGenerator->linkTo(
  101. '',
  102. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  103. )
  104. ),
  105. IAction::TYPE_DELETE
  106. );
  107. $sourceUser = $this->getUser($param['sourceUser']);
  108. $notification->addParsedAction($approveAction)
  109. ->addParsedAction($disapproveAction)
  110. ->setRichSubject(
  111. $l->t('Incoming ownership transfer from {user}'),
  112. [
  113. 'user' => [
  114. 'type' => 'user',
  115. 'id' => $sourceUser->getUID(),
  116. 'name' => $sourceUser->getDisplayName(),
  117. ],
  118. ])
  119. ->setRichMessage(
  120. $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
  121. [
  122. 'path' => [
  123. 'type' => 'highlight',
  124. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  125. 'name' => $param['nodeName'],
  126. ]
  127. ]);
  128. return $notification;
  129. }
  130. public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
  131. $l = $this->l10nFactory->get('files', $languageCode);
  132. $param = $notification->getSubjectParameters();
  133. $targetUser = $this->getUser($param['targetUser']);
  134. $notification->setRichSubject($l->t('Ownership transfer failed'))
  135. ->setRichMessage(
  136. $l->t('Your ownership transfer of {path} to {user} failed.'),
  137. [
  138. 'path' => [
  139. 'type' => 'highlight',
  140. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  141. 'name' => $param['nodeName'],
  142. ],
  143. 'user' => [
  144. 'type' => 'user',
  145. 'id' => $targetUser->getUID(),
  146. 'name' => $targetUser->getDisplayName(),
  147. ],
  148. ]);
  149. return $notification;
  150. }
  151. public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
  152. $l = $this->l10nFactory->get('files', $languageCode);
  153. $param = $notification->getSubjectParameters();
  154. $sourceUser = $this->getUser($param['sourceUser']);
  155. $notification->setRichSubject($l->t('Ownership transfer failed'))
  156. ->setRichMessage(
  157. $l->t('The ownership transfer of {path} from {user} failed.'),
  158. [
  159. 'path' => [
  160. 'type' => 'highlight',
  161. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  162. 'name' => $param['nodeName'],
  163. ],
  164. 'user' => [
  165. 'type' => 'user',
  166. 'id' => $sourceUser->getUID(),
  167. 'name' => $sourceUser->getDisplayName(),
  168. ],
  169. ]);
  170. return $notification;
  171. }
  172. public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification {
  173. $l = $this->l10nFactory->get('files', $languageCode);
  174. $param = $notification->getSubjectParameters();
  175. $targetUser = $this->getUser($param['targetUser']);
  176. $notification->setRichSubject($l->t('Ownership transfer done'))
  177. ->setRichMessage(
  178. $l->t('Your ownership transfer of {path} to {user} has completed.'),
  179. [
  180. 'path' => [
  181. 'type' => 'highlight',
  182. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  183. 'name' => $param['nodeName'],
  184. ],
  185. 'user' => [
  186. 'type' => 'user',
  187. 'id' => $targetUser->getUID(),
  188. 'name' => $targetUser->getDisplayName(),
  189. ],
  190. ]);
  191. return $notification;
  192. }
  193. public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification {
  194. $l = $this->l10nFactory->get('files', $languageCode);
  195. $param = $notification->getSubjectParameters();
  196. $sourceUser = $this->getUser($param['sourceUser']);
  197. $notification->setRichSubject($l->t('Ownership transfer done'))
  198. ->setRichMessage(
  199. $l->t('The ownership transfer of {path} from {user} has completed.'),
  200. [
  201. 'path' => [
  202. 'type' => 'highlight',
  203. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  204. 'name' => $param['nodeName'],
  205. ],
  206. 'user' => [
  207. 'type' => 'user',
  208. 'id' => $sourceUser->getUID(),
  209. 'name' => $sourceUser->getDisplayName(),
  210. ],
  211. ]);
  212. return $notification;
  213. }
  214. public function dismissNotification(INotification $notification): void {
  215. if ($notification->getApp() !== 'files') {
  216. throw new \InvalidArgumentException('Unhandled app');
  217. }
  218. // TODO: This should all be moved to a service that also the transferownershipController uses.
  219. try {
  220. $transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
  221. } catch (DoesNotExistException $e) {
  222. return;
  223. }
  224. $notification = $this->notificationManager->createNotification();
  225. $notification->setUser($transferOwnership->getSourceUser())
  226. ->setApp('files')
  227. ->setDateTime($this->timeFactory->getDateTime())
  228. ->setSubject('transferownershipRequestDenied', [
  229. 'sourceUser' => $transferOwnership->getSourceUser(),
  230. 'targetUser' => $transferOwnership->getTargetUser(),
  231. 'nodeName' => $transferOwnership->getNodeName()
  232. ])
  233. ->setObject('transfer', (string)$transferOwnership->getId());
  234. $this->notificationManager->notify($notification);
  235. $this->mapper->delete($transferOwnership);
  236. }
  237. protected function getUser(string $userId): IUser {
  238. $user = $this->userManager->get($userId);
  239. if ($user instanceof IUser) {
  240. return $user;
  241. }
  242. throw new \InvalidArgumentException('User not found');
  243. }
  244. }