Merge pull request #23601 from nextcloud/techdebt/noid/comments-manager-improvements

Comments manager improvements/extensions needed by Talk
This commit is contained in:
Roeland Jago Douma 2020-10-27 19:57:55 +01:00 committed by GitHub
commit eadce7fadd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 222 additions and 18 deletions

View File

@ -31,6 +31,7 @@ namespace OC\Comments;
use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Exception\InvalidFieldNameException; use Doctrine\DBAL\Exception\InvalidFieldNameException;
use OCA\Comments\AppInfo\Application; use OCA\Comments\AppInfo\Application;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\CommentsEvent; use OCP\Comments\CommentsEvent;
use OCP\Comments\IComment; use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler; use OCP\Comments\ICommentsEventHandler;
@ -55,6 +56,9 @@ class Manager implements ICommentsManager {
/** @var IConfig */ /** @var IConfig */
protected $config; protected $config;
/** @var ITimeFactory */
protected $timeFactory;
/** @var IInitialStateService */ /** @var IInitialStateService */
protected $initialStateService; protected $initialStateService;
@ -73,10 +77,12 @@ class Manager implements ICommentsManager {
public function __construct(IDBConnection $dbConn, public function __construct(IDBConnection $dbConn,
LoggerInterface $logger, LoggerInterface $logger,
IConfig $config, IConfig $config,
ITimeFactory $timeFactory,
IInitialStateService $initialStateService) { IInitialStateService $initialStateService) {
$this->dbConn = $dbConn; $this->dbConn = $dbConn;
$this->logger = $logger; $this->logger = $logger;
$this->config = $config; $this->config = $config;
$this->timeFactory = $timeFactory;
$this->initialStateService = $initialStateService; $this->initialStateService = $initialStateService;
} }
@ -395,6 +401,7 @@ class Manager implements ICommentsManager {
* @param string $sortDirection direction of the comments (`asc` or `desc`) * @param string $sortDirection direction of the comments (`asc` or `desc`)
* @param int $limit optional, number of maximum comments to be returned. if * @param int $limit optional, number of maximum comments to be returned. if
* set to 0, all comments are returned. * set to 0, all comments are returned.
* @param bool $includeLastKnown
* @return IComment[] * @return IComment[]
* @return array * @return array
*/ */
@ -403,7 +410,8 @@ class Manager implements ICommentsManager {
string $objectId, string $objectId,
int $lastKnownCommentId, int $lastKnownCommentId,
string $sortDirection = 'asc', string $sortDirection = 'asc',
int $limit = 30 int $limit = 30,
bool $includeLastKnown = false
): array { ): array {
$comments = []; $comments = [];
@ -427,6 +435,11 @@ class Manager implements ICommentsManager {
if ($lastKnownComment instanceof IComment) { if ($lastKnownComment instanceof IComment) {
$lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime(); $lastKnownCommentDateTime = $lastKnownComment->getCreationDateTime();
if ($sortDirection === 'desc') { if ($sortDirection === 'desc') {
if ($includeLastKnown) {
$idComparison = $query->expr()->lte('id', $query->createNamedParameter($lastKnownCommentId));
} else {
$idComparison = $query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId));
}
$query->andWhere( $query->andWhere(
$query->expr()->orX( $query->expr()->orX(
$query->expr()->lt( $query->expr()->lt(
@ -440,11 +453,16 @@ class Manager implements ICommentsManager {
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
IQueryBuilder::PARAM_DATE IQueryBuilder::PARAM_DATE
), ),
$query->expr()->lt('id', $query->createNamedParameter($lastKnownCommentId)) $idComparison
) )
) )
); );
} else { } else {
if ($includeLastKnown) {
$idComparison = $query->expr()->gte('id', $query->createNamedParameter($lastKnownCommentId));
} else {
$idComparison = $query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId));
}
$query->andWhere( $query->andWhere(
$query->expr()->orX( $query->expr()->orX(
$query->expr()->gt( $query->expr()->gt(
@ -458,7 +476,7 @@ class Manager implements ICommentsManager {
$query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE), $query->createNamedParameter($lastKnownCommentDateTime, IQueryBuilder::PARAM_DATE),
IQueryBuilder::PARAM_DATE IQueryBuilder::PARAM_DATE
), ),
$query->expr()->gt('id', $query->createNamedParameter($lastKnownCommentId)) $idComparison
) )
) )
); );
@ -517,6 +535,25 @@ class Manager implements ICommentsManager {
* @return IComment[] * @return IComment[]
*/ */
public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array { public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array {
$objectIds = [];
if ($objectId) {
$objectIds[] = $objectIds;
}
return $this->searchForObjects($search, $objectType, $objectIds, $verb, $offset, $limit);
}
/**
* Search for comments on one or more objects with a given content
*
* @param string $search content to search for
* @param string $objectType Limit the search by object type
* @param array $objectIds Limit the search by object ids
* @param string $verb Limit the verb of the comment
* @param int $offset
* @param int $limit
* @return IComment[]
*/
public function searchForObjects(string $search, string $objectType, array $objectIds, string $verb, int $offset, int $limit = 50): array {
$query = $this->dbConn->getQueryBuilder(); $query = $this->dbConn->getQueryBuilder();
$query->select('*') $query->select('*')
@ -531,8 +568,8 @@ class Manager implements ICommentsManager {
if ($objectType !== '') { if ($objectType !== '') {
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType))); $query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType)));
} }
if ($objectId !== '') { if (!empty($objectIds)) {
$query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId))); $query->andWhere($query->expr()->in('object_id', $query->createNamedParameter($objectIds, IQueryBuilder::PARAM_STR_ARRAY)));
} }
if ($verb !== '') { if ($verb !== '') {
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb))); $query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
@ -587,6 +624,100 @@ class Manager implements ICommentsManager {
return (int)$data[0]; return (int)$data[0];
} }
/**
* @param string $objectType
* @param string $objectId
* @param int $lastRead
* @param string $verb
* @return int
* @since 21.0.0
*/
public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int {
$query = $this->dbConn->getQueryBuilder();
$query->select($query->func()->count('id', 'num_messages'))
->from('comments')
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
->andWhere($query->expr()->gt('id', $query->createNamedParameter($lastRead)));
if ($verb !== '') {
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
}
$result = $query->execute();
$data = $result->fetch();
$result->closeCursor();
return (int) ($data['num_messages'] ?? 0);
}
/**
* @param string $objectType
* @param string $objectId
* @param \DateTime $beforeDate
* @param string $verb
* @return int
* @since 21.0.0
*/
public function getLastCommentBeforeDate(string $objectType, string $objectId, \DateTime $beforeDate, string $verb = ''): int {
$query = $this->dbConn->getQueryBuilder();
$query->select('id')
->from('comments')
->where($query->expr()->eq('object_type', $query->createNamedParameter($objectType)))
->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)))
->andWhere($query->expr()->lt('creation_timestamp', $query->createNamedParameter($beforeDate, IQueryBuilder::PARAM_DATE)))
->orderBy('creation_timestamp', 'desc');
if ($verb !== '') {
$query->andWhere($query->expr()->eq('verb', $query->createNamedParameter($verb)));
}
$result = $query->execute();
$data = $result->fetch();
$result->closeCursor();
return (int) ($data['id'] ?? 0);
}
/**
* @param string $objectType
* @param string $objectId
* @param string $verb
* @param string $actorType
* @param string[] $actors
* @return \DateTime[] Map of "string actor" => "\DateTime most recent comment date"
* @psalm-return array<string, \DateTime>
* @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 * Get the number of unread comments for all files in a folder
* *

View File

@ -27,9 +27,7 @@ namespace OC\Comments;
use OCP\Comments\ICommentsManager; use OCP\Comments\ICommentsManager;
use OCP\Comments\ICommentsManagerFactory; use OCP\Comments\ICommentsManagerFactory;
use OCP\IInitialStateService;
use OCP\IServerContainer; use OCP\IServerContainer;
use Psr\Log\LoggerInterface;
class ManagerFactory implements ICommentsManagerFactory { class ManagerFactory implements ICommentsManagerFactory {
@ -56,11 +54,6 @@ class ManagerFactory implements ICommentsManagerFactory {
* @since 9.0.0 * @since 9.0.0
*/ */
public function getManager() { public function getManager() {
return new Manager( return $this->serverContainer->get(Manager::class);
$this->serverContainer->getDatabaseConnection(),
$this->serverContainer->get(LoggerInterface::class),
$this->serverContainer->getConfig(),
$this->serverContainer->get(IInitialStateService::class)
);
} }
} }

