Add friends, DMs, presence, block, and party chat

- Fix social response key mismatch (incomingRequests, blocked)
- Fix DmOpenResponse to return full ChatChannelDto so client can
  add the channel without a separate fetch
- Add Party channel type to ChatChannelType enum
- Add PartyInviteHandler, PartyInviteRespondHandler, PartyLeaveHandler
  with IClientPush invite notification to the invitee
- Add party tab to chat overlay (action bar, messages, Leave button)
- Wire Invite to Party in both player and friend context menus
- Show accept/decline popup dialog on incoming party invite push

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:
dingo
2026-03-06 16:12:51 -05:00
co-authored by Claude Happy
parent aa0c17490b
commit 9dadeda7c1
9 changed files with 532 additions and 9 deletions
+24 -1
View File
@@ -63,6 +63,19 @@ public sealed record GetNotificationsRequest(bool UnreadOnly = false);
/// </summary>
public sealed record MarkNotificationsReadRequest(IEnumerable<Guid>? NotificationIds = null);
/// <summary>
/// Invite another player to the caller's party.
/// Creates a Party channel for the caller if they don't have one yet.
/// Fails if either player has blocked the other, or the invitee is already in a party.
/// </summary>
public sealed record PartyInviteRequest(Guid InviteeAccountId);
/// <summary>Accept or decline a party invitation.</summary>
public sealed record PartyInviteRespondRequest(Guid ChannelId, bool Accept);
/// <summary>Leave the caller's current party channel.</summary>
public sealed record PartyLeaveRequest; // identity from connection
// ── Response / DTO types (server → client) ────────────────────────────────────
public sealed record SendMessageResponse(ChatMessageDto Message);
@@ -73,7 +86,7 @@ public sealed record GetMessagesResponse(
IEnumerable<ChatMessageDto> Messages,
bool HasMore);
public sealed record DmOpenResponse(Guid ChannelId);
public sealed record DmOpenResponse(ChatChannelDto Channel);
public sealed record GetSocialResponse(
IEnumerable<FriendDto> Friends,
@@ -89,6 +102,16 @@ public sealed record GetNotificationsResponse(
IEnumerable<NotificationDto> Notifications,
int UnreadCount);
public sealed record MarkNotificationsReadResponse;
/// <summary>Returned to the inviter after a successful party invite is dispatched.</summary>
public sealed record PartyInviteResponse(ChatChannelDto Channel);
/// <summary>Returned to the invitee after they accept or decline.</summary>
public sealed record PartyInviteRespondResponse(bool Accepted, ChatChannelDto? Channel);
public sealed record PartyLeaveResponse;
// ── DTOs ────────────────────────────────────────────────────────────────────
public sealed record ChatChannelDto(
@@ -29,6 +29,9 @@ internal sealed class ChatDmOpenHandler : ChatHandlerBase, IMessageHandler
public string MessageType => "chat.dm_open";
private static ChatChannelDto ToDto(ChatChannel c) =>
new(c.Id, c.Name, c.ChannelType.ToString(), c.CreatedAt);
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
@@ -62,8 +65,12 @@ internal sealed class ChatDmOpenHandler : ChatHandlerBase, IMessageHandler
.FirstOrDefaultAsync(cancellationToken);
if (existingChannelId != Guid.Empty)
{
var existing = await _chatDb.Channels.FindAsync(
new object[] { existingChannelId }, cancellationToken);
return ServerMessage.Reply("chat.dm_open.ack", message.Seq,
new DmOpenResponse(existingChannelId));
new DmOpenResponse(ToDto(existing!)));
}
// ── Create new DM channel ─────────────────────────────────────────────
var now = DateTimeOffset.UtcNow;
@@ -81,6 +88,6 @@ internal sealed class ChatDmOpenHandler : ChatHandlerBase, IMessageHandler
await _chatDb.SaveChangesAsync(cancellationToken);
return ServerMessage.Reply("chat.dm_open.ack", message.Seq,
new DmOpenResponse(channel.Id));
new DmOpenResponse(ToDto(channel)));
}
}
@@ -0,0 +1,106 @@
namespace SOR.Chat.Handlers;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Database.Entities.Chat;
using SOR.Database.Enums;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Push;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
/// <summary>
/// chat.party.invite — invites another player into the caller's party.
///
/// If the caller has no active party a new Party channel is created for them.
/// A push notification is sent to the invitee so their client can show an accept/decline dialog.
/// Fails if either player has blocked the other or the invitee is already in a party.
/// </summary>
internal sealed class ChatPartyInviteHandler : ChatHandlerBase, IMessageHandler
{
private readonly IMessageSerializer _serializer;
private readonly ChatDbContext _chatDb;
private readonly IClientPush _push;
public ChatPartyInviteHandler(
IMessageSerializer serializer,
ChatDbContext chatDb,
IClientPush push)
{
_serializer = serializer;
_chatDb = chatDb;
_push = push;
}
public string MessageType => "chat.party.invite";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken cancellationToken)
{
var req = _serializer.DeserializePayload<PartyInviteRequest>(message.Payload);
var accountId = Guid.Parse(connection.UserId);
if (req.InviteeAccountId == accountId)
return ServerMessage.Error(message.Seq, "INVALID_RECIPIENT", "Cannot invite yourself to a party.");
// ── Block check ───────────────────────────────────────────────────────
var blocked = await IsBlockedEitherWayAsync(_chatDb, accountId, req.InviteeAccountId, cancellationToken);
if (blocked)
return ServerMessage.Error(message.Seq, "BLOCKED", "Cannot invite this player.");
// ── Check invitee is not already in a party ────────────────────────────
var inviteeInParty = await _chatDb.Members
.AnyAsync(m => m.AccountId == req.InviteeAccountId
&& m.LeftAt == null
&& _chatDb.Channels.Any(c => c.Id == m.ChannelId && c.ChannelType == ChatChannelType.Party),
cancellationToken);
if (inviteeInParty)
return ServerMessage.Error(message.Seq, "ALREADY_IN_PARTY", "That player is already in a party.");
// ── Find or create the caller's party channel ─────────────────────────
var partyChannel = await _chatDb.Members
.Where(m => m.AccountId == accountId && m.LeftAt == null)
.Join(_chatDb.Channels.Where(c => c.ChannelType == ChatChannelType.Party),
m => m.ChannelId, c => c.Id, (_, c) => c)
.FirstOrDefaultAsync(cancellationToken);
if (partyChannel is null)
{
var now = DateTimeOffset.UtcNow;
partyChannel = new ChatChannel
{
Name = $"{connection.DisplayName}'s Party",
ChannelType = ChatChannelType.Party,
CreatedAt = now,
};
_chatDb.Channels.Add(partyChannel);
await _chatDb.SaveChangesAsync(cancellationToken);
_chatDb.Members.Add(new ChatMember
{
ChannelId = partyChannel.Id,
AccountId = accountId,
JoinedAt = now,
});
await _chatDb.SaveChangesAsync(cancellationToken);
}
// ── Push invite notification to the invitee ────────────────────────────
await _push.PushAsync(
req.InviteeAccountId.ToString(),
ServerMessage.Push("chat.party_invite.push", new
{
channelId = partyChannel.Id,
inviterName = connection.DisplayName,
}),
cancellationToken);
return ServerMessage.Reply("chat.party.invite.ack", message.Seq,
new PartyInviteResponse(ToDto(partyChannel)));
}
private static ChatChannelDto ToDto(ChatChannel c) =>
new(c.Id, c.Name, c.ChannelType.ToString(), c.CreatedAt);
}
@@ -0,0 +1,83 @@
namespace SOR.Chat.Handlers;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Database.Entities.Chat;
using SOR.Database.Enums;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
/// <summary>
/// chat.party.invite_respond — accept or decline a party invitation.
///
/// On accept: caller is added to the party channel as a member and the channel DTO is returned.
/// On decline: a bare acknowledgement is returned; the party channel is unaffected.
/// Fails if the channel does not exist, is not a Party channel, or the caller is already in a party.
/// </summary>
internal sealed class ChatPartyInviteRespondHandler : ChatHandlerBase, IMessageHandler
{
private readonly IMessageSerializer _serializer;
private readonly ChatDbContext _chatDb;
public ChatPartyInviteRespondHandler(
IMessageSerializer serializer,
ChatDbContext chatDb)
{
_serializer = serializer;
_chatDb = chatDb;
}
public string MessageType => "chat.party.invite_respond";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken cancellationToken)
{
var req = _serializer.DeserializePayload<PartyInviteRespondRequest>(message.Payload);
var accountId = Guid.Parse(connection.UserId);
// ── Validate the target channel ───────────────────────────────────────
var channel = await _chatDb.Channels.FindAsync(new object[] { req.ChannelId }, cancellationToken);
if (channel is null || channel.ChannelType != ChatChannelType.Party)
return ServerMessage.Error(message.Seq, "NOT_FOUND", "Party channel not found.");
if (!req.Accept)
return ServerMessage.Reply("chat.party.invite_respond.ack", message.Seq,
new PartyInviteRespondResponse(false, null));
// ── Accept: check caller isn't already in another party ───────────────
var alreadyInParty = await _chatDb.Members
.AnyAsync(m => m.AccountId == accountId
&& m.LeftAt == null
&& _chatDb.Channels.Any(c => c.Id == m.ChannelId && c.ChannelType == ChatChannelType.Party),
cancellationToken);
if (alreadyInParty)
return ServerMessage.Error(message.Seq, "ALREADY_IN_PARTY", "You are already in a party. Leave it first.");
// ── Add or re-activate membership ─────────────────────────────────────
var membership = await _chatDb.Members
.FirstOrDefaultAsync(m => m.ChannelId == req.ChannelId && m.AccountId == accountId, cancellationToken);
if (membership is null)
{
_chatDb.Members.Add(new ChatMember
{
ChannelId = req.ChannelId,
AccountId = accountId,
JoinedAt = DateTimeOffset.UtcNow,
});
}
else
{
membership.LeftAt = null;
}
await _chatDb.SaveChangesAsync(cancellationToken);
return ServerMessage.Reply("chat.party.invite_respond.ack", message.Seq,
new PartyInviteRespondResponse(true, new ChatChannelDto(
channel.Id, channel.Name, channel.ChannelType.ToString(), channel.CreatedAt)));
}
}
@@ -0,0 +1,47 @@
namespace SOR.Chat.Handlers;
using Microsoft.EntityFrameworkCore;
using SOR.Database.Contexts;
using SOR.Database.Enums;
using SOR.Networking.Abstractions.Messaging;
using SOR.Networking.Abstractions.Transport;
using SOR.Shared.Networking;
/// <summary>
/// chat.party.leave — removes the caller from their current party channel.
///
/// Sets LeftAt on the caller's membership record. No-ops silently if not in a party.
/// </summary>
internal sealed class ChatPartyLeaveHandler : IMessageHandler
{
private readonly ChatDbContext _chatDb;
public ChatPartyLeaveHandler(ChatDbContext chatDb)
{
_chatDb = chatDb;
}
public string MessageType => "chat.party.leave";
public async Task<IServerMessage?> HandleAsync(
IClientMessage message,
AuthenticatedConnection connection,
CancellationToken cancellationToken)
{
var accountId = Guid.Parse(connection.UserId);
var membership = await _chatDb.Members
.Where(m => m.AccountId == accountId && m.LeftAt == null)
.Join(_chatDb.Channels.Where(c => c.ChannelType == ChatChannelType.Party),
m => m.ChannelId, c => c.Id, (m, _) => m)
.FirstOrDefaultAsync(cancellationToken);
if (membership is not null)
{
membership.LeftAt = DateTimeOffset.UtcNow;
await _chatDb.SaveChangesAsync(cancellationToken);
}
return ServerMessage.Reply("chat.party.leave.ack", message.Seq, new PartyLeaveResponse());
}
}
@@ -35,6 +35,9 @@ public static class ServiceRegistration
services.AddTransient<IMessageHandler, ChatGetPresenceHandler>();
services.AddTransient<IMessageHandler, ChatGetNotificationsHandler>();
services.AddTransient<IMessageHandler, ChatMarkNotificationsReadHandler>();
services.AddTransient<IMessageHandler, ChatPartyInviteHandler>();
services.AddTransient<IMessageHandler, ChatPartyInviteRespondHandler>();
services.AddTransient<IMessageHandler, ChatPartyLeaveHandler>();
services.AddHostedService<ChatLifecycleHostedService>();
return services;