Now the JoinResponse include ChatMember details

This commit is contained in:
LittleSheep 2025-05-25 20:48:47 +08:00
parent cfe29f5def
commit e20666160f
2 changed files with 52 additions and 21 deletions

View File

@ -323,7 +323,7 @@ public class LivekitRealtimeService : IRealtimeService
} }
// Get current participants // Get current participants
var participants = await GetRoomParticipantsAsync(roomName); var livekitParticipants = await GetRoomParticipantsAsync(roomName);
// Get all room members who should receive this update // Get all room members who should receive this update
var roomMembers = await _db.ChatMembers var roomMembers = await _db.ChatMembers
@ -331,16 +331,33 @@ public class LivekitRealtimeService : IRealtimeService
.Select(m => m.AccountId) .Select(m => m.AccountId)
.ToListAsync(); .ToListAsync();
// Create the update packet // Get member profiles for participants who have account IDs
var participantsDto = participants.Select(p => new var accountIds = livekitParticipants
.Where(p => p.AccountId.HasValue)
.Select(p => p.AccountId!.Value)
.ToList();
var memberProfiles = new Dictionary<Guid, ChatMember>();
if (accountIds.Any())
{ {
p.Identity, memberProfiles = await _db.ChatMembers
p.Name, .Where(m => m.ChatRoomId == roomInfo.RoomId && accountIds.Contains(m.AccountId))
p.AccountId, .ToDictionaryAsync(m => m.AccountId, m => m);
State = p.State.ToString(), }
p.JoinedAt
// Convert to CallParticipant objects
var participants = livekitParticipants.Select(p => new CallParticipant
{
Identity = p.Identity,
Name = p.Name,
AccountId = p.AccountId,
JoinedAt = p.JoinedAt,
Profile = p.AccountId.HasValue && memberProfiles.TryGetValue(p.AccountId.Value, out var profile)
? profile
: null
}).ToList(); }).ToList();
// Create the update packet with CallParticipant objects
var updatePacket = new WebSocketPacket var updatePacket = new WebSocketPacket
{ {
Type = WebSocketPacketType.CallParticipantsUpdate, Type = WebSocketPacketType.CallParticipantsUpdate,
@ -348,7 +365,7 @@ public class LivekitRealtimeService : IRealtimeService
{ {
{ "room_id", roomInfo.RoomId }, { "room_id", roomInfo.RoomId },
{ "call_id", roomInfo.Id }, { "call_id", roomInfo.Id },
{ "participants", participantsDto } { "participants", participants }
} }
}; };

View File

@ -97,18 +97,32 @@ public class RealtimeCallController(
var endpoint = _config.Endpoint ?? var endpoint = _config.Endpoint ??
throw new InvalidOperationException("LiveKit endpoint configuration is missing"); throw new InvalidOperationException("LiveKit endpoint configuration is missing");
// Inject the ChatRoomService
var chatRoomService = HttpContext.RequestServices.GetRequiredService<ChatRoomService>();
// Get current participants from the LiveKit service // Get current participants from the LiveKit service
var participants = new List<CallParticipant>(); var participants = new List<CallParticipant>();
if (realtime is LivekitRealtimeService livekitService) if (realtime is LivekitRealtimeService livekitService)
{ {
var roomParticipants = await livekitService.GetRoomParticipantsAsync(ongoingCall.SessionId); var roomParticipants = await livekitService.GetRoomParticipantsAsync(ongoingCall.SessionId);
participants = roomParticipants.Select(p => new CallParticipant participants = new List<CallParticipant>();
foreach (var p in roomParticipants)
{
var participant = new CallParticipant
{ {
Identity = p.Identity, Identity = p.Identity,
Name = p.Name, Name = p.Name,
AccountId = p.AccountId, AccountId = p.AccountId,
JoinedAt = p.JoinedAt JoinedAt = p.JoinedAt
}).ToList(); };
// Fetch the ChatMember profile if we have an account ID
if (p.AccountId.HasValue)
participant.Profile = await chatRoomService.GetChannelMember(p.AccountId.Value, roomId);
participants.Add(participant);
}
} }
// Create the response model // Create the response model