NotificationsController.php 2.8 KB

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