79 lines
2.6 KiB
C#
79 lines
2.6 KiB
C#
using DeluxeBackend.Models;
|
|
using DeluxeBackend.Services;
|
|
using Discord;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using System.Numerics;
|
|
using System.Security.Claims;
|
|
|
|
namespace DeluxeBackend.Hubs
|
|
{
|
|
[Authorize]
|
|
public class NotificationHub : Hub
|
|
{
|
|
private readonly ILiteDbService db;
|
|
private readonly IJwtService jwt;
|
|
private readonly INotificationService ws;
|
|
//private readonly INotificationService notificationService;
|
|
|
|
public NotificationHub(ILiteDbService _db, IJwtService _jet, INotificationService _ws)
|
|
{
|
|
db = _db;
|
|
jwt = _jet;
|
|
ws= _ws;
|
|
}
|
|
public class SubscriptionParam
|
|
{
|
|
public List<long> PlayerIds { get; set; } = new();
|
|
}
|
|
|
|
|
|
public async Task SubscribeToPlayers(SubscriptionParam parms)
|
|
{
|
|
/*var subscriberId = Context.User?.FindFirst("sub")?.Value;
|
|
if (subscriberId == null) return;
|
|
|
|
var currentSubs = Context.Items["subs"] as HashSet<long> ?? [];
|
|
var newSet = new HashSet<long>(parms.PlayerIds);
|
|
|
|
foreach (var oldTarget in currentSubs.Where(id => !newSet.Contains(id)))
|
|
{
|
|
await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"subs_{oldTarget}");
|
|
}
|
|
|
|
foreach (var newTarget in newSet.Where(id => !currentSubs.Contains(id)))
|
|
{
|
|
await Groups.AddToGroupAsync(Context.ConnectionId, $"subs_{newTarget}");
|
|
}
|
|
|
|
Context.Items["subs"] = newSet;*/
|
|
}
|
|
|
|
public override async Task OnDisconnectedAsync(Exception? exception)
|
|
{
|
|
await base.OnDisconnectedAsync(exception);
|
|
Account? account = await jwt.GetLogin(Context.User);
|
|
|
|
if (account == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
AccountPresence presence = db.Presences.Include(x => x.Account).Include(x => x.Instance).Include(x => x.Instance!.SubRoom).Include(x => x.Instance!.SubRoom!.Room).Include(x => x.Instance!.SubRoom!.Room!.Creator).FindOne(x => x.Account.Id == account.Id);
|
|
if (presence == null)
|
|
{
|
|
presence = new AccountPresence
|
|
{
|
|
Account = account,
|
|
};
|
|
}
|
|
presence.Instance = null;
|
|
presence.LastOnline = DateTime.UtcNow;
|
|
presence.IsOnline = false;
|
|
db.Presences.Upsert(presence);
|
|
await ws.SendToAllPlayer("PresenceUpdate", presence.ToDictionary());
|
|
}
|
|
}
|
|
|
|
}
|