Get the last comment date for a list of actors (to allow sorting mention suggestions e.g.)

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-10-21 09:18:51 +02:00
parent 89651c5233
commit d462439a63
No known key found for this signature in database
GPG key ID: 7076EA9751AACDDA
2 changed files with 61 additions and 0 deletions

View file

@ -31,6 +31,7 @@ namespace OC\Comments;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\InvalidFieldNameException;
use OCA\Comments\AppInfo\Application;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler;
@ -55,6 +56,9 @@ class Manager implements ICommentsManager {
/** @var IConfig */
protected $config;
/** @var ITimeFactory */
protected $timeFactory;
/** @var IInitialStateService */
protected $initialStateService;
@ -73,10 +77,12 @@ class Manager implements ICommentsManager {
public function __construct(IDBConnection $dbConn,
LoggerInterface $logger,
IConfig $config,
ITimeFactory $timeFactory,
IInitialStateService $initialStateService) {
$this->dbConn = $dbConn;
$this->logger = $logger;
$this->config = $config;
$this->timeFactory = $timeFactory;
$this->initialStateService = $initialStateService;
}
@ -665,6 +671,44 @@ class Manager implements ICommentsManager {
return (int) ($data['id'] ?? 0);
}
/**
* @param string $objectType
* @param string $objectId
* @param string $verb
* @param string $actorType
* @param string[] $actors
* @return array
* @since 21.0.0
*/
public function getLastCommentDateByActor(
string $objectType,
string $objectId,
string $verb,
string $actorType,
array $actors
): array {
$lastComments = [];
$query = $this->dbConn->getQueryBuilder();
$query->select('actor_id')
->selectAlias($query->createFunction('MAX(' . $query->getColumnName('creation_timestamp') . ')'), 'last_comment')
->from('comments')
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)))
->andWhere($query->expr()->eq('actor_type', $query->createNamedParameter($actorType)))
->andWhere($query->expr()->in('actor_id', $query->createNamedParameter($actors, IQueryBuilder::PARAM_STR_ARRAY)))
->groupBy('actor_id');
$result = $query->execute();
while ($row = $result->fetch()) {
$lastComments[$row['actor_id']] = $this->timeFactory->getDateTime($row['last_comment']);
}
$result->closeCursor();
return $lastComments;
}
/**
* Get the number of unread comments for all files in a folder
*

View file

@ -199,6 +199,23 @@ interface ICommentsManager {
*/
public function getLastCommentBeforeDate(string $objectType, string $objectId, \DateTime $beforeDate, string $verb = ''): int;
/**
* @param string $objectType
* @param string $objectId
* @param string $verb
* @param string $actorType
* @param string[] $actors
* @return array
* @since 21.0.0
*/
public function getLastCommentDateByActor(
string $objectType,
string $objectId,
string $verb,
string $actorType,
array $actors
): array;
/**
* Get the number of unread comments for all files in a folder
*