Skip to content

Conversation

Copy link

Copilot AI commented Dec 1, 2025

Messages were automatically marked as read when fetching chat history via getChatHistoryForAnonymous, preventing users from previewing chats without clearing unread counts. The frontend should control when messages are marked read via the MARK_AS_READ WebSocket message.

Changes

  • ChatService.getChatHistoryForAnonymous: Removed markMessagesAsRead() call, changed to @Transactional(readOnly = true)
  • MessagingService.setMessageAsRead: Now requires user email for proper authorization
    public void setMessageAsRead(String email, String sessionId) {
        User user = userRepository.findByEmail(email)
                .orElseThrow(() -> new RuntimeException("User not found: " + email));
        chatMessageRepository.markMessagesAsRead(user.getId(), sessionId);
    }
  • ChatWebSocketHandler: MARK_AS_READ handler extracts user from JWT and validates both email and chatId before processing
  • ChatMessageRepository: Removed unused markMessagesAsRead(String sessionId) overload that lacked user scoping

Tests

Added ChatServiceTest and MessagingServiceTest to verify:

  • getChatHistoryForAnonymous no longer calls markMessagesAsRead
  • setMessageAsRead correctly scopes updates to user + session
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor: "Mark as Read" Logic is Automatic and Inflexible</issue_title>
<issue_description>### Description
Currently, messages are marked as read in two places:

  1. Automatically when an anonymous user (or a user) calls getChatHistoryForAnonymous. This is bad UX, as the user may just be previewing the chat, not reading it.
  2. Via a MARK_AS_READ WebSocket message type.

The first method should be removed in favor of the second, which gives the frontend control over when a chat is marked as read.

Acceptance Criteria

  1. Remove the chatMessageRepository.markMessagesAsRead(recipient.getId(), sessionId); line from ChatService.getChatHistoryForAnonymous.
  2. Ensure the MARK_AS_READ case in ChatWebSocketHandler.handleTextMessage correctly uses the payload.getChatId() (which seems to be the sessionId) to mark messages as read.
  3. The ChatService.getUserChatSessions method, which calculates unreadCount, will now correctly reflect the true unread count until the user's client sends the MARK_AS_READ message.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…mous and improve MARK_AS_READ WebSocket handler

Co-authored-by: fred-maina <150168105+fred-maina@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor 'Mark as Read' logic for improved user control Refactor: Remove automatic "Mark as Read" from chat history fetch Dec 1, 2025
Copilot AI requested a review from fred-maina December 1, 2025 12:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor: "Mark as Read" Logic is Automatic and Inflexible

2 participants