From 0cb4a0c07b0efd2da3e08ba5371338bfd76026b9 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 20 Dec 2023 16:24:42 +0100 Subject: [PATCH] I'm too tired --- lib/Chat/ChatManager.php | 39 ++++++++++++++++++++++++++++++ lib/Chat/Notifier.php | 30 +++++++++++++++++++++++ lib/Service/ParticipantService.php | 6 +++-- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 96d4763821b6..ccc87d791f79 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -498,11 +498,50 @@ public function editMessage(Room $chat, IComment $comment, Participant $particip $metaData['last_edited_by_id'] = $participant->getAttendee()->getActorId(); $metaData['last_edited_time'] = $editTime->getTimestamp(); $comment->setMetaData($metaData); + + // FIXME Caption support + $mentionsBefore = $comment->getMentions(); + $usersDirectlyMentionedBefore = $this->notifier->getMentionedUserIds($comment); + $notifyMentionedUsersBefore = $this->notifier->notifyMentionedUsers($chat, $comment, [], silent: false); $comment->setMessage($message, self::MAX_CHAT_LENGTH); + $mentionsAfter = $comment->getMentions(); + $usersDirectlyMentionedAfter = $this->notifier->getMentionedUserIds($comment); + $notifyMentionedUsersAfter = $this->notifier->notifyMentionedUsers($chat, $comment, [], silent: false); + $this->commentsManager->save($comment); $this->referenceManager->invalidateCache($chat->getToken()); + \OC::$server->getLogger()->error('$mentionsBefore' . json_encode($mentionsBefore)); + \OC::$server->getLogger()->error('$mentionsAfter' . json_encode($mentionsAfter)); + \OC::$server->getLogger()->error('$notifyMentionedUsersBefore' . json_encode($notifyMentionedUsersBefore)); + \OC::$server->getLogger()->error('$notifyMentionedUsersAfter' . json_encode($notifyMentionedUsersAfter)); // TODO update mentions/notifications + // FIXME handle @all distinct + // FIXME handle reply-parent author + $removedMentions = empty($mentionsAfter) ? $mentionsBefore : array_diff($mentionsBefore, $mentionsAfter); + $addedMentions = empty($mentionsBefore) ? $mentionsAfter : array_diff($mentionsAfter, $mentionsBefore); + + \OC::$server->getLogger()->error('$removedMentions' . json_encode($removedMentions)); + \OC::$server->getLogger()->error('$addedMentions' . json_encode($addedMentions)); + if (!empty($removedMentions)) { + $removedUsersDirectMentioned = array_diff($usersDirectlyMentionedBefore, $usersDirectlyMentionedAfter); + // FIXME Not needed when it was silent, once it's stored in metadata + $this->notifier->removeMentionNotificationAfterEdit($chat, $comment, $removedUsersDirectMentioned); + } + + if (!empty($addedMentions)) { + $addedUsersDirectMentioned = array_diff($usersDirectlyMentionedAfter, $usersDirectlyMentionedBefore); + \OC::$server->getLogger()->error('$addedUsersDirectMentioned' . json_encode($addedUsersDirectMentioned)); + + // FIXME silent support, once it's stored in metadata + $alreadyNotifiedUsers = $this->notifier->notifyMentionedUsers($chat, $comment, [], silent: false); + \OC::$server->getLogger()->error('$alreadyNotifiedUsers' . json_encode($alreadyNotifiedUsers)); + if (!empty($alreadyNotifiedUsers)) { + $userIds = array_column($alreadyNotifiedUsers, 'id'); + \OC::$server->getLogger()->error('$userIds' . json_encode($userIds)); + $this->participantService->markUsersAsMentioned($chat, $userIds, (int) $comment->getId(), $addedUsersDirectMentioned); + } + } return $this->addSystemMessage( $chat, diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index 72974cdb12d0..e1c22d96e6f0 100644 --- a/lib/Chat/Notifier.php +++ b/lib/Chat/Notifier.php @@ -364,6 +364,36 @@ public function markMentionNotificationsRead(Room $chat, ?string $userId): void } } + /** + * Remove all mention notifications of users that got their mention removed + * + * @param list $userIds + */ + public function removeMentionNotificationAfterEdit(Room $chat, IComment $comment, array $userIds): void { + $shouldFlush = $this->notificationManager->defer(); + $notification = $this->notificationManager->createNotification(); + + $notification + ->setApp('spreed') + ->setObject('chat', $chat->getToken()) + // FIXME message_parameters are not handled by notification app, so this removes all notifications :( + ->setMessage('comment', [ + 'commentId' => $comment->getId(), + ]); + + foreach (['mention_all', 'mention_direct'] as $subject) { + $notification->setSubject($subject); + foreach ($userIds as $userId) { + $notification->setUser($userId); + $this->notificationManager->markProcessed($notification); + } + } + + if ($shouldFlush) { + $this->notificationManager->flush(); + } + } + /** * Returns the IDs of the users mentioned in the given comment. * diff --git a/lib/Service/ParticipantService.php b/lib/Service/ParticipantService.php index 645d6e800491..6d7d20bef6dc 100644 --- a/lib/Service/ParticipantService.php +++ b/lib/Service/ParticipantService.php @@ -1267,7 +1267,8 @@ public function markUsersAsMentioned(Room $room, array $userIds, int $messageId, ->set('last_mention_message', $update->createNamedParameter($messageId, IQueryBuilder::PARAM_INT)) ->where($update->expr()->eq('room_id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT))) ->andWhere($update->expr()->eq('actor_type', $update->createNamedParameter(Attendee::ACTOR_USERS))) - ->andWhere($update->expr()->in('actor_id', $update->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))); + ->andWhere($update->expr()->in('actor_id', $update->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))) + ->andWhere($update->expr()->lt('last_mention_message', $update->createNamedParameter($messageId, IQueryBuilder::PARAM_INT))); $update->executeStatement(); if (!empty($usersDirectlyMentioned)) { @@ -1276,7 +1277,8 @@ public function markUsersAsMentioned(Room $room, array $userIds, int $messageId, ->set('last_mention_direct', $update->createNamedParameter($messageId, IQueryBuilder::PARAM_INT)) ->where($update->expr()->eq('room_id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT))) ->andWhere($update->expr()->eq('actor_type', $update->createNamedParameter(Attendee::ACTOR_USERS))) - ->andWhere($update->expr()->in('actor_id', $update->createNamedParameter($usersDirectlyMentioned, IQueryBuilder::PARAM_STR_ARRAY))); + ->andWhere($update->expr()->in('actor_id', $update->createNamedParameter($usersDirectlyMentioned, IQueryBuilder::PARAM_STR_ARRAY))) + ->andWhere($update->expr()->lt('last_mention_direct', $update->createNamedParameter($messageId, IQueryBuilder::PARAM_INT))); $update->executeStatement(); } }