Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CommentPropertiesPlugin.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
  43. $this->commentsManager = $commentsManager;
  44. $this->userSession = $userSession;
  45. }
  46. /**
  47. * This initializes the plugin.
  48. *
  49. * This function is called by Sabre\DAV\Server, after
  50. * addPlugin is called.
  51. *
  52. * This method should set up the required event subscriptions.
  53. *
  54. * @param \Sabre\DAV\Server $server
  55. * @return void
  56. */
  57. public function initialize(\Sabre\DAV\Server $server) {
  58. $this->server = $server;
  59. $this->server->on('propFind', [$this, 'handleGetProperties']);
  60. }
  61. private function cacheDirectory(Directory $directory) {
  62. $children = $directory->getChildren();
  63. $ids = [];
  64. foreach ($children as $child) {
  65. if (!($child instanceof File || $child instanceof Directory)) {
  66. continue;
  67. }
  68. $id = $child->getId();
  69. if ($id === null) {
  70. continue;
  71. }
  72. $ids[] = (string)$id;
  73. }
  74. $ids[] = (string) $directory->getId();
  75. $unread = $this->commentsManager->getNumberOfUnreadCommentsForObjects('files', $ids, $this->userSession->getUser());
  76. foreach ($unread as $id => $count) {
  77. $this->cachedUnreadCount[(int)$id] = $count;
  78. }
  79. }
  80. /**
  81. * Adds tags and favorites properties to the response,
  82. * if requested.
  83. *
  84. * @param PropFind $propFind
  85. * @param \Sabre\DAV\INode $node
  86. * @return void
  87. */
  88. public function handleGetProperties(
  89. PropFind $propFind,
  90. \Sabre\DAV\INode $node
  91. ) {
  92. if (!($node instanceof File) && !($node instanceof Directory)) {
  93. return;
  94. }
  95. // need prefetch ?
  96. if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
  97. && $propFind->getDepth() !== 0
  98. && !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
  99. ) {
  100. $this->cacheDirectory($node);
  101. }
  102. $propFind->handle(self::PROPERTY_NAME_COUNT, function () use ($node) {
  103. return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId());
  104. });
  105. $propFind->handle(self::PROPERTY_NAME_HREF, function () use ($node) {
  106. return $this->getCommentsLink($node);
  107. });
  108. $propFind->handle(self::PROPERTY_NAME_UNREAD, function () use ($node) {
  109. if (isset($this->cachedUnreadCount[$node->getId()])) {
  110. return $this->cachedUnreadCount[$node->getId()];
  111. }
  112. return $this->getUnreadCount($node);
  113. });
  114. }
  115. /**
  116. * returns a reference to the comments node
  117. *
  118. * @param Node $node
  119. * @return mixed|string
  120. */
  121. public function getCommentsLink(Node $node) {
  122. $href = $this->server->getBaseUri();
  123. $entryPoint = strpos($href, '/remote.php/');
  124. if ($entryPoint === false) {
  125. // in case we end up somewhere else, unexpectedly.
  126. return null;
  127. }
  128. $commentsPart = 'dav/comments/files/' . rawurldecode($node->getId());
  129. $href = substr_replace($href, $commentsPart, $entryPoint + strlen('/remote.php/'));
  130. return $href;
  131. }
  132. /**
  133. * returns the number of unread comments for the currently logged in user
  134. * on the given file or directory node
  135. *
  136. * @param Node $node
  137. * @return Int|null
  138. */
  139. public function getUnreadCount(Node $node) {
  140. $user = $this->userSession->getUser();
  141. if (is_null($user)) {
  142. return null;
  143. }
  144. $lastRead = $this->commentsManager->getReadMark('files', (string)$node->getId(), $user);
  145. return $this->commentsManager->getNumberOfCommentsForObject('files', (string)$node->getId(), $lastRead);
  146. }
  147. }