From e9bc0ad78aa1a3f530ea4c1a52d34ea05e45e707 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 11 Feb 2021 22:20:31 +0100 Subject: Reuse existing attendee when a guest refreshes Signed-off-by: Joas Schilling --- lib/Controller/RoomController.php | 2 +- lib/Service/ParticipantService.php | 34 ++++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/lib/Controller/RoomController.php b/lib/Controller/RoomController.php index 2fa28da53..113583337 100644 --- a/lib/Controller/RoomController.php +++ b/lib/Controller/RoomController.php @@ -1618,7 +1618,7 @@ class RoomController extends AEnvironmentAwareController { $participant = $this->participantService->joinRoom($room, $user, $password, $result['result']); $this->participantService->generatePinForParticipant($room, $participant); } else { - $participant = $this->participantService->joinRoomAsNewGuest($room, $password, $result['result']); + $participant = $this->participantService->joinRoomAsNewGuest($room, $password, $result['result'], $previousParticipant); } } catch (InvalidPasswordException $e) { return new DataResponse([], Http::STATUS_FORBIDDEN); diff --git a/lib/Service/ParticipantService.php b/lib/Service/ParticipantService.php index 3da1c82be..6e1d99588 100644 --- a/lib/Service/ParticipantService.php +++ b/lib/Service/ParticipantService.php @@ -213,11 +213,12 @@ class ParticipantService { * @param Room $room * @param string $password * @param bool $passedPasswordProtection + * @param ?Participant $previousParticipant * @return Participant * @throws InvalidPasswordException * @throws UnauthorizedException */ - public function joinRoomAsNewGuest(Room $room, string $password, bool $passedPasswordProtection = false): Participant { + public function joinRoomAsNewGuest(Room $room, string $password, bool $passedPasswordProtection = false, ?Participant $previousParticipant = null): Participant { $event = new JoinRoomGuestEvent($room, $password, $passedPasswordProtection); $this->dispatcher->dispatch(Room::EVENT_BEFORE_GUEST_CONNECT, $event); @@ -234,21 +235,27 @@ class ParticipantService { $lastMessage = (int) $room->getLastMessage()->getId(); } - $randomActorId = $this->secureRandom->generate(255); + if ($previousParticipant instanceof Participant) { + $attendee = $previousParticipant->getAttendee(); + } else { + $randomActorId = $this->secureRandom->generate(255); - $attendee = new Attendee(); - $attendee->setRoomId($room->getId()); - $attendee->setActorType(Attendee::ACTOR_GUESTS); - $attendee->setActorId($randomActorId); - $attendee->setParticipantType(Participant::GUEST); - $attendee->setLastReadMessage($lastMessage); - $this->attendeeMapper->insert($attendee); + $attendee = new Attendee(); + $attendee->setRoomId($room->getId()); + $attendee->setActorType(Attendee::ACTOR_GUESTS); + $attendee->setActorId($randomActorId); + $attendee->setParticipantType(Participant::GUEST); + $attendee->setLastReadMessage($lastMessage); + $this->attendeeMapper->insert($attendee); + } $session = $this->sessionService->createSessionForAttendee($attendee); - // Update the random guest id - $attendee->setActorId(sha1($session->getSessionId())); - $this->attendeeMapper->update($attendee); + if (!$previousParticipant instanceof Participant) { + // Update the random guest id + $attendee->setActorId(sha1($session->getSessionId())); + $this->attendeeMapper->update($attendee); + } $this->dispatcher->dispatch(Room::EVENT_AFTER_GUEST_CONNECT, $event); @@ -381,8 +388,7 @@ class ParticipantService { $this->sessionMapper->deleteByAttendeeId($participant->getAttendee()->getId()); } - if ($participant->getAttendee()->getActorType() === Attendee::ACTOR_GUESTS - || $participant->getAttendee()->getParticipantType() === Participant::USER_SELF_JOINED) { + if ($participant->getAttendee()->getParticipantType() === Participant::USER_SELF_JOINED) { $this->attendeeMapper->delete($participant->getAttendee()); } -- cgit v1.2.3