commentpropertiesplugin.php 3.6 KB

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