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

[stable27] fix(API): Reuse participant objects we already created #10536

Merged
merged 2 commits into from
Sep 19, 2023
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
6 changes: 4 additions & 2 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function addSystemMessage(
* @return IComment
*/
public function addChangelogMessage(Room $chat, string $message): IComment {
$comment = $this->commentsManager->create(Attendee::ACTOR_GUESTS, 'changelog', 'chat', (string) $chat->getId());
$comment = $this->commentsManager->create(Attendee::ACTOR_GUESTS, Attendee::ACTOR_ID_CHANGELOG, 'chat', (string) $chat->getId());

$comment->setMessage($message, self::MAX_CHAT_LENGTH);
$comment->setCreationDateTime($this->timeFactory->getDateTime());
Expand Down Expand Up @@ -300,7 +300,9 @@ public function sendMessage(Room $chat, ?Participant $participant, string $actor
}

// Update last_message
if ($comment->getActorType() !== Attendee::ACTOR_BOTS || $comment->getActorId() === 'changelog' || str_starts_with($comment->getActorId(), Attendee::ACTOR_BOT_PREFIX)) {
if ($comment->getActorType() !== Attendee::ACTOR_BOTS
|| $comment->getActorId() === Attendee::ACTOR_ID_CHANGELOG
|| str_starts_with($comment->getActorId(), Attendee::ACTOR_BOT_PREFIX)) {
$this->roomService->setLastMessage($chat, $comment);
$this->unreadCountCache->clear($chat->getId() . '-');
} else {
Expand Down
6 changes: 6 additions & 0 deletions lib/Chat/MessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ protected function setActor(Message $message): void {
} elseif ($comment->getActorType() === Attendee::ACTOR_BRIDGED) {
$displayName = $comment->getActorId();
$actorId = MatterbridgeManager::BRIDGE_BOT_USERID;
} elseif ($comment->getActorType() === Attendee::ACTOR_GUESTS
&& $comment->getActorId() === Attendee::ACTOR_ID_CLI) {
$actorId = Attendee::ACTOR_ID_CLI;
} elseif ($comment->getActorType() === Attendee::ACTOR_GUESTS
&& $comment->getActorId() === Attendee::ACTOR_ID_CHANGELOG) {
$actorId = Attendee::ACTOR_ID_CHANGELOG;
} elseif ($comment->getActorType() === Attendee::ACTOR_GUESTS) {
if (isset($guestNames[$comment->getActorId()])) {
$displayName = $this->guestNames[$comment->getActorId()];
Expand Down
4 changes: 2 additions & 2 deletions lib/Chat/Parser/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class Changelog {
*/
public function parseMessage(Message $chatMessage): void {
if ($chatMessage->getActorType() !== Attendee::ACTOR_GUESTS ||
$chatMessage->getActorId() !== 'changelog') {
$chatMessage->getActorId() !== Attendee::ACTOR_ID_CHANGELOG) {
throw new \OutOfBoundsException('Not a changelog');
}

$l = $chatMessage->getL10n();
$chatMessage->setActor('bots', 'changelog', $l->t('Talk updates ✅'));
$chatMessage->setActor(Attendee::ACTOR_BOTS, Attendee::ACTOR_ID_CHANGELOG, $l->t('Talk updates ✅'));
}
}
4 changes: 4 additions & 0 deletions lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ protected function getGuest(Room $room, string $actorType, string $actorId): arr
}

protected function getGuestName(Room $room, string $actorType, string $actorId): string {
if ($actorId === Attendee::ACTOR_ID_CLI) {
return $this->l->t('Guest');
}

try {
$participant = $this->participantService->getParticipantByActor($room, $actorType, $actorId);
$name = $participant->getAttendee()->getDisplayName();
Expand Down
2 changes: 1 addition & 1 deletion lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ protected function sendSystemMessage(Room $room, string $message, array $paramet
$actorId = $user->getUID();
} elseif (\OC::$CLI || $this->session->exists('talk-overwrite-actor-cli')) {
$actorType = Attendee::ACTOR_GUESTS;
$actorId = 'cli';
$actorId = Attendee::ACTOR_ID_CLI;
} elseif ($this->session->exists('talk-overwrite-actor-type')) {
$actorType = $this->session->get('talk-overwrite-actor-type');
$actorId = $this->session->get('talk-overwrite-actor-id');
Expand Down
17 changes: 13 additions & 4 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,9 @@ public function getRoomsForActor(string $actorType, string $actorId, array $sess

$room = $this->createRoomObject($row);
if ($actorType === Attendee::ACTOR_USERS && isset($row['actor_id'])) {
$room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row));
$participant = $this->createParticipantObject($room, $row);
$this->participantService->cacheParticipant($room, $participant);
$room->setParticipant($row['actor_id'], $participant);
}
$rooms[] = $room;
}
Expand Down Expand Up @@ -572,7 +574,9 @@ public function getRoomForUser(int $roomId, ?string $userId): Room {

$room = $this->createRoomObject($row);
if ($userId !== null && isset($row['actor_id'])) {
$room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row));
$participant = $this->createParticipantObject($room, $row);
$this->participantService->cacheParticipant($room, $participant);
$room->setParticipant($row['actor_id'], $participant);
}

if ($userId === null && $room->getType() !== Room::TYPE_PUBLIC) {
Expand Down Expand Up @@ -643,7 +647,9 @@ public function getRoomForUserByToken(string $token, ?string $userId, ?string $s

$room = $this->createRoomObject($row);
if ($userId !== null && isset($row['actor_id'])) {
$room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row));
$participant = $this->createParticipantObject($room, $row);
$this->participantService->cacheParticipant($room, $participant);
$room->setParticipant($row['actor_id'], $participant);
}

if ($isSIPBridgeRequest || $room->getType() === Room::TYPE_PUBLIC) {
Expand Down Expand Up @@ -746,7 +752,9 @@ public function getRoomByActor(string $token, string $actorType, string $actorId

$room = $this->createRoomObject($row);
if ($actorType === Attendee::ACTOR_USERS && isset($row['actor_id'])) {
$room->setParticipant($row['actor_id'], $this->createParticipantObject($room, $row));
$participant = $this->createParticipantObject($room, $row);
$this->participantService->cacheParticipant($room, $participant);
$room->setParticipant($row['actor_id'], $participant);
}

return $room;
Expand Down Expand Up @@ -900,6 +908,7 @@ public function getRoomForSession(?string $userId, ?string $sessionId): Room {

$room = $this->createRoomObject($row);
$participant = $this->createParticipantObject($room, $row);
$this->participantService->cacheParticipant($room, $participant);
$room->setParticipant($row['actor_id'], $participant);

if ($room->getType() === Room::TYPE_PUBLIC || !in_array($participant->getAttendee()->getParticipantType(), [Participant::GUEST, Participant::GUEST_MODERATOR, Participant::USER_SELF_JOINED], true)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/Model/Attendee.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ class Attendee extends Entity {
public const ACTOR_BRIDGED = 'bridged';
public const ACTOR_BOTS = 'bots';
public const ACTOR_FEDERATED_USERS = 'federated_users';

// Special actor IDs
public const ACTOR_BOT_PREFIX = 'bot-';
public const ACTOR_ID_CLI = 'cli';
public const ACTOR_ID_CHANGELOG = 'changelog';

public const PERMISSIONS_DEFAULT = 0;
public const PERMISSIONS_CUSTOM = 1;
Expand Down
15 changes: 15 additions & 0 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,21 @@ public function hasActiveSessions(Room $room): bool {
return (bool) $row;
}

public function cacheParticipant(Room $room, Participant $participant): void {
$attendee = $participant->getAttendee();
if ($attendee->getActorType() !== Attendee::ACTOR_USERS) {
return;
}

$this->userCache[$room->getId()] ??= [];
$this->userCache[$room->getId()][$attendee->getActorId()] = $participant;
if ($participant->getSession()) {
$participantSessionId = $participant->getSession()->getSessionId();
$this->sessionCache[$room->getId()] ??= [];
$this->sessionCache[$room->getId()][$participantSessionId] = $participant;
}
}

/**
* @param Room $room
* @return bool
Expand Down
Loading