Notifier.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. ->setRichMessage(
  139. $l->t("Do you want to accept {path}?\n\nNote: The transfer process after accepting may take up to 1 hour."),
  140. [
  141. 'path' => [
  142. 'type' => 'highlight',
  143. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  144. 'name' => $param['nodeName'],
  145. ]
  146. ]);
  147. return $notification;
  148. }
  149. public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
  150. $l = $this->l10nFactory->get('files', $languageCode);
  151. $param = $notification->getSubjectParameters();
  152. $targetUser = $this->getUser($param['targetUser']);
  153. $notification->setRichSubject($l->t('Ownership transfer failed'))
  154. ->setRichMessage(
  155. $l->t('Your ownership transfer of {path} to {user} failed.'),
  156. [
  157. 'path' => [
  158. 'type' => 'highlight',
  159. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  160. 'name' => $param['nodeName'],
  161. ],
  162. 'user' => [
  163. 'type' => 'user',
  164. 'id' => $targetUser->getUID(),
  165. 'name' => $targetUser->getDisplayName(),
  166. ],
  167. ]);
  168. return $notification;
  169. }
  170. public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
  171. $l = $this->l10nFactory->get('files', $languageCode);
  172. $param = $notification->getSubjectParameters();
  173. $sourceUser = $this->getUser($param['sourceUser']);
  174. $notification->setRichSubject($l->t('Ownership transfer failed'))
  175. ->setRichMessage(
  176. $l->t('The ownership transfer of {path} from {user} failed.'),
  177. [
  178. 'path' => [
  179. 'type' => 'highlight',
  180. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  181. 'name' => $param['nodeName'],
  182. ],
  183. 'user' => [
  184. 'type' => 'user',
  185. 'id' => $sourceUser->getUID(),
  186. 'name' => $sourceUser->getDisplayName(),
  187. ],
  188. ]);
  189. return $notification;
  190. }
  191. public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification {
  192. $l = $this->l10nFactory->get('files', $languageCode);
  193. $param = $notification->getSubjectParameters();
  194. $targetUser = $this->getUser($param['targetUser']);
  195. $notification->setRichSubject($l->t('Ownership transfer done'))
  196. ->setRichMessage(
  197. $l->t('Your ownership transfer of {path} to {user} has completed.'),
  198. [
  199. 'path' => [
  200. 'type' => 'highlight',
  201. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  202. 'name' => $param['nodeName'],
  203. ],
  204. 'user' => [
  205. 'type' => 'user',
  206. 'id' => $targetUser->getUID(),
  207. 'name' => $targetUser->getDisplayName(),
  208. ],
  209. ]);
  210. return $notification;
  211. }
  212. public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification {
  213. $l = $this->l10nFactory->get('files', $languageCode);
  214. $param = $notification->getSubjectParameters();
  215. $sourceUser = $this->getUser($param['sourceUser']);
  216. $notification->setRichSubject($l->t('Ownership transfer done'))
  217. ->setRichMessage(
  218. $l->t('The ownership transfer of {path} from {user} has completed.'),
  219. [
  220. 'path' => [
  221. 'type' => 'highlight',
  222. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  223. 'name' => $param['nodeName'],
  224. ],
  225. 'user' => [
  226. 'type' => 'user',
  227. 'id' => $sourceUser->getUID(),
  228. 'name' => $sourceUser->getDisplayName(),
  229. ],
  230. ]);
  231. return $notification;
  232. }
  233. public function dismissNotification(INotification $notification): void {
  234. if ($notification->getApp() !== 'files') {
  235. throw new \InvalidArgumentException('Unhandled app');
  236. }
  237. // TODO: This should all be moved to a service that also the transferownershipController uses.
  238. try {
  239. $transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
  240. } catch (DoesNotExistException $e) {
  241. return;
  242. }
  243. $notification = $this->notificationManager->createNotification();
  244. $notification->setUser($transferOwnership->getSourceUser())
  245. ->setApp('files')
  246. ->setDateTime($this->timeFactory->getDateTime())
  247. ->setSubject('transferownershipRequestDenied', [
  248. 'sourceUser' => $transferOwnership->getSourceUser(),
  249. 'targetUser' => $transferOwnership->getTargetUser(),
  250. 'nodeName' => $transferOwnership->getNodeName()
  251. ])
  252. ->setObject('transfer', (string)$transferOwnership->getId());
  253. $this->notificationManager->notify($notification);
  254. $this->mapper->delete($transferOwnership);
  255. }
  256. protected function getUser(string $userId): IUser {
  257. $user = $this->userManager->get($userId);
  258. if ($user instanceof IUser) {
  259. return $user;
  260. }
  261. throw new \InvalidArgumentException('User not found');
  262. }
  263. }