View File

@ -128,6 +128,7 @@ interface ICommentsManager {
* @param string $sortDirection direction of the comments (`asc` or `desc`) * @param string $sortDirection direction of the comments (`asc` or `desc`)
* @param int $limit optional, number of maximum comments to be returned. if * @param int $limit optional, number of maximum comments to be returned. if
* set to 0, all comments are returned. * set to 0, all comments are returned.
* @param bool $includeLastKnown
* @return IComment[] * @return IComment[]
* @since 14.0.0 * @since 14.0.0
*/ */
@ -136,7 +137,8 @@ interface ICommentsManager {
string $objectId, string $objectId,
int $lastKnownCommentId, int $lastKnownCommentId,
string $sortDirection = 'asc', string $sortDirection = 'asc',
int $limit = 30 int $limit = 30,
bool $includeLastKnown = false
): array; ): array;
/** /**
@ -153,6 +155,20 @@ interface ICommentsManager {
*/ */
public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array; public function search(string $search, string $objectType, string $objectId, string $verb, int $offset, int $limit = 50): array;
/**
* Search for comments on one or more objects with a given content
*
* @param string $search content to search for
* @param string $objectType Limit the search by object type
* @param array $objectIds Limit the search by object ids
* @param string $verb Limit the verb of the comment
* @param int $offset
* @param int $limit
* @return IComment[]
* @since 21.0.0
*/
public function searchForObjects(string $search, string $objectType, array $objectIds, string $verb, int $offset, int $limit = 50): array;
/** /**
* @param $objectType string the object type, e.g. 'files' * @param $objectType string the object type, e.g. 'files'
* @param $objectId string the id of the object * @param $objectId string the id of the object
@ -164,6 +180,44 @@ interface ICommentsManager {
*/ */
public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = ''); public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null, $verb = '');
/**
* @param string $objectType
* @param string $objectId
* @param int $lastRead
* @param string $verb
* @return int
* @since 21.0.0
*/
public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int;
/**
* @param string $objectType
* @param string $objectId
* @param \DateTime $beforeDate
* @param string $verb
* @return int
* @since 21.0.0
*/
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 \DateTime[] Map of "string actor" => "\DateTime most recent comment date"
* @psalm-return array<string, \DateTime>
* @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 * Get the number of unread comments for all files in a folder
* *

