Notifications.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Comments\Controller;
  26. use OCP\AppFramework\Controller;
  27. use OCP\AppFramework\Http\NotFoundResponse;
  28. use OCP\AppFramework\Http\RedirectResponse;
  29. use OCP\AppFramework\Http\Response;
  30. use OCP\Comments\IComment;
  31. use OCP\Comments\ICommentsManager;
  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. protected IRootFolder $rootFolder;
  45. protected ICommentsManager $commentsManager;
  46. protected IURLGenerator $urlGenerator;
  47. protected IManager $notificationManager;
  48. protected IUserSession $userSession;
  49. /**
  50. * Notifications constructor.
  51. */
  52. public function __construct(
  53. string $appName,
  54. IRequest $request,
  55. ICommentsManager $commentsManager,
  56. IRootFolder $rootFolder,
  57. IURLGenerator $urlGenerator,
  58. IManager $notificationManager,
  59. IUserSession $userSession
  60. ) {
  61. parent::__construct($appName, $request);
  62. $this->commentsManager = $commentsManager;
  63. $this->rootFolder = $rootFolder;
  64. $this->urlGenerator = $urlGenerator;
  65. $this->notificationManager = $notificationManager;
  66. $this->userSession = $userSession;
  67. }
  68. /**
  69. * @PublicPage
  70. * @NoCSRFRequired
  71. */
  72. public function view(string $id): Response {
  73. $currentUser = $this->userSession->getUser();
  74. if (!$currentUser instanceof IUser) {
  75. return new RedirectResponse(
  76. $this->urlGenerator->linkToRoute('core.login.showLoginForm', [
  77. 'redirect_url' => $this->urlGenerator->linkToRoute(
  78. 'comments.Notifications.view',
  79. ['id' => $id]
  80. ),
  81. ])
  82. );
  83. }
  84. try {
  85. $comment = $this->commentsManager->get($id);
  86. if ($comment->getObjectType() !== 'files') {
  87. return new NotFoundResponse();
  88. }
  89. $userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
  90. $files = $userFolder->getById((int)$comment->getObjectId());
  91. $this->markProcessed($comment, $currentUser);
  92. if (empty($files)) {
  93. return new NotFoundResponse();
  94. }
  95. $url = $this->urlGenerator->linkToRouteAbsolute(
  96. 'files.viewcontroller.showFile',
  97. [ 'fileid' => $comment->getObjectId() ]
  98. );
  99. return new RedirectResponse($url);
  100. } catch (\Exception $e) {
  101. return new NotFoundResponse();
  102. }
  103. }
  104. /**
  105. * Marks the notification about a comment as processed
  106. */
  107. protected function markProcessed(IComment $comment, IUser $currentUser): void {
  108. $notification = $this->notificationManager->createNotification();
  109. $notification->setApp('comments')
  110. ->setObject('comment', $comment->getId())
  111. ->setSubject('mention')
  112. ->setUser($currentUser->getUID());
  113. $this->notificationManager->markProcessed($notification);
  114. }
  115. }