diff --git a/lib/Chat/AutoComplete/Sorter.php b/lib/Chat/AutoComplete/Sorter.php index 7514f4db4..802556c0f 100644 --- a/lib/Chat/AutoComplete/Sorter.php +++ b/lib/Chat/AutoComplete/Sorter.php @@ -22,10 +22,18 @@ namespace OCA\Spreed\Chat\AutoComplete; +use OCA\Spreed\Chat\CommentsManager; use OCP\Collaboration\AutoComplete\ISorter; class Sorter implements ISorter { + /** @var CommentsManager */ + protected $commentsManager; + + public function __construct(CommentsManager $commentsManager) { + $this->commentsManager = $commentsManager; + } + /** * @return string The ID of the sorter, e.g. commenters * @since 13.0.0 @@ -42,6 +50,25 @@ class Sorter implements ISorter { * @since 13.0.0 */ public function sort(array &$sortArray, array $context) { - // TODO: Implement sort() method. + foreach ($sortArray as $type => &$byType) { + $lastComments = $this->commentsManager->getLastCommentDateByActor( + $context['itemType'], + $context['itemId'], + 'comment', + $type, + array_map(function($suggestion) { + return $suggestion['value']['shareWith']; + }, $byType)); + + usort($byType, function ($a, $b) use ($lastComments) { + if (!isset($lastComments[$b['value']['shareWith']])) { + return -1; + } + if (!isset($lastComments[$a['value']['shareWith']])) { + return 1; + } + return $lastComments[$a['value']['shareWith']] - $lastComments[$b['value']['shareWith']]; + }); + } } } diff --git a/lib/Chat/CommentsManager.php b/lib/Chat/CommentsManager.php index a0ff0a3b3..85cb24ab8 100644 --- a/lib/Chat/CommentsManager.php +++ b/lib/Chat/CommentsManager.php @@ -143,4 +143,41 @@ class CommentsManager extends Manager { return null; } + + /** + * @param string $objectType + * @param string $objectId + * @param string $verb + * @param string $actorType + * @param string[] $actors + * @return array + */ + public function getLastCommentDateByActor( + $objectType, + $objectId, + $verb, + $actorType, + array $actors + ) { + $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']] = new \DateTime($row['latest_comment']); + } + $result->closeCursor(); + + return $lastComments; + } } diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 2c1839c2d..0b108bddc 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -25,6 +25,7 @@ namespace OCA\Spreed\Controller; use OC\Collaboration\Collaborators\SearchResult; use OCA\Spreed\Chat\AutoComplete\SearchPlugin; +use OCA\Spreed\Chat\AutoComplete\Sorter; use OCA\Spreed\Chat\ChatManager; use OCA\Spreed\Chat\RichMessageHelper; use OCA\Spreed\Exceptions\ParticipantNotFoundException; @@ -325,6 +326,7 @@ class ChatController extends OCSController { unset($results['exact']); $results = array_merge_recursive($exactMatches, $results); + $this->autoCompleteManager->registerSorter(Sorter::class); $this->autoCompleteManager->runSorters(['talk_chat_participants'], $results, [ 'itemType' => 'chat', 'itemId' => $room->getId(),