EntityCollection.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 Joas Schilling <coding@schilljs.com>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\DAV\Comments;
  25. use OCP\Comments\ICommentsManager;
  26. use OCP\Comments\NotFoundException;
  27. use OCP\IUserManager;
  28. use OCP\IUserSession;
  29. use Psr\Log\LoggerInterface;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAV\IProperties;
  32. use Sabre\DAV\PropPatch;
  33. /**
  34. * Class EntityCollection
  35. *
  36. * this represents a specific holder of comments, identified by an entity type
  37. * (class member $name) and an entity id (class member $id).
  38. *
  39. * @package OCA\DAV\Comments
  40. */
  41. class EntityCollection extends RootCollection implements IProperties {
  42. public const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker';
  43. /** @var string */
  44. protected $id;
  45. protected LoggerInterface $logger;
  46. /**
  47. * @param string $id
  48. * @param string $name
  49. * @param ICommentsManager $commentsManager
  50. * @param IUserManager $userManager
  51. * @param IUserSession $userSession
  52. * @param LoggerInterface $logger
  53. */
  54. public function __construct(
  55. $id,
  56. $name,
  57. ICommentsManager $commentsManager,
  58. IUserManager $userManager,
  59. IUserSession $userSession,
  60. LoggerInterface $logger
  61. ) {
  62. foreach (['id', 'name'] as $property) {
  63. $$property = trim($$property);
  64. if (empty($$property) || !is_string($$property)) {
  65. throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string');
  66. }
  67. }
  68. $this->id = $id;
  69. $this->name = $name;
  70. $this->commentsManager = $commentsManager;
  71. $this->logger = $logger;
  72. $this->userManager = $userManager;
  73. $this->userSession = $userSession;
  74. }
  75. /**
  76. * returns the ID of this entity
  77. *
  78. * @return string
  79. */
  80. public function getId() {
  81. return $this->id;
  82. }
  83. /**
  84. * Returns a specific child node, referenced by its name
  85. *
  86. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  87. * exist.
  88. *
  89. * @param string $name
  90. * @return \Sabre\DAV\INode
  91. * @throws NotFound
  92. */
  93. public function getChild($name) {
  94. try {
  95. $comment = $this->commentsManager->get($name);
  96. return new CommentNode(
  97. $this->commentsManager,
  98. $comment,
  99. $this->userManager,
  100. $this->userSession,
  101. $this->logger
  102. );
  103. } catch (NotFoundException $e) {
  104. throw new NotFound();
  105. }
  106. }
  107. /**
  108. * Returns an array with all the child nodes
  109. *
  110. * @return \Sabre\DAV\INode[]
  111. */
  112. public function getChildren() {
  113. return $this->findChildren();
  114. }
  115. /**
  116. * Returns an array of comment nodes. Result can be influenced by offset,
  117. * limit and date time parameters.
  118. *
  119. * @param int $limit
  120. * @param int $offset
  121. * @param \DateTime|null $datetime
  122. * @return CommentNode[]
  123. */
  124. public function findChildren($limit = 0, $offset = 0, \DateTime $datetime = null) {
  125. $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime);
  126. $result = [];
  127. foreach ($comments as $comment) {
  128. $result[] = new CommentNode(
  129. $this->commentsManager,
  130. $comment,
  131. $this->userManager,
  132. $this->userSession,
  133. $this->logger
  134. );
  135. }
  136. return $result;
  137. }
  138. /**
  139. * Checks if a child-node with the specified name exists
  140. *
  141. * @param string $name
  142. * @return bool
  143. */
  144. public function childExists($name) {
  145. try {
  146. $this->commentsManager->get($name);
  147. return true;
  148. } catch (NotFoundException $e) {
  149. return false;
  150. }
  151. }
  152. /**
  153. * Sets the read marker to the specified date for the logged in user
  154. */
  155. public function setReadMarker(?string $value): bool {
  156. $dateTime = new \DateTime($value ?? 'now');
  157. $user = $this->userSession->getUser();
  158. $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user);
  159. return true;
  160. }
  161. /**
  162. * @inheritdoc
  163. */
  164. public function propPatch(PropPatch $propPatch) {
  165. $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']);
  166. }
  167. /**
  168. * @inheritdoc
  169. */
  170. public function getProperties($properties) {
  171. $marker = null;
  172. $user = $this->userSession->getUser();
  173. if (!is_null($user)) {
  174. $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user);
  175. }
  176. return [self::PROPERTY_NAME_READ_MARKER => $marker];
  177. }
  178. }