aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@owncloud.com>2016-01-28 22:59:48 +0100
committerArthur Schiwon <blizzz@owncloud.com>2016-01-29 13:08:02 +0100
commit01cdc70f9c8fd98d2ea49bcfba9005f2797c815d (patch)
treeb853e7991ab474b1519e54c30bfc1ed0e5a37953
parent24025f32f448d9bef1087bea01d771dd7e871996 (diff)
downloadnextcloud-server-01cdc70f9c8fd98d2ea49bcfba9005f2797c815d.tar.gz
nextcloud-server-01cdc70f9c8fd98d2ea49bcfba9005f2797c815d.zip
introduce comments read marke tables, comes with user cleanup after deletion
-rw-r--r--db_structure.xml43
-rw-r--r--lib/private/comments/manager.php101
-rw-r--r--lib/private/user/user.php1
-rw-r--r--lib/public/comments/icommentsmanager.php19
-rw-r--r--tests/lib/comments/fakemanager.php4
-rw-r--r--tests/lib/comments/manager.php57
-rw-r--r--version.php2
7 files changed, 213 insertions, 14 deletions
diff --git a/db_structure.xml b/db_structure.xml
index 99bfa519b40..ea1b89e28da 100644
--- a/db_structure.xml
+++ b/db_structure.xml
@@ -1582,6 +1582,49 @@
<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
-->
<name>*dbprefix*credentials</name>
diff --git a/lib/private/comments/manager.php b/lib/private/comments/manager.php
index 580bc2a8816..23add3fd2c3 100644
--- a/lib/private/comments/manager.php
+++ b/lib/private/comments/manager.php
@@ -583,6 +583,28 @@ class Manager implements ICommentsManager {
}
/**
+ * 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) {
+ $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 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);
- $dateTime = $this->config->getUserValue(
- $user->getUID(),
- 'comments',
- 'marker.' . $objectType .'.'. $objectId,
- null
- );
- if(!is_null($dateTime)) {
- $dateTime = new \DateTime($dateTime);
+
+ $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 $dateTime;
+ return ($affectedRows > 0);
}
}
diff --git a/lib/private/user/user.php b/lib/private/user/user.php
index e8ce6b8cc87..5e556575118 100644
--- a/lib/private/user/user.php
+++ b/lib/private/user/user.php
@@ -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) {
diff --git a/lib/public/comments/icommentsmanager.php b/lib/public/comments/icommentsmanager.php
index abcc0a7b159..f5b290bf8b2 100644
--- a/lib/public/comments/icommentsmanager.php
+++ b/lib/public/comments/icommentsmanager.php
@@ -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);
+
}
diff --git a/tests/lib/comments/fakemanager.php b/tests/lib/comments/fakemanager.php
index 8e513cb301a..7186529e718 100644
--- a/tests/lib/comments/fakemanager.php
+++ b/tests/lib/comments/fakemanager.php
@@ -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) {}
}
diff --git a/tests/lib/comments/manager.php b/tests/lib/comments/manager.php
index 2578fcc5189..a71f78f2818 100644
--- a/tests/lib/comments/manager.php
+++ b/tests/lib/comments/manager.php
@@ -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);
+ }
+
}
diff --git a/version.php b/version.php
index a82e4ad1c0f..0b7eb6f79d2 100644
--- a/version.php
+++ b/version.php
@@ -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';