1
0

EntityCollection.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /**
  27. * @param string $id
  28. * @param string $name
  29. * @param ICommentsManager $commentsManager
  30. * @param IUserManager $userManager
  31. * @param IUserSession $userSession
  32. * @param LoggerInterface $logger
  33. */
  34. public function __construct(
  35. protected $id,
  36. $name,
  37. ICommentsManager $commentsManager,
  38. IUserManager $userManager,
  39. IUserSession $userSession,
  40. protected LoggerInterface $logger,
  41. ) {
  42. foreach (['id', 'name'] as $property) {
  43. $$property = trim($$property);
  44. if (empty($$property) || !is_string($$property)) {
  45. throw new \InvalidArgumentException('"' . $property . '" parameter must be non-empty string');
  46. }
  47. }
  48. $this->name = $name;
  49. $this->commentsManager = $commentsManager;
  50. $this->userManager = $userManager;
  51. $this->userSession = $userSession;
  52. }
  53. /**
  54. * returns the ID of this entity
  55. *
  56. * @return string
  57. */
  58. public function getId() {
  59. return $this->id;
  60. }
  61. /**
  62. * Returns a specific child node, referenced by its name
  63. *
  64. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  65. * exist.
  66. *
  67. * @param string $name
  68. * @return \Sabre\DAV\INode
  69. * @throws NotFound
  70. */
  71. public function getChild($name) {
  72. try {
  73. $comment = $this->commentsManager->get($name);
  74. return new CommentNode(
  75. $this->commentsManager,
  76. $comment,
  77. $this->userManager,
  78. $this->userSession,
  79. $this->logger
  80. );
  81. } catch (NotFoundException $e) {
  82. throw new NotFound();
  83. }
  84. }
  85. /**
  86. * Returns an array with all the child nodes
  87. *
  88. * @return \Sabre\DAV\INode[]
  89. */
  90. public function getChildren() {
  91. return $this->findChildren();
  92. }
  93. /**
  94. * Returns an array of comment nodes. Result can be influenced by offset,
  95. * limit and date time parameters.
  96. *
  97. * @param int $limit
  98. * @param int $offset
  99. * @param \DateTime|null $datetime
  100. * @return CommentNode[]
  101. */
  102. public function findChildren($limit = 0, $offset = 0, ?\DateTime $datetime = null) {
  103. $comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime);
  104. $result = [];
  105. foreach ($comments as $comment) {
  106. $result[] = new CommentNode(
  107. $this->commentsManager,
  108. $comment,
  109. $this->userManager,
  110. $this->userSession,
  111. $this->logger
  112. );
  113. }
  114. return $result;
  115. }
  116. /**
  117. * Checks if a child-node with the specified name exists
  118. *
  119. * @param string $name
  120. * @return bool
  121. */
  122. public function childExists($name) {
  123. try {
  124. $this->commentsManager->get($name);
  125. return true;
  126. } catch (NotFoundException $e) {
  127. return false;
  128. }
  129. }
  130. /**
  131. * Sets the read marker to the specified date for the logged in user
  132. */
  133. public function setReadMarker(?string $value): bool {
  134. $dateTime = new \DateTime($value ?? 'now');
  135. $user = $this->userSession->getUser();
  136. $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user);
  137. return true;
  138. }
  139. /**
  140. * @inheritdoc
  141. */
  142. public function propPatch(PropPatch $propPatch) {
  143. $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']);
  144. }
  145. /**
  146. * @inheritdoc
  147. */
  148. public function getProperties($properties) {
  149. $marker = null;
  150. $user = $this->userSession->getUser();
  151. if (!is_null($user)) {
  152. $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user);
  153. }
  154. return [self::PROPERTY_NAME_READ_MARKER => $marker];
  155. }
  156. }