From 71a0fe0d630e070399c76839694c97c1a9cd3c78 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 19 Dec 2024 12:48:48 +0100 Subject: [PATCH] fix(call): Fix call summary when the connection was interrupted Signed-off-by: Joas Schilling --- lib/Chat/Parser/SystemMessage.php | 27 ++++++++++++--------- tests/php/Chat/Parser/SystemMessageTest.php | 13 ++++++++-- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index aebaebf5eb3..074e3140dfa 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -22,6 +22,7 @@ use OCA\Talk\Service\ParticipantService; use OCA\Talk\Share\Helper\FilesMetadataCache; use OCA\Talk\Share\RoomShareProvider; +use OCP\AppFramework\Services\IAppConfig; use OCP\Comments\IComment; use OCP\Comments\ICommentsManager; use OCP\EventDispatcher\Event; @@ -67,6 +68,7 @@ class SystemMessage implements IEventListener { protected array $currentFederatedUserDetails = []; public function __construct( + protected IAppConfig $appConfig, protected IUserManager $userManager, protected IGroupManager $groupManager, protected GuestManager $guestManager, @@ -1108,6 +1110,9 @@ protected function parseMissedCall(Room $room, array $parameters, ?string $curre protected function parseCall(Room $room, string $message, array $parameters, array $params): array { $actorIsSystem = $params['actor']['type'] === 'guest' && $params['actor']['id'] === 'guest/' . Attendee::ACTOR_ID_SYSTEM; + $maxDuration = $this->appConfig->getAppValueInt('max_call_duration'); + $maxDurationWasReached = $message === 'call_ended_everyone' && $actorIsSystem && $maxDuration > 0 && $parameters['duration'] > $maxDuration; + if ($message === 'call_ended_everyone') { if ($params['actor']['type'] === 'user') { $entry = array_keys($parameters['users'], $params['actor']['id'], true); @@ -1133,7 +1138,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr case 0: if ($parameters['guests'] === 0) { // Call without users and guests - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call ended (Duration {duration})'); @@ -1141,7 +1146,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr $subject = $this->l->t('{actor} ended the call (Duration {duration})'); } } else { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->n( 'Call with %n guest was ended, as it reached the maximum call duration (Duration {duration})', 'Call with %n guests was ended, as it reached the maximum call duration (Duration {duration})', @@ -1164,7 +1169,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr break; case 1: if ($parameters['guests'] === 0) { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1} ended (Duration {duration})'); @@ -1172,7 +1177,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr $subject = $this->l->t('{actor} ended the call with {user1} (Duration {duration})'); } } else { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1} and {user2} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1} and {user2} ended (Duration {duration})'); @@ -1184,7 +1189,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr break; case 2: if ($parameters['guests'] === 0) { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1} and {user2} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1} and {user2} ended (Duration {duration})'); @@ -1192,7 +1197,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr $subject = $this->l->t('{actor} ended the call with {user1} and {user2} (Duration {duration})'); } } else { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1}, {user2} and {user3} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1}, {user2} and {user3} ended (Duration {duration})'); @@ -1204,7 +1209,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr break; case 3: if ($parameters['guests'] === 0) { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1}, {user2} and {user3} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1}, {user2} and {user3} ended (Duration {duration})'); @@ -1212,7 +1217,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr $subject = $this->l->t('{actor} ended the call with {user1}, {user2} and {user3} (Duration {duration})'); } } else { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1}, {user2}, {user3} and {user4} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1}, {user2}, {user3} and {user4} ended (Duration {duration})'); @@ -1224,7 +1229,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr break; case 4: if ($parameters['guests'] === 0) { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1}, {user2}, {user3} and {user4} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1}, {user2}, {user3} and {user4} ended (Duration {duration})'); @@ -1232,7 +1237,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr $subject = $this->l->t('{actor} ended the call with {user1}, {user2}, {user3} and {user4} (Duration {duration})'); } } else { - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1}, {user2}, {user3}, {user4} and {user5} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1}, {user2}, {user3}, {user4} and {user5} ended (Duration {duration})'); @@ -1244,7 +1249,7 @@ protected function parseCall(Room $room, string $message, array $parameters, arr break; case 5: default: - if ($actorIsSystem) { + if ($maxDurationWasReached) { $subject = $this->l->t('Call with {user1}, {user2}, {user3}, {user4} and {user5} was ended, as it reached the maximum call duration (Duration {duration})'); } elseif ($message === 'call_ended') { $subject = $this->l->t('Call with {user1}, {user2}, {user3}, {user4} and {user5} ended (Duration {duration})'); diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index a1609886049..7ab0b71fac2 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -22,6 +22,7 @@ use OCA\Talk\Service\ParticipantService; use OCA\Talk\Share\Helper\FilesMetadataCache; use OCA\Talk\Share\RoomShareProvider; +use OCP\AppFramework\Services\IAppConfig; use OCP\Comments\IComment; use OCP\Federation\ICloudId; use OCP\Federation\ICloudIdManager; @@ -45,6 +46,7 @@ * @group DB */ class SystemMessageTest extends TestCase { + protected IAppConfig&MockObject $appConfig; protected IUserManager&MockObject $userManager; protected IGroupManager&MockObject $groupManager; protected GuestManager&MockObject $guestManager; @@ -62,6 +64,7 @@ class SystemMessageTest extends TestCase { public function setUp(): void { parent::setUp(); + $this->appConfig = $this->createMock(IAppConfig::class); $this->userManager = $this->createMock(IUserManager::class); $this->groupManager = $this->createMock(IGroupManager::class); $this->guestManager = $this->createMock(GuestManager::class); @@ -94,6 +97,7 @@ protected function getParser(array $methods = []): SystemMessage { if (!empty($methods)) { $mock = $this->getMockBuilder(SystemMessage::class) ->setConstructorArgs([ + $this->appConfig, $this->userManager, $this->groupManager, $this->guestManager, @@ -113,6 +117,7 @@ protected function getParser(array $methods = []): SystemMessage { return $mock; } return new SystemMessage( + $this->appConfig, $this->userManager, $this->groupManager, $this->guestManager, @@ -1549,7 +1554,7 @@ public static function dataParseCall(): array { // Automatically ended by background job (max_call_duration reached) 'max_call_duration cleanup' => [ 'call_ended_everyone', - ['users' => ['user1', 'user2', 'user3', 'user4'], 'guests' => 4, 'duration' => 42], + ['users' => ['user1', 'user2', 'user3', 'user4'], 'guests' => 4, 'duration' => 90], ['type' => 'guest', 'id' => 'guest/system', 'name' => 'system'], [ 'Call with {user1}, {user2}, {user3}, {user4} and 4 guests was ended, as it reached the maximum call duration (Duration "duration")', @@ -1567,9 +1572,13 @@ public function testParseCall(string $message, array $parameters, array $actor, $parser = $this->getParser(['getDuration', 'getUser']); $parser->expects($this->once()) ->method('getDuration') - ->with(42) + ->with($parameters['duration']) ->willReturn('"duration"'); + $this->appConfig->method('getAppValueInt') + ->with('max_call_duration') + ->willReturn(60); + $parser->expects($this->any()) ->method('getUser') ->willReturnCallback(function ($user) {