You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentNode.php 8.6KB

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