use OCP\Comments\ICommentsManager;
use OCP\ILogger;
use OCP\IUserManager;
+use OCP\IUserSession;
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\PropPatch;
class CommentNode implements \Sabre\DAV\INode, \Sabre\DAV\IProperties {
const NS_OWNCLOUD = 'http://owncloud.org/ns';
+ const PROPERTY_NAME_UNREAD = '{http://owncloud.org/ns}isUnread';
+ const PROPERTY_NAME_MESSAGE = '{http://owncloud.org/ns}message';
+ const PROPERTY_NAME_ACTOR_DISPLAYNAME = '{http://owncloud.org/ns}actorDisplayName';
+
/** @var IComment */
public $comment;
/** @var IUserManager */
protected $userManager;
+ /** @var IUserSession */
+ protected $userSession;
+
/**
* CommentNode constructor.
*
* @param ICommentsManager $commentsManager
* @param IComment $comment
* @param IUserManager $userManager
+ * @param IUserSession $userSession
* @param ILogger $logger
*/
public function __construct(
ICommentsManager $commentsManager,
IComment $comment,
IUserManager $userManager,
+ IUserSession $userSession,
ILogger $logger
) {
$this->commentsManager = $commentsManager;
$this->properties[$name] = $getter;
}
$this->userManager = $userManager;
+ $this->userSession = $userSession;
}
/**
'{http://owncloud.org/ns}parentId',
'{http://owncloud.org/ns}topmostParentId',
'{http://owncloud.org/ns}childrenCount',
- '{http://owncloud.org/ns}message',
'{http://owncloud.org/ns}verb',
'{http://owncloud.org/ns}actorType',
'{http://owncloud.org/ns}actorId',
- '{http://owncloud.org/ns}actorDisplayName',
'{http://owncloud.org/ns}creationDateTime',
'{http://owncloud.org/ns}latestChildDateTime',
'{http://owncloud.org/ns}objectType',
'{http://owncloud.org/ns}objectId',
+ // re-used property names are defined as constants
+ self::PROPERTY_NAME_MESSAGE,
+ self::PROPERTY_NAME_ACTOR_DISPLAYNAME,
+ self::PROPERTY_NAME_UNREAD
];
}
*/
function propPatch(PropPatch $propPatch) {
// other properties than 'message' are read only
- $propPatch->handle('{'.self::NS_OWNCLOUD.'}message', [$this, 'updateComment']);
+ $propPatch->handle(self::PROPERTY_NAME_MESSAGE, [$this, 'updateComment']);
}
/**
if($this->comment->getActorType() === 'users') {
$user = $this->userManager->get($this->comment->getActorId());
$displayName = is_null($user) ? null : $user->getDisplayName();
- $result['{' . self::NS_OWNCLOUD . '}actorDisplayName'] = $displayName;
+ $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;
}
* @param ICommentsManager $commentsManager
* @param Folder $fileRoot
* @param IUserManager $userManager
+ * @param IUserSession $userSession
* @param ILogger $logger
*/
public function __construct(
ICommentsManager $commentsManager,
Folder $fileRoot,
IUserManager $userManager,
+ IUserSession $userSession,
ILogger $logger
) {
foreach(['id', 'name'] as $property) {
$this->fileRoot = $fileRoot;
$this->logger = $logger;
$this->userManager = $userManager;
+ $this->userSession = $userSession;
}
/**
function getChild($name) {
try {
$comment = $this->commentsManager->get($name);
- return new CommentNode($this->commentsManager, $comment, $this->userManager, $this->logger);
+ return new CommentNode(
+ $this->commentsManager,
+ $comment,
+ $this->userManager,
+ $this->userSession,
+ $this->logger
+ );
} catch (\OCP\Comments\NotFoundException $e) {
throw new NotFound();
}
$comments = $this->commentsManager->getForObject($this->name, $this->id, $limit, $offset, $datetime);
$result = [];
foreach($comments as $comment) {
- $result[] = new CommentNode($this->commentsManager, $comment, $this->userManager, $this->logger);
+ $result[] = new CommentNode(
+ $this->commentsManager,
+ $comment,
+ $this->userManager,
+ $this->userSession,
+ $this->logger
+ );
}
return $result;
}
use OCP\Files\Folder;
use OCP\ILogger;
use OCP\IUserManager;
+use OCP\IUserSession;
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\Exception\NotFound;
* @param ICommentsManager $commentsManager
* @param Folder $fileRoot
* @param IUserManager $userManager
+ * @param IUserSession $userSession
* @param ILogger $logger
*/
public function __construct(
ICommentsManager $commentsManager,
Folder $fileRoot,
IUserManager $userManager,
+ IUserSession $userSession,
ILogger $logger
) {
$name = trim($name);
$this->fileRoot = $fileRoot;
$this->logger = $logger;
$this->userManager = $userManager;
+ $this->userSession = $userSession;
}
/**
$this->commentsManager,
$this->fileRoot,
$this->userManager,
+ $this->userSession,
$this->logger
);
}
$this->commentsManager,
$userFolder,
$this->userManager,
+ $this->userSession,
$this->logger
);
}
protected $node;
protected $userManager;
protected $logger;
+ protected $userSession;
public function setUp() {
parent::setUp();
$this->commentsManager = $this->getMock('\OCP\Comments\ICommentsManager');
$this->comment = $this->getMock('\OCP\Comments\IComment');
$this->userManager = $this->getMock('\OCP\IUserManager');
+ $this->userSession = $this->getMock('\OCP\IUserSession');
$this->logger = $this->getMock('\OCP\ILogger');
- $this->node = new CommentNode($this->commentsManager, $this->comment, $this->userManager, $this->logger);
+ $this->node = new CommentNode(
+ $this->commentsManager,
+ $this->comment,
+ $this->userManager,
+ $this->userSession,
+ $this->logger
+ );
}
public function testDelete() {
$ns . 'latestChildDateTime' => new \DateTime('2016-01-12 18:48:00'),
$ns . 'objectType' => 'files',
$ns . 'objectId' => '1848',
+ $ns . 'isUnread' => null,
];
$this->comment->expects($this->once())
$properties = $this->node->getProperties(null);
foreach($properties as $name => $value) {
- $this->assertTrue(isset($expected[$name]));
+ $this->assertTrue(array_key_exists($name, $expected));
$this->assertSame($expected[$name], $value);
unset($expected[$name]);
}
$this->assertTrue(empty($expected));
}
+
+ public function readCommentProvider() {
+ $creationDT = new \DateTime('2016-01-19 18:48:00');
+ $diff = new \DateInterval('PT2H');
+ $readDT1 = clone $creationDT; $readDT1->sub($diff);
+ $readDT2 = clone $creationDT; $readDT2->add($diff);
+ return [
+ [$creationDT, $readDT1, 'true'],
+ [$creationDT, $readDT2, 'false'],
+ [$creationDT, null, 'true'],
+ ];
+ }
+
+ /**
+ * @dataProvider readCommentProvider
+ * @param $expected
+ */
+ public function testGetPropertiesUnreadProperty($creationDT, $readDT, $expected) {
+ $this->comment->expects($this->any())
+ ->method('getCreationDateTime')
+ ->will($this->returnValue($creationDT));
+
+ $this->commentsManager->expects($this->once())
+ ->method('getReadMark')
+ ->will($this->returnValue($readDT));
+
+ $this->userSession->expects($this->once())
+ ->method('getUser')
+ ->will($this->returnValue($this->getMock('\OCP\IUser')));
+
+ $properties = $this->node->getProperties(null);
+
+ $this->assertTrue(array_key_exists(CommentNode::PROPERTY_NAME_UNREAD, $properties));
+ $this->assertSame($properties[CommentNode::PROPERTY_NAME_UNREAD], $expected);
+ }
}