Rewrite getNumberOfUnreadCommentsForFolder query

Before the joining and filtering removed unkown files. Resulting in
manual queries for all the files with no (unread) comments (the 99%).

Long story short. This will return a list of all the files in the parent
folder with their unread comment count (can be 0). But this makes sure
that the result is properly cached. In the dav handling.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Roeland Jago Douma 2018-12-19 21:01:48 +01:00
parent 4b0cb0df7f
commit f394bf356e
No known key found for this signature in database
GPG key ID: F941078878347C0C

View file

@ -581,27 +581,42 @@ class Manager implements ICommentsManager {
* @param int $folderId
* @param IUser $user
* @return array [$fileId => $unreadCount]
*
* @suppress SqlInjectionChecker
*/
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
$qb = $this->dbConn->getQueryBuilder();
$query = $qb->select('f.fileid')
->addSelect($qb->func()->count('c.id', 'num_ids'))
->from('comments', 'c')
->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
$qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT))
->from('filecache', 'f')
->leftJoin('f', 'comments', 'c', $qb->expr()->eq(
'f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT)
))
->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
$qb->expr()->eq('m.object_id', 'c.object_id'),
$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID()))
->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->eq(
'c.object_id', 'm.object_id'
))
->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)))
->andWhere($qb->expr()->orX(
$qb->expr()->gt('c.creation_timestamp', 'marker_datetime'),
$qb->expr()->isNull('marker_datetime')
))
->groupBy('f.fileid');
->where(
$qb->expr()->andX(
$qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)),
$qb->expr()->orX(
$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
$qb->expr()->isNull('c.object_type')
),
$qb->expr()->orX(
$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
$qb->expr()->isNull('m.object_type')
),
$qb->expr()->orX(
$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID())),
$qb->expr()->isNull('m.user_id')
),
$qb->expr()->orX(
$qb->expr()->gt('c.creation_timestamp', 'm.marker_datetime'),
$qb->expr()->isNull('m.marker_datetime')
)
)
)->groupBy('f.fileid');
$resultStatement = $query->execute();