2025-11-16 12:31:25 -06:00
|
|
|
|
using LANCommander.Server.Data;
|
2025-11-12 18:26:25 -06:00
|
|
|
|
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())
|
|
|
|
|
|
{
|
2026-01-11 14:38:24 -06:00
|
|
|
|
if (context.ChatThreadReadStatuses != null && context.ChatMessages != null)
|
2025-11-12 18:26:25 -06:00
|
|
|
|
{
|
2026-01-11 14:38:24 -06:00
|
|
|
|
// Get the latest message in the thread
|
|
|
|
|
|
var latestMessage = await context.ChatMessages
|
|
|
|
|
|
.Where(m => m.ThreadId == threadId)
|
|
|
|
|
|
.OrderByDescending(m => m.CreatedOn)
|
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
|
|
|
|
|
|
|
if (latestMessage == null)
|
|
|
|
|
|
return; // No messages in thread, nothing to mark as read
|
|
|
|
|
|
|
2025-11-12 18:26:25 -06:00
|
|
|
|
var status = await context.ChatThreadReadStatuses.FirstOrDefaultAsync(rs => rs.ThreadId == threadId && rs.UserId == userId);
|
|
|
|
|
|
|
|
|
|
|
|
if (status == null)
|
|
|
|
|
|
{
|
2026-01-11 14:38:24 -06:00
|
|
|
|
await context.ChatThreadReadStatuses.AddAsync(new ChatThreadReadStatus
|
2025-11-12 18:26:25 -06:00
|
|
|
|
{
|
|
|
|
|
|
ThreadId = threadId,
|
|
|
|
|
|
UserId = userId,
|
2026-01-11 14:38:24 -06:00
|
|
|
|
LastReadMessageId = latestMessage.Id,
|
2025-11-12 18:26:25 -06:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2026-01-11 14:38:24 -06:00
|
|
|
|
status.LastReadMessageId = latestMessage.Id;
|
2025-11-12 18:26:25 -06:00
|
|
|
|
context.ChatThreadReadStatuses.Update(status);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 14:38:24 -06:00
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
}
|
2025-11-12 18:26:25 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 14:38:24 -06:00
|
|
|
|
public async Task<Guid?> GetLastReadMessageIdAsync(Guid threadId, Guid userId)
|
2025-11-12 18:26:25 -06:00
|
|
|
|
{
|
|
|
|
|
|
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)
|
2026-01-11 14:38:24 -06:00
|
|
|
|
return result.LastReadMessageId;
|
2025-11-12 18:26:25 -06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 14:38:24 -06:00
|
|
|
|
public async Task<int> GetUnreadCountAsync(Guid threadId, Guid? lastReadMessageId)
|
2025-11-12 18:26:25 -06:00
|
|
|
|
{
|
|
|
|
|
|
using (var context = await contextFactory.CreateDbContextAsync())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (context.ChatMessages != null)
|
|
|
|
|
|
{
|
2026-01-11 14:38:24 -06:00
|
|
|
|
if (lastReadMessageId == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// No read status, count all messages in thread
|
|
|
|
|
|
return await context.ChatMessages
|
|
|
|
|
|
.Where(m => m.ThreadId == threadId)
|
|
|
|
|
|
.CountAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the last read message to find its CreatedOn timestamp
|
|
|
|
|
|
var lastReadMessage = await context.ChatMessages
|
|
|
|
|
|
.FirstOrDefaultAsync(m => m.Id == lastReadMessageId.Value);
|
|
|
|
|
|
|
|
|
|
|
|
if (lastReadMessage == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Last read message doesn't exist, count all messages
|
|
|
|
|
|
return await context.ChatMessages
|
|
|
|
|
|
.Where(m => m.ThreadId == threadId)
|
|
|
|
|
|
.CountAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Count messages created after the last read message
|
2025-11-12 18:26:25 -06:00
|
|
|
|
return await context.ChatMessages
|
2026-01-11 14:38:24 -06:00
|
|
|
|
.Where(m => m.ThreadId == threadId && m.CreatedOn > lastReadMessage.CreatedOn)
|
2025-11-12 18:26:25 -06:00
|
|
|
|
.CountAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|