Settings for the server are now combined with the settings from the SDK. This introduces a large breaking change and a migration should be created. This refactor utilizes .NET Configuration and the Options pattern. This will allow for the overriding of any setting using envionment variables. It also means that Settings.yml can be used to override any .NET configuration. A SettingsProvider implementation was created, and any updating of settings was changed from SettingsService.SaveSettings() to SettingsProvider.Update(s => { ... })
70 lines
2.4 KiB
C#
70 lines
2.4 KiB
C#
using LANCommander.Server.Data;
|
|
using LANCommander.Server.Data.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace LANCommander.Server.Services
|
|
{
|
|
public sealed class ChatThreadReadStatusService(
|
|
IDbContextFactory<DatabaseContext> contextFactory)
|
|
{
|
|
public async Task UpdateReadStatus(Guid threadId, Guid userId)
|
|
{
|
|
using (var context = await contextFactory.CreateDbContextAsync())
|
|
{
|
|
if (context.ChatThreadReadStatuses != null)
|
|
{
|
|
var status = await context.ChatThreadReadStatuses.FirstOrDefaultAsync(rs => rs.ThreadId == threadId && rs.UserId == userId);
|
|
|
|
if (status == null)
|
|
{
|
|
var result = await context.ChatThreadReadStatuses.AddAsync(new ChatThreadReadStatus
|
|
{
|
|
ThreadId = threadId,
|
|
UserId = userId,
|
|
LastReadOn = DateTime.Now,
|
|
});
|
|
}
|
|
else
|
|
{
|
|
status.LastReadOn = DateTime.Now;
|
|
|
|
context.ChatThreadReadStatuses.Update(status);
|
|
}
|
|
}
|
|
|
|
await context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task<DateTime?> GetLastReadAsync(Guid threadId, Guid userId)
|
|
{
|
|
using (var context = await contextFactory.CreateDbContextAsync())
|
|
{
|
|
if (context.ChatThreadReadStatuses != null)
|
|
{
|
|
var result = await context.ChatThreadReadStatuses.FirstOrDefaultAsync(rs => rs.ThreadId == threadId && rs.UserId == userId);
|
|
|
|
if (result != null)
|
|
return result.LastReadOn;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public async Task<int> GetUnreadCountAsync(Guid threadId, DateTime? lastReadOn)
|
|
{
|
|
using (var context = await contextFactory.CreateDbContextAsync())
|
|
{
|
|
if (context.ChatMessages != null)
|
|
{
|
|
return await context.ChatMessages
|
|
.Where(m => m.ThreadId == threadId && m.CreatedOn > lastReadOn)
|
|
.CountAsync();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
}
|
|
}
|