Sort suggestions by last chat message

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2018-04-25 17:27:16 +02:00 коммит произвёл Daniel Calviño Sánchez
Родитель 1472935fb5
Коммит 63831a4455
3 изменённых файлов: 67 добавлений и 1 удалений

Просмотреть файл

@ -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']];
});
}
}
}

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -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(),