From 9db9abc179f4aade0c6e2beb8fd6eaa39535a814 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Mon, 11 Jul 2022 17:49:10 -0300 Subject: [PATCH] Make possible disable background job if haven't room with expire message Signed-off-by: Vitor Mattos --- lib/Service/RoomService.php | 35 ++++++++++++++++--- .../features/bootstrap/FeatureContext.php | 12 +++++++ .../features/chat/message-expiration.feature | 11 ++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/Service/RoomService.php b/lib/Service/RoomService.php index 9aa260a438..5b63c687da 100644 --- a/lib/Service/RoomService.php +++ b/lib/Service/RoomService.php @@ -547,20 +547,45 @@ class RoomService { ->set('message_expiration', $update->createNamedParameter($seconds, IQueryBuilder::PARAM_INT)) ->where($update->expr()->eq('id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT))); $update->executeStatement(); + $room->setMessageExpiration($seconds); + + $this->toggleJobExpireMesssageIfNecessary($seconds); + $this->addMessageExpirationSystemMessage($room, $participant, $seconds); + + $this->dispatcher->dispatch(Room::EVENT_AFTER_SET_MESSAGE_EXPIRATION, $event); + } + + private function toggleJobExpireMesssageIfNecessary(int $seconds): void { if ($seconds > 0) { if (!$this->jobList->has(ExpireChatMessages::class, null)) { $this->jobList->add(ExpireChatMessages::class, null); } - $this->addMessageExpirationSystemMessage($room, $participant, $seconds, 'message_expiration_enabled'); - } else { - $this->addMessageExpirationSystemMessage($room, $participant, $seconds, 'message_expiration_disabled'); + return; } + $this->disableExpireMessageJobIfNecesary(); + } - $this->dispatcher->dispatch(Room::EVENT_AFTER_SET_MESSAGE_EXPIRATION, $event); + private function disableExpireMessageJobIfNecesary(): void { + $qb = $this->db->getQueryBuilder(); + $qb->select($qb->func()->count('*', 'total')) + ->from('talk_rooms') + ->where($qb->expr()->gt('message_expiration', $qb->createNamedParameter(0))); + $result = $qb->executeQuery(); + $count = (int) $result->fetchOne(); + $result->closeCursor(); + + if (!$count) { + $this->jobList->remove(ExpireChatMessages::class, null); + } } - private function addMessageExpirationSystemMessage(Room $room, Participant $participant, int $seconds, string $message): void { + private function addMessageExpirationSystemMessage(Room $room, Participant $participant, int $seconds): void { + if ($seconds > 0) { + $message = 'message_expiration_enabled'; + } else { + $message = 'message_expiration_disabled'; + } $this->chatManager->addSystemMessage( $room, $participant->getAttendee()->getActorType(), diff --git a/tests/integration/features/bootstrap/FeatureContext.php b/tests/integration/features/bootstrap/FeatureContext.php index 5aec92abc6..07c94e0474 100644 --- a/tests/integration/features/bootstrap/FeatureContext.php +++ b/tests/integration/features/bootstrap/FeatureContext.php @@ -2602,6 +2602,18 @@ class FeatureContext implements Context, SnippetAcceptingContext { $this->setCurrentUser($currentUser); } + /** + * @When message expiration job don't exists + */ + public function messageExpirationJobDontExists(): void { + $currentUser = $this->currentUser; + $this->setCurrentUser('admin'); + $this->sendRequest('GET', '/apps/spreedcheats/get_message_expiration_job'); + $response = $this->getDataFromResponse($this->response); + Assert::assertEmpty($response, 'Job found'); + $this->setCurrentUser($currentUser); + } + /* * Requests */ diff --git a/tests/integration/features/chat/message-expiration.feature b/tests/integration/features/chat/message-expiration.feature index 97aff7e2ca..2282ded80a 100644 --- a/tests/integration/features/chat/message-expiration.feature +++ b/tests/integration/features/chat/message-expiration.feature @@ -23,3 +23,14 @@ Feature: room/message-expiration Then user "participant1" sees the following messages in room "room" with 200 | room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage | | room | users | participant1 | participant1-displayname | Message 1 | [] | | + + + Scenario: Toggle message expiration job + Given user "participant1" creates room "room" (v4) + | roomType | 3 | + | roomName | room | + And user "participant1" adds user "participant2" to room "room" with 200 (v4) + And user "participant1" set the message expiration to 10 of room "room" with 200 (v4) + And apply message expiration job + And user "participant1" set the message expiration to 0 of room "room" with 200 (v4) + Then message expiration job don't exists