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 andGroupId = 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
- Test user (member of Test3 only) sends “Hello from Test3”
- Penny user (not in Test3) should NOT see this message
- Other Test3 members should see the message
- Admin users should only see admin broadcasts, not regular group messages
Scenario 2: Chat Clearing on Login
- User logs in with existing chat messages
- Chat display should be cleared immediately
- After data loads, only messages from their groups should appear
- Loading state should show briefly during data loading
Scenario 3: Multi-Group Message Distribution
- User in groups A and B sends “Hi everyone”
- Group A members see the message
- Group B members see the message
- 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
Services/LobbyChatService.cs- Fixed group filtering logic
- Improved message visibility rules
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.