View File

@ -30,7 +30,8 @@ class FakeManager implements ICommentsManager {
string $objectId, string $objectId,
int $lastKnownCommentId, int $lastKnownCommentId,
string $sortDirection = 'asc', string $sortDirection = 'asc',
int $limit = 30 int $limit = 30,
bool $includeLastKnown = false
): array { ): array {
return []; return [];
} }
@ -86,4 +87,20 @@ class FakeManager implements ICommentsManager {
public function load(): void { public function load(): void {
} }
public function searchForObjects(string $search, string $objectType, array $objectIds, string $verb, int $offset, int $limit = 50): array {
return [];
}
public function getNumberOfCommentsForObjectSinceComment(string $objectType, string $objectId, int $lastRead, string $verb = ''): int {
return 0;
}
public function getLastCommentBeforeDate(string $objectType, string $objectId, \DateTime $beforeDate, string $verb = ''): int {
return 0;
}
public function getLastCommentDateByActor(string $objectType, string $objectId, string $verb, string $actorType, array $actors): array {
return [];
}
} }

View File

@ -3,13 +3,17 @@
namespace Test\Comments; namespace Test\Comments;
use OC\Comments\Comment; use OC\Comments\Comment;
use OC\Comments\ManagerFactory; use OC\Comments\Manager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment; use OCP\Comments\IComment;
use OCP\Comments\ICommentsEventHandler; use OCP\Comments\ICommentsEventHandler;
use OCP\Comments\ICommentsManager; use OCP\Comments\ICommentsManager;
use OCP\Comments\NotFoundException; use OCP\Comments\NotFoundException;
use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\IInitialStateService;
use OCP\IUser; use OCP\IUser;
use Psr\Log\LoggerInterface;
use Test\TestCase; use Test\TestCase;
/** /**
@ -63,8 +67,13 @@ class ManagerTest extends TestCase {
} }
protected function getManager() { protected function getManager() {
$factory = new ManagerFactory(\OC::$server); return new Manager(
return $factory->getManager(); $this->connection,
$this->createMock(LoggerInterface::class),
$this->createMock(IConfig::class),
$this->createMock(ITimeFactory::class),
$this->createMock(IInitialStateService::class)
);
} }