CommentNode.php 7.9 KB

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