❇️ 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

@ -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; }