1
0

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