♻️ Refactor the last read at system of chat

This commit is contained in:
2025-05-24 01:29:17 +08:00
parent 1b2ca34aad
commit 213d81a5ca
8 changed files with 44 additions and 99 deletions

View File

@ -10,22 +10,17 @@ public class MessageReadReceiptFlushHandler(IServiceProvider serviceProvider) :
{
public async Task FlushAsync(IReadOnlyList<MessageReadReceipt> items)
{
var distinctItems = items
.DistinctBy(x => new { x.MessageId, x.SenderId })
.Select(x =>
{
x.CreatedAt = SystemClock.Instance.GetCurrentInstant();
x.UpdatedAt = x.CreatedAt;
return x;
})
var now = SystemClock.Instance.GetCurrentInstant();
var distinctId = items
.DistinctBy(x => x.SenderId)
.Select(x => x.SenderId)
.ToList();
using var scope = serviceProvider.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
await db.BulkInsertAsync(distinctItems, config => {
config.ConflictOption = ConflictOption.Ignore;
config.UpdateByProperties = [nameof(MessageReadReceipt.MessageId), nameof(MessageReadReceipt.SenderId)];
});
await db.ChatMembers.Where(r => distinctId.Contains(r.Id))
.ExecuteUpdateAsync(s => s.SetProperty(m => m.LastReadAt, now)
);
}
}
@ -36,14 +31,3 @@ public class ReadReceiptFlushJob(FlushBufferService fbs, MessageReadReceiptFlush
await fbs.FlushAsync(hdl);
}
}
public class ReadReceiptRecyclingJob(AppDatabase db) : IJob
{
public async Task Execute(IJobExecutionContext context)
{
var cutoff = SystemClock.Instance.GetCurrentInstant().Minus(Duration.FromDays(30));
await db.ChatReadReceipts
.Where(r => r.CreatedAt < cutoff)
.ExecuteDeleteAsync();
}
}