Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
Expand Down
51 changes: 38 additions & 13 deletions tests/php/Chat/Parser/SystemMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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, '[email protected]'],
];
}

/**
* @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, '[email protected]', 'name', 'name (guest)'],
[Attendee::ACTOR_EMAILS, '[email protected]', '', '[email protected] (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 */
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down