summaryrefslogtreecommitdiffstats
path: root/lib/private/Comments/Manager.php
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2018-04-10 10:50:57 +0200
committerJoas Schilling <coding@schilljs.com>2018-04-18 14:23:15 +0200
commitf50abde7ac99fd6f7563fb60b662ca21ac320f34 (patch)
treec43bb540e701b1bf09522abcce1b22207ac63d81 /lib/private/Comments/Manager.php
parent63dfbb2127ec9a930778dd5d31b640c5f2cc3652 (diff)
downloadnextcloud-server-f50abde7ac99fd6f7563fb60b662ca21ac320f34.tar.gz
nextcloud-server-f50abde7ac99fd6f7563fb60b662ca21ac320f34.zip
Add proper comment offset support
The offset is based on the last known comment instead of limit-offset, so new comments don't mess up requests which get the history of an object- Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/private/Comments/Manager.php')
-rw-r--r--lib/private/Comments/Manager.php115
1 files changed, 115 insertions, 0 deletions
diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php
index 2b43a27b7ac..37b5f033097 100644
--- a/lib/private/Comments/Manager.php
+++ b/lib/private/Comments/Manager.php
@@ -377,6 +377,121 @@ class Manager implements ICommentsManager {
}
/**
+ * @param string $objectType
+ * @param string $objectId
+ * @param int $lastKnownCommentId
+ * @param string $sortDirection
+ * @param int $limit
+ * @return array
+ */
+ public function getForObjectSince(
+ string $objectType,
+ string $objectId,
+ int $lastKnownCommentId,
+ string $sortDirection = 'asc',
+ int $limit = 30
+ ): array {
+ $comments = [];
+
+ $query = $this->dbConn->getQueryBuilder();
+ $query->select('*')
+ ->from('comments')
+ ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
+ ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
+ ->orderBy('creation_timestamp', $sortDirection === 'desc' ? 'DESC' : 'ASC')
+ ->addOrderBy('id', $sortDirection === 'desc' ? 'DESC' : 'ASC');
+
+ if ($limit > 0) {
+ $query->setMaxResults($limit);
+ }
+
+ $lastKnownComment = $this->getLastKnownComment(
+ $objectType,
+ $objectId,
+ $lastKnownCommentId
+ );
+ if ($lastKnownComment instanceof IComment) {
+ $lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime();
+ if ($sortDirection === 'desc') {
+ $query->andWhere(
+ $query->expr()->orX(
+ $query->expr()->lt(
+ 'creation_timestamp',
+ $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
+ IQueryBuilder::PARAM_DATE
+ ),
+ $query->expr()->andX(
+ $query->expr()->eq(
+ 'creation_timestamp',
+ $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
+ IQueryBuilder::PARAM_DATE
+ ),
+ $query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId))
+ )
+ )
+ );
+ } else {
+ $query->andWhere(
+ $query->expr()->orX(
+ $query->expr()->gt(
+ 'creation_timestamp',
+ $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
+ IQueryBuilder::PARAM_DATE
+ ),
+ $query->expr()->andX(
+ $query->expr()->eq(
+ 'creation_timestamp',
+ $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
+ IQueryBuilder::PARAM_DATE
+ ),
+ $query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId))
+ )
+ )
+ );
+ }
+ }
+
+ $resultStatement = $query->execute();
+ while ($data = $resultStatement->fetch()) {
+ $comment = new Comment($this->normalizeDatabaseData($data));
+ $this->cache($comment);
+ $comments[] = $comment;
+ }
+ $resultStatement->closeCursor();
+
+ return $comments;
+ }
+
+ /**
+ * @param string $objectType
+ * @param string $objectId
+ * @param int $id
+ * @return Comment|null
+ */
+ protected function getLastKnownComment(string $objectType,
+ string $objectId,
+ int $id) {
+ $query = $this->dbConn->getQueryBuilder();
+ $query->select('*')
+ ->from('comments')
+ ->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
+ ->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
+ ->andWhere($query->expr()->eq('id', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
+
+ $result = $query->execute();
+ $row = $result->fetch();
+ $result->closeCursor();
+
+ if ($row) {
+ $comment = new Comment($this->normalizeDatabaseData($row));
+ $this->cache($comment);
+ return $comment;
+ }
+
+ return null;
+ }
+
+ /**
* @param $objectType string the object type, e.g. 'files'
* @param $objectId string the id of the object
* @param \DateTime $notOlderThan optional, timestamp of the oldest comments