aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Comments
diff options
context:
space:
mode:
authorVitor Mattos <vitor@php.rio>2022-01-14 11:14:52 -0300
committerVitor Mattos <vitor@php.rio>2022-01-21 08:39:39 -0300
commitf7cd9956127be66dfd3141e1f05b6d3cfaceea05 (patch)
tree27a035e7accd6442c61360093c42dcc704d2defb /lib/private/Comments
parent1ce894a50c2720a5881b22533d730062b7b4b426 (diff)
downloadnextcloud-server-f7cd9956127be66dfd3141e1f05b6d3cfaceea05.tar.gz
nextcloud-server-f7cd9956127be66dfd3141e1f05b6d3cfaceea05.zip
Order the reaction summary, improvements on validations
Signed-off-by: Vitor Mattos <vitor@php.rio>
Diffstat (limited to 'lib/private/Comments')
-rw-r--r--lib/private/Comments/Manager.php51
1 files changed, 38 insertions, 13 deletions
diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php
index 534de6a937b..2e9acb6ca24 100644
--- a/lib/private/Comments/Manager.php
+++ b/lib/private/Comments/Manager.php
@@ -41,6 +41,7 @@ use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUser;
use OCP\IInitialStateService;
+use OCP\PreConditionNotMetException;
use OCP\Util;
use Psr\Log\LoggerInterface;
@@ -136,6 +137,10 @@ class Manager implements ICommentsManager {
throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
}
+ if ($comment->getVerb() === 'reaction' && strlen($comment->getMessage()) > 8) {
+ throw new \UnexpectedValueException('Reactions cannot be longer than 8 bytes');
+ }
+
if ($comment->getId() === '') {
$comment->setChildrenCount(0);
$comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC')));
@@ -912,9 +917,6 @@ class Manager implements ICommentsManager {
}
private function deleteReaction(IComment $reaction): void {
- if (!$this->supportReactions()) {
- return;
- }
$qb = $this->dbConn->getQueryBuilder();
$qb->delete('reactions')
->where($qb->expr()->eq('parent_id', $qb->createNamedParameter($reaction->getParentId())))
@@ -926,18 +928,20 @@ class Manager implements ICommentsManager {
/**
* Get comment related with user reaction
*
+ * Throws PreConditionNotMetException when the system haven't the minimum requirements to
+ * use reactions
+ *
* @param integer $parentId
* @param string $actorType
* @param string $actorId
* @param string $reaction
* @return IComment
* @throws NotFoundException
+ * @throws PreConditionNotMetException
* @since 24.0.0
*/
public function getReactionComment(int $parentId, string $actorType, string $actorId, string $reaction): IComment {
- if (!$this->supportReactions()) {
- throw new NotFoundException('The database does not support reactions');
- }
+ $this->throwIfNotSupportReactions();
$qb = $this->dbConn->getQueryBuilder();
$messageId = $qb
->select('message_id')
@@ -984,22 +988,40 @@ class Manager implements ICommentsManager {
return $comments;
}
+ /**
+ * Support reactions
+ *
+ * @return boolean
+ * @since 24.0.0
+ */
public function supportReactions(): bool {
return $this->dbConn->supports4ByteText();
}
/**
+ * @throws PreConditionNotMetException
+ * @since 24.0.0
+ */
+ private function throwIfNotSupportReactions() {
+ if (!$this->supportReactions()) {
+ throw new PreConditionNotMetException('The database does not support reactions');
+ }
+ }
+
+ /**
* Retrieve all reactions of a message
*
+ * Throws PreConditionNotMetException when the system haven't the minimum requirements to
+ * use reactions
+ *
* @param integer $parentId
* @param string $reaction
+ * @throws PreConditionNotMetException
* @return IComment[]
* @since 24.0.0
*/
public function retrieveAllReactions(int $parentId): array {
- if (!$this->supportReactions()) {
- return [];
- }
+ $this->throwIfNotSupportReactions();
$qb = $this->dbConn->getQueryBuilder();
$result = $qb
->select('message_id')
@@ -1058,9 +1080,13 @@ class Manager implements ICommentsManager {
* Throws NotFoundException when a comment that is to be updated does not
* exist anymore at this point of time.
*
+ * Throws PreConditionNotMetException when the system haven't the minimum requirements to
+ * use reactions
+ *
* @param IComment $comment
* @return bool
* @throws NotFoundException
+ * @throws PreConditionNotMetException
* @since 9.0.0
*/
public function save(IComment $comment) {
@@ -1140,9 +1166,6 @@ class Manager implements ICommentsManager {
}
private function addReaction(IComment $reaction): void {
- if (!$this->supportReactions()) {
- return;
- }
// Prevent violate constraint
$qb = $this->dbConn->getQueryBuilder();
$qb->select($qb->func()->count('*'))
@@ -1191,7 +1214,9 @@ class Manager implements ICommentsManager {
)
->from('reactions', 'r')
->where($totalQuery->expr()->eq('r.parent_id', $qb->createNamedParameter($parentId)))
- ->groupBy('r.reaction');
+ ->groupBy('r.reaction')
+ ->orderBy($totalQuery->func()->count('id'), 'DESC')
+ ->setMaxResults(200);
$jsonQuery = $this->dbConn->getQueryBuilder();
$jsonQuery