Notifier.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Sascha Wiswedel <sascha.wiswedel@nextcloud.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Files\Notification;
  27. use OCA\Files\Db\TransferOwnershipMapper;
  28. use OCP\AppFramework\Db\DoesNotExistException;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IURLGenerator;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\L10N\IFactory;
  34. use OCP\Notification\IAction;
  35. use OCP\Notification\IDismissableNotifier;
  36. use OCP\Notification\IManager;
  37. use OCP\Notification\INotification;
  38. use OCP\Notification\INotifier;
  39. class Notifier implements INotifier, IDismissableNotifier {
  40. /** @var IFactory */
  41. protected $l10nFactory;
  42. /** @var IURLGenerator */
  43. protected $urlGenerator;
  44. /** @var TransferOwnershipMapper */
  45. private $mapper;
  46. /** @var IManager */
  47. private $notificationManager;
  48. /** @var IUserManager */
  49. private $userManager;
  50. /** @var ITimeFactory */
  51. private $timeFactory;
  52. public function __construct(IFactory $l10nFactory,
  53. IURLGenerator $urlGenerator,
  54. TransferOwnershipMapper $mapper,
  55. IManager $notificationManager,
  56. IUserManager $userManager,
  57. ITimeFactory $timeFactory) {
  58. $this->l10nFactory = $l10nFactory;
  59. $this->urlGenerator = $urlGenerator;
  60. $this->mapper = $mapper;
  61. $this->notificationManager = $notificationManager;
  62. $this->userManager = $userManager;
  63. $this->timeFactory = $timeFactory;
  64. }
  65. public function getID(): string {
  66. return 'files';
  67. }
  68. public function getName(): string {
  69. return $this->l10nFactory->get('files')->t('Files');
  70. }
  71. /**
  72. * @param INotification $notification
  73. * @param string $languageCode The code of the language that should be used to prepare the notification
  74. * @return INotification
  75. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  76. */
  77. public function prepare(INotification $notification, string $languageCode): INotification {
  78. if ($notification->getApp() !== 'files') {
  79. throw new \InvalidArgumentException('Unhandled app');
  80. }
  81. return match($notification->getSubject()) {
  82. 'transferownershipRequest' => $this->handleTransferownershipRequest($notification, $languageCode),
  83. 'transferownershipRequestDenied' => $this->handleTransferOwnershipRequestDenied($notification, $languageCode),
  84. 'transferOwnershipFailedSource' => $this->handleTransferOwnershipFailedSource($notification, $languageCode),
  85. 'transferOwnershipFailedTarget' => $this->handleTransferOwnershipFailedTarget($notification, $languageCode),
  86. 'transferOwnershipDoneSource' => $this->handleTransferOwnershipDoneSource($notification, $languageCode),
  87. 'transferOwnershipDoneTarget' => $this->handleTransferOwnershipDoneTarget($notification, $languageCode),
  88. default => throw new \InvalidArgumentException('Unhandled subject')
  89. };
  90. }
  91. public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification {
  92. $l = $this->l10nFactory->get('files', $languageCode);
  93. $id = $notification->getObjectId();
  94. $param = $notification->getSubjectParameters();
  95. $approveAction = $notification->createAction()
  96. ->setParsedLabel($l->t('Accept'))
  97. ->setPrimary(true)
  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_POST
  106. );
  107. $disapproveAction = $notification->createAction()
  108. ->setParsedLabel($l->t('Reject'))
  109. ->setPrimary(false)
  110. ->setLink(
  111. $this->urlGenerator->getAbsoluteURL(
  112. $this->urlGenerator->linkTo(
  113. '',
  114. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  115. )
  116. ),
  117. IAction::TYPE_DELETE
  118. );
  119. $sourceUser = $this->getUser($param['sourceUser']);
  120. $notification->addParsedAction($approveAction)
  121. ->addParsedAction($disapproveAction)
  122. ->setRichSubject(
  123. $l->t('Incoming ownership transfer from {user}'),
  124. [
  125. 'user' => [
  126. 'type' => 'user',
  127. 'id' => $sourceUser->getUID(),
  128. 'name' => $sourceUser->getDisplayName(),
  129. ],
  130. ])
  131. ->setRichMessage(
  132. $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
  133. [
  134. 'path' => [
  135. 'type' => 'highlight',
  136. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  137. 'name' => $param['nodeName'],
  138. ]
  139. ]);
  140. return $notification;
  141. }
  142. public function handleTransferOwnershipRequestDenied(INotification $notification, string $languageCode): INotification {
  143. $l = $this->l10nFactory->get('files', $languageCode);
  144. $param = $notification->getSubjectParameters();
  145. $targetUser = $this->getUser($param['targetUser']);
  146. $notification->setRichSubject($l->t('Ownership transfer denied'))
  147. ->setRichMessage(
  148. $l->t('Your ownership transfer of {path} was denied by {user}.'),
  149. [
  150. 'path' => [
  151. 'type' => 'highlight',
  152. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  153. 'name' => $param['nodeName'],
  154. ],
  155. 'user' => [
  156. 'type' => 'user',
  157. 'id' => $targetUser->getUID(),
  158. 'name' => $targetUser->getDisplayName(),
  159. ],
  160. ]);
  161. return $notification;
  162. }
  163. public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
  164. $l = $this->l10nFactory->get('files', $languageCode);
  165. $param = $notification->getSubjectParameters();
  166. $targetUser = $this->getUser($param['targetUser']);
  167. $notification->setRichSubject($l->t('Ownership transfer failed'))
  168. ->setRichMessage(
  169. $l->t('Your ownership transfer of {path} to {user} failed.'),
  170. [
  171. 'path' => [
  172. 'type' => 'highlight',
  173. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  174. 'name' => $param['nodeName'],
  175. ],
  176. 'user' => [
  177. 'type' => 'user',
  178. 'id' => $targetUser->getUID(),
  179. 'name' => $targetUser->getDisplayName(),
  180. ],
  181. ]);
  182. return $notification;
  183. }
  184. public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
  185. $l = $this->l10nFactory->get('files', $languageCode);
  186. $param = $notification->getSubjectParameters();
  187. $sourceUser = $this->getUser($param['sourceUser']);
  188. $notification->setRichSubject($l->t('Ownership transfer failed'))
  189. ->setRichMessage(
  190. $l->t('The ownership transfer of {path} from {user} failed.'),
  191. [
  192. 'path' => [
  193. 'type' => 'highlight',
  194. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  195. 'name' => $param['nodeName'],
  196. ],
  197. 'user' => [
  198. 'type' => 'user',
  199. 'id' => $sourceUser->getUID(),
  200. 'name' => $sourceUser->getDisplayName(),
  201. ],
  202. ]);
  203. return $notification;
  204. }
  205. public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification {
  206. $l = $this->l10nFactory->get('files', $languageCode);
  207. $param = $notification->getSubjectParameters();
  208. $targetUser = $this->getUser($param['targetUser']);
  209. $notification->setRichSubject($l->t('Ownership transfer done'))
  210. ->setRichMessage(
  211. $l->t('Your ownership transfer of {path} to {user} has completed.'),
  212. [
  213. 'path' => [
  214. 'type' => 'highlight',
  215. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  216. 'name' => $param['nodeName'],
  217. ],
  218. 'user' => [
  219. 'type' => 'user',
  220. 'id' => $targetUser->getUID(),
  221. 'name' => $targetUser->getDisplayName(),
  222. ],
  223. ]);
  224. return $notification;
  225. }
  226. public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification {
  227. $l = $this->l10nFactory->get('files', $languageCode);
  228. $param = $notification->getSubjectParameters();
  229. $sourceUser = $this->getUser($param['sourceUser']);
  230. $notification->setRichSubject($l->t('Ownership transfer done'))
  231. ->setRichMessage(
  232. $l->t('The ownership transfer of {path} from {user} has completed.'),
  233. [
  234. 'path' => [
  235. 'type' => 'highlight',
  236. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  237. 'name' => $param['nodeName'],
  238. ],
  239. 'user' => [
  240. 'type' => 'user',
  241. 'id' => $sourceUser->getUID(),
  242. 'name' => $sourceUser->getDisplayName(),
  243. ],
  244. ]);
  245. return $notification;
  246. }
  247. public function dismissNotification(INotification $notification): void {
  248. if ($notification->getApp() !== 'files') {
  249. throw new \InvalidArgumentException('Unhandled app');
  250. }
  251. // TODO: This should all be moved to a service that also the transferownershipController uses.
  252. try {
  253. $transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
  254. } catch (DoesNotExistException $e) {
  255. return;
  256. }
  257. $notification = $this->notificationManager->createNotification();
  258. $notification->setUser($transferOwnership->getSourceUser())
  259. ->setApp('files')
  260. ->setDateTime($this->timeFactory->getDateTime())
  261. ->setSubject('transferownershipRequestDenied', [
  262. 'sourceUser' => $transferOwnership->getSourceUser(),
  263. 'targetUser' => $transferOwnership->getTargetUser(),
  264. 'nodeName' => $transferOwnership->getNodeName()
  265. ])
  266. ->setObject('transfer', (string)$transferOwnership->getId());
  267. $this->notificationManager->notify($notification);
  268. $this->mapper->delete($transferOwnership);
  269. }
  270. protected function getUser(string $userId): IUser {
  271. $user = $this->userManager->get($userId);
  272. if ($user instanceof IUser) {
  273. return $user;
  274. }
  275. throw new \InvalidArgumentException('User not found');
  276. }
  277. }