❇️ Chat room summary api

This commit is contained in:
2025-05-23 00:04:31 +08:00
parent c3390d7248
commit 19174de873
2 changed files with 54 additions and 1 deletions

View File

@ -107,7 +107,7 @@ public class ChatService(AppDatabase db, FileService fs, IServiceScopeFactory sc
return messages.Count(m => !m.IsRead);
}
public async Task<Dictionary<Guid, int>> CountUnreadMessagesForJoinedRoomsAsync(Guid userId)
public async Task<Dictionary<Guid, int>> CountUnreadMessageForUser(Guid userId)
{
var cutoff = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromDays(30));
var userRooms = await db.ChatMembers
@ -133,6 +133,29 @@ public class ChatService(AppDatabase db, FileService fs, IServiceScopeFactory sc
);
}
public async Task<Dictionary<Guid, Message?>> ListLastMessageForUser(Guid userId)
{
var userRooms = await db.ChatMembers
.Where(m => m.AccountId == userId)
.Select(m => m.ChatRoomId)
.ToListAsync();
var messages = await db.ChatMessages
.IgnoreQueryFilters()
.Include(m => m.Sender)
.Include(m => m.Sender.Account)
.Include(m => m.Sender.Account.Profile)
.Where(m => userRooms.Contains(m.ChatRoomId))
.GroupBy(m => m.ChatRoomId)
.Select(g => g.OrderByDescending(m => m.CreatedAt).FirstOrDefault())
.ToDictionaryAsync(
m => m!.ChatRoomId,
m => m
);
return messages;
}
public async Task<RealtimeCall> CreateCallAsync(ChatRoom room, ChatMember sender)
{
var call = new RealtimeCall