CommentNode.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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\IComment;
  9. use OCP\Comments\ICommentsManager;
  10. use OCP\Comments\MessageTooLongException;
  11. use OCP\IUserManager;
  12. use OCP\IUserSession;
  13. use Psr\Log\LoggerInterface;
  14. use Sabre\DAV\Exception\BadRequest;
  15. use Sabre\DAV\Exception\Forbidden;
  16. use Sabre\DAV\Exception\MethodNotAllowed;
  17. use Sabre\DAV\PropPatch;
  18. class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties {
  19. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  20. public const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}isUnread';
  21. public const PROPERTY_NAME_MESSAGE = '{http://owncloud.org/ns}message';
  22. public const PROPERTY_NAME_ACTOR_DISPLAYNAME = '{http://owncloud.org/ns}actorDisplayName';
  23. public const PROPERTY_NAME_MENTIONS = '{http://owncloud.org/ns}mentions';
  24. public const PROPERTY_NAME_MENTION = '{http://owncloud.org/ns}mention';
  25. public const PROPERTY_NAME_MENTION_TYPE = '{http://owncloud.org/ns}mentionType';
  26. public const PROPERTY_NAME_MENTION_ID = '{http://owncloud.org/ns}mentionId';
  27. public const PROPERTY_NAME_MENTION_DISPLAYNAME = '{http://owncloud.org/ns}mentionDisplayName';
  28. /** @var array list of properties with key being their name and value their setter */
  29. protected $properties = [];
  30. /**
  31. * CommentNode constructor.
  32. */
  33. public function __construct(
  34. protected ICommentsManager $commentsManager,
  35. public IComment $comment,
  36. protected IUserManager $userManager,
  37. protected IUserSession $userSession,
  38. protected LoggerInterface $logger,
  39. ) {
  40. $methods = get_class_methods($this->comment);
  41. $methods = array_filter($methods, function ($name) {
  42. return str_starts_with($name, 'get');
  43. });
  44. foreach ($methods as $getter) {
  45. if ($getter === 'getMentions') {
  46. continue; // special treatment
  47. }
  48. $name = '{' . self::NS_OWNCLOUD . '}' . lcfirst(substr($getter, 3));
  49. $this->properties[$name] = $getter;
  50. }
  51. }
  52. /**
  53. * returns a list of all possible property names
  54. *
  55. * @return array
  56. */
  57. public static function getPropertyNames() {
  58. return [
  59. '{http://owncloud.org/ns}id',
  60. '{http://owncloud.org/ns}parentId',
  61. '{http://owncloud.org/ns}topmostParentId',
  62. '{http://owncloud.org/ns}childrenCount',
  63. '{http://owncloud.org/ns}verb',
  64. '{http://owncloud.org/ns}actorType',
  65. '{http://owncloud.org/ns}actorId',
  66. '{http://owncloud.org/ns}creationDateTime',
  67. '{http://owncloud.org/ns}latestChildDateTime',
  68. '{http://owncloud.org/ns}objectType',
  69. '{http://owncloud.org/ns}objectId',
  70. // re-used property names are defined as constants
  71. self::PROPERTY_NAME_MESSAGE,
  72. self::PROPERTY_NAME_ACTOR_DISPLAYNAME,
  73. self::PROPERTY_NAME_UNREAD,
  74. self::PROPERTY_NAME_MENTIONS,
  75. self::PROPERTY_NAME_MENTION,
  76. self::PROPERTY_NAME_MENTION_TYPE,
  77. self::PROPERTY_NAME_MENTION_ID,
  78. self::PROPERTY_NAME_MENTION_DISPLAYNAME,
  79. ];
  80. }
  81. protected function checkWriteAccessOnComment() {
  82. $user = $this->userSession->getUser();
  83. if ($this->comment->getActorType() !== 'users'
  84. || is_null($user)
  85. || $this->comment->getActorId() !== $user->getUID()
  86. ) {
  87. throw new Forbidden('Only authors are allowed to edit their comment.');
  88. }
  89. }
  90. /**
  91. * Deleted the current node
  92. *
  93. * @return void
  94. */
  95. public function delete() {
  96. $this->checkWriteAccessOnComment();
  97. $this->commentsManager->delete($this->comment->getId());
  98. }
  99. /**
  100. * Returns the name of the node.
  101. *
  102. * This is used to generate the url.
  103. *
  104. * @return string
  105. */
  106. public function getName() {
  107. return $this->comment->getId();
  108. }
  109. /**
  110. * Renames the node
  111. *
  112. * @param string $name The new name
  113. * @throws MethodNotAllowed
  114. */
  115. public function setName($name) {
  116. throw new MethodNotAllowed();
  117. }
  118. /**
  119. * Returns the last modification time, as a unix timestamp
  120. */
  121. public function getLastModified(): ?int {
  122. return null;
  123. }
  124. /**
  125. * update the comment's message
  126. *
  127. * @param $propertyValue
  128. * @return bool
  129. * @throws BadRequest
  130. * @throws \Exception
  131. */
  132. public function updateComment($propertyValue) {
  133. $this->checkWriteAccessOnComment();
  134. try {
  135. $this->comment->setMessage($propertyValue);
  136. $this->commentsManager->save($this->comment);
  137. return true;
  138. } catch (\Exception $e) {
  139. $this->logger->error($e->getMessage(), ['app' => 'dav/comments', 'exception' => $e]);
  140. if ($e instanceof MessageTooLongException) {
  141. $msg = 'Message exceeds allowed character limit of ';
  142. throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e);
  143. }
  144. throw $e;
  145. }
  146. }
  147. /**
  148. * Updates properties on this node.
  149. *
  150. * This method received a PropPatch object, which contains all the
  151. * information about the update.
  152. *
  153. * To update specific properties, call the 'handle' method on this object.
  154. * Read the PropPatch documentation for more information.
  155. *
  156. * @param PropPatch $propPatch
  157. * @return void
  158. */
  159. public function propPatch(PropPatch $propPatch) {
  160. // other properties than 'message' are read only
  161. $propPatch->handle(self::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']);
  162. }
  163. /**
  164. * Returns a list of properties for this nodes.
  165. *
  166. * The properties list is a list of propertynames the client requested,
  167. * encoded in clark-notation {xmlnamespace}tagname
  168. *
  169. * If the array is empty, it means 'all properties' were requested.
  170. *
  171. * Note that it's fine to liberally give properties back, instead of
  172. * conforming to the list of requested properties.
  173. * The Server class will filter out the extra.
  174. *
  175. * @param array $properties
  176. * @return array
  177. */
  178. public function getProperties($properties) {
  179. $properties = array_keys($this->properties);
  180. $result = [];
  181. foreach ($properties as $property) {
  182. $getter = $this->properties[$property];
  183. if (method_exists($this->comment, $getter)) {
  184. $result[$property] = $this->comment->$getter();
  185. }
  186. }
  187. if ($this->comment->getActorType() === 'users') {
  188. $user = $this->userManager->get($this->comment->getActorId());
  189. $displayName = is_null($user) ? null : $user->getDisplayName();
  190. $result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName;
  191. }
  192. $result[self::PROPERTY_NAME_MENTIONS] = $this->composeMentionsPropertyValue();
  193. $unread = null;
  194. $user = $this->userSession->getUser();
  195. if (!is_null($user)) {
  196. $readUntil = $this->commentsManager->getReadMark(
  197. $this->comment->getObjectType(),
  198. $this->comment->getObjectId(),
  199. $user
  200. );
  201. if (is_null($readUntil)) {
  202. $unread = 'true';
  203. } else {
  204. $unread = $this->comment->getCreationDateTime() > $readUntil;
  205. // re-format for output
  206. $unread = $unread ? 'true' : 'false';
  207. }
  208. }
  209. $result[self::PROPERTY_NAME_UNREAD] = $unread;
  210. return $result;
  211. }
  212. /**
  213. * transforms a mentions array as returned from IComment->getMentions to an
  214. * array with DAV-compatible structure that can be assigned to the
  215. * PROPERTY_NAME_MENTION property.
  216. *
  217. * @return array
  218. */
  219. protected function composeMentionsPropertyValue() {
  220. return array_map(function ($mention) {
  221. try {
  222. $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']);
  223. } catch (\OutOfBoundsException $e) {
  224. $this->logger->error($e->getMessage(), ['exception' => $e]);
  225. // No displayname, upon client's discretion what to display.
  226. $displayName = '';
  227. }
  228. return [
  229. self::PROPERTY_NAME_MENTION => [
  230. self::PROPERTY_NAME_MENTION_TYPE => $mention['type'],
  231. self::PROPERTY_NAME_MENTION_ID => $mention['id'],
  232. self::PROPERTY_NAME_MENTION_DISPLAYNAME => $displayName,
  233. ]
  234. ];
  235. }, $this->comment->getMentions());
  236. }
  237. }