NotificationsController.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Comments\Controller;
  7. use OCP\AppFramework\Controller;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\Attribute\OpenAPI;
  10. use OCP\AppFramework\Http\NotFoundResponse;
  11. use OCP\AppFramework\Http\RedirectResponse;
  12. use OCP\Comments\IComment;
  13. use OCP\Comments\ICommentsManager;
  14. use OCP\Files\IRootFolder;
  15. use OCP\IRequest;
  16. use OCP\IURLGenerator;
  17. use OCP\IUser;
  18. use OCP\IUserSession;
  19. use OCP\Notification\IManager;
  20. /**
  21. * @package OCA\Comments\Controller
  22. */
  23. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  24. class NotificationsController extends Controller {
  25. public function __construct(
  26. string $appName,
  27. IRequest $request,
  28. protected ICommentsManager $commentsManager,
  29. protected IRootFolder $rootFolder,
  30. protected IURLGenerator $urlGenerator,
  31. protected IManager $notificationManager,
  32. protected IUserSession $userSession
  33. ) {
  34. parent::__construct($appName, $request);
  35. }
  36. /**
  37. * @PublicPage
  38. * @NoCSRFRequired
  39. *
  40. * View a notification
  41. *
  42. * @param string $id ID of the notification
  43. *
  44. * @return RedirectResponse<Http::STATUS_SEE_OTHER, array{}>|NotFoundResponse<Http::STATUS_NOT_FOUND, array{}>
  45. *
  46. * 303: Redirected to notification
  47. * 404: Notification not found
  48. */
  49. public function view(string $id): RedirectResponse|NotFoundResponse {
  50. $currentUser = $this->userSession->getUser();
  51. if (!$currentUser instanceof IUser) {
  52. return new RedirectResponse(
  53. $this->urlGenerator->linkToRoute('core.login.showLoginForm', [
  54. 'redirect_url' => $this->urlGenerator->linkToRoute(
  55. 'comments.Notifications.view',
  56. ['id' => $id]
  57. ),
  58. ])
  59. );
  60. }
  61. try {
  62. $comment = $this->commentsManager->get($id);
  63. if ($comment->getObjectType() !== 'files') {
  64. return new NotFoundResponse();
  65. }
  66. $userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
  67. $files = $userFolder->getById((int)$comment->getObjectId());
  68. $this->markProcessed($comment, $currentUser);
  69. if (empty($files)) {
  70. return new NotFoundResponse();
  71. }
  72. $url = $this->urlGenerator->linkToRouteAbsolute(
  73. 'files.viewcontroller.showFile',
  74. [ 'fileid' => $comment->getObjectId() ]
  75. );
  76. return new RedirectResponse($url);
  77. } catch (\Exception $e) {
  78. return new NotFoundResponse();
  79. }
  80. }
  81. /**
  82. * Marks the notification about a comment as processed
  83. */
  84. protected function markProcessed(IComment $comment, IUser $currentUser): void {
  85. $notification = $this->notificationManager->createNotification();
  86. $notification->setApp('comments')
  87. ->setObject('comment', $comment->getId())
  88. ->setSubject('mention')
  89. ->setUser($currentUser->getUID());
  90. $this->notificationManager->markProcessed($notification);
  91. }
  92. }