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.

Notifications.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  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\Comments\Controller;
  25. use OCP\AppFramework\Controller;
  26. use OCP\AppFramework\Http\NotFoundResponse;
  27. use OCP\AppFramework\Http\RedirectResponse;
  28. use OCP\AppFramework\Http\Response;
  29. use OCP\Comments\IComment;
  30. use OCP\Comments\ICommentsManager;
  31. use OCP\Files\IRootFolder;
  32. use OCP\IRequest;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\IUserSession;
  36. use OCP\Notification\IManager;
  37. /**
  38. * Class Notifications
  39. *
  40. * @package OCA\Comments\Controller
  41. */
  42. class Notifications extends Controller {
  43. /** @var IRootFolder */
  44. protected $rootFolder;
  45. /** @var ICommentsManager */
  46. protected $commentsManager;
  47. /** @var IURLGenerator */
  48. protected $urlGenerator;
  49. /** @var IManager */
  50. protected $notificationManager;
  51. /** @var IUserSession */
  52. protected $userSession;
  53. /**
  54. * Notifications constructor.
  55. *
  56. * @param string $appName
  57. * @param IRequest $request
  58. * @param ICommentsManager $commentsManager
  59. * @param IRootFolder $rootFolder
  60. * @param IURLGenerator $urlGenerator
  61. * @param IManager $notificationManager
  62. * @param IUserSession $userSession
  63. */
  64. public function __construct(
  65. $appName,
  66. IRequest $request,
  67. ICommentsManager $commentsManager,
  68. IRootFolder $rootFolder,
  69. IURLGenerator $urlGenerator,
  70. IManager $notificationManager,
  71. IUserSession $userSession
  72. ) {
  73. parent::__construct($appName, $request);
  74. $this->commentsManager = $commentsManager;
  75. $this->rootFolder = $rootFolder;
  76. $this->urlGenerator = $urlGenerator;
  77. $this->notificationManager = $notificationManager;
  78. $this->userSession = $userSession;
  79. }
  80. /**
  81. * @PublicPage
  82. * @NoCSRFRequired
  83. *
  84. * @param string $id the comment ID
  85. * @return Response
  86. */
  87. public function view($id) {
  88. $currentUser = $this->userSession->getUser();
  89. if (!$currentUser instanceof IUser) {
  90. return new RedirectResponse(
  91. $this->urlGenerator->linkToRoute('core.login.showLoginForm', [
  92. 'redirect_url' => $this->urlGenerator->linkToRoute(
  93. 'comments.Notifications.view',
  94. ['id' => $id]
  95. ),
  96. ])
  97. );
  98. }
  99. try {
  100. $comment = $this->commentsManager->get($id);
  101. if ($comment->getObjectType() !== 'files') {
  102. return new NotFoundResponse();
  103. }
  104. $userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
  105. $files = $userFolder->getById((int)$comment->getObjectId());
  106. $this->markProcessed($comment, $currentUser);
  107. if (empty($files)) {
  108. return new NotFoundResponse();
  109. }
  110. $url = $this->urlGenerator->linkToRouteAbsolute(
  111. 'files.viewcontroller.showFile',
  112. [ 'fileid' => $comment->getObjectId() ]
  113. );
  114. return new RedirectResponse($url);
  115. } catch (\Exception $e) {
  116. return new NotFoundResponse();
  117. }
  118. }
  119. /**
  120. * Marks the notification about a comment as processed
  121. * @param IComment $comment
  122. * @param IUser $currentUser
  123. */
  124. protected function markProcessed(IComment $comment, IUser $currentUser) {
  125. $notification = $this->notificationManager->createNotification();
  126. $notification->setApp('comments')
  127. ->setObject('comment', $comment->getId())
  128. ->setSubject('mention')
  129. ->setUser($currentUser->getUID());
  130. $this->notificationManager->markProcessed($notification);
  131. }
  132. }