CommentNode.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 strpos($name, 'get') === 0;
  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. * @return int
  153. */
  154. public function getLastModified() {
  155. return null;
  156. }
  157. /**
  158. * update the comment's message
  159. *
  160. * @param $propertyValue
  161. * @return bool
  162. * @throws BadRequest
  163. * @throws \Exception
  164. */
  165. public function updateComment($propertyValue) {
  166. $this->checkWriteAccessOnComment();
  167. try {
  168. $this->comment->setMessage($propertyValue);
  169. $this->commentsManager->save($this->comment);
  170. return true;
  171. } catch (\Exception $e) {
  172. $this->logger->error($e->getMessage(), ['app' => 'dav/comments', 'exception' => $e]);
  173. if ($e instanceof MessageTooLongException) {
  174. $msg = 'Message exceeds allowed character limit of ';
  175. throw new BadRequest($msg . IComment::MAX_MESSAGE_LENGTH, 0, $e);
  176. }
  177. throw $e;
  178. }
  179. }
  180. /**
  181. * Updates properties on this node.
  182. *
  183. * This method received a PropPatch object, which contains all the
  184. * information about the update.
  185. *
  186. * To update specific properties, call the 'handle' method on this object.
  187. * Read the PropPatch documentation for more information.
  188. *
  189. * @param PropPatch $propPatch
  190. * @return void
  191. */
  192. public function propPatch(PropPatch $propPatch) {
  193. // other properties than 'message' are read only
  194. $propPatch->handle(self::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']);
  195. }
  196. /**
  197. * Returns a list of properties for this nodes.
  198. *
  199. * The properties list is a list of propertynames the client requested,
  200. * encoded in clark-notation {xmlnamespace}tagname
  201. *
  202. * If the array is empty, it means 'all properties' were requested.
  203. *
  204. * Note that it's fine to liberally give properties back, instead of
  205. * conforming to the list of requested properties.
  206. * The Server class will filter out the extra.
  207. *
  208. * @param array $properties
  209. * @return array
  210. */
  211. public function getProperties($properties) {
  212. $properties = array_keys($this->properties);
  213. $result = [];
  214. foreach ($properties as $property) {
  215. $getter = $this->properties[$property];
  216. if (method_exists($this->comment, $getter)) {
  217. $result[$property] = $this->comment->$getter();
  218. }
  219. }
  220. if ($this->comment->getActorType() === 'users') {
  221. $user = $this->userManager->get($this->comment->getActorId());
  222. $displayName = is_null($user) ? null : $user->getDisplayName();
  223. $result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName;
  224. }
  225. $result[self::PROPERTY_NAME_MENTIONS] = $this->composeMentionsPropertyValue();
  226. $unread = null;
  227. $user = $this->userSession->getUser();
  228. if (!is_null($user)) {
  229. $readUntil = $this->commentsManager->getReadMark(
  230. $this->comment->getObjectType(),
  231. $this->comment->getObjectId(),
  232. $user
  233. );
  234. if (is_null($readUntil)) {
  235. $unread = 'true';
  236. } else {
  237. $unread = $this->comment->getCreationDateTime() > $readUntil;
  238. // re-format for output
  239. $unread = $unread ? 'true' : 'false';
  240. }
  241. }
  242. $result[self::PROPERTY_NAME_UNREAD] = $unread;
  243. return $result;
  244. }
  245. /**
  246. * transforms a mentions array as returned from IComment->getMentions to an
  247. * array with DAV-compatible structure that can be assigned to the
  248. * PROPERTY_NAME_MENTION property.
  249. *
  250. * @return array
  251. */
  252. protected function composeMentionsPropertyValue() {
  253. return array_map(function ($mention) {
  254. try {
  255. $displayName = $this->commentsManager->resolveDisplayName($mention['type'], $mention['id']);
  256. } catch (\OutOfBoundsException $e) {
  257. $this->logger->error($e->getMessage(), ['exception' => $e]);
  258. // No displayname, upon client's discretion what to display.
  259. $displayName = '';
  260. }
  261. return [
  262. self::PROPERTY_NAME_MENTION => [
  263. self::PROPERTY_NAME_MENTION_TYPE => $mention['type'],
  264. self::PROPERTY_NAME_MENTION_ID => $mention['id'],
  265. self::PROPERTY_NAME_MENTION_DISPLAYNAME => $displayName,
  266. ]
  267. ];
  268. }, $this->comment->getMentions());
  269. }
  270. }