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.6KB

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