Add versioning, content, and chat presence/notifications services
- SOR.Versioning: version.check handler, ClientRelease entity + admin UI - SOR.Content: incremental card sync (DefinitionVersion cursor via Postgres trigger), content.sync / content.sets / content.card handlers - Chat: presence (account_presence) + notifications with unread count and mark-read - CardDefinition: DefinitionVersion bigint (trigger-assigned) + IsRetired soft-delete - 3 migrations applied to pg-forge-01 (Identity, Chat, Catalog) Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
namespace SOR.Chat;
|
||||
|
||||
using SOR.Database.Enums;
|
||||
|
||||
// ── Request types (client → server) ──────────────────────────────────────────
|
||||
|
||||
/// <summary>Send a message to a channel the caller is a member of.</summary>
|
||||
@@ -44,6 +46,23 @@ public sealed record UnblockRequest(Guid TargetAccountId);
|
||||
/// <summary>Returns friends, pending requests, and blocked users for the caller.</summary>
|
||||
public sealed record GetSocialRequest; // identity from connection
|
||||
|
||||
/// <summary>Update the caller's presence status.</summary>
|
||||
public sealed record SetPresenceRequest(
|
||||
PresenceStatus Status,
|
||||
Guid? CurrentMatchId = null);
|
||||
|
||||
/// <summary>Batch-fetch presence for a set of account IDs (max 100).</summary>
|
||||
public sealed record GetPresenceRequest(IEnumerable<Guid>? AccountIds);
|
||||
|
||||
/// <summary>Fetch the caller's notifications. UnreadOnly=true filters to unread only.</summary>
|
||||
public sealed record GetNotificationsRequest(bool UnreadOnly = false);
|
||||
|
||||
/// <summary>
|
||||
/// Mark notifications as read.
|
||||
/// Null or empty NotificationIds = mark ALL unread notifications for the caller.
|
||||
/// </summary>
|
||||
public sealed record MarkNotificationsReadRequest(IEnumerable<Guid>? NotificationIds = null);
|
||||
|
||||
// ── Response / DTO types (server → client) ────────────────────────────────────
|
||||
|
||||
public sealed record SendMessageResponse(ChatMessageDto Message);
|
||||
@@ -62,6 +81,14 @@ public sealed record GetSocialResponse(
|
||||
IEnumerable<FriendRequestDto> OutgoingRequests,
|
||||
IEnumerable<BlockedUserDto> Blocked);
|
||||
|
||||
public sealed record SetPresenceResponse(PresenceDto Presence);
|
||||
|
||||
public sealed record GetPresenceResponse(IEnumerable<PresenceDto> Presences);
|
||||
|
||||
public sealed record GetNotificationsResponse(
|
||||
IEnumerable<NotificationDto> Notifications,
|
||||
int UnreadCount);
|
||||
|
||||
// ── DTOs ────────────────────────────────────────────────────────────────────
|
||||
|
||||
public sealed record ChatChannelDto(
|
||||
@@ -95,3 +122,18 @@ public sealed record BlockedUserDto(
|
||||
Guid AccountId,
|
||||
string DisplayName,
|
||||
DateTimeOffset BlockedAt);
|
||||
|
||||
public sealed record PresenceDto(
|
||||
Guid AccountId,
|
||||
string Status,
|
||||
Guid? CurrentMatchId,
|
||||
DateTimeOffset LastSeenAt);
|
||||
|
||||
public sealed record NotificationDto(
|
||||
Guid Id,
|
||||
string NotificationType,
|
||||
string Title,
|
||||
string Body,
|
||||
string? PayloadJson,
|
||||
DateTimeOffset CreatedAt,
|
||||
DateTimeOffset? ReadAt);
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
namespace SOR.Chat.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// chat.notifications.get — fetch the caller's notifications (most recent 50),
|
||||
/// with a total unread count.
|
||||
/// </summary>
|
||||
internal sealed class ChatGetNotificationsHandler : ChatHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly ChatDbContext _chatDb;
|
||||
|
||||
public ChatGetNotificationsHandler(IMessageSerializer serializer, ChatDbContext chatDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_chatDb = chatDb;
|
||||
}
|
||||
|
||||
public string MessageType => "chat.notifications.get";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GetNotificationsRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
|
||||
var query = _chatDb.Notifications
|
||||
.Where(n => n.RecipientAccountId == accountId);
|
||||
|
||||
if (req.UnreadOnly)
|
||||
query = query.Where(n => n.ReadAt == null);
|
||||
|
||||
var notifications = await query
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Take(50)
|
||||
.ToListAsync(ct);
|
||||
|
||||
var unreadCount = await _chatDb.Notifications
|
||||
.CountAsync(n => n.RecipientAccountId == accountId && n.ReadAt == null, ct);
|
||||
|
||||
var dtos = notifications.Select(n => new NotificationDto(
|
||||
n.Id,
|
||||
n.NotificationType,
|
||||
n.Title,
|
||||
n.Body,
|
||||
n.PayloadJson,
|
||||
n.CreatedAt,
|
||||
n.ReadAt));
|
||||
|
||||
return ServerMessage.Reply("chat.notifications.get.ack", message.Seq,
|
||||
new GetNotificationsResponse(dtos, unreadCount));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
namespace SOR.Chat.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// chat.presence.get — batch-fetch presence for a set of account IDs.
|
||||
/// Accounts with no presence record are returned as Offline.
|
||||
/// Capped at 100 accounts per request.
|
||||
/// </summary>
|
||||
internal sealed class ChatGetPresenceHandler : ChatHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly ChatDbContext _chatDb;
|
||||
|
||||
public ChatGetPresenceHandler(IMessageSerializer serializer, ChatDbContext chatDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_chatDb = chatDb;
|
||||
}
|
||||
|
||||
public string MessageType => "chat.presence.get";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<GetPresenceRequest>(message.Payload);
|
||||
|
||||
var accountIds = (req.AccountIds ?? Enumerable.Empty<Guid>())
|
||||
.Distinct()
|
||||
.Take(100)
|
||||
.ToList();
|
||||
|
||||
if (accountIds.Count == 0)
|
||||
return ServerMessage.Reply("chat.presence.get.ack", message.Seq,
|
||||
new GetPresenceResponse(Array.Empty<PresenceDto>()));
|
||||
|
||||
var presences = await _chatDb.AccountPresences
|
||||
.Where(p => accountIds.Contains(p.AccountId))
|
||||
.ToListAsync(ct);
|
||||
|
||||
var dtos = accountIds.Select(id =>
|
||||
{
|
||||
var p = presences.FirstOrDefault(x => x.AccountId == id);
|
||||
return p != null
|
||||
? ChatSetPresenceHandler.ToDto(p)
|
||||
: new PresenceDto(id, "Offline", null, DateTimeOffset.MinValue);
|
||||
});
|
||||
|
||||
return ServerMessage.Reply("chat.presence.get.ack", message.Seq,
|
||||
new GetPresenceResponse(dtos));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
namespace SOR.Chat.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// chat.notifications.read — mark one or more notifications as read.
|
||||
/// If NotificationIds is null or empty, marks ALL unread notifications for the caller.
|
||||
/// </summary>
|
||||
internal sealed class ChatMarkNotificationsReadHandler : ChatHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly ChatDbContext _chatDb;
|
||||
|
||||
public ChatMarkNotificationsReadHandler(IMessageSerializer serializer, ChatDbContext chatDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_chatDb = chatDb;
|
||||
}
|
||||
|
||||
public string MessageType => "chat.notifications.read";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<MarkNotificationsReadRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
if (req.NotificationIds == null || !req.NotificationIds.Any())
|
||||
{
|
||||
// Mark all unread for this account.
|
||||
await _chatDb.Notifications
|
||||
.Where(n => n.RecipientAccountId == accountId && n.ReadAt == null)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(n => n.ReadAt, now), ct);
|
||||
}
|
||||
else
|
||||
{
|
||||
var ids = req.NotificationIds.ToList();
|
||||
await _chatDb.Notifications
|
||||
.Where(n => n.RecipientAccountId == accountId
|
||||
&& ids.Contains(n.Id)
|
||||
&& n.ReadAt == null)
|
||||
.ExecuteUpdateAsync(s => s.SetProperty(n => n.ReadAt, now), ct);
|
||||
}
|
||||
|
||||
return ServerMessage.Reply("chat.notifications.read.ack", message.Seq, new { });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
namespace SOR.Chat.Handlers;
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SOR.Database.Contexts;
|
||||
using SOR.Database.Entities.Chat;
|
||||
using SOR.Networking.Abstractions.Messaging;
|
||||
using SOR.Networking.Abstractions.Transport;
|
||||
using SOR.Shared.Networking;
|
||||
|
||||
/// <summary>
|
||||
/// chat.presence.set — upserts the caller's presence status.
|
||||
/// Clients should call this on connect (Online), when entering a match (InMatch),
|
||||
/// and on disconnect/background (Offline/Away).
|
||||
/// </summary>
|
||||
internal sealed class ChatSetPresenceHandler : ChatHandlerBase, IMessageHandler
|
||||
{
|
||||
private readonly IMessageSerializer _serializer;
|
||||
private readonly ChatDbContext _chatDb;
|
||||
|
||||
public ChatSetPresenceHandler(IMessageSerializer serializer, ChatDbContext chatDb)
|
||||
{
|
||||
_serializer = serializer;
|
||||
_chatDb = chatDb;
|
||||
}
|
||||
|
||||
public string MessageType => "chat.presence.set";
|
||||
|
||||
public async Task<IServerMessage?> HandleAsync(
|
||||
IClientMessage message,
|
||||
AuthenticatedConnection connection,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var req = _serializer.DeserializePayload<SetPresenceRequest>(message.Payload);
|
||||
var accountId = Guid.Parse(connection.UserId);
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
|
||||
var presence = await _chatDb.AccountPresences
|
||||
.FirstOrDefaultAsync(p => p.AccountId == accountId, ct);
|
||||
|
||||
if (presence == null)
|
||||
{
|
||||
presence = new AccountPresence
|
||||
{
|
||||
AccountId = accountId,
|
||||
Status = req.Status,
|
||||
CurrentMatchId = req.CurrentMatchId,
|
||||
UpdatedAt = now,
|
||||
LastSeenAt = now,
|
||||
};
|
||||
_chatDb.AccountPresences.Add(presence);
|
||||
}
|
||||
else
|
||||
{
|
||||
presence.Status = req.Status;
|
||||
presence.CurrentMatchId = req.CurrentMatchId;
|
||||
presence.UpdatedAt = now;
|
||||
presence.LastSeenAt = now;
|
||||
}
|
||||
|
||||
await _chatDb.SaveChangesAsync(ct);
|
||||
|
||||
return ServerMessage.Reply("chat.presence.set.ack", message.Seq,
|
||||
new SetPresenceResponse(ToDto(presence)));
|
||||
}
|
||||
|
||||
internal static PresenceDto ToDto(AccountPresence p)
|
||||
=> new(p.AccountId, p.Status.ToString(), p.CurrentMatchId, p.LastSeenAt);
|
||||
}
|
||||
@@ -31,6 +31,10 @@ public static class ServiceRegistration
|
||||
services.AddTransient<IMessageHandler, ChatBlockHandler>();
|
||||
services.AddTransient<IMessageHandler, ChatUnblockHandler>();
|
||||
services.AddTransient<IMessageHandler, ChatGetSocialHandler>();
|
||||
services.AddTransient<IMessageHandler, ChatSetPresenceHandler>();
|
||||
services.AddTransient<IMessageHandler, ChatGetPresenceHandler>();
|
||||
services.AddTransient<IMessageHandler, ChatGetNotificationsHandler>();
|
||||
services.AddTransient<IMessageHandler, ChatMarkNotificationsReadHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user