fix(chat): Handle last-edit displayname and deleted users

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2024-01-18 16:44:40 +01:00
parent 3902cba02f
commit f5fe5dccf4
No known key found for this signature in database
GPG key ID: 74434EFE0D2E2205
6 changed files with 105 additions and 43 deletions

View file

@ -34,8 +34,10 @@ use OCA\Talk\Room;
use OCA\Talk\Service\BotService;
use OCA\Talk\Service\ParticipantService;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserManager;
/**
@ -67,42 +69,82 @@ class MessageParser {
$verb = ChatManager::VERB_SYSTEM;
}
$message->setMessageType($verb);
$this->setActor($message);
$this->setMessageActor($message);
$this->setLastEditInfo($message);
$event = new MessageParseEvent($message->getRoom(), $message);
$this->dispatcher->dispatchTyped($event);
}
protected function setActor(Message $message): void {
$comment = $message->getComment();
protected function setMessageActor(Message $message): void {
[$actorType, $actorId, $displayName] = $this->getActorInformation(
$message,
$message->getComment()->getActorType(),
$message->getComment()->getActorId()
);
$actorId = $comment->getActorId();
$displayName = '';
if ($comment->getActorType() === Attendee::ACTOR_USERS) {
$displayName = $this->userManager->getDisplayName($comment->getActorId()) ?? $comment->getActorId();
} elseif ($comment->getActorType() === Attendee::ACTOR_BRIDGED) {
$displayName = $comment->getActorId();
$message->setActor(
$actorType,
$actorId,
$displayName
);
}
protected function setLastEditInfo(Message $message): void {
$metaData = $message->getComment()->getMetaData();
if (!empty($metaData)) {
if (isset($metaData['last_edited_by_type'], $metaData['last_edited_by_id'], $metaData['last_edited_time'])) {
[$actorType, $actorId, $displayName] = $this->getActorInformation(
$message,
$metaData['last_edited_by_type'],
$metaData['last_edited_by_id'],
$metaData['last_edited_by_displayname'] ?? '',
);
$message->setLastEdit(
$actorType,
$actorId,
$displayName,
$metaData['last_edited_time']
);
}
}
}
protected function getActorInformation(Message $message, string $actorType, string $actorId, string $displayName = ''): array {
if ($actorType === Attendee::ACTOR_USERS) {
$tempDisplayName = $this->userManager->getDisplayName($actorId);
if ($tempDisplayName === null) {
$user = $this->userManager->get($actorId);
if (!$user instanceof IUser) {
// Deleted user
return [
ICommentsManager::DELETED_USER,
ICommentsManager::DELETED_USER,
'',
];
}
$displayName = $user->getDisplayName();
} else {
$displayName = $tempDisplayName;
}
} elseif ($actorType === Attendee::ACTOR_BRIDGED) {
$displayName = $actorId;
$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()];
} elseif ($actorType === Attendee::ACTOR_GUESTS
&& !in_array($actorId, [Attendee::ACTOR_ID_CLI, Attendee::ACTOR_ID_CHANGELOG], true)) {
if (isset($guestNames[$actorId])) {
$displayName = $this->guestNames[$actorId];
} else {
try {
$participant = $this->participantService->getParticipantByActor($message->getRoom(), Attendee::ACTOR_GUESTS, $comment->getActorId());
$participant = $this->participantService->getParticipantByActor($message->getRoom(), Attendee::ACTOR_GUESTS, $actorId);
$displayName = $participant->getAttendee()->getDisplayName();
} catch (ParticipantNotFoundException $e) {
}
$this->guestNames[$comment->getActorId()] = $displayName;
$this->guestNames[$actorId] = $displayName;
}
} elseif ($comment->getActorType() === Attendee::ACTOR_BOTS) {
$actorId = $comment->getActorId();
$displayName = $comment->getActorId() . '-bot';
} elseif ($actorType === Attendee::ACTOR_BOTS) {
$displayName = $actorId . '-bot';
$token = $message->getRoom()->getToken();
if (str_starts_with($actorId, Attendee::ACTOR_BOT_PREFIX)) {
$urlHash = substr($actorId, strlen(Attendee::ACTOR_BOT_PREFIX));
@ -111,16 +153,16 @@ class MessageParser {
$displayName = $botName . ' (Bot)';
}
}
} elseif ($comment->getActorType() === Attendee::ACTOR_FEDERATED_USERS) {
} elseif ($actorType === Attendee::ACTOR_FEDERATED_USERS) {
// FIXME Read from some addressbooks?
$displayName = $actorId;
}
$message->setActor(
$comment->getActorType(),
return [
$actorType,
$actorId,
$displayName
);
];
}
protected function getBotNameByUrlHashForConversation(string $token, string $urlHash): ?string {

View file

@ -37,6 +37,7 @@ use OCA\Talk\Service\ParticipantService;
use OCA\Talk\Share\Helper\FilesMetadataCache;
use OCA\Talk\Share\RoomShareProvider;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Federation\ICloudIdManager;
@ -803,7 +804,7 @@ class SystemMessage implements IEventListener {
if ($this->displayNames[$uid] === null) {
return [
'type' => 'highlight',
'id' => 'deleted_user',
'id' => ICommentsManager::DELETED_USER,
'name' => $this->l->t('Deleted user'),
];
}
@ -964,7 +965,7 @@ class SystemMessage implements IEventListener {
[
'user' => [
'type' => 'highlight',
'id' => 'deleted_user',
'id' => ICommentsManager::DELETED_USER,
'name' => $room->getName(),
],
],

View file

@ -61,6 +61,18 @@ class Message {
/** @var string */
protected $actorDisplayName = '';
/** @var string */
protected $lastEditActorType = '';
/** @var string */
protected $lastEditActorId = '';
/** @var string */
protected $lastEditActorDisplayName = '';
/** @var int */
protected $lastEditTimestamp = 0;
public function __construct(
protected Room $room,
protected ?Participant $participant,
@ -133,6 +145,13 @@ class Message {
$this->actorDisplayName = $displayName;
}
public function setLastEdit(string $type, string $id, string $displayName, int $timestamp): void {
$this->lastEditActorType = $type;
$this->lastEditActorId = $id;
$this->lastEditActorDisplayName = $displayName;
$this->lastEditTimestamp = $timestamp;
}
public function getActorType(): string {
return $this->actorType;
}
@ -190,15 +209,11 @@ class Message {
'markdown' => $this->getMessageType() === ChatManager::VERB_SYSTEM ? false : true,
];
$metaData = $this->getComment()->getMetaData();
if (!empty($metaData)) {
if (isset($metaData['last_edited_by_type'], $metaData['last_edited_by_id'], $metaData['last_edited_time'])) {
// FIXME @see MessageParser::setActor
$data['lastEditActorDisplayName'] = $metaData['last_edited_by_id'];
$data['lastEditActorId'] = $metaData['last_edited_by_id'];
$data['lastEditActorType'] = $metaData['last_edited_by_type'];
$data['lastEditTimestamp'] = $metaData['last_edited_time'];
}
if ($this->lastEditActorType && $this->lastEditActorId && $this->lastEditTimestamp) {
$data['lastEditActorType'] = $this->lastEditActorType;
$data['lastEditActorId'] = $this->lastEditActorId;
$data['lastEditActorDisplayName'] = $this->lastEditActorDisplayName;
$data['lastEditTimestamp'] = $this->lastEditTimestamp;
}
if ($this->getMessageType() === ChatManager::VERB_MESSAGE_DELETED) {

View file

@ -32,6 +32,7 @@ use OCA\Talk\Model\Vote;
use OCA\Talk\Model\VoteMapper;
use OCA\Talk\Participant;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Comments\ICommentsManager;
use OCP\DB\Exception;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
@ -339,8 +340,8 @@ class PollService {
$update = $this->connection->getQueryBuilder();
$update->update('talk_polls')
->set('display_name', $update->createNamedParameter(''))
->set('actor_type', $update->createNamedParameter('deleted_users'))
->set('actor_id', $update->createNamedParameter('deleted_users'))
->set('actor_type', $update->createNamedParameter(ICommentsManager::DELETED_USER))
->set('actor_id', $update->createNamedParameter(ICommentsManager::DELETED_USER))
->where($update->expr()->eq('actor_type', $update->createNamedParameter($actorType)))
->andWhere($update->expr()->eq('actor_id', $update->createNamedParameter($actorId)));
$update->executeStatement();
@ -348,8 +349,8 @@ class PollService {
$update = $this->connection->getQueryBuilder();
$update->update('talk_poll_votes')
->set('display_name', $update->createNamedParameter(''))
->set('actor_type', $update->createNamedParameter('deleted_users'))
->set('actor_id', $update->createNamedParameter('deleted_users'))
->set('actor_type', $update->createNamedParameter(ICommentsManager::DELETED_USER))
->set('actor_id', $update->createNamedParameter(ICommentsManager::DELETED_USER))
->where($update->expr()->eq('actor_type', $update->createNamedParameter($actorType)))
->andWhere($update->expr()->eq('actor_id', $update->createNamedParameter($actorId)));
$update->executeStatement();

View file

@ -140,6 +140,9 @@ export default {
} else if (this.messages[0].lastEditActorId === this.$store.getters.getActorId()
&& this.messages[0].lastEditActorType === this.$store.getters.getActorType()) {
return t('spreed', '(edited by you)')
} else if (this.lastEditActorId === 'deleted_users'
&& this.lastEditActorType === 'deleted_users') {
return t('spreed', '(edited by a deleted user)')
} else {
return t('spreed', '(edited by {moderator})', { moderator: this.messages[0].lastEditActorDisplayName })
}

View file

@ -92,7 +92,7 @@ Feature: command/user-remove
Then user "participant1" sees the following system messages in room "room" with 200
| room | actorType | actorId | systemMessage | message | messageParameters |
| room | guests | cli | read_only | An administrator locked the conversation | {"actor":{"type":"guest","id":"guest\/cli","name":"Guest"}} |
| room | users | participant1 | call_tried | You tried to call {user} | {"user":{"type":"highlight","id":"deleted_user","name":"participant2-displayname"}} |
| room | users | participant1 | call_tried | You tried to call {user} | {"user":{"type":"highlight","id":"deleted_users","name":"participant2-displayname"}} |
| room | users | participant1 | call_left | You left the call | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"}} |
| room | users | participant1 | call_started | You started a call | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"}} |
| room | users | participant1 | conversation_created | You created the conversation | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"}} |