Notifier.php 4.8 KB

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