CommentPropertiesPlugin.php 3.7 KB

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