diff options
author | Vitor Mattos <vitor@php.rio> | 2022-01-09 19:10:20 -0300 |
---|---|---|
committer | Vitor Mattos <vitor@php.rio> | 2022-01-21 08:37:06 -0300 |
commit | a7c0868a4cc7a0f041484800ad56901f11086459 (patch) | |
tree | e8d462c2767dc95f77b9c8feb98dc2874eba6349 /lib | |
parent | 1dda03e1b59a2da7e9c08511120db20c825afeae (diff) | |
download | nextcloud-server-a7c0868a4cc7a0f041484800ad56901f11086459.tar.gz nextcloud-server-a7c0868a4cc7a0f041484800ad56901f11086459.zip |
Prevent violate constraint
Signed-off-by: Vitor Mattos <vitor@php.rio>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/Comments/Manager.php | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/lib/private/Comments/Manager.php b/lib/private/Comments/Manager.php index 599c4e106d7..382ffbb9028 100644 --- a/lib/private/Comments/Manager.php +++ b/lib/private/Comments/Manager.php @@ -1123,16 +1123,35 @@ class Manager implements ICommentsManager { } private function addReaction(IComment $reaction): void { + // Prevent violate constraint $qb = $this->dbConn->getQueryBuilder(); - $qb->insert('reactions') - ->values([ - 'parent_id' => $qb->createNamedParameter($reaction->getParentId()), - 'message_id' => $qb->createNamedParameter($reaction->getId()), - 'actor_type' => $qb->createNamedParameter($reaction->getActorType()), - 'actor_id' => $qb->createNamedParameter($reaction->getActorId()), - 'reaction' => $qb->createNamedParameter($reaction->getMessage()), - ]) - ->executeStatement(); + $qb->select($qb->func()->count('*')) + ->from('reactions') + ->where($qb->expr()->eq('parent_id', $qb->createNamedParameter($reaction->getParentId()))) + ->andWhere($qb->expr()->eq('actor_type', $qb->createNamedParameter($reaction->getActorType()))) + ->andWhere($qb->expr()->eq('actor_id', $qb->createNamedParameter($reaction->getActorId()))) + ->andWhere($qb->expr()->eq('reaction', $qb->createNamedParameter($reaction->getMessage()))); + $result = $qb->executeQuery(); + $exists = (int) $result->fetchOne(); + if (!$exists) { + $qb = $this->dbConn->getQueryBuilder(); + try { + $qb->insert('reactions') + ->values([ + 'parent_id' => $qb->createNamedParameter($reaction->getParentId()), + 'message_id' => $qb->createNamedParameter($reaction->getId()), + 'actor_type' => $qb->createNamedParameter($reaction->getActorType()), + 'actor_id' => $qb->createNamedParameter($reaction->getActorId()), + 'reaction' => $qb->createNamedParameter($reaction->getMessage()), + ]) + ->executeStatement(); + } catch (\Exception $e) { + $this->logger->error($e->getMessage(), [ + 'exception' => $e, + 'app' => 'core_comments', + ]); + } + } $this->sumReactions($reaction->getParentId()); } |