NotificationsController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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;
  28. use OCP\AppFramework\Http\Attribute\OpenAPI;
  29. use OCP\AppFramework\Http\NotFoundResponse;
  30. use OCP\AppFramework\Http\RedirectResponse;
  31. use OCP\Comments\IComment;
  32. use OCP\Comments\ICommentsManager;
  33. use OCP\Files\IRootFolder;
  34. use OCP\IRequest;
  35. use OCP\IURLGenerator;
  36. use OCP\IUser;
  37. use OCP\IUserSession;
  38. use OCP\Notification\IManager;
  39. /**
  40. * @package OCA\Comments\Controller
  41. */
  42. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  43. class NotificationsController extends Controller {
  44. public function __construct(
  45. string $appName,
  46. IRequest $request,
  47. protected ICommentsManager $commentsManager,
  48. protected IRootFolder $rootFolder,
  49. protected IURLGenerator $urlGenerator,
  50. protected IManager $notificationManager,
  51. protected IUserSession $userSession
  52. ) {
  53. parent::__construct($appName, $request);
  54. }
  55. /**
  56. * @PublicPage
  57. * @NoCSRFRequired
  58. *
  59. * View a notification
  60. *
  61. * @param string $id ID of the notification
  62. *
  63. * @return RedirectResponse<Http::STATUS_SEE_OTHER, array{}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
  64. *
  65. * 303: Redirected to notification
  66. * 404: Notification not found
  67. */
  68. public function view(string $id): RedirectResponse|NotFoundResponse {
  69. $currentUser = $this->userSession->getUser();
  70. if (!$currentUser instanceof IUser) {
  71. return new RedirectResponse(
  72. $this->urlGenerator->linkToRoute('core.login.showLoginForm', [
  73. 'redirect_url' => $this->urlGenerator->linkToRoute(
  74. 'comments.Notifications.view',
  75. ['id' => $id]
  76. ),
  77. ])
  78. );
  79. }
  80. try {
  81. $comment = $this->commentsManager->get($id);
  82. if ($comment->getObjectType() !== 'files') {
  83. return new NotFoundResponse();
  84. }
  85. $userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
  86. $files = $userFolder->getById((int)$comment->getObjectId());
  87. $this->markProcessed($comment, $currentUser);
  88. if (empty($files)) {
  89. return new NotFoundResponse();
  90. }
  91. $url = $this->urlGenerator->linkToRouteAbsolute(
  92. 'files.viewcontroller.showFile',
  93. [ 'fileid' => $comment->getObjectId() ]
  94. );
  95. return new RedirectResponse($url);
  96. } catch (\Exception $e) {
  97. return new NotFoundResponse();
  98. }
  99. }
  100. /**
  101. * Marks the notification about a comment as processed
  102. */
  103. protected function markProcessed(IComment $comment, IUser $currentUser): void {
  104. $notification = $this->notificationManager->createNotification();
  105. $notification->setApp('comments')
  106. ->setObject('comment', $comment->getId())
  107. ->setSubject('mention')
  108. ->setUser($currentUser->getUID());
  109. $this->notificationManager->markProcessed($notification);
  110. }
  111. }