CommentNode.php 6.9 KB

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