From 641f5ca6965db27fda7ec3699d3aabc0526ba59d Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Mon, 30 Jan 2023 14:43:13 +0100 Subject: Return the rooms when possible Signed-off-by: Joas Schilling --- lib/Controller/BreakoutRoomController.php | 83 +++++++++++++++++++++++-------- lib/Service/BreakoutRoomService.php | 44 ++++++++++++---- 2 files changed, 96 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/Controller/BreakoutRoomController.php b/lib/Controller/BreakoutRoomController.php index 161ec1aff..fef7a6732 100644 --- a/lib/Controller/BreakoutRoomController.php +++ b/lib/Controller/BreakoutRoomController.php @@ -26,7 +26,10 @@ declare(strict_types=1); namespace OCA\Talk\Controller; use InvalidArgumentException; +use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\Service\BreakoutRoomService; +use OCA\Talk\Service\ParticipantService; +use OCA\Talk\Service\RoomFormatter; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\Comments\MessageTooLongException; @@ -37,6 +40,9 @@ class BreakoutRoomController extends AEnvironmentAwareController { string $appName, IRequest $request, protected BreakoutRoomService $breakoutRoomService, + protected ParticipantService $participantService, + protected RoomFormatter $roomFormatter, + protected ?string $userId, ) { parent::__construct($appName, $request); } @@ -52,13 +58,13 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function configureBreakoutRooms(int $mode, int $amount, string $attendeeMap = '[]'): DataResponse { try { - $this->breakoutRoomService->setupBreakoutRooms($this->room, $mode, $amount, $attendeeMap); + $rooms = $this->breakoutRoomService->setupBreakoutRooms($this->room, $mode, $amount, $attendeeMap); } catch (InvalidArgumentException $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - // FIXME make a useful response? - return new DataResponse(); + $rooms[] = $this->room; + return new DataResponse($this->formatMultipleRooms($rooms), Http::STATUS_OK); } /** @@ -67,7 +73,13 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function removeBreakoutRooms(): DataResponse { $this->breakoutRoomService->removeBreakoutRooms($this->room); - return new DataResponse(); + + return new DataResponse($this->roomFormatter->formatRoom( + $this->getResponseFormat(), + [], + $this->room, + $this->participant, + )); } /** @@ -76,13 +88,14 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function broadcastChatMessage(string $message): DataResponse { try { - $this->breakoutRoomService->broadcastChatMessage($this->room, $this->participant, $message); + $rooms = $this->breakoutRoomService->broadcastChatMessage($this->room, $this->participant, $message); } catch (MessageTooLongException $e) { return new DataResponse(['error' => 'message'], Http::STATUS_REQUEST_ENTITY_TOO_LARGE); } catch (InvalidArgumentException $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse([], Http::STATUS_CREATED); + $rooms[] = $this->room; + return new DataResponse($this->formatMultipleRooms($rooms), Http::STATUS_CREATED); } /** @@ -91,18 +104,17 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function applyAttendeeMap(string $attendeeMap): DataResponse { try { - $this->breakoutRoomService->applyAttendeeMap($this->room, $attendeeMap); + $rooms = $this->breakoutRoomService->applyAttendeeMap($this->room, $attendeeMap); } catch (InvalidArgumentException $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse([], Http::STATUS_OK); + $rooms[] = $this->room; + return new DataResponse($this->formatMultipleRooms($rooms), Http::STATUS_OK); } /** * @NoAdminRequired * @RequireLoggedInParticipant - * - * @return DataResponse */ public function requestAssistance(): DataResponse { try { @@ -111,14 +123,17 @@ class BreakoutRoomController extends AEnvironmentAwareController { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse(); + return new DataResponse($this->roomFormatter->formatRoom( + $this->getResponseFormat(), + [], + $this->room, + $this->participant, + )); } /** * @NoAdminRequired * @RequireLoggedInParticipant - * - * @return DataResponse */ public function resetRequestForAssistance(): DataResponse { try { @@ -127,7 +142,12 @@ class BreakoutRoomController extends AEnvironmentAwareController { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse(); + return new DataResponse($this->roomFormatter->formatRoom( + $this->getResponseFormat(), + [], + $this->room, + $this->participant, + )); } /** @@ -136,12 +156,13 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function startBreakoutRooms(): DataResponse { try { - $this->breakoutRoomService->startBreakoutRooms($this->room); + $rooms = $this->breakoutRoomService->startBreakoutRooms($this->room); } catch (InvalidArgumentException $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse(); + $rooms[] = $this->room; + return new DataResponse($this->formatMultipleRooms($rooms), Http::STATUS_OK); } /** @@ -150,12 +171,13 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function stopBreakoutRooms(): DataResponse { try { - $this->breakoutRoomService->stopBreakoutRooms($this->room); + $rooms = $this->breakoutRoomService->stopBreakoutRooms($this->room); } catch (InvalidArgumentException $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse(); + $rooms[] = $this->room; + return new DataResponse($this->formatMultipleRooms($rooms), Http::STATUS_OK); } /** @@ -164,11 +186,32 @@ class BreakoutRoomController extends AEnvironmentAwareController { */ public function switchBreakoutRoom(string $target): DataResponse { try { - $this->breakoutRoomService->switchBreakoutRoom($this->room, $this->participant, $target); + $room = $this->breakoutRoomService->switchBreakoutRoom($this->room, $this->participant, $target); } catch (InvalidArgumentException $e) { return new DataResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } - return new DataResponse(); + return new DataResponse($this->roomFormatter->formatRoom( + $this->getResponseFormat(), + [], + $room, + $this->participant, + )); + } + + protected function formatMultipleRooms(array $rooms): array { + $return = []; + foreach ($rooms as $room) { + try { + $return[] = $this->roomFormatter->formatRoom( + $this->getResponseFormat(), + [], + $room, + $this->participantService->getParticipant($room, $this->userId) + ); + } catch (ParticipantNotFoundException $e) { + } + } + return $return; } } diff --git a/lib/Service/BreakoutRoomService.php b/lib/Service/BreakoutRoomService.php index caf3c8bcd..9872ecdd4 100644 --- a/lib/Service/BreakoutRoomService.php +++ b/lib/Service/BreakoutRoomService.php @@ -189,9 +189,10 @@ class BreakoutRoomService { /** * @param Room $parent * @param string $attendeeMap + * @return Room[] * @throws InvalidArgumentException When the map was invalid, breakout rooms are disabled or not configured for this conversation */ - public function applyAttendeeMap(Room $parent, string $attendeeMap): void { + public function applyAttendeeMap(Room $parent, string $attendeeMap): array { if (!$this->config->isBreakoutRoomsEnabled()) { throw new InvalidArgumentException('config'); } @@ -253,6 +254,8 @@ class BreakoutRoomService { } $this->addOthersToBreakoutRooms($breakoutRooms, $map); + + return $breakoutRooms; } /** @@ -342,7 +345,13 @@ class BreakoutRoomService { } } - public function broadcastChatMessage(Room $parent, Participant $participant, string $message): void { + /** + * @param Room $parent + * @param Participant $participant + * @param string $message + * @return Room[] + */ + public function broadcastChatMessage(Room $parent, Participant $participant, string $message): array { if ($parent->getBreakoutRoomMode() === BreakoutRoom::MODE_NOT_CONFIGURED) { throw new \InvalidArgumentException('mode'); } @@ -356,13 +365,16 @@ class BreakoutRoomService { try { foreach ($breakoutRooms as $breakoutRoom) { $breakoutParticipant = $this->participantService->getParticipantByActor($breakoutRoom, $attendeeType, $attendeeId); - $this->chatManager->sendMessage($breakoutRoom, $breakoutParticipant, $attendeeType, $attendeeId, $message, $creationDateTime, null, '', false); + $comment = $this->chatManager->sendMessage($breakoutRoom, $breakoutParticipant, $attendeeType, $attendeeId, $message, $creationDateTime, null, '', false); + $breakoutRoom->setLastMessage($comment); } } finally { if ($shouldFlush) { $this->notificationManager->flush(); } } + + return $breakoutRooms; } public function requestAssistance(Room $breakoutRoom): void { @@ -392,7 +404,11 @@ class BreakoutRoomService { $this->roomService->setBreakoutRoomStatus($breakoutRoom, $status); } - public function startBreakoutRooms(Room $parent): void { + /** + * @param Room $parent + * @return Room[] + */ + public function startBreakoutRooms(Room $parent): array { if ($parent->getBreakoutRoomMode() === BreakoutRoom::MODE_NOT_CONFIGURED) { throw new \InvalidArgumentException('mode'); } @@ -404,10 +420,14 @@ class BreakoutRoomService { $this->roomService->setBreakoutRoomStatus($parent, BreakoutRoom::STATUS_STARTED); - // FIXME missing to send the signaling messages so participants are moved + return $breakoutRooms; } - public function stopBreakoutRooms(Room $parent): void { + /** + * @param Room $parent + * @return Room[] + */ + public function stopBreakoutRooms(Room $parent): array { if ($parent->getBreakoutRoomMode() === BreakoutRoom::MODE_NOT_CONFIGURED) { throw new \InvalidArgumentException('mode'); } @@ -423,10 +443,10 @@ class BreakoutRoomService { $this->roomService->setBreakoutRoomStatus($parent, BreakoutRoom::STATUS_STOPPED); - // FIXME missing to send the signaling messages so participants are moved back + return $breakoutRooms; } - public function switchBreakoutRoom(Room $parent, Participant $participant, string $targetToken): void { + public function switchBreakoutRoom(Room $parent, Participant $participant, string $targetToken): Room { if ($parent->getBreakoutRoomMode() !== BreakoutRoom::MODE_FREE) { throw new \InvalidArgumentException('mode'); } @@ -444,15 +464,15 @@ class BreakoutRoomService { $breakoutRooms = $this->manager->getMultipleRoomsByObject(BreakoutRoom::PARENT_OBJECT_TYPE, $parent->getToken()); - $foundTarget = false; + $target = null; foreach ($breakoutRooms as $breakoutRoom) { if ($targetToken === $breakoutRoom->getToken()) { - $foundTarget = true; + $target = $breakoutRoom; break; } } - if (!$foundTarget) { + if ($target === null) { throw new \InvalidArgumentException('target'); } @@ -489,6 +509,8 @@ class BreakoutRoomService { } } } + + return $target; } /** -- cgit v1.2.3