Limit the summary and sort it afterwards

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2022-01-17 11:56:24 +01:00 committed by Vitor Mattos
parent f071b4dfbb
commit 189f9f96ce
No known key found for this signature in database
GPG key ID: B7AB4B76A7CA7318
2 changed files with 44 additions and 10 deletions

View file

@ -104,7 +104,18 @@ class Manager implements ICommentsManager {
$data['children_count'] = (int)$data['children_count'];
$data['reference_id'] = $data['reference_id'] ?? null;
if ($this->supportReactions()) {
$data['reactions'] = json_decode($data['reactions'], true);
$list = json_decode($data['reactions'], true);
// Ordering does not work on the database with group concat and Oracle,
// So we simply sort on the output.
if (is_array($list)) {
uasort($list, static function($a, $b) {
if ($a === $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
});
}
$data['reactions'] = $list;
}
return $data;
}
@ -1033,10 +1044,8 @@ class Manager implements ICommentsManager {
while ($data = $result->fetch()) {
$commentIds[] = $data['message_id'];
}
$comments = [];
$comments = $this->getCommentsById($commentIds);
return $comments;
return $this->getCommentsById($commentIds);
}
/**
@ -1217,14 +1226,14 @@ class Manager implements ICommentsManager {
->where($totalQuery->expr()->eq('r.parent_id', $qb->createNamedParameter($parentId)))
->groupBy('r.reaction')
->orderBy('total', 'DESC')
->setMaxResults(200);
->setMaxResults(20);
$jsonQuery = $this->dbConn->getQueryBuilder();
$jsonQuery
->selectAlias(
$jsonQuery->func()->concat(
$jsonQuery->expr()->literal('{'),
$jsonQuery->func()->groupConcat('colonseparatedvalue', ',', $jsonQuery->getColumnName('total') . ' DESC'),
$jsonQuery->func()->groupConcat('colonseparatedvalue', ','),
$jsonQuery->expr()->literal('}')
),
'json'

View file

@ -1175,7 +1175,7 @@ class ManagerTest extends TestCase {
/**
* @dataProvider providerTestReactionsSummarizeOrdered
*/
public function testReactionsSummarizeOrdered(array $comments, $expected) {
public function testReactionsSummarizeOrdered(array $comments, array $expected, bool $isFullMatch) {
$this->skipIfNotSupport4ByteUTF();
$manager = $this->getManager();
@ -1192,7 +1192,13 @@ class ManagerTest extends TestCase {
}
}
$actual = $manager->get($comment->getParentId());
$this->assertSame($expected, $actual->getReactions());
if ($isFullMatch) {
$this->assertSame($expected, $actual->getReactions());
} else {
$subResult = array_slice($actual->getReactions(), 0, count($expected));
$this->assertSame($expected, $subResult);
}
}
public function providerTestReactionsSummarizeOrdered(): array {
@ -1203,11 +1209,31 @@ class ManagerTest extends TestCase {
['👍', 'alice', 'reaction', 'message'],
],
['👍' => 1],
true,
],
[
[
['message', 'alice', 'comment', null],
['👎', 'John', 'reaction', 'message'],
['💼', 'Luke', 'reaction', 'message'],
['📋', 'Luke', 'reaction', 'message'],
['🚀', 'Luke', 'reaction', 'message'],
['🖤', 'Luke', 'reaction', 'message'],
['😜', 'Luke', 'reaction', 'message'],
['🌖', 'Luke', 'reaction', 'message'],
['💖', 'Luke', 'reaction', 'message'],
['📥', 'Luke', 'reaction', 'message'],
['🐉', 'Luke', 'reaction', 'message'],
['☕', 'Luke', 'reaction', 'message'],
['🐄', 'Luke', 'reaction', 'message'],
['🐕', 'Luke', 'reaction', 'message'],
['🐈', 'Luke', 'reaction', 'message'],
['🛂', 'Luke', 'reaction', 'message'],
['🕸', 'Luke', 'reaction', 'message'],
['🏰', 'Luke', 'reaction', 'message'],
['⚙️', 'Luke', 'reaction', 'message'],
['🚨', 'Luke', 'reaction', 'message'],
['👥', 'Luke', 'reaction', 'message'],
['👍', 'Paul', 'reaction', 'message'],
['👍', 'Peter', 'reaction', 'message'],
['💜', 'Matthew', 'reaction', 'message'],
@ -1215,11 +1241,10 @@ class ManagerTest extends TestCase {
['💜', 'Luke', 'reaction', 'message'],
],
[
'💜' => 3,
'👍' => 2,
'👎' => 1,
],
false,
],
];
}