Notifier.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Files\Notification;
  25. use OCP\IURLGenerator;
  26. use OCP\L10N\IFactory;
  27. use OCP\Notification\IAction;
  28. use OCP\Notification\INotification;
  29. use OCP\Notification\INotifier;
  30. class Notifier implements INotifier {
  31. /** @var IFactory */
  32. protected $l10nFactory;
  33. /** @var IURLGenerator */
  34. protected $urlGenerator;
  35. /**
  36. * @param IFactory $l10nFactory
  37. * @param IURLGenerator $urlGenerator
  38. */
  39. public function __construct(IFactory $l10nFactory, IURLGenerator $urlGenerator) {
  40. $this->l10nFactory = $l10nFactory;
  41. $this->urlGenerator = $urlGenerator;
  42. }
  43. public function getID(): string {
  44. return 'files';
  45. }
  46. public function getName(): string {
  47. return $this->l10nFactory->get('files')->t('Files');
  48. }
  49. /**
  50. * @param INotification $notification
  51. * @param string $languageCode The code of the language that should be used to prepare the notification
  52. * @return INotification
  53. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  54. */
  55. public function prepare(INotification $notification, string $languageCode): INotification {
  56. if ($notification->getApp() !== 'files') {
  57. throw new \InvalidArgumentException('Unhandled app');
  58. }
  59. if ($notification->getSubject() === 'transferownershipRequest') {
  60. return $this->handleTransferownershipRequest($notification, $languageCode);
  61. }
  62. if ($notification->getSubject() === 'transferOwnershipFailedSource') {
  63. return $this->handleTransferOwnershipFailedSource($notification, $languageCode);
  64. }
  65. if ($notification->getSubject() === 'transferOwnershipFailedTarget') {
  66. return $this->handleTransferOwnershipFailedTarget($notification, $languageCode);
  67. }
  68. if ($notification->getSubject() === 'transferOwnershipDoneSource') {
  69. return $this->handleTransferOwnershipDoneSource($notification, $languageCode);
  70. }
  71. if ($notification->getSubject() === 'transferOwnershipDoneTarget') {
  72. return $this->handleTransferOwnershipDoneTarget($notification, $languageCode);
  73. }
  74. throw new \InvalidArgumentException('Unhandled subject');
  75. }
  76. public function handleTransferownershipRequest(INotification $notification, string $languageCode): INotification {
  77. $l = $this->l10nFactory->get('files', $languageCode);
  78. $id = $notification->getObjectId();
  79. $param = $notification->getSubjectParameters();
  80. $approveAction = $notification->createAction()
  81. ->setParsedLabel($l->t('Accept'))
  82. ->setPrimary(true)
  83. ->setLink(
  84. $this->urlGenerator->getAbsoluteURL(
  85. $this->urlGenerator->linkTo(
  86. '',
  87. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  88. )
  89. ),
  90. IAction::TYPE_POST
  91. );
  92. $disapproveAction = $notification->createAction()
  93. ->setParsedLabel($l->t('Decline'))
  94. ->setPrimary(false)
  95. ->setLink(
  96. $this->urlGenerator->getAbsoluteURL(
  97. $this->urlGenerator->linkTo(
  98. '',
  99. 'ocs/v2.php/apps/files/api/v1/transferownership/' . $id
  100. )
  101. ),
  102. IAction::TYPE_DELETE
  103. );
  104. $notification->addParsedAction($approveAction)
  105. ->addParsedAction($disapproveAction)
  106. ->setRichSubject(
  107. $l->t('Incomming file transfer from {user}'),
  108. [
  109. 'user' => [
  110. 'type' => 'user',
  111. 'id' => $param['sourceUser'],
  112. 'name' => $param['sourceUser'],
  113. ],
  114. ])
  115. ->setParsedSubject(str_replace('{user}', $param['sourceUser'], $l->t('Incomming file transfer from {user}')))
  116. ->setRichMessage(
  117. $l->t('Do you want to accept {path}?'),
  118. [
  119. 'path' => [
  120. 'type' => 'highlight',
  121. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  122. 'name' => $param['nodeName'],
  123. ]
  124. ])
  125. ->setParsedMessage(str_replace('{path}', $param['nodeName'], $l->t('Do you want to accept {path}?')));
  126. return $notification;
  127. }
  128. public function handleTransferOwnershipFailedSource(INotification $notification, string $languageCode): INotification {
  129. $l = $this->l10nFactory->get('files', $languageCode);
  130. $param = $notification->getSubjectParameters();
  131. $notification->setRichSubject($l->t('File transfer failed'))
  132. ->setParsedSubject(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your transfer of {path} to {user} failed.')))
  133. ->setRichMessage(
  134. $l->t('Your transfer of {path} to {user} failed.'),
  135. [
  136. 'path' => [
  137. 'type' => 'highlight',
  138. 'id' => $param['targetUser'] . '::' . $param['nodeName'],
  139. 'name' => $param['nodeName'],
  140. ],
  141. 'user' => [
  142. 'type' => 'user',
  143. 'id' => $param['targetUser'],
  144. 'name' => $param['targetUser'],
  145. ],
  146. ])
  147. ->setParsedMessage($l->t('File transfer failed'));
  148. return $notification;
  149. }
  150. public function handleTransferOwnershipFailedTarget(INotification $notification, string $languageCode): INotification {
  151. $l = $this->l10nFactory->get('files', $languageCode);
  152. $param = $notification->getSubjectParameters();
  153. $notification->setRichSubject($l->t('File transfer failed'))
  154. ->setParsedSubject(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The transfer of {path} from {user} failed.')))
  155. ->setRichMessage(
  156. $l->t('The transfer of {path} from {user} failed.'),
  157. [
  158. 'path' => [
  159. 'type' => 'highlight',
  160. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  161. 'name' => $param['nodeName'],
  162. ],
  163. 'user' => [
  164. 'type' => 'user',
  165. 'id' => $param['sourceUser'],
  166. 'name' => $param['sourceUser'],
  167. ],
  168. ])
  169. ->setParsedMessage($l->t('File transfer failed'));
  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. $notification->setRichSubject($l->t('File transfer done'))
  176. ->setParsedSubject(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['targetUser']], $l->t('Your transfer of {path} to {user} has completed.')))
  177. ->setRichMessage(
  178. $l->t('Your 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' => $param['targetUser'],
  188. 'name' => $param['targetUser'],
  189. ],
  190. ])
  191. ->setParsedMessage($l->t('File transfer done'));
  192. return $notification;
  193. }
  194. public function handleTransferOwnershipDoneTarget(INotification $notification, string $languageCode): INotification {
  195. $l = $this->l10nFactory->get('files', $languageCode);
  196. $param = $notification->getSubjectParameters();
  197. $notification->setRichSubject($l->t('File transfer done'))
  198. ->setParsedSubject(str_replace(['{path}', '{user}'], [$param['nodeName'], $param['sourceUser']], $l->t('The transfer of {path} from {user} has completed.')))
  199. ->setRichMessage(
  200. $l->t('The transfer of {path} from {user} has completed.'),
  201. [
  202. 'path' => [
  203. 'type' => 'highlight',
  204. 'id' => $param['sourceUser'] . '::' . $param['nodeName'],
  205. 'name' => $param['nodeName'],
  206. ],
  207. 'user' => [
  208. 'type' => 'user',
  209. 'id' => $param['sourceUser'],
  210. 'name' => $param['sourceUser'],
  211. ],
  212. ])
  213. ->setParsedMessage($l->t('File transfer done'));
  214. return $notification;
  215. }
  216. }