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

feat(federation): Implement notifications for mentions, reply and full #11691

Merged
merged 5 commits into from
Mar 6, 2024
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
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m

]]></description>

<version>19.0.0-dev.3</version>
<version>19.0.0-dev.4</version>
<licence>agpl</licence>

<author>Daniel CalviΓ±o SΓ‘nchez</author>
Expand Down
2 changes: 2 additions & 0 deletions docs/events.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Used to parse mentions, replace parameters in messages with rich objects, transf
* Before event: `OCA\Talk\Events\BeforeChatMessageSentEvent`
* After event: `OCA\Talk\Events\ChatMessageSentEvent`
* Since: 18.0.0
* Since: 19.0.0 - Method `getParent()` was added

### Duplicate share sent

Expand All @@ -153,6 +154,7 @@ listen to the `OCA\Talk\Events\SystemMessagesMultipleSentEvent` event instead.
* After event: `OCA\Talk\Events\SystemMessageSentEvent`
* Final event: `OCA\Talk\Events\SystemMessagesMultipleSentEvent` - Only sent once as per above explanation
* Since: 18.0.0
* Since: 19.0.0 - Method `getParent()` was added

### Deprecated events

Expand Down
29 changes: 15 additions & 14 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Talk\Exceptions\MessagingNotAllowedException;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\Message;
use OCA\Talk\Model\Poll;
use OCA\Talk\Participant;
use OCA\Talk\Room;
Expand Down Expand Up @@ -165,15 +166,15 @@ public function addSystemMessage(

if ($silent) {
$comment->setMetaData([
'silent' => true,
Message::METADATA_SILENT => true,
]);
}

$this->setMessageExpiration($chat, $comment);

$shouldFlush = $this->notificationManager->defer();

$event = new BeforeSystemMessageSentEvent($chat, $comment, silent: $silent, skipLastActivityUpdate: $shouldSkipLastMessageUpdate);
$event = new BeforeSystemMessageSentEvent($chat, $comment, silent: $silent, skipLastActivityUpdate: $shouldSkipLastMessageUpdate, parent: $replyTo);
$this->dispatcher->dispatchTyped($event);
try {
$this->commentsManager->save($comment);
Expand Down Expand Up @@ -228,7 +229,7 @@ public function addSystemMessage(
}
}

$event = new SystemMessageSentEvent($chat, $comment, silent: $silent, skipLastActivityUpdate: $shouldSkipLastMessageUpdate);
$event = new SystemMessageSentEvent($chat, $comment, silent: $silent, skipLastActivityUpdate: $shouldSkipLastMessageUpdate, parent: $replyTo);
$this->dispatcher->dispatchTyped($event);
} catch (NotFoundException $e) {
}
Expand Down Expand Up @@ -322,11 +323,11 @@ public function sendMessage(Room $chat, ?Participant $participant, string $actor

if ($silent) {
$comment->setMetaData([
'silent' => true,
Message::METADATA_SILENT => true,
]);
}

$event = new BeforeChatMessageSentEvent($chat, $comment, $participant, $silent);
$event = new BeforeChatMessageSentEvent($chat, $comment, $participant, $silent, $replyTo);
$this->dispatcher->dispatchTyped($event);

$shouldFlush = $this->notificationManager->defer();
Expand Down Expand Up @@ -371,7 +372,7 @@ public function sendMessage(Room $chat, ?Participant $participant, string $actor
// User was not mentioned, send a normal notification
$this->notifier->notifyOtherParticipant($chat, $comment, $alreadyNotifiedUsers, $silent);

$event = new ChatMessageSentEvent($chat, $comment, $participant, $silent);
$event = new ChatMessageSentEvent($chat, $comment, $participant, $silent, $replyTo);
$this->dispatcher->dispatchTyped($event);
} catch (NotFoundException $e) {
}
Expand Down Expand Up @@ -500,11 +501,11 @@ public function deleteMessage(Room $chat, IComment $comment, Participant $partic
$comment->setVerb(self::VERB_MESSAGE_DELETED);

$metaData = $comment->getMetaData() ?? [];
if (isset($metaData['last_edited_by_type'])) {
if (isset($metaData[Message::METADATA_LAST_EDITED_BY_TYPE])) {
unset(
$metaData['last_edited_by_type'],
$metaData['last_edited_by_id'],
$metaData['last_edited_time']
$metaData[Message::METADATA_LAST_EDITED_BY_TYPE],
$metaData[Message::METADATA_LAST_EDITED_BY_ID],
$metaData[Message::METADATA_LAST_EDITED_TIME],
);
$comment->setMetaData($metaData);
}
Expand Down Expand Up @@ -557,12 +558,12 @@ public function editMessage(Room $chat, IComment $comment, Participant $particip
}

$metaData = $comment->getMetaData() ?? [];
$metaData['last_edited_by_type'] = $participant->getAttendee()->getActorType();
$metaData['last_edited_by_id'] = $participant->getAttendee()->getActorId();
$metaData['last_edited_time'] = $editTime->getTimestamp();
$metaData[Message::METADATA_LAST_EDITED_BY_TYPE] = $participant->getAttendee()->getActorType();
$metaData[Message::METADATA_LAST_EDITED_BY_ID] = $participant->getAttendee()->getActorId();
$metaData[Message::METADATA_LAST_EDITED_TIME] = $editTime->getTimestamp();
$comment->setMetaData($metaData);

$wasSilent = $metaData['silent'] ?? false;
$wasSilent = $metaData[Message::METADATA_SILENT] ?? false;

if (!$wasSilent) {
$mentionsBefore = $comment->getMentions();
Expand Down
26 changes: 23 additions & 3 deletions lib/Chat/MessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Talk\MatterbridgeManager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\Message;
use OCA\Talk\Model\ProxyCacheMessage;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\BotService;
Expand All @@ -51,17 +52,36 @@ class MessageParser {
protected array $botNames = [];

public function __construct(
protected IEventDispatcher $dispatcher,
protected IUserManager $userManager,
protected IEventDispatcher $dispatcher,
protected IUserManager $userManager,
protected ParticipantService $participantService,
protected BotService $botService,
protected BotService $botService,
) {
}

public function createMessage(Room $room, ?Participant $participant, IComment $comment, IL10N $l): Message {
return new Message($room, $participant, $comment, $l);
}

public function createMessageFromProxyCache(Room $room, ?Participant $participant, ProxyCacheMessage $proxy, IL10N $l): Message {
$message = new Message($room, $participant, null, $l, $proxy);

$message->setActor(
$proxy->getActorType(),
$proxy->getActorId(),
$proxy->getActorDisplayName(),
);

$message->setMessageType($proxy->getMessageType());

$message->setMessage(
$proxy->getMessage(),
$proxy->getParsedMessageParameters()
);

return $message;
}

public function parseMessage(Message $message): void {
$message->setMessage($message->getComment()->getMessage(), []);

Expand Down
2 changes: 1 addition & 1 deletion lib/Chat/Parser/SystemMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected function parseMessage(Message $chatMessage): void {
}
} elseif ($message === 'call_started') {
$metaData = $comment->getMetaData() ?? [];
$silentCall = $metaData['silent'] ?? false;
$silentCall = $metaData[Message::METADATA_SILENT] ?? false;
if ($silentCall) {
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You started a silent call');
Expand Down
2 changes: 1 addition & 1 deletion lib/Chat/Parser/UserMention.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ protected function parseMessage(Message $chatMessage): void {
$messageParameters[$mentionParameterId] = [
'type' => $mention['type'],
'id' => $chatMessage->getRoom()->getToken(),
'name' => $chatMessage->getRoom()->getDisplayName($userId),
'name' => $chatMessage->getRoom()->getDisplayName($userId, true),
'call-type' => $this->getRoomType($chatMessage->getRoom()),
'icon-url' => $this->avatarService->getAvatarUrl($chatMessage->getRoom()),
];
Expand Down
5 changes: 3 additions & 2 deletions lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\BreakoutRoom;
use OCA\Talk\Model\Message;
use OCA\Talk\Model\Session;
use OCA\Talk\Participant;
use OCA\Talk\Room;
Expand Down Expand Up @@ -425,8 +426,8 @@ protected function fixMimeTypeOfVoiceMessage(ShareCreatedEvent|BeforeDuplicateSh
}
}

if (isset($metaData['silent'])) {
$silent = (bool) $metaData['silent'];
if (isset($metaData[Message::METADATA_SILENT])) {
$silent = (bool) $metaData[Message::METADATA_SILENT];
} else {
$silent = false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ protected function prepareCommentsAsDataResponse(array $comments, int $lastCommo
$message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l);
$this->messageParser->parseMessage($message);

$expireDate = $message->getComment()->getExpireDate();
$expireDate = $message->getExpirationDateTime();
if ($expireDate instanceof \DateTime && $expireDate < $now) {
$commentIdToIndex[$id] = null;
continue;
Expand Down
5 changes: 5 additions & 0 deletions lib/Events/AMessageSentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function __construct(
protected IComment $comment,
protected ?Participant $participant = null,
protected bool $silent = false,
protected ?IComment $parent = null,
) {
parent::__construct(
$room,
Expand All @@ -50,4 +51,8 @@ public function getParticipant(): ?Participant {
public function isSilentMessage(): bool {
return $this->silent;
}

public function getParent(): ?IComment {
return $this->parent;
}
}
4 changes: 3 additions & 1 deletion lib/Events/ASystemMessageSentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public function __construct(
IComment $comment,
?Participant $participant = null,
bool $silent = false,
?IComment $parent = null,
protected bool $skipLastActivityUpdate = false,
) {
parent::__construct(
$room,
$comment,
$participant,
$silent
$silent,
$parent,
);
}

Expand Down
1 change: 1 addition & 0 deletions lib/Federation/BackendNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ public function sendRoomModifiedUpdate(
* Send information to remote participants that a message was posted
* Sent from Host server to Remote participant server
*
* @param array{remoteMessageId: int, actorType: string, actorId: string, actorDisplayName: string, messageType: string, systemMessage: string, expirationDatetime: string, message: string, messageParameter: string, creationDatetime: string, metaData: string} $messageData
* @param array{unreadMessages: int, unreadMention: bool, unreadMentionDirect: bool} $unreadInfo
*/
public function sendMessageUpdate(
Expand Down
39 changes: 32 additions & 7 deletions lib/Federation/CloudFederationProviderTalk.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
use OCA\Talk\Events\AttendeesAddedEvent;
use OCA\Talk\Exceptions\ParticipantNotFoundException;
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Federation\Proxy\TalkV1\UserConverter;
use OCA\Talk\Manager;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\AttendeeMapper;
use OCA\Talk\Model\Invitation;
use OCA\Talk\Model\InvitationMapper;
use OCA\Talk\Model\ProxyCacheMessages;
use OCA\Talk\Model\ProxyCacheMessagesMapper;
use OCA\Talk\Model\ProxyCacheMessage;
use OCA\Talk\Model\ProxyCacheMessageMapper;
use OCA\Talk\Notification\FederationChatNotifier;
use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCA\Talk\Service\ParticipantService;
Expand Down Expand Up @@ -87,7 +89,9 @@ public function __construct(
private ISession $session,
private IEventDispatcher $dispatcher,
private LoggerInterface $logger,
private ProxyCacheMessagesMapper $proxyCacheMessagesMapper,
private ProxyCacheMessageMapper $proxyCacheMessageMapper,
private FederationChatNotifier $federationChatNotifier,
private UserConverter $userConverter,
ICacheFactory $cacheFactory,
) {
$this->proxyCacheMessages = $cacheFactory->isAvailable() ? $cacheFactory->createDistributed('talk/pcm/') : null;
Expand Down Expand Up @@ -316,7 +320,7 @@ private function roomModified(int $remoteAttendeeId, array $notification): array

/**
* @param int $remoteAttendeeId
* @param array{remoteServerUrl: string, sharedSecret: string, remoteToken: string, messageData: array{remoteMessageId: int, actorType: string, actorId: string, actorDisplayName: string, messageType: string, systemMessage: string, expirationDatetime: string, message: string, messageParameter: string}, unreadInfo: array{unreadMessages: int, unreadMention: bool, unreadMentionDirect: bool}} $notification
* @param array{remoteServerUrl: string, sharedSecret: string, remoteToken: string, messageData: array{remoteMessageId: int, actorType: string, actorId: string, actorDisplayName: string, messageType: string, systemMessage: string, expirationDatetime: string, message: string, messageParameter: string, creationDatetime: string, metaData: string}, unreadInfo: array{unreadMessages: int, unreadMention: bool, unreadMentionDirect: bool}} $notification
* @return array
* @throws ActionNotSupportedException
* @throws AuthenticationFailedException
Expand All @@ -335,7 +339,7 @@ private function messagePosted(int $remoteAttendeeId, array $notification): arra
throw new ShareNotFound();
}

$message = new ProxyCacheMessages();
$message = new ProxyCacheMessage();
$message->setLocalToken($room->getToken());
$message->setRemoteServerUrl($notification['remoteServerUrl']);
$message->setRemoteToken($notification['remoteToken']);
Expand All @@ -346,12 +350,25 @@ private function messagePosted(int $remoteAttendeeId, array $notification): arra
$message->setMessageType($notification['messageData']['messageType']);
$message->setSystemMessage($notification['messageData']['systemMessage']);
if ($notification['messageData']['expirationDatetime']) {
$message->setExpirationDatetime(new \DateTimeImmutable($notification['messageData']['expirationDatetime']));
$message->setExpirationDatetime(new \DateTime($notification['messageData']['expirationDatetime']));
}

// We transform the parameters when storing in the PCM, so we only have
// to do it once for each message.
$convertedParameters = $this->userConverter->convertMessageParameters($room, [
'message' => $notification['messageData']['message'],
'messageParameters' => json_decode($notification['messageData']['messageParameter'], true, flags: JSON_THROW_ON_ERROR),
]);
$notification['messageData']['message'] = $convertedParameters['message'];
$notification['messageData']['messageParameter'] = json_encode($convertedParameters['messageParameters'], JSON_THROW_ON_ERROR);

$message->setMessage($notification['messageData']['message']);
$message->setMessageParameters($notification['messageData']['messageParameter']);
$message->setCreationDatetime(new \DateTime($notification['messageData']['creationDatetime']));
$message->setMetaData($notification['messageData']['metaData']);

try {
$this->proxyCacheMessagesMapper->insert($message);
$this->proxyCacheMessageMapper->insert($message);

$lastMessageId = $room->getLastMessageId();
if ($notification['messageData']['remoteMessageId'] > $lastMessageId) {
Expand All @@ -374,6 +391,12 @@ private function messagePosted(int $remoteAttendeeId, array $notification): arra
$this->logger->error('Error saving proxy cache message failed: ' . $e->getMessage(), ['exception' => $e]);
throw $e;
}

$message = $this->proxyCacheMessageMapper->findByRemote(
$notification['remoteServerUrl'],
$notification['remoteToken'],
$notification['messageData']['remoteMessageId'],
);
}

try {
Expand All @@ -390,6 +413,8 @@ private function messagePosted(int $remoteAttendeeId, array $notification): arra
$notification['unreadInfo']['unreadMentionDirect'],
);

$this->federationChatNotifier->handleChatMessage($room, $participant, $message, $notification);

return [];
}

Expand Down
14 changes: 13 additions & 1 deletion lib/Federation/Proxy/TalkV1/Notifier/MessageSentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
use OCA\Talk\Events\SystemMessagesMultipleSentEvent;
use OCA\Talk\Federation\BackendNotifier;
use OCA\Talk\Model\Attendee;
use OCA\Talk\Model\ProxyCacheMessage;
use OCA\Talk\Service\ParticipantService;
use OCP\Comments\IComment;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Federation\ICloudIdManager;
Expand Down Expand Up @@ -78,6 +80,14 @@ public function handle(Event $event): void {
}

$expireDate = $event->getComment()->getExpireDate();
$creationDate = $event->getComment()->getCreationDateTime();

$metaData = $event->getComment()->getMetaData() ?? [];
$parent = $event->getParent();
if ($parent instanceof IComment) {
$metaData[ProxyCacheMessage::METADATA_REPLYTO_TYPE] = $parent->getActorType();
$metaData[ProxyCacheMessage::METADATA_REPLYTO_ID] = $parent->getActorId();
}

$messageData = [
'remoteMessageId' => (int) $event->getComment()->getId(),
Expand All @@ -88,7 +98,9 @@ public function handle(Event $event): void {
'systemMessage' => $chatMessage->getMessageType() === ChatManager::VERB_SYSTEM ? $chatMessage->getMessageRaw() : '',
'expirationDatetime' => $expireDate ? $expireDate->format(\DateTime::ATOM) : '',
'message' => $chatMessage->getMessage(),
'messageParameter' => json_encode($chatMessage->getMessageParameters()),
'messageParameter' => json_encode($chatMessage->getMessageParameters(), JSON_THROW_ON_ERROR),
'creationDatetime' => $creationDate->format(\DateTime::ATOM),
'metaData' => json_encode($metaData, JSON_THROW_ON_ERROR),
];

$participants = $this->participantService->getParticipantsByActorType($event->getRoom(), Attendee::ACTOR_FEDERATED_USERS);
Expand Down
Loading
Loading