Notifications.php 3.9 KB

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