❇️ Chat room summary api
This commit is contained in:
		| @@ -24,6 +24,36 @@ public partial class ChatController(AppDatabase db, ChatService cs) : Controller | ||||
|         public Guid ChatRoomId { get; set; } | ||||
|     } | ||||
|  | ||||
|     public class ChatSummaryResponse | ||||
|     { | ||||
|         public int UnreadCount { get; set; } | ||||
|         public Message? LastMessage { get; set; } | ||||
|     } | ||||
|  | ||||
|     [HttpGet("summary")] | ||||
|     [Authorize] | ||||
|     public async Task<ActionResult<Dictionary<Guid, ChatSummaryResponse>>> GetChatSummary() | ||||
|     { | ||||
|         if (HttpContext.Items["CurrentUser"] is not Account.Account currentUser) return Unauthorized(); | ||||
|  | ||||
|         var unreadMessages = await cs.CountUnreadMessageForUser(currentUser.Id); | ||||
|         var lastMessages = await cs.ListLastMessageForUser(currentUser.Id); | ||||
|          | ||||
|         var result = unreadMessages.Keys | ||||
|             .Union(lastMessages.Keys) | ||||
|             .ToDictionary( | ||||
|                 roomId => roomId, | ||||
|                 roomId => new ChatSummaryResponse | ||||
|                 { | ||||
|                     UnreadCount = unreadMessages.GetValueOrDefault(roomId), | ||||
|                     LastMessage = lastMessages.GetValueOrDefault(roomId) | ||||
|                 } | ||||
|             ); | ||||
|              | ||||
|         return Ok(result); | ||||
|          | ||||
|     } | ||||
|          | ||||
|     public class SendMessageRequest | ||||
|     { | ||||
|         [MaxLength(4096)] public string? Content { get; set; } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user