EntityCollection.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Comments;
  8. use OCP\Comments\ICommentsManager;
  9. use OCP\Comments\NotFoundException;
  10. use OCP\IUserManager;
  11. use OCP\IUserSession;
  12. use Psr\Log\LoggerInterface;
  13. use Sabre\DAV\Exception\NotFound;
  14. use Sabre\DAV\IProperties;
  15. use Sabre\DAV\PropPatch;
  16. /**
  17. * Class EntityCollection
  18. *
  19. * this represents a specific holder of comments, identified by an entity type
  20. * (class member $name) and an entity id (class member $id).
  21. *
  22. * @package OCA\DAV\Comments
  23. */
  24. class EntityCollection extends RootCollection implements IProperties {
  25. public const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker';
  26. /** @var string */
  27. protected $id;
  28. protected LoggerInterface $logger;
  29. /**
  30. * @param string $id
  31. * @param string $name
  32. * @param ICommentsManager $commentsManager
  33. * @param IUserManager $userManager
  34. * @param IUserSession $userSession
  35. * @param LoggerInterface $logger
  36. */
  37. public function __construct(
  38. $id,
  39. $name,
  40. ICommentsManager $commentsManager,
  41. IUserManager $userManager,
  42. IUserSession $userSession,
  43. LoggerInterface $logger,
  44. ) {
  45. foreach (['id', 'name'] as $property) {
  46. $$property = trim($$property);
  47. if (empty($$property) || !is_string($$property)) {
  48. throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string');
  49. }
  50. }
  51. $this->id = $id;
  52. $this->name = $name;
  53. $this->commentsManager = $commentsManager;
  54. $this->logger = $logger;
  55. $this->userManager = $userManager;
  56. $this->userSession = $userSession;
  57. }
  58. /**
  59. * returns the ID of this entity
  60. *
  61. * @return string
  62. */
  63. public function getId() {
  64. return $this->id;
  65. }
  66. /**
  67. * Returns a specific child node, referenced by its name
  68. *
  69. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  70. * exist.
  71. *
  72. * @param string $name
  73. * @return \Sabre\DAV\INode
  74. * @throws NotFound
  75. */
  76. public function getChild($name) {
  77. try {
  78. $comment = $this->commentsManager->get($name);
  79. return new CommentNode(
  80. $this->commentsManager,
  81. $comment,
  82. $this->userManager,
  83. $this->userSession,
  84. $this->logger
  85. );
  86. } catch (NotFoundException $e) {
  87. throw new NotFound();
  88. }
  89. }
  90. /**
  91. * Returns an array with all the child nodes
  92. *
  93. * @return \Sabre\DAV\INode[]
  94. */
  95. public function getChildren() {
  96. return $this->findChildren();
  97. }
  98. /**
  99. * Returns an array of comment nodes. Result can be influenced by offset,
  100. * limit and date time parameters.
  101. *
  102. * @param int $limit
  103. * @param int $offset
  104. * @param \DateTime|null $datetime
  105. * @return CommentNode[]
  106. */
  107. public function findChildren($limit = 0, $offset = 0, ?\DateTime $datetime = null) {
  108. $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime);
  109. $result = [];
  110. foreach ($comments as $comment) {
  111. $result[] = new CommentNode(
  112. $this->commentsManager,
  113. $comment,
  114. $this->userManager,
  115. $this->userSession,
  116. $this->logger
  117. );
  118. }
  119. return $result;
  120. }
  121. /**
  122. * Checks if a child-node with the specified name exists
  123. *
  124. * @param string $name
  125. * @return bool
  126. */
  127. public function childExists($name) {
  128. try {
  129. $this->commentsManager->get($name);
  130. return true;
  131. } catch (NotFoundException $e) {
  132. return false;
  133. }
  134. }
  135. /**
  136. * Sets the read marker to the specified date for the logged in user
  137. */
  138. public function setReadMarker(?string $value): bool {
  139. $dateTime = new \DateTime($value ?? 'now');
  140. $user = $this->userSession->getUser();
  141. $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user);
  142. return true;
  143. }
  144. /**
  145. * @inheritdoc
  146. */
  147. public function propPatch(PropPatch $propPatch) {
  148. $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']);
  149. }
  150. /**
  151. * @inheritdoc
  152. */
  153. public function getProperties($properties) {
  154. $marker = null;
  155. $user = $this->userSession->getUser();
  156. if (!is_null($user)) {
  157. $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user);
  158. }
  159. return [self::PROPERTY_NAME_READ_MARKER => $marker];
  160. }
  161. }