Fix some edge cases in the streaming of messages
This commit is contained in:
@@ -31,12 +31,26 @@ public class GetMessages(
|
||||
.Where(c => c.SubjectId == req.SubjectId)
|
||||
.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)
|
||||
query = query.Where(c => c.Id < req.LastId.Value);
|
||||
if (lastMessage != null)
|
||||
{
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user