DM groups

This commit is contained in:
2025-05-17 22:36:46 +08:00
parent 3d197b667a
commit 27f934c634
2 changed files with 56 additions and 40 deletions

View File

@ -11,6 +11,46 @@ public class ChatRoomService(AppDatabase db, NotificationService nty)
$"You just got invited to join {member.ChatRoom.Name}");
}
public async Task<List<ChatRoom>> LoadDirectMessageMembers(List<ChatRoom> rooms, Guid userId)
{
var directRoomsId = rooms
.Where(r => r.Type == ChatRoomType.DirectMessage)
.Select(r => r.Id)
.ToList();
if (directRoomsId.Count == 0) return rooms;
var directMembers = directRoomsId.Count != 0
? await db.ChatMembers
.Where(m => directRoomsId.Contains(m.ChatRoomId))
.Where(m => m.AccountId != userId)
.Include(m => m.Account)
.Include(m => m.Account.Profile)
.GroupBy(m => m.ChatRoomId)
.ToDictionaryAsync(g => g.Key, g => g.ToList())
: new Dictionary<Guid, List<ChatMember>>();
return rooms.Select(r =>
{
if (r.Type == ChatRoomType.DirectMessage && directMembers.TryGetValue(r.Id, out var otherMembers))
r.DirectMembers = otherMembers.Select(ChatMemberTransmissionObject.FromEntity).ToList();
return r;
}).ToList();
}
public async Task<ChatRoom> LoadDirectMessageMembers(ChatRoom room, Guid userId)
{
if (room.Type != ChatRoomType.DirectMessage) return room;
var members = await db.ChatMembers
.Where(m => m.ChatRoomId == room.Id && m.AccountId != userId)
.Include(m => m.Account)
.Include(m => m.Account.Profile)
.ToListAsync();
if (members.Count > 0)
room.DirectMembers = members.Select(ChatMemberTransmissionObject.FromEntity).ToList();
return room;
}
public async Task<bool> IsMemberWithRole(Guid roomId, Guid accountId, ChatMemberRole requiredRole)
{
var member = await db.ChatMembers