CommentPropertiesPlugin.php 3.6 KB

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