:zsap: Pagination in chat sync

This commit is contained in:
2025-11-23 12:07:58 +08:00
parent 2024972832
commit 726a752fbb
3 changed files with 26 additions and 17 deletions

View File

@@ -553,12 +553,13 @@ public partial class ChatController(
if (!isMember)
return StatusCode(403, "You are not a member of this chat room.");
var response = await cs.GetSyncDataAsync(roomId, request.LastSyncTimestamp);
var response = await cs.GetSyncDataAsync(roomId, request.LastSyncTimestamp, 500);
Response.Headers["X-Total"] = response.TotalCount.ToString();
return Ok(response);
}
[HttpPost("{roomId:guid}/autocomplete")]
public async Task<ActionResult<List<DysonNetwork.Shared.Models.Autocompletion>>> ChatAutoComplete(
public async Task<ActionResult<List<Shared.Models.Autocompletion>>> ChatAutoComplete(
[FromBody] AutocompletionRequest request, Guid roomId)
{
if (HttpContext.Items["CurrentUser"] is not Account currentUser)

View File

@@ -624,16 +624,22 @@ public partial class ChatService(
.FirstOrDefaultAsync();
}
public async Task<SyncResponse> GetSyncDataAsync(Guid roomId, long lastSyncTimestamp)
public async Task<SyncResponse> GetSyncDataAsync(Guid roomId, long lastSyncTimestamp, int limit = 500)
{
var timestamp = Instant.FromUnixTimeMilliseconds(lastSyncTimestamp);
var lastSyncInstant = Instant.FromUnixTimeMilliseconds(lastSyncTimestamp);
// Get all messages that have been modified since the last sync
// Count total newer messages
var totalCount = await db.ChatMessages
.Where(m => m.ChatRoomId == roomId && m.CreatedAt > lastSyncInstant)
.CountAsync();
// Get up to limit messages that have been created since the last sync
var syncMessages = await db.ChatMessages
.IgnoreQueryFilters()
.Include(m => m.Sender)
.Where(m => m.ChatRoomId == roomId)
.Where(m => m.UpdatedAt > timestamp || m.DeletedAt > timestamp)
.Where(m => m.CreatedAt > lastSyncInstant)
.OrderBy(m => m.CreatedAt)
.Take(limit)
.Include(m => m.Sender)
.ToListAsync();
// Load member accounts for messages that need them
@@ -658,13 +664,14 @@ public partial class ChatService(
}
var latestTimestamp = syncMessages.Count > 0
? syncMessages.Max(m => m.UpdatedAt)
? syncMessages.Last().CreatedAt
: SystemClock.Instance.GetCurrentInstant();
return new SyncResponse
{
Messages = syncMessages.OrderBy(m => m.UpdatedAt).ToList(),
CurrentTimestamp = latestTimestamp
Messages = syncMessages,
CurrentTimestamp = latestTimestamp,
TotalCount = totalCount
};
}
@@ -816,4 +823,5 @@ public class SyncResponse
{
public List<SnChatMessage> Messages { get; set; } = [];
public Instant CurrentTimestamp { get; set; }
public int TotalCount { get; set; } = 0;
}

View File

@@ -217,7 +217,7 @@ public class BroadcastEventHandler(
return;
}
var requestData = packet.GetData<ChatController.MarkMessageReadRequest>();
var requestData = packet.GetData<Chat.ChatController.MarkMessageReadRequest>();
if (requestData == null)
{
await SendErrorResponse(evt, "Invalid request data");
@@ -245,7 +245,7 @@ public class BroadcastEventHandler(
return;
}
var requestData = packet.GetData<ChatController.ChatRoomWsUniversalRequest>();
var requestData = packet.GetData<Chat.ChatController.ChatRoomWsUniversalRequest>();
if (requestData == null)
{
await SendErrorResponse(evt, "Invalid request data");
@@ -299,7 +299,7 @@ public class BroadcastEventHandler(
return;
}
var requestData = packet.GetData<ChatController.ChatRoomWsUniversalRequest>();
var requestData = packet.GetData<Chat.ChatController.ChatRoomWsUniversalRequest>();
if (requestData == null)
{
await SendErrorResponse(evt, "Invalid request data");
@@ -327,7 +327,7 @@ public class BroadcastEventHandler(
return;
}
var requestData = packet.GetData<ChatController.ChatRoomWsUniversalRequest>();
var requestData = packet.GetData<Chat.ChatController.ChatRoomWsUniversalRequest>();
if (requestData == null)
{
await SendErrorResponse(evt, "Invalid request data");