introduce comments read marke tables, comes with user cleanup after deletion

This commit is contained in:
Arthur Schiwon 2016-01-28 22:59:48 +01:00
parent 24025f32f4
commit 01cdc70f9c
7 changed files with 214 additions and 15 deletions

View File

@ -1580,6 +1580,49 @@
</table>
<table>
<!--
default place to store per user and object read markers
-->
<name>*dbprefix*comments_read_markers</name>
<declaration>
<field>
<name>user_id</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>marker_datetime</name>
<type>timestamp</type>
<default></default>
<notnull>false</notnull>
</field>
<field>
<name>object_type</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>
<field>
<name>object_id</name>
<type>text</type>
<default></default>
<notnull>true</notnull>
<length>64</length>
</field>
</declaration>
</table>
<table>
<!--
Encrypted credentials storage

View File

@ -582,6 +582,28 @@ class Manager implements ICommentsManager {
return is_int($affectedRows);
}
/**
* deletes the read markers for the specified user
*
* @param \OCP\IUser $user
* @return bool
* @since 9.0.0
*/
public function deleteReadMarksFromUser(IUser $user) {
$qb = $this->dbConn->getQueryBuilder();
$query = $qb->delete('comments_read_markers')
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
->setParameter('user_id', $user->getUID());
try {
$affectedRows = $query->execute();
} catch (DriverException $e) {
$this->logger->logException($e, ['app' => 'core_comments']);
return false;
}
return ($affectedRows > 0);
}
/**
* sets the read marker for a given file to the specified date for the
* provided user
@ -594,8 +616,31 @@ class Manager implements ICommentsManager {
*/
public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) {
$this->checkRoleParameters('Object', $objectType, $objectId);
$dateTime = $dateTime->format('Y-m-d H:i:s');
$this->config->setUserValue($user->getUID(), 'comments', 'marker.' . $objectType .'.'. $objectId, $dateTime);
$qb = $this->dbConn->getQueryBuilder();
$values = [
'user_id' => $qb->createNamedParameter($user->getUID()),
'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'),
'object_type' => $qb->createNamedParameter($objectType),
'object_id' => $qb->createNamedParameter($objectId),
];
// Strategy: try to update, if this does not return affected rows, do an insert.
$affectedRows = $qb
->update('comments_read_markers')
->set('user_id', $values['user_id'])
->set('marker_datetime', $values['marker_datetime'], 'datetime')
->set('object_type', $values['object_type'])
->set('object_id', $values['object_id'])
->execute();
if ($affectedRows > 0) {
return;
}
$qb->insert('comments_read_markers')
->values($values)
->execute();
}
/**
@ -610,16 +655,50 @@ class Manager implements ICommentsManager {
* @since 9.0.0
*/
public function getReadMark($objectType, $objectId, IUser $user) {
$this->checkRoleParameters('Object', $objectType, $objectId);
$dateTime = $this->config->getUserValue(
$user->getUID(),
'comments',
'marker.' . $objectType .'.'. $objectId,
null
);
if(!is_null($dateTime)) {
$dateTime = new \DateTime($dateTime);
$qb = $this->dbConn->getQueryBuilder();
$resultStatement = $qb->select('marker_datetime')
->from('comments_read_markers')
->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
->setParameter('user_id', $user->getUID(), \PDO::PARAM_STR)
->setParameter('object_type', $objectType, \PDO::PARAM_STR)
->setParameter('object_id', $objectId, \PDO::PARAM_STR)
->execute();
$data = $resultStatement->fetch();
$resultStatement->closeCursor();
if(!$data || is_null($data['marker_datetime'])) {
return null;
}
return $dateTime;
return new \DateTime($data['marker_datetime']);
}
/**
* deletes the read markers on the specified object
*
* @param string $objectType
* @param string $objectId
* @return bool
* @since 9.0.0
*/
public function deleteReadMarksOnObject($objectType, $objectId) {
$this->checkRoleParameters('Object', $objectType, $objectId);
$qb = $this->dbConn->getQueryBuilder();
$query = $qb->delete('comments_read_markers')
->where($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
->setParameter('object_type', $objectType)
->setParameter('object_id', $objectId);
try {
$affectedRows = $query->execute();
} catch (DriverException $e) {
$this->logger->logException($e, ['app' => 'core_comments']);
return false;
}
return ($affectedRows > 0);
}
}

View File

@ -211,6 +211,7 @@ class User implements IUser {
\OC\Files\Cache\Storage::remove('home::' . $this->uid);
\OC::$server->getCommentsManager()->deleteReferencesOfActor('user', $this->uid);
\OC::$server->getCommentsManager()->deleteReadMarksFromUser($this);
}
if ($this->emitter) {

View File

@ -215,4 +215,23 @@ interface ICommentsManager {
*/
public function getReadMark($objectType, $objectId, \OCP\IUser $user);
/**
* deletes the read markers for the specified user
*
* @param \OCP\IUser $user
* @return bool
* @since 9.0.0
*/
public function deleteReadMarksFromUser(\OCP\IUser $user);
/**
* deletes the read markers on the specified object
*
* @param string $objectType
* @param string $objectId
* @return bool
* @since 9.0.0
*/
public function deleteReadMarksOnObject($objectType, $objectId);
}

View File

@ -34,4 +34,8 @@ class FakeManager implements \OCP\Comments\ICommentsManager {
public function setReadMark($objectType, $objectId, \DateTime $dateTime, \OCP\IUser $user) {}
public function getReadMark($objectType, $objectId, \OCP\IUser $user) {}
public function deleteReadMarksFromUser(\OCP\IUser $user) {}
public function deleteReadMarksOnObject($objectType, $objectId) {}
}

View File

@ -570,11 +570,64 @@ class Test_Comments_Manager extends TestCase
$dateTimeSet = new \DateTime();
$manager = $this->getManager();
$manager->setReadMark('files', '36', $dateTimeSet, $user);
$manager->setReadMark('robot', '36', $dateTimeSet, $user);
$dateTimeGet = $manager->getReadMark('files', '36', $user);
$dateTimeGet = $manager->getReadMark('robot', '36', $user);
$this->assertEquals($dateTimeGet, $dateTimeSet);
}
public function testSetMarkReadUpdate() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->any())
->method('getUID')
->will($this->returnValue('alice'));
$dateTimeSet = new \DateTime('yesterday');
$manager = $this->getManager();
$manager->setReadMark('robot', '36', $dateTimeSet, $user);
$dateTimeSet = new \DateTime('today');
$manager->setReadMark('robot', '36', $dateTimeSet, $user);
$dateTimeGet = $manager->getReadMark('robot', '36', $user);
$this->assertEquals($dateTimeGet, $dateTimeSet);
}
public function testReadMarkDeleteUser() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->any())
->method('getUID')
->will($this->returnValue('alice'));
$dateTimeSet = new \DateTime();
$manager = $this->getManager();
$manager->setReadMark('robot', '36', $dateTimeSet, $user);
$manager->deleteReadMarksFromUser($user);
$dateTimeGet = $manager->getReadMark('robot', '36', $user);
$this->assertNull($dateTimeGet);
}
public function testReadMarkDeleteObject() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->any())
->method('getUID')
->will($this->returnValue('alice'));
$dateTimeSet = new \DateTime();
$manager = $this->getManager();
$manager->setReadMark('robot', '36', $dateTimeSet, $user);
$manager->deleteReadMarksOnObject('robot', '36');
$dateTimeGet = $manager->getReadMark('robot', '36', $user);
$this->assertNull($dateTimeGet);
}
}

View File

@ -25,7 +25,7 @@
// We only can count up. The 4. digit is only for the internal patchlevel to trigger DB upgrades
// between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel
// when updating major/minor version number.
$OC_Version = array(9, 0, 0, 7);
$OC_Version = array(9, 0, 0, 8);
// The human readable string
$OC_VersionString = '9.0 pre alpha';