@page "/chat/social" @using Microsoft.EntityFrameworkCore @using SOR.Database.Contexts @using SOR.Database.Entities.Identity @using SOR.Database.Enums @inject ChatDbContext ChatDb @inject SorIdentityDbContext IdentityDb Social Graph — SOR Admin

Social Graph

Player Lookup
@if (!string.IsNullOrEmpty(_lookupError)) {
@_lookupError
}
@if (_loading) {
Loading...
} else if (_accountId.HasValue) {
Showing social graph for @_accountId @if (!string.IsNullOrEmpty(_accountName)) { — @_accountName }
Friends @_friends.Count
@foreach (var f in _friends) { } @if (!_friends.Any()) { }
Account IDNameSince
@f.AccountId @f.DisplayName @f.Since.ToString("yyyy-MM-dd")
No friends.
Pending Friend Requests @_pending.Count
@foreach (var r in _pending) { } @if (!_pending.Any()) { }
DirectionOther AccountNameSince
@(r.IsIncoming ? "Incoming" : "Outgoing") @r.AccountId @r.DisplayName @r.CreatedAt.ToString("yyyy-MM-dd")
No pending requests.
Blocked Players @_blocked.Count
@foreach (var b in _blocked) { } @if (!_blocked.Any()) { }
Account IDNameBlocked At
@b.AccountId @b.DisplayName @b.BlockedAt.ToString("yyyy-MM-dd")
Not blocking anyone.
} @code { private bool _loading = false; private string _lookupInput = ""; private string _lookupError = ""; private Guid? _accountId; private string _accountName = ""; private record FriendRow(Guid RowId, Guid AccountId, string DisplayName, DateTimeOffset Since); private record PendingRow(Guid RowId, Guid AccountId, string DisplayName, bool IsIncoming, DateTimeOffset CreatedAt); private record BlockedRow(Guid RowId, Guid AccountId, string DisplayName, DateTimeOffset BlockedAt); private List _friends = new(); private List _pending = new(); private List _blocked = new(); private async Task OnLookupKeyDown(KeyboardEventArgs e) { if (e.Key == "Enter") await LookupAsync(); } private async Task LookupAsync() { _lookupError = ""; if (!Guid.TryParse(_lookupInput.Trim(), out var id)) { _lookupError = "Invalid GUID."; return; } _loading = true; _accountId = id; // Resolve display name var account = await IdentityDb.Set() .Where(a => a.Id == id) .Select(a => new { a.DisplayName, a.Email }) .FirstOrDefaultAsync(); _accountName = account?.DisplayName ?? account?.Email ?? ""; // Load all social rows var friendRows = await ChatDb.FriendRequests .Where(f => f.RequesterAccountId == id || f.RecipientAccountId == id) .ToListAsync(); var blockedRows = await ChatDb.BlockedUsers .Where(b => b.BlockerAccountId == id) .ToListAsync(); // Collect other account IDs for name resolution var otherIds = new HashSet(); foreach (var f in friendRows) { otherIds.Add(f.RequesterAccountId); otherIds.Add(f.RecipientAccountId); } foreach (var b in blockedRows) otherIds.Add(b.BlockedAccountId); otherIds.Remove(id); var names = new Dictionary(); if (otherIds.Any()) { names = await IdentityDb.Set() .Where(a => otherIds.Contains(a.Id)) .ToDictionaryAsync(a => a.Id, a => a.DisplayName ?? a.Email ?? a.Id.ToString()); } _friends = friendRows .Where(f => f.Status == FriendRequestStatus.Accepted) .Select(f => { var other = f.RequesterAccountId == id ? f.RecipientAccountId : f.RequesterAccountId; return new FriendRow(f.Id, other, names.GetValueOrDefault(other, other.ToString()), f.UpdatedAt); }) .ToList(); _pending = friendRows .Where(f => f.Status == FriendRequestStatus.Pending) .Select(f => { var isIncoming = f.RecipientAccountId == id; var other = isIncoming ? f.RequesterAccountId : f.RecipientAccountId; return new PendingRow(f.Id, other, names.GetValueOrDefault(other, other.ToString()), isIncoming, f.CreatedAt); }) .ToList(); _blocked = blockedRows .Select(b => new BlockedRow( b.Id, b.BlockedAccountId, names.GetValueOrDefault(b.BlockedAccountId, b.BlockedAccountId.ToString()), b.BlockedAt)) .ToList(); _loading = false; } private async Task RemoveFriendAsync(Guid rowId) { var row = await ChatDb.FriendRequests.FindAsync(rowId); if (row != null) { ChatDb.FriendRequests.Remove(row); await ChatDb.SaveChangesAsync(); } _friends.RemoveAll(f => f.RowId == rowId); } private async Task CancelRequestAsync(Guid rowId) { var row = await ChatDb.FriendRequests.FindAsync(rowId); if (row != null) { ChatDb.FriendRequests.Remove(row); await ChatDb.SaveChangesAsync(); } _pending.RemoveAll(r => r.RowId == rowId); } private async Task UnblockAsync(Guid rowId) { var row = await ChatDb.BlockedUsers.FindAsync(rowId); if (row != null) { ChatDb.BlockedUsers.Remove(row); await ChatDb.SaveChangesAsync(); } _blocked.RemoveAll(b => b.RowId == rowId); } }