CommentNode.php 8.5 KB

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