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

fix(federation): Fix parsing system messages for federated users #11688

Merged
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
28 changes: 21 additions & 7 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ protected function parseMessage(Message $chatMessage): void {

$participant = $chatMessage->getParticipant();
if ($participant === null) {
$currentActorType = null;
$currentActorId = null;
$currentUserIsActor = false;
} elseif ($this->federationAuthenticator->isFederationRequest()) {
Expand All @@ -149,16 +150,20 @@ protected function parseMessage(Message $chatMessage): void {
];
}

$currentActorType = $participant->getAttendee()->getActorType();
$currentActorId = $participant->getAttendee()->getActorId();
$currentUserIsActor = isset($parsedParameters['actor']['server']) &&
$parsedParameters['actor']['type'] === 'user' &&
$this->currentFederatedUserDetails['user'] === $parsedParameters['actor']['id'] &&
$this->currentFederatedUserDetails['server'] === $parsedParameters['actor']['server'];
} elseif (!$participant->isGuest()) {
$currentActorType = $participant->getAttendee()->getActorType();
$currentActorId = $participant->getAttendee()->getActorId();
$currentUserIsActor = $parsedParameters['actor']['type'] === 'user' &&
$participant->getAttendee()->getActorType() === Attendee::ACTOR_USERS &&
$currentActorId === $parsedParameters['actor']['id'];
} else {
$currentActorType = $participant->getAttendee()->getActorType();
$currentActorId = $participant->getAttendee()->getActorId();
$currentUserIsActor = $parsedParameters['actor']['type'] === 'guest' &&
$participant->getAttendee()->getActorType() === 'guest' &&
Expand Down Expand Up @@ -227,7 +232,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('You left the call');
}
} elseif ($message === 'call_missed') {
[$parsedMessage, $parsedParameters, $message] = $this->parseMissedCall($room, $parameters, $currentActorId);
[$parsedMessage, $parsedParameters, $message] = $this->parseMissedCall($room, $parameters, $currentActorType === Attendee::ACTOR_FEDERATED_USERS ? null : $currentActorId);
} elseif ($message === 'call_ended' || $message === 'call_ended_everyone') {
[$parsedMessage, $parsedParameters] = $this->parseCall($message, $parameters, $parsedParameters);
} elseif ($message === 'read_only_off') {
Expand Down Expand Up @@ -326,7 +331,7 @@ protected function parseMessage(Message $chatMessage): void {
}
} elseif ($currentUserIsActor) {
$parsedMessage = $this->l->t('You added {user}');
} elseif ($participant && !$participant->isGuest() && $currentActorId === $parsedParameters['user']['id']) {
} elseif ($this->isCurrentParticipantChangedUser($currentActorType, $currentActorId, $parsedParameters['user'])) {
$parsedMessage = $this->l->t('{actor} added you');
if ($cliIsActor) {
$parsedMessage = $this->l->t('An administrator added you');
Expand All @@ -346,7 +351,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('{actor} removed {user}');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You removed {user}');
} elseif ($participant && !$participant->isGuest() && $currentActorId === $parsedParameters['user']['id']) {
} elseif ($this->isCurrentParticipantChangedUser($currentActorType, $currentActorId, $parsedParameters['user'])) {
$parsedMessage = $this->l->t('{actor} removed you');
if ($cliIsActor) {
$parsedMessage = $this->l->t('An administrator removed you');
Expand Down Expand Up @@ -431,7 +436,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('{actor} promoted {user} to moderator');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You promoted {user} to moderator');
} elseif ($participant && !$participant->isGuest() && $currentActorId === $parsedParameters['user']['id']) {
} elseif ($this->isCurrentParticipantChangedUser($currentActorType, $currentActorId, $parsedParameters['user'])) {
$parsedMessage = $this->l->t('{actor} promoted you to moderator');
if ($cliIsActor) {
$parsedMessage = $this->l->t('An administrator promoted you to moderator');
Expand All @@ -444,7 +449,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('{actor} demoted {user} from moderator');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You demoted {user} from moderator');
} elseif ($participant && !$participant->isGuest() && $currentActorId === $parsedParameters['user']['id']) {
} elseif ($this->isCurrentParticipantChangedUser($currentActorType, $currentActorId, $parsedParameters['user'])) {
$parsedMessage = $this->l->t('{actor} demoted you from moderator');
if ($cliIsActor) {
$parsedMessage = $this->l->t('An administrator demoted you from moderator');
Expand All @@ -457,7 +462,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('{actor} promoted {user} to moderator');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You promoted {user} to moderator');
} elseif ($participant && $participant->isGuest() && $currentActorId === $parsedParameters['user']['id']) {
} elseif ($this->isCurrentParticipantChangedUser($currentActorType, $currentActorId, $parsedParameters['user'])) {
$parsedMessage = $this->l->t('{actor} promoted you to moderator');
if ($cliIsActor) {
$parsedMessage = $this->l->t('An administrator promoted you to moderator');
Expand All @@ -470,7 +475,7 @@ protected function parseMessage(Message $chatMessage): void {
$parsedMessage = $this->l->t('{actor} demoted {user} from moderator');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You demoted {user} from moderator');
} elseif ($participant && $participant->isGuest() && $currentActorId === $parsedParameters['user']['id']) {
} elseif ($this->isCurrentParticipantChangedUser($currentActorType, $currentActorId, $parsedParameters['user'])) {
$parsedMessage = $this->l->t('{actor} demoted you from moderator');
if ($cliIsActor) {
$parsedMessage = $this->l->t('An administrator demoted you from moderator');
Expand Down Expand Up @@ -819,6 +824,15 @@ protected function getFileFromShare(?Participant $participant, string $shareId):
return $data;
}

protected function isCurrentParticipantChangedUser(?string $currentActorType, ?string $currentActorId, array $parameter): bool {
if (isset($parameter['server']) && $currentActorType === Attendee::ACTOR_FEDERATED_USERS) {
return $this->currentFederatedUserDetails['user'] === $parameter['id']
&& $this->currentFederatedUserDetails['server'] === $parameter['server'];
}

return $currentActorType === Attendee::ACTOR_USERS && $currentActorId === $parameter['id'];
}

protected function getActorFromComment(Room $room, IComment $comment): array {
return $this->getActor($room, $comment->getActorType(), $comment->getActorId());
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Federation/Proxy/TalkV1/UserConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,18 @@
/**
* @param Room $room
* @param TalkChatMessageWithParent $message
* @return TalkChatMessageWithParent

Check failure on line 103 in lib/Federation/Proxy/TalkV1/UserConverter.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidReturnType

lib/Federation/Proxy/TalkV1/UserConverter.php:103:13: InvalidReturnType: The declared return type 'array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, parent?: array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}' for OCA\Talk\Federation\Proxy\TalkV1\UserConverter::convertMessage is incorrect, got 'array{parent?: array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, parent?: array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}|mixed, token: string, ...<array-key, mixed>}' (see https://psalm.dev/011)
*/
public function convertMessage(Room $room, array $message): array {
$message = $this->convertAttendee($room, $message, 'actorType', 'actorId', 'actorDisplayName');
$message = $this->convertAttendee($room, $message, 'lastEditActorType', 'lastEditActorId', 'lastEditActorDisplayName');
$message = $this->convertMessageParameters($room, $message);
$message['token'] = $room->getToken();

if (isset($message['parent'])) {
$message['parent'] = $this->convertMessage($room, $message['parent']);
}
return $message;

Check failure on line 114 in lib/Federation/Proxy/TalkV1/UserConverter.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidReturnStatement

lib/Federation/Proxy/TalkV1/UserConverter.php:114:10: InvalidReturnStatement: The inferred type 'array{parent?: array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, parent?: array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}|mixed, token: string, ...<array-key, mixed>}' does not match the declared return type 'array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, parent?: array{actorDisplayName: string, actorId: string, actorType: string, deleted?: true, expirationTimestamp: int, id: int, isReplyable: bool, lastEditActorDisplayName?: string, lastEditActorId?: string, lastEditActorType?: string, lastEditTimestamp?: int, markdown: bool, message: string, messageParameters: array<string, array<string, mixed>>, messageType: string, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}, reactions: array<string, int>|stdClass, referenceId: string, silent?: bool, systemMessage: string, timestamp: int, token: string}' for OCA\Talk\Federation\Proxy\TalkV1\UserConverter::convertMessage (see https://psalm.dev/128)
}

/**
Expand Down
Loading