Allow getting the unread comment count for an entire folder at once
Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
parent
1ee7e1c0b1
commit
429f8ae011
|
@ -42,6 +42,10 @@ class CommentPropertiesPlugin extends ServerPlugin {
|
|||
/** @var IUserSession */
|
||||
private $userSession;
|
||||
|
||||
private $cachedUnreadCount = [];
|
||||
|
||||
private $cachedFolders = [];
|
||||
|
||||
public function __construct(ICommentsManager $commentsManager, IUserSession $userSession) {
|
||||
$this->commentsManager = $commentsManager;
|
||||
$this->userSession = $userSession;
|
||||
|
@ -79,6 +83,18 @@ class CommentPropertiesPlugin extends ServerPlugin {
|
|||
return;
|
||||
}
|
||||
|
||||
// need prefetch ?
|
||||
if ($node instanceof \OCA\DAV\Connector\Sabre\Directory
|
||||
&& $propFind->getDepth() !== 0
|
||||
&& !is_null($propFind->getStatus(self::PROPERTY_NAME_UNREAD))
|
||||
) {
|
||||
$unreadCounts = $this->commentsManager->getNumberOfUnreadCommentsForFolder($node->getId(), $this->userSession->getUser());
|
||||
$this->cachedFolders[] = $node->getPath();
|
||||
foreach ($unreadCounts as $id => $count) {
|
||||
$this->cachedUnreadCount[$id] = $count;
|
||||
}
|
||||
}
|
||||
|
||||
$propFind->handle(self::PROPERTY_NAME_COUNT, function() use ($node) {
|
||||
return $this->commentsManager->getNumberOfCommentsForObject('files', strval($node->getId()));
|
||||
});
|
||||
|
@ -88,7 +104,20 @@ class CommentPropertiesPlugin extends ServerPlugin {
|
|||
});
|
||||
|
||||
$propFind->handle(self::PROPERTY_NAME_UNREAD, function() use ($node) {
|
||||
if (isset($this->cachedUnreadCount[$node->getId()])) {
|
||||
return $this->cachedUnreadCount[$node->getId()];
|
||||
} else {
|
||||
list($parentPath,) = \Sabre\Uri\split($node->getPath());
|
||||
if ($parentPath === '') {
|
||||
$parentPath = '/';
|
||||
}
|
||||
// if we already cached the folder this file is in we know there are no shares for this file
|
||||
if (array_search($parentPath, $this->cachedFolders) === false) {
|
||||
return $this->getUnreadCount($node);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -21,9 +21,13 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OC\Comments;
|
||||
|
||||
use Doctrine\DBAL\Exception\DriverException;
|
||||
use OC\DB\QueryBuilder\Literal;
|
||||
use OC\DB\QueryBuilder\QueryBuilder;
|
||||
use OC\DB\QueryBuilder\QueryFunction;
|
||||
use OCP\Comments\CommentsEvent;
|
||||
use OCP\Comments\IComment;
|
||||
use OCP\Comments\ICommentsEventHandler;
|
||||
|
@ -401,6 +405,40 @@ class Manager implements ICommentsManager {
|
|||
return intval($data[0]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of unread comments for all files in a folder
|
||||
*
|
||||
* @param int $folderId
|
||||
* @param IUser $user
|
||||
* @return array [$fileId => $unreadCount]
|
||||
*/
|
||||
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
|
||||
$qb = $this->dbConn->getQueryBuilder();
|
||||
$query = $qb->select('fileid', $qb->createFunction(
|
||||
'COUNT(' . $qb->getColumnName('c.id') . ')')
|
||||
)->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->createFunction(
|
||||
'cast(' . $qb->getColumnName('c.object_id') . ' as 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()))
|
||||
))
|
||||
->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');
|
||||
|
||||
$resultStatement = $query->execute();
|
||||
return $resultStatement->fetchAll(\PDO::FETCH_KEY_PAIR);
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a new comment and returns it. At this point of time, it is not
|
||||
* saved in the used data storage. Use save() after setting other fields
|
||||
|
|
|
@ -73,7 +73,7 @@ class QuoteHelper {
|
|||
return $string;
|
||||
}
|
||||
|
||||
return $alias . '.`' . $columnName . '`';
|
||||
return '`' . $alias . '`.`' . $columnName . '`';
|
||||
}
|
||||
|
||||
return '`' . $string . '`';
|
||||
|
|
|
@ -23,6 +23,8 @@
|
|||
*/
|
||||
namespace OCP\Comments;
|
||||
|
||||
use OCP\IUser;
|
||||
|
||||
/**
|
||||
* Interface ICommentsManager
|
||||
*
|
||||
|
@ -125,6 +127,16 @@ interface ICommentsManager {
|
|||
*/
|
||||
public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null);
|
||||
|
||||
/**
|
||||
* Get the number of unread comments for all files in a folder
|
||||
*
|
||||
* @param int $folderId
|
||||
* @param IUser $user
|
||||
* @return array [$fileId => $unreadCount]
|
||||
* @since 12.0.0
|
||||
*/
|
||||
public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user);
|
||||
|
||||
/**
|
||||
* creates a new comment and returns it. At this point of time, it is not
|
||||
* saved in the used data storage. Use save() after setting other fields
|
||||
|
|
Loading…
Reference in New Issue