Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable30] feat(email): Recognize guests invited via email #13617

Merged
merged 14 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix(email): Fix handling of email guests in system messages
Signed-off-by: Joas Schilling <[email protected]>
  • Loading branch information
nickvergessen authored and backportbot[bot] committed Oct 23, 2024
commit 69ba9f18d3cb10e2f31bddc427aa3ca4d0af403e
25 changes: 22 additions & 3 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ protected function parseMessage(Message $chatMessage): void {
$participant->getAttendee()->getActorType() === Attendee::ACTOR_USERS &&
$currentActorId === $parsedParameters['actor']['id'] &&
empty($parsedParameters['actor']['server']);
} elseif ($participant->getAttendee()->getActorType() === Attendee::ACTOR_EMAILS) {
$currentActorType = $participant->getAttendee()->getActorType();
$currentActorId = $participant->getAttendee()->getActorId();
$currentUserIsActor = $parsedParameters['actor']['type'] === 'email' &&
$participant->getAttendee()->getActorType() === Attendee::ACTOR_EMAILS &&
$participant->getAttendee()->getActorId() === $parsedParameters['actor']['id'];
} else {
$currentActorType = $participant->getAttendee()->getActorType();
$currentActorId = $participant->getAttendee()->getActorId();
Expand Down Expand Up @@ -457,7 +463,12 @@ protected 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, Attendee::ACTOR_GUESTS, $parameters['session']);
if (isset($parameters['type'], $parameters['id'])) {
$parsedParameters['user'] = $this->getGuest($room, $parameters['type'], $parameters['id']);
} else {
// Before Nextcloud 30
$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 @@ -470,7 +481,12 @@ protected 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, Attendee::ACTOR_GUESTS, $parameters['session']);
if (isset($parameters['type'], $parameters['id'])) {
$parsedParameters['user'] = $this->getGuest($room, $parameters['type'], $parameters['id']);
} else {
// Before Nextcloud 30
$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 @@ -847,6 +863,9 @@ protected function isCurrentParticipantChangedUser(?string $currentActorType, ?s
if ($currentActorType === Attendee::ACTOR_GUESTS) {
return $parameter['type'] === 'guest' && $currentActorId === $parameter['id'];
}
if ($currentActorType === Attendee::ACTOR_EMAILS) {
return $parameter['type'] === 'guest' && 'email/' . $currentActorId === $parameter['id'];
}

if (isset($parameter['server'])
&& $currentActorType === Attendee::ACTOR_FEDERATED_USERS
Expand Down Expand Up @@ -1019,7 +1038,7 @@ protected function getGuest(Room $room, string $actorType, string $actorId): arr

return [
'type' => 'guest',
'id' => 'guest/' . $actorId,
'id' => ($actorType === Attendee::ACTOR_GUESTS ? 'guest/' : 'email/') . $actorId,
'name' => $this->guestNames[$key],
];
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,9 @@ public function sendSystemMessageAboutPromoteOrDemoteModerator(ParticipantModifi
$this->sendSystemMessage($room, 'moderator_demoted', ['user' => $attendee->getActorId()]);
}
} elseif ($event->getNewValue() === Participant::GUEST_MODERATOR) {
$this->sendSystemMessage($room, 'guest_moderator_promoted', ['session' => $attendee->getActorId()]);
$this->sendSystemMessage($room, 'guest_moderator_promoted', ['type' => $attendee->getActorType(), 'id' => $attendee->getActorId()]);
} elseif ($event->getNewValue() === Participant::GUEST) {
$this->sendSystemMessage($room, 'guest_moderator_demoted', ['session' => $attendee->getActorId()]);
$this->sendSystemMessage($room, 'guest_moderator_demoted', ['type' => $attendee->getActorType(), 'id' => $attendee->getActorId()]);
}
}

Expand Down
10 changes: 5 additions & 5 deletions tests/php/Chat/Parser/SystemMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1217,15 +1217,15 @@ public function testGetDisplayNameGroup(string $gid, bool $validGroup, string $n

public static function dataGetGuest(): array {
return [
[Attendee::ACTOR_GUESTS, sha1('name')],
[Attendee::ACTOR_EMAILS, '[email protected]'],
[Attendee::ACTOR_GUESTS, sha1('name'), 'guest/' . sha1('name')],
[Attendee::ACTOR_EMAILS, hash('sha256', '[email protected]'), 'email/' . hash('sha256', '[email protected]')],
];
}

/**
* @dataProvider dataGetGuest
*/
public function testGetGuest(string $attendeeType, string $actorId): void {
public function testGetGuest(string $attendeeType, string $actorId, string $expected): void {
/** @var Room&MockObject $room */
$room = $this->createMock(Room::class);

Expand All @@ -1237,14 +1237,14 @@ public function testGetGuest(string $attendeeType, string $actorId): void {

$this->assertSame([
'type' => 'guest',
'id' => 'guest/' . $actorId,
'id' => $expected,
'name' => 'name',
], self::invokePrivate($parser, 'getGuest', [$room, $attendeeType, $actorId]));

// Cached call: no call to getGuestName() again
$this->assertSame([
'type' => 'guest',
'id' => 'guest/' . $actorId,
'id' => $expected,
'name' => 'name',
], self::invokePrivate($parser, 'getGuest', [$room, $attendeeType, $actorId]));
}
Expand Down
16 changes: 14 additions & 2 deletions tests/php/Chat/SystemMessage/ListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,25 @@ public static function dataParticipantTypeChange(): array {
Attendee::ACTOR_GUESTS,
Participant::GUEST,
Participant::GUEST_MODERATOR,
[['message' => 'guest_moderator_promoted', 'parameters' => ['session' => 'bob_participant']]],
[['message' => 'guest_moderator_promoted', 'parameters' => ['type' => 'guests', 'id' => 'bob_participant']]],
],
[
Attendee::ACTOR_GUESTS,
Participant::GUEST_MODERATOR,
Participant::GUEST,
[['message' => 'guest_moderator_demoted', 'parameters' => ['session' => 'bob_participant']]],
[['message' => 'guest_moderator_demoted', 'parameters' => ['type' => 'guests', 'id' => 'bob_participant']]],
],
[
Attendee::ACTOR_EMAILS,
Participant::GUEST,
Participant::GUEST_MODERATOR,
[['message' => 'guest_moderator_promoted', 'parameters' => ['type' => 'emails', 'id' => 'bob_participant']]],
],
[
Attendee::ACTOR_EMAILS,
Participant::GUEST_MODERATOR,
Participant::GUEST,
[['message' => 'guest_moderator_demoted', 'parameters' => ['type' => 'emails', 'id' => 'bob_participant']]],
],
[
Attendee::ACTOR_USERS,
Expand Down