You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.9 KiB

  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\IRootFolder;
  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 IRootFolder */
  34. protected $rootFolder;
  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. IRootFolder $rootFolder,
  44. ICommentsManager $commentsManager,
  45. IURLGenerator $url,
  46. IUserManager $userManager
  47. ) {
  48. $this->l10nFactory = $l10nFactory;
  49. $this->rootFolder = $rootFolder;
  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. $userFolder = $this->rootFolder->getUserFolder($notification->getUser());
  86. $nodes = $userFolder->getById((int)$parameters[1]);
  87. if(empty($nodes)) {
  88. throw new \InvalidArgumentException('Cannot resolve file id to Node instance');
  89. }
  90. $node = $nodes[0];
  91. if ($isDeletedActor) {
  92. $notification->setParsedSubject($l->t(
  93. 'A (now) deleted user mentioned you in a comment on “%s”',
  94. [$node->getName()]
  95. ))
  96. ->setRichSubject(
  97. $l->t('A (now) deleted user mentioned you in a comment on “{file}”'),
  98. [
  99. 'file' => [
  100. 'type' => 'file',
  101. 'id' => $comment->getObjectId(),
  102. 'name' => $node->getName(),
  103. 'path' => $node->getPath(),
  104. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]),
  105. ],
  106. ]
  107. );
  108. } else {
  109. $notification->setParsedSubject($l->t(
  110. '%1$s mentioned you in a comment on “%2$s”',
  111. [$displayName, $node->getName()]
  112. ))
  113. ->setRichSubject(
  114. $l->t('{user} mentioned you in a comment on “{file}”'),
  115. [
  116. 'user' => [
  117. 'type' => 'user',
  118. 'id' => $comment->getActorId(),
  119. 'name' => $displayName,
  120. ],
  121. 'file' => [
  122. 'type' => 'file',
  123. 'id' => $comment->getObjectId(),
  124. 'name' => $node->getName(),
  125. 'path' => $node->getPath(),
  126. 'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $comment->getObjectId()]),
  127. ],
  128. ]
  129. );
  130. }
  131. $notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/comment.svg')))
  132. ->setLink($this->url->linkToRouteAbsolute(
  133. 'comments.Notifications.view',
  134. ['id' => $comment->getId()])
  135. );
  136. return $notification;
  137. break;
  138. default:
  139. throw new \InvalidArgumentException('Invalid subject');
  140. }
  141. }
  142. }