feat: load more message history when scrolling to top

This commit is contained in:
Stone_Red
2026-04-20 17:42:05 +02:00
committed by HueByte
parent fe6dfd3d4d
commit e42f1a0965
10 changed files with 110 additions and 12 deletions
+2 -2
View File
@@ -106,11 +106,11 @@ public class ChatHub : Hub<IEchoHubClient>
}
}
public async Task<List<MessageDto>> GetChannelHistory(string channelName, int count = HubConstants.DefaultHistoryCount)
public async Task<List<MessageDto>> GetChannelHistory(string channelName, int count = HubConstants.DefaultHistoryCount, int offset = 0)
{
try
{
return await _chatService.GetChannelHistoryAsync(channelName, count);
return await _chatService.GetChannelHistoryAsync(channelName, count, offset);
}
catch (Exception ex)
{
+5 -3
View File
@@ -245,15 +245,16 @@ public class ChatService : IChatService
return null;
}
public async Task<List<MessageDto>> GetChannelHistoryAsync(string channelName, int count)
public async Task<List<MessageDto>> GetChannelHistoryAsync(string channelName, int count, int offset = 0)
{
channelName = channelName.ToLowerInvariant().Trim();
count = Math.Clamp(count, 1, ValidationConstants.MaxHistoryCount);
offset = Math.Max(offset, 0);
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<EchoHubDbContext>();
return await GetChannelHistoryInternalAsync(db, channelName, count);
return await GetChannelHistoryInternalAsync(db, channelName, count, offset);
}
public async Task<string?> UpdateStatusAsync(Guid userId, string username, UserStatus status, string? statusMessage)
@@ -366,7 +367,7 @@ public class ChatService : IChatService
return string.Join('\n', result);
}
private async Task<List<MessageDto>> GetChannelHistoryInternalAsync(EchoHubDbContext db, string channelName, int count)
private async Task<List<MessageDto>> GetChannelHistoryInternalAsync(EchoHubDbContext db, string channelName, int count, int offset = 0)
{
var channel = await db.Channels.FirstOrDefaultAsync(c => c.Name == channelName);
if (channel is null)
@@ -375,6 +376,7 @@ public class ChatService : IChatService
var raw = await db.Messages
.Where(m => m.ChannelId == channel.Id)
.OrderByDescending(m => m.SentAt)
.Skip(offset)
.Take(count)
.Join(db.Users,
m => m.SenderUserId,