Notifier.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Comments\Notification;
  25. use OCP\Comments\ICommentsManager;
  26. use OCP\Comments\NotFoundException;
  27. use OCP\Files\IRootFolder;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserManager;
  30. use OCP\L10N\IFactory;
  31. use OCP\Notification\INotification;
  32. use OCP\Notification\INotifier;
  33. class Notifier implements INotifier {
  34. /** @var IFactory */
  35. protected $l10nFactory;
  36. /** @var IRootFolder */
  37. protected $rootFolder;
  38. /** @var ICommentsManager */
  39. protected $commentsManager;
  40. /** @var IURLGenerator */
  41. protected $url;
  42. /** @var IUserManager */
  43. protected $userManager;
  44. public function __construct(
  45. IFactory $l10nFactory,
  46. IRootFolder $rootFolder,
  47. ICommentsManager $commentsManager,
  48. IURLGenerator $url,
  49. IUserManager $userManager
  50. ) {
  51. $this->l10nFactory = $l10nFactory;
  52. $this->rootFolder = $rootFolder;
  53. $this->commentsManager = $commentsManager;
  54. $this->url = $url;
  55. $this->userManager = $userManager;
  56. }
  57. /**
  58. * @param INotification $notification
  59. * @param string $languageCode The code of the language that should be used to prepare the notification
  60. * @return INotification
  61. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  62. */
  63. public function prepare(INotification $notification, $languageCode) {
  64. if($notification->getApp() !== 'comments') {
  65. throw new \InvalidArgumentException();
  66. }
  67. try {
  68. $comment = $this->commentsManager->get($notification->getObjectId());
  69. } catch(NotFoundException $e) {
  70. // needs to be converted to InvalidArgumentException, otherwise none Notifications will be shown at all
  71. throw new \InvalidArgumentException('Comment not found', 0, $e);
  72. }
  73. $l = $this->l10nFactory->get('comments', $languageCode);
  74. $displayName = $comment->getActorId();
  75. $isDeletedActor = $comment->getActorType() === ICommentsManager::DELETED_USER;
  76. if($comment->getActorType() === 'users') {
  77. $commenter = $this->userManager->get($comment->getActorId());
  78. if(!is_null($commenter)) {
  79. $displayName = $commenter->getDisplayName();
  80. }
  81. }
  82. switch($notification->getSubject()) {
  83. case 'mention':
  84. $parameters = $notification->getSubjectParameters();
  85. if($parameters[0] !== 'files') {
  86. throw new \InvalidArgumentException('Unsupported comment object');
  87. }
  88. $userFolder = $this->rootFolder->getUserFolder($notification->getUser());
  89. $nodes = $userFolder->getById((int)$parameters[1]);
  90. if(empty($nodes)) {
  91. throw new \InvalidArgumentException('Cannot resolve file id to Node instance');
  92. }
  93. $node = $nodes[0];
  94. if ($isDeletedActor) {
  95. $notification->setParsedSubject($l->t(
  96. 'A (now) deleted user mentioned you in a comment on “%s”',
  97. [$node->getName()]
  98. ))
  99. ->setRichSubject(
  100. $l->t('A (now) deleted user mentioned you in a comment on “{file}”'),
  101. [
  102. 'file' => [
  103. 'type' => 'file',
  104. 'id' => $comment->getObjectId(),
  105. 'name' => $node->getName(),
  106. 'path' => $node->getPath(),
  107. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]),
  108. ],
  109. ]
  110. );
  111. } else {
  112. $notification->setParsedSubject($l->t(
  113. '%1$s mentioned you in a comment on “%2$s”',
  114. [$displayName, $node->getName()]
  115. ))
  116. ->setRichSubject(
  117. $l->t('{user} mentioned you in a comment on “{file}”'),
  118. [
  119. 'user' => [
  120. 'type' => 'user',
  121. 'id' => $comment->getActorId(),
  122. 'name' => $displayName,
  123. ],
  124. 'file' => [
  125. 'type' => 'file',
  126. 'id' => $comment->getObjectId(),
  127. 'name' => $node->getName(),
  128. 'path' => $node->getPath(),
  129. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]),
  130. ],
  131. ]
  132. );
  133. }
  134. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')))
  135. ->setLink($this->url->linkToRouteAbsolute(
  136. 'comments.Notifications.view',
  137. ['id' => $comment->getId()])
  138. );
  139. return $notification;
  140. break;
  141. default:
  142. throw new \InvalidArgumentException('Invalid subject');
  143. }
  144. }
  145. }