Notifier.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\IURLGenerator;
  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 ITimeFactory */
  49. private $timeFactory;
  50. /**
  51. * @param IFactory $l10nFactory
  52. * @param IURLGenerator $urlGenerator
  53. */
  54. public function __construct(IFactory $l10nFactory,
  55. IURLGenerator $urlGenerator,
  56. TransferOwnershipMapper $mapper,
  57. IManager $notificationManager,
  58. ITimeFactory $timeFactory) {
  59. $this->l10nFactory = $l10nFactory;
  60. $this->urlGenerator = $urlGenerator;
  61. $this->mapper = $mapper;
  62. $this->notificationManager = $notificationManager;
  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. $notification->addParsedAction($approveAction)
  127. ->addParsedAction($disapproveAction)
  128. ->setRichSubject(
  129. $l->t('Incoming ownership transfer from {user}'),
  130. [
  131. 'user' => [
  132. 'type' => 'user',
  133. 'id' => $param['sourceUser'],
  134. 'name' => $param['sourceUser'],
  135. ],
  136. ])
  137. ->setParsedSubject(str_replace('{user}', $param['sourceUser'], $l->t('Incoming ownership transfer from {user}')))
  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. ->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.")));
  148. return $notification;
  149. }
  150. public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
  151. $l = $this->l10nFactory->get('files', $languageCode);
  152. $param = $notification->getSubjectParameters();
  153. $notification->setRichSubject($l->t('Ownership transfer failed'))
  154. ->setParsedSubject($l->t('Ownership transfer failed'))
  155. ->setRichMessage(
  156. $l->t('Your ownership transfer of {path} to {user} failed.'),
  157. [
  158. 'path' => [
  159. 'type' => 'highlight',
  160. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  161. 'name' => $param['nodeName'],
  162. ],
  163. 'user' => [
  164. 'type' => 'user',
  165. 'id' => $param['targetUser'],
  166. 'name' => $param['targetUser'],
  167. ],
  168. ])
  169. ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your ownership transfer of {path} to {user} failed.')));
  170. return $notification;
  171. }
  172. public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
  173. $l = $this->l10nFactory->get('files', $languageCode);
  174. $param = $notification->getSubjectParameters();
  175. $notification->setRichSubject($l->t('Ownership transfer failed'))
  176. ->setParsedSubject($l->t('Ownership transfer failed'))
  177. ->setRichMessage(
  178. $l->t('The ownership transfer of {path} from {user} failed.'),
  179. [
  180. 'path' => [
  181. 'type' => 'highlight',
  182. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  183. 'name' => $param['nodeName'],
  184. ],
  185. 'user' => [
  186. 'type' => 'user',
  187. 'id' => $param['sourceUser'],
  188. 'name' => $param['sourceUser'],
  189. ],
  190. ])
  191. ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The ownership transfer of {path} from {user} failed.')));
  192. return $notification;
  193. }
  194. public function handleTransferOwnershipDoneSource(INotification $notification, string $languageCode): INotification {
  195. $l = $this->l10nFactory->get('files', $languageCode);
  196. $param = $notification->getSubjectParameters();
  197. $notification->setRichSubject($l->t('Ownership transfer done'))
  198. ->setParsedSubject($l->t('Ownership transfer done'))
  199. ->setRichMessage(
  200. $l->t('Your ownership transfer of {path} to {user} has completed.'),
  201. [
  202. 'path' => [
  203. 'type' => 'highlight',
  204. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  205. 'name' => $param['nodeName'],
  206. ],
  207. 'user' => [
  208. 'type' => 'user',
  209. 'id' => $param['targetUser'],
  210. 'name' => $param['targetUser'],
  211. ],
  212. ])
  213. ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your ownership transfer of {path} to {user} has completed.')));
  214. return $notification;
  215. }
  216. public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification {
  217. $l = $this->l10nFactory->get('files', $languageCode);
  218. $param = $notification->getSubjectParameters();
  219. $notification->setRichSubject($l->t('Ownership transfer done'))
  220. ->setParsedSubject($l->t('Ownership transfer done'))
  221. ->setRichMessage(
  222. $l->t('The ownership transfer of {path} from {user} has completed.'),
  223. [
  224. 'path' => [
  225. 'type' => 'highlight',
  226. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  227. 'name' => $param['nodeName'],
  228. ],
  229. 'user' => [
  230. 'type' => 'user',
  231. 'id' => $param['sourceUser'],
  232. 'name' => $param['sourceUser'],
  233. ],
  234. ])
  235. ->setParsedMessage(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The ownership transfer of {path} from {user} has completed.')));
  236. return $notification;
  237. }
  238. public function dismissNotification(INotification $notification): void {
  239. if ($notification->getApp() !== 'files') {
  240. throw new \InvalidArgumentException('Unhandled app');
  241. }
  242. // TODO: This should all be moved to a service that also the transferownershipContoller uses.
  243. try {
  244. $transferOwnership = $this->mapper->getById((int)$notification->getObjectId());
  245. } catch (DoesNotExistException $e) {
  246. return;
  247. }
  248. $notification = $this->notificationManager->createNotification();
  249. $notification->setUser($transferOwnership->getSourceUser())
  250. ->setApp('files')
  251. ->setDateTime($this->timeFactory->getDateTime())
  252. ->setSubject('transferownershipRequestDenied', [
  253. 'sourceUser' => $transferOwnership->getSourceUser(),
  254. 'targetUser' => $transferOwnership->getTargetUser(),
  255. 'nodeName' => $transferOwnership->getNodeName()
  256. ])
  257. ->setObject('transfer', (string)$transferOwnership->getId());
  258. $this->notificationManager->notify($notification);
  259. $this->mapper->delete($transferOwnership);
  260. }
  261. }