CommentPropertiesPlugin.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\Connector\Sabre;
  9. use OCP\Comments\ICommentsManager;
  10. use OCP\IUserSession;
  11. use Sabre\DAV\PropFind;
  12. use Sabre\DAV\Server;
  13. use Sabre\DAV\ServerPlugin;
  14. class CommentPropertiesPlugin extends ServerPlugin {
  15. public const PROPERTY_NAME_HREF = '{http://owncloud.org/ns}comments-href';
  16. public const PROPERTY_NAME_COUNT = '{http://owncloud.org/ns}comments-count';
  17. public const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread';
  18. protected ?Server $server = null;
  19. private ICommentsManager $commentsManager;
  20. private IUserSession $userSession;
  21. private array $cachedUnreadCount = [];
  22. public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
  23. $this->commentsManager = $commentsManager;
  24. $this->userSession = $userSession;
  25. }
  26. /**
  27. * This initializes the plugin.
  28. *
  29. * This function is called by Sabre\DAV\Server, after
  30. * addPlugin is called.
  31. *
  32. * This method should set up the required event subscriptions.
  33. *
  34. * @param \Sabre\DAV\Server $server
  35. * @return void
  36. */
  37. public function initialize(\Sabre\DAV\Server $server) {
  38. $this->server = $server;
  39. $this->server->on('propFind', [$this, 'handleGetProperties']);
  40. }
  41. private function cacheDirectory(Directory $directory): void {
  42. $children = $directory->getChildren();
  43. $ids = [];
  44. foreach ($children as $child) {
  45. if (!($child instanceof File || $child instanceof Directory)) {
  46. continue;
  47. }
  48. $id = $child->getId();
  49. if ($id === null) {
  50. continue;
  51. }
  52. $ids[] = (string)$id;
  53. }
  54. $ids[] = (string)$directory->getId();
  55. $unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser());
  56. foreach ($unread as $id => $count) {
  57. $this->cachedUnreadCount[(int)$id] = $count;
  58. }
  59. }
  60. /**
  61. * Adds tags and favorites properties to the response,
  62. * if requested.
  63. *
  64. * @param PropFind $propFind
  65. * @param \Sabre\DAV\INode $node
  66. * @return void
  67. */
  68. public function handleGetProperties(
  69. PropFind $propFind,
  70. \Sabre\DAV\INode $node
  71. ) {
  72. if (!($node instanceof File) && !($node instanceof Directory)) {
  73. return;
  74. }
  75. // need prefetch ?
  76. if ($node instanceof Directory
  77. && $propFind->getDepth() !== 0
  78. && !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
  79. ) {
  80. $this->cacheDirectory($node);
  81. }
  82. $propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node): int {
  83. return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
  84. });
  85. $propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node): ?string {
  86. return $this->getCommentsLink($node);
  87. });
  88. $propFind->handle(self::PROPERTY_NAME_UNREAD, function () use ($node): ?int {
  89. return $this->cachedUnreadCount[$node->getId()] ?? $this->getUnreadCount($node);
  90. });
  91. }
  92. /**
  93. * Returns a reference to the comments node
  94. */
  95. public function getCommentsLink(Node $node): ?string {
  96. $href = $this->server->getBaseUri();
  97. $entryPoint = strpos($href, '/remote.php/');
  98. if ($entryPoint === false) {
  99. // in case we end up somewhere else, unexpectedly.
  100. return null;
  101. }
  102. $commentsPart = 'dav/comments/files/' . rawurldecode((string)$node->getId());
  103. return substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
  104. }
  105. /**
  106. * Returns the number of unread comments for the currently logged in user
  107. * on the given file or directory node
  108. */
  109. public function getUnreadCount(Node $node): ?int {
  110. $user = $this->userSession->getUser();
  111. if (is_null($user)) {
  112. return null;
  113. }
  114. $lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
  115. return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
  116. }
  117. }