Group Chat Fixes

Issues Fixed

🔍 Issue 1: Cross-Group Message Leakage

Problem: Players were receiving messages from groups they weren’t members of.

Root Cause: The filtering logic in LobbyChatService.GetMessages() was showing ALL public messages (GroupId = null) to ALL users with group access, regardless of their actual group membership.

Solution: Updated filtering logic to only show:

  • Admin broadcasts (messages with [ADMIN] prefix and GroupId = null)
  • Messages from groups the user is actually a member of

🔍 Issue 2: Chat Not Clearing on Login

Problem: Lobby chat wasn’t being cleared when users logged in.

Root Cause: The RefreshChat() method was called before user data was loaded, so the chat clearing logic wasn’t applied with proper user context.

Solution: Added RefreshChat() call after user data loading to ensure proper filtering and clearing.

Technical Changes

1. LobbyChatService.cs - Group Filtering Fix

Before (Problematic):

messages = messages.Where(m => !m.GroupId.HasValue || groupIds.Contains(m.GroupId.Value)).ToList();

Issue: Shows ALL public messages to ALL users with group access.

After (Fixed):

messages = messages.Where(m => 
    (!m.GroupId.HasValue && m.SenderName.StartsWith("[ADMIN]")) || 
    (m.GroupId.HasValue && groupIds.Contains(m.GroupId.Value))
).ToList();

Fix: Only shows admin broadcasts publicly, filters group messages by actual membership.

2. LobbyChat.razor - Message Sending Fix

Before (Problematic):

var ok = LobbyChatSvc.AddMessageWithGroupCheck(senderId, senderName, text, userGroupIds, groupId: null);

Issue: Sends messages as public (GroupId = null), making them visible to all users.

After (Fixed):

var messageSent = false;
foreach (var groupId in userGroupIds)
{
    var success = LobbyChatSvc.AddMessageWithGroupCheck(senderId, senderName, text, userGroupIds, groupId);
    if (success) messageSent = true;
}

Fix: Creates separate messages for each group the user belongs to, ensuring proper filtering.

3. LobbyChat.razor - Chat Clearing Fix

Before (Problematic):

_dataLoaded = true;
await InvokeAsync(StateHasChanged);

Issue: Chat refresh wasn’t called after data loading.

After (Fixed):

_dataLoaded = true;

// Refresh chat after data is loaded to apply proper filtering
RefreshChat();
await InvokeAsync(StateHasChanged);

Fix: Ensures chat is refreshed with proper user context and filtering.

Expected Behavior After Fixes

âś… Group Message Isolation

  • Test (in Test3 group) sends message → Only Test3 members see it
  • Penny (not in Test3) does NOT see the message âś…
  • Admin broadcasts still visible to all users with group access âś…

âś… Chat Clearing on Login

  • User logs in → Chat display is cleared âś…
  • User data loads → Chat refreshes with proper filtering âś…
  • Loading state prevents premature rendering âś…

âś… Message Routing

  • User sends message → Goes to all their groups âś…
  • Multiple group members → See the message in their respective groups âś…
  • Non-members → Don’t see messages from groups they’re not in âś…

Testing Scenarios

Scenario 1: Cross-Group Message Prevention

  1. Test user (member of Test3 only) sends “Hello from Test3”
  2. Penny user (not in Test3) should NOT see this message
  3. Other Test3 members should see the message
  4. Admin users should only see admin broadcasts, not regular group messages

Scenario 2: Chat Clearing on Login

  1. User logs in with existing chat messages
  2. Chat display should be cleared immediately
  3. After data loads, only messages from their groups should appear
  4. Loading state should show briefly during data loading

Scenario 3: Multi-Group Message Distribution

  1. User in groups A and B sends “Hi everyone”
  2. Group A members see the message
  3. Group B members see the message
  4. Users not in A or B don’t see the message

Code Architecture Benefits

Improved Security

  • Group isolation prevents message leakage between groups
  • Admin-only broadcasts for global communications
  • Proper access control based on group membership

Better User Experience

  • Clean chat on login prevents confusion
  • Relevant messages only based on group membership
  • Loading states for better perceived performance

Scalable Design

  • Multiple group support for complex user hierarchies
  • Admin broadcast system for important announcements
  • Individual message tracking for personal chat management

Files Modified

  1. Services/LobbyChatService.cs
    • Fixed group filtering logic
    • Improved message visibility rules
  2. Components/LobbyChat.razor
    • Fixed message sending to use proper group routing
    • Fixed chat clearing on login
    • Added proper data loading sequence

Verification Checklist

âś… Group Isolation

  • Users only see messages from groups they belong to
  • Admin broadcasts visible to all users with group access
  • No cross-group message leakage

âś… Chat Clearing

  • Chat cleared on user login
  • Proper loading sequence maintained
  • No premature message display

âś… Message Routing

  • Messages sent to user’s actual groups
  • Multiple group support working
  • Non-members properly excluded

Status: âś… COMPLETE

All group chat filtering and login clearing issues have been resolved. The chat system now properly isolates messages by group membership and clears appropriately on user login.