1
0

EntityCollection.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\Comments;
  24. use OCP\Comments\ICommentsManager;
  25. use OCP\Comments\NotFoundException;
  26. use OCP\ILogger;
  27. use OCP\IUserManager;
  28. use OCP\IUserSession;
  29. use Sabre\DAV\Exception\NotFound;
  30. use Sabre\DAV\IProperties;
  31. use Sabre\DAV\PropPatch;
  32. /**
  33. * Class EntityCollection
  34. *
  35. * this represents a specific holder of comments, identified by an entity type
  36. * (class member $name) and an entity id (class member $id).
  37. *
  38. * @package OCA\DAV\Comments
  39. */
  40. class EntityCollection extends RootCollection implements IProperties {
  41. const PROPERTY_NAME_READ_MARKER = '{http://owncloud.org/ns}readMarker';
  42. /** @var string */
  43. protected $id;
  44. /** @var ILogger */
  45. protected $logger;
  46. /**
  47. * @param string $id
  48. * @param string $name
  49. * @param ICommentsManager $commentsManager
  50. * @param IUserManager $userManager
  51. * @param IUserSession $userSession
  52. * @param ILogger $logger
  53. */
  54. public function __construct(
  55. $id,
  56. $name,
  57. ICommentsManager $commentsManager,
  58. IUserManager $userManager,
  59. IUserSession $userSession,
  60. ILogger $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. 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. 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. 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. 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. * @param \DateTime $value
  156. * @return bool
  157. */
  158. public function setReadMarker($value) {
  159. $dateTime = new \DateTime($value);
  160. $user = $this->userSession->getUser();
  161. $this->commentsManager->setReadMark($this->name, $this->id, $dateTime, $user);
  162. return true;
  163. }
  164. /**
  165. * @inheritdoc
  166. */
  167. function propPatch(PropPatch $propPatch) {
  168. $propPatch->handle(self::PROPERTY_NAME_READ_MARKER, [$this, 'setReadMarker']);
  169. }
  170. /**
  171. * @inheritdoc
  172. */
  173. function getProperties($properties) {
  174. $marker = null;
  175. $user = $this->userSession->getUser();
  176. if(!is_null($user)) {
  177. $marker = $this->commentsManager->getReadMark($this->name, $this->id, $user);
  178. }
  179. return [self::PROPERTY_NAME_READ_MARKER => $marker];
  180. }
  181. }