Browse Source

Make possible disable background job if haven't room with expire message

Signed-off-by: Vitor Mattos <vitor@php.rio>
pull/7571/head
Vitor Mattos 3 years ago
parent
commit
9db9abc179
No known key found for this signature in database GPG Key ID: B7AB4B76A7CA7318
  1. 35
      lib/Service/RoomService.php
  2. 12
      tests/integration/features/bootstrap/FeatureContext.php
  3. 11
      tests/integration/features/chat/message-expiration.feature

35
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(),

12
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
*/

11
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
Loading…
Cancel
Save