You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentPropertiesPlugin.php 4.8KB

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