Fix some edge cases in the streaming of messages

This commit is contained in:
Jonathan Bourdon
2024-07-20 04:35:34 -04:00
parent 06fccdee80
commit f256adadca

View File

@@ -31,12 +31,26 @@ public class GetMessages(
.Where(c => c.SubjectId == req.SubjectId) .Where(c => c.SubjectId == req.SubjectId)
.Where(c => c.ParentId == null); .Where(c => c.ParentId == null);
query = query.OrderByDescending(c => c.CreatedAt); if (req.LastId.HasValue)
{
var lastMessage = await context
.Messages
.Where(c => c.Id == req.LastId.Value)
.Select(c => new { c.CreatedAt, c.Id })
.FirstOrDefaultAsync(cancellationToken: ct);
if (req.LastId is not null) if (lastMessage != null)
query = query.Where(c => c.Id < req.LastId.Value); {
query = query
.Where(c => c.CreatedAt < lastMessage.CreatedAt
|| (c.CreatedAt == lastMessage.CreatedAt && c.Id < lastMessage.Id));
}
}
query = query.Take(req.PageSize); query = query
.OrderByDescending(c => c.CreatedAt)
.ThenByDescending(c => c.Id)
.Take(req.PageSize);
var messages = await query.ToListAsync(cancellationToken: ct); var messages = await query.ToListAsync(cancellationToken: ct);