CommentPropertiesPlugin.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use OCP\Comments\ICommentsManager;
  29. use OCP\IUserSession;
  30. use Sabre\DAV\PropFind;
  31. use Sabre\DAV\ServerPlugin;
  32. use Sabre\DAV\Server;
  33. class CommentPropertiesPlugin extends ServerPlugin {
  34. public const PROPERTY_NAME_HREF = '{http://owncloud.org/ns}comments-href';
  35. public const PROPERTY_NAME_COUNT = '{http://owncloud.org/ns}comments-count';
  36. public const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}comments-unread';
  37. protected ?Server $server = null;
  38. private ICommentsManager $commentsManager;
  39. private IUserSession $userSession;
  40. private array $cachedUnreadCount = [];
  41. public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
  42. $this->commentsManager = $commentsManager;
  43. $this->userSession = $userSession;
  44. }
  45. /**
  46. * This initializes the plugin.
  47. *
  48. * This function is called by Sabre\DAV\Server, after
  49. * addPlugin is called.
  50. *
  51. * This method should set up the required event subscriptions.
  52. *
  53. * @param \Sabre\DAV\Server $server
  54. * @return void
  55. */
  56. public function initialize(\Sabre\DAV\Server $server) {
  57. $this->server = $server;
  58. $this->server->on('propFind', [$this, 'handleGetProperties']);
  59. }
  60. private function cacheDirectory(Directory $directory): void {
  61. $children = $directory->getChildren();
  62. $ids = [];
  63. foreach ($children as $child) {
  64. if (!($child instanceof File || $child instanceof Directory)) {
  65. continue;
  66. }
  67. $id = $child->getId();
  68. if ($id === null) {
  69. continue;
  70. }
  71. $ids[] = (string)$id;
  72. }
  73. $ids[] = (string) $directory->getId();
  74. $unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser());
  75. foreach ($unread as $id => $count) {
  76. $this->cachedUnreadCount[(int)$id] = $count;
  77. }
  78. }
  79. /**
  80. * Adds tags and favorites properties to the response,
  81. * if requested.
  82. *
  83. * @param PropFind $propFind
  84. * @param \Sabre\DAV\INode $node
  85. * @return void
  86. */
  87. public function handleGetProperties(
  88. PropFind $propFind,
  89. \Sabre\DAV\INode $node
  90. ) {
  91. if (!($node instanceof File) && !($node instanceof Directory)) {
  92. return;
  93. }
  94. // need prefetch ?
  95. if ($node instanceof Directory
  96. && $propFind->getDepth() !== 0
  97. && !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
  98. ) {
  99. $this->cacheDirectory($node);
  100. }
  101. $propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node): int {
  102. return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
  103. });
  104. $propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node): ?string {
  105. return $this->getCommentsLink($node);
  106. });
  107. $propFind->handle(self::PROPERTY_NAME_UNREAD, function () use ($node): ?int {
  108. return $this->cachedUnreadCount[$node->getId()] ?? $this->getUnreadCount($node);
  109. });
  110. }
  111. /**
  112. * Returns a reference to the comments node
  113. */
  114. public function getCommentsLink(Node $node): ?string {
  115. $href = $this->server->getBaseUri();
  116. $entryPoint = strpos($href, '/remote.php/');
  117. if ($entryPoint === false) {
  118. // in case we end up somewhere else, unexpectedly.
  119. return null;
  120. }
  121. $commentsPart = 'dav/comments/files/' . rawurldecode((string)$node->getId());
  122. return substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
  123. }
  124. /**
  125. * Returns the number of unread comments for the currently logged in user
  126. * on the given file or directory node
  127. */
  128. public function getUnreadCount(Node $node): ?int {
  129. $user = $this->userSession->getUser();
  130. if (is_null($user)) {
  131. return null;
  132. }
  133. $lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
  134. return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
  135. }
  136. }