Skip to content

Commit

Permalink
I'm too tired
Browse files Browse the repository at this point in the history
  • Loading branch information
nickvergessen committed Dec 20, 2023
1 parent ae8ad0b commit 9631669
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
39 changes: 39 additions & 0 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> $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.
*
Expand Down
6 changes: 4 additions & 2 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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();
}
}
Expand Down

0 comments on commit 9631669

Please sign in to comment.