diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index 27352b275fa..225a639ab65 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -367,7 +367,7 @@ public function parseMessage(Message $chatMessage): void { $parsedMessage = $this->l->t('An administrator demoted {user} from moderator'); } } elseif ($message === 'guest_moderator_promoted') { - $parsedParameters['user'] = $this->getGuest($room, $parameters['session']); + $parsedParameters['user'] = $this->getGuest($room, Attendee::ACTOR_GUESTS, $parameters['session']); $parsedMessage = $this->l->t('{actor} promoted {user} to moderator'); if ($currentUserIsActor) { $parsedMessage = $this->l->t('You promoted {user} to moderator'); @@ -380,7 +380,7 @@ public function parseMessage(Message $chatMessage): void { $parsedMessage = $this->l->t('An administrator promoted {user} to moderator'); } } elseif ($message === 'guest_moderator_demoted') { - $parsedParameters['user'] = $this->getGuest($room, $parameters['session']); + $parsedParameters['user'] = $this->getGuest($room, Attendee::ACTOR_GUESTS, $parameters['session']); $parsedMessage = $this->l->t('{actor} demoted {user} from moderator'); if ($currentUserIsActor) { $parsedMessage = $this->l->t('You demoted {user} from moderator'); @@ -660,8 +660,8 @@ protected function getActorFromComment(Room $room, IComment $comment): array { } protected function getActor(Room $room, string $actorType, string $actorId): array { - if ($actorType === Attendee::ACTOR_GUESTS) { - return $this->getGuest($room, $actorId); + if ($actorType === Attendee::ACTOR_GUESTS || $actorType === Attendee::ACTOR_EMAILS) { + return $this->getGuest($room, $actorType, $actorId); } if ($actorType === Attendee::ACTOR_FEDERATED_USERS) { return $this->getRemoteUser($actorId); @@ -769,23 +769,26 @@ protected function loadCircleDetails(string $circleId): void { } } - protected function getGuest(Room $room, string $actorId): array { - if (!isset($this->guestNames[$actorId])) { - $this->guestNames[$actorId] = $this->getGuestName($room, $actorId); + protected function getGuest(Room $room, string $actorType, string $actorId): array { + $key = $room->getId() . '/' . $actorType . '/' . $actorId; + if (!isset($this->guestNames[$key])) { + $this->guestNames[$key] = $this->getGuestName($room, $actorType, $actorId); } return [ 'type' => 'guest', 'id' => 'guest/' . $actorId, - 'name' => $this->guestNames[$actorId], + 'name' => $this->guestNames[$key], ]; } - protected function getGuestName(Room $room, string $actorId): string { + protected function getGuestName(Room $room, string $actorType, string $actorId): string { try { - $participant = $room->getParticipantByActor(Attendee::ACTOR_GUESTS, $actorId); + $participant = $room->getParticipantByActor($actorType, $actorId); $name = $participant->getAttendee()->getDisplayName(); - if ($name === '') { + if ($name === '' && $actorType === Attendee::ACTOR_EMAILS) { + $name = $actorId; + } elseif ($name === '') { return $this->l->t('Guest'); } return $this->l->t('%s (guest)', [$name]); diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index 5f0bce5f125..ec5f5543b2d 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -492,7 +492,7 @@ public function testParseMessage(string $message, array $parameters, $recipientI ->willReturn(['id' => $parameters['group'] ?? 'group', 'type' => 'group']); $parser->expects($this->any()) ->method('getGuest') - ->with($room, $parameters['session'] ?? 'guest') + ->with($room, Attendee::ACTOR_GUESTS, $parameters['session'] ?? 'guest') ->willReturn(['id' => $parameters['session'] ?? 'guest', 'type' => 'guest']); if ($message === 'call_ended') { @@ -885,7 +885,7 @@ public function testGetActor(string $actorType, array $guestData, array $userDat } else { $parser->expects($this->once()) ->method('getGuest') - ->with($room, 'author-id') + ->with($room, Attendee::ACTOR_GUESTS, 'author-id') ->willReturn($guestData); } @@ -1041,40 +1041,65 @@ public function testGetDisplayNameGroup(string $gid, bool $validGroup, string $n self::assertSame($name, self::invokePrivate($parser, 'getDisplayNameGroup', [$gid])); } - public function testGetGuest() { - $actorId = sha1('name'); + public function dataGetGuest(): array { + return [ + [Attendee::ACTOR_GUESTS, sha1('name')], + [Attendee::ACTOR_EMAILS, 'test@test.tld'], + ]; + } + /** + * @dataProvider dataGetGuest + * @param string $attendeeType + * @param string $actorId + */ + public function testGetGuest(string $attendeeType, string $actorId): void { /** @var Room|MockObject $room */ $room = $this->createMock(Room::class); $parser = $this->getParser(['getGuestName']); $parser->expects($this->once()) ->method('getGuestName') - ->with($room, $actorId) + ->with($room, $attendeeType, $actorId) ->willReturn('name'); $this->assertSame([ 'type' => 'guest', 'id' => 'guest/' . $actorId, 'name' => 'name', - ], self::invokePrivate($parser, 'getGuest', [$room, $actorId])); + ], self::invokePrivate($parser, 'getGuest', [$room, $attendeeType, $actorId])); // Cached call: no call to getGuestName() again $this->assertSame([ 'type' => 'guest', 'id' => 'guest/' . $actorId, 'name' => 'name', - ], self::invokePrivate($parser, 'getGuest', [$room, $actorId])); + ], self::invokePrivate($parser, 'getGuest', [$room, $attendeeType, $actorId])); } - public function testGetGuestName() { - $actorId = sha1('name'); + public function dataGetGuestName(): array { + return [ + [Attendee::ACTOR_GUESTS, sha1('name'), 'name', 'name (guest)'], + [Attendee::ACTOR_GUESTS, sha1('name'), '', 'Guest'], + [Attendee::ACTOR_EMAILS, 'test@test.tld', 'name', 'name (guest)'], + [Attendee::ACTOR_EMAILS, 'test@test.tld', '', 'test@test.tld (guest)'], + ]; + } + + /** + * @dataProvider dataGetGuestName + * @param string $actorType + * @param string $actorId + * @param string $attendeeName + * @param string $expected + */ + public function testGetGuestName(string $actorType, string $actorId, string $attendeeName, string $expected): void { /** @var Room|MockObject $room */ $room = $this->createMock(Room::class); $attendee = Attendee::fromParams([ - 'displayName' => 'name', + 'displayName' => $attendeeName, ]); /** @var Participant|MockObject $room */ @@ -1083,12 +1108,12 @@ public function testGetGuestName() { ->willReturn($attendee); $room->method('getParticipantByActor') - ->with(Attendee::ACTOR_GUESTS, $actorId) + ->with($actorType, $actorId) ->willReturn($participant); $parser = $this->getParser(); self::invokePrivate($parser, 'l', [$this->l]); - $this->assertSame('name (guest)', self::invokePrivate($parser, 'getGuestName', [$room, $actorId])); + $this->assertSame($expected, self::invokePrivate($parser, 'getGuestName', [$room, $actorType, $actorId])); } public function testGetGuestNameThrows() { @@ -1103,7 +1128,7 @@ public function testGetGuestNameThrows() { $parser = $this->getParser(); self::invokePrivate($parser, 'l', [$this->l]); - $this->assertSame('Guest', self::invokePrivate($parser, 'getGuestName', [$room, $actorId])); + $this->assertSame('Guest', self::invokePrivate($parser, 'getGuestName', [$room, Attendee::ACTOR_GUESTS, $actorId])); } public function dataParseCall(): array {