Ejemplo n.º 1
0
 /**
  * @expectedException \Sabre\DAV\Exception\BadRequest
  * @expectedExceptionMessage Message exceeds allowed character limit of
  */
 public function testCreateCommentMessageTooLong()
 {
     $commentData = ['actorType' => 'users', 'verb' => 'comment', 'message' => str_pad('', IComment::MAX_MESSAGE_LENGTH + 1, 'x')];
     $comment = new Comment(['objectType' => 'files', 'objectId' => '42', 'actorType' => 'users', 'actorId' => 'alice', 'verb' => 'comment']);
     $comment->setId('23');
     $path = 'comments/files/42';
     $requestData = json_encode($commentData);
     $user = $this->getMock('OCP\\IUser');
     $user->expects($this->once())->method('getUID')->will($this->returnValue('alice'));
     $node = $this->getMockBuilder('\\OCA\\DAV\\Comments\\EntityCollection')->disableOriginalConstructor()->getMock();
     $node->expects($this->once())->method('getName')->will($this->returnValue('files'));
     $node->expects($this->once())->method('getId')->will($this->returnValue('42'));
     $node->expects($this->never())->method('setReadMarker');
     $this->commentsManager->expects($this->once())->method('create')->with('users', 'alice', 'files', '42')->will($this->returnValue($comment));
     $this->userSession->expects($this->once())->method('getUser')->will($this->returnValue($user));
     // technically, this is a shortcut. Inbetween EntityTypeCollection would
     // be returned, but doing it exactly right would not be really
     // unit-testing like, as it would require to haul in a lot of other
     // things.
     $this->tree->expects($this->any())->method('getNodeForPath')->with('/' . $path)->will($this->returnValue($node));
     $request = $this->getMockBuilder('Sabre\\HTTP\\RequestInterface')->disableOriginalConstructor()->getMock();
     $response = $this->getMockBuilder('Sabre\\HTTP\\ResponseInterface')->disableOriginalConstructor()->getMock();
     $request->expects($this->once())->method('getPath')->will($this->returnValue('/' . $path));
     $request->expects($this->once())->method('getBodyAsString')->will($this->returnValue($requestData));
     $request->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('application/json'));
     $response->expects($this->never())->method('setHeader');
     $this->server->expects($this->any())->method('getRequestUri')->will($this->returnValue($path));
     $this->plugin->initialize($this->server);
     $this->plugin->httpPost($request, $response);
 }
Ejemplo n.º 2
0
 /**
  * Returns a list of properties for this nodes.
  *
  * The properties list is a list of propertynames the client requested,
  * encoded in clark-notation {xmlnamespace}tagname
  *
  * If the array is empty, it means 'all properties' were requested.
  *
  * Note that it's fine to liberally give properties back, instead of
  * conforming to the list of requested properties.
  * The Server class will filter out the extra.
  *
  * @param array $properties
  * @return array
  */
 function getProperties($properties)
 {
     $properties = array_keys($this->properties);
     $result = [];
     foreach ($properties as $property) {
         $getter = $this->properties[$property];
         if (method_exists($this->comment, $getter)) {
             $result[$property] = $this->comment->{$getter}();
         }
     }
     if ($this->comment->getActorType() === 'users') {
         $user = $this->userManager->get($this->comment->getActorId());
         $displayName = is_null($user) ? null : $user->getDisplayName();
         $result[self::PROPERTY_NAME_ACTOR_DISPLAYNAME] = $displayName;
     }
     $unread = null;
     $user = $this->userSession->getUser();
     if (!is_null($user)) {
         $readUntil = $this->commentsManager->getReadMark($this->comment->getObjectType(), $this->comment->getObjectId(), $user);
         if (is_null($readUntil)) {
             $unread = 'true';
         } else {
             $unread = $this->comment->getCreationDateTime() > $readUntil;
             // re-format for output
             $unread = $unread ? 'true' : 'false';
         }
     }
     $result[self::PROPERTY_NAME_UNREAD] = $unread;
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Creates a new comment
  *
  * @param string $objectType e.g. "files"
  * @param string $objectId e.g. the file id
  * @param string $data JSON encoded string containing the properties of the tag to create
  * @param string $contentType content type of the data
  * @return IComment newly created comment
  *
  * @throws BadRequest if a field was missing
  * @throws UnsupportedMediaType if the content type is not supported
  */
 private function createComment($objectType, $objectId, $data, $contentType = 'application/json')
 {
     if (explode(';', $contentType)[0] === 'application/json') {
         $data = json_decode($data, true);
     } else {
         throw new UnsupportedMediaType();
     }
     $actorType = $data['actorType'];
     $actorId = null;
     if ($actorType === 'users') {
         $user = $this->userSession->getUser();
         if (!is_null($user)) {
             $actorId = $user->getUID();
         }
     }
     if (is_null($actorId)) {
         throw new BadRequest('Invalid actor "' . $actorType . '"');
     }
     try {
         $comment = $this->commentsManager->create($actorType, $actorId, $objectType, $objectId);
         $comment->setMessage($data['message']);
         $comment->setVerb($data['verb']);
         $this->commentsManager->save($comment);
         return $comment;
     } catch (\InvalidArgumentException $e) {
         throw new BadRequest('Invalid input values', 0, $e);
     }
 }
Ejemplo n.º 4
0
 /**
  * returns the number of unread comments for the currently logged in user
  * on the given file or directory node
  *
  * @param Node $node
  * @return Int|null
  */
 public function getUnreadCount(Node $node)
 {
     $user = $this->userSession->getUser();
     if (is_null($user)) {
         return null;
     }
     $lastRead = $this->commentsManager->getReadMark('files', strval($node->getId()), $user);
     return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()), $lastRead);
 }
Ejemplo n.º 5
0
 /**
  * update the comment's message
  *
  * @param $propertyValue
  * @return bool
  */
 public function updateComment($propertyValue)
 {
     try {
         $this->comment->setMessage($propertyValue);
         $this->commentsManager->save($this->comment);
         return true;
     } catch (\Exception $e) {
         $this->logger->logException($e, ['app' => 'dav/comments']);
         return false;
     }
 }
Ejemplo n.º 6
0
 /**
  * @param string $parameter
  * @return string
  */
 protected function convertParameterToComment($parameter, $maxLength = 0)
 {
     if (preg_match('/^\\<parameter\\>(\\d*)\\<\\/parameter\\>$/', $parameter, $matches)) {
         try {
             $comment = $this->commentsManager->get((int) $matches[1]);
             $message = $comment->getMessage();
             $message = str_replace("\n", '<br />', str_replace(['<', '>'], ['&lt;', '&gt;'], $message));
             if ($maxLength && isset($message[$maxLength + 20])) {
                 $findSpace = strpos($message, ' ', $maxLength);
                 if ($findSpace !== false && $findSpace < $maxLength + 20) {
                     return substr($message, 0, $findSpace) . '…';
                 }
                 return substr($message, 0, $maxLength + 20) . '…';
             }
             return $message;
         } catch (NotFoundException $e) {
             return '';
         }
     }
     return '';
 }
 public function testChildExistsFalse()
 {
     $this->commentsManager->expects($this->once())->method('get')->with('44')->will($this->throwException(new \OCP\Comments\NotFoundException()));
     $this->assertFalse($this->collection->childExists('44'));
 }