summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2022-01-17 11:56:24 +0100
committerVitor Mattos <vitor@php.rio>2022-01-21 08:39:39 -0300
commit189f9f96ce8a93e228acc5821099d8e870576292 (patch)
treef3b0c689e2005b135463795b584825f66af13a8c
parentf071b4dfbbd5fccae9b7b07b9a13ed71ddc91ce4 (diff)
downloadnextcloud-server-189f9f96ce8a93e228acc5821099d8e870576292.tar.gz
nextcloud-server-189f9f96ce8a93e228acc5821099d8e870576292.zip
Limit the summary and sort it afterwards
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--lib/private/Comments/Manager.php21
-rw-r--r--tests/lib/Comments/ManagerTest.php33
2 files changed, 44 insertions, 10 deletions
diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php
index 410fcf26f4d..95fb7f24e53 100644
--- a/lib/private/Comments/Manager.php
+++ b/lib/private/Comments/Manager.php
@@ -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'
diff --git a/tests/lib/Comments/ManagerTest.php b/tests/lib/Comments/ManagerTest.php
index 9e34cce6476..cba314e880b 100644
--- a/tests/lib/Comments/ManagerTest.php
+++ b/tests/lib/Comments/ManagerTest.php
@@ -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,
],
];
}