1
0

CommentNode.php 8.5 KB

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