mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-16 02:02:55 -07:00
More work
This commit is contained in:
parent
bc026e7e72
commit
396740b2c4
7 changed files with 87 additions and 13 deletions
|
@ -1,28 +1,56 @@
|
|||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Xml;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Ombi.Core.Authentication;
|
||||
using Ombi.Helpers;
|
||||
|
||||
namespace Ombi.Hubs
|
||||
{
|
||||
public class NotificationHub : Hub
|
||||
{
|
||||
public static ConcurrentDictionary<string, string> UsersOnline = new ConcurrentDictionary<string, string>();
|
||||
public NotificationHub(OmbiUserManager um)
|
||||
{
|
||||
_userManager = um;
|
||||
}
|
||||
|
||||
public override Task OnConnectedAsync()
|
||||
public static ConcurrentDictionary<string, HubUsers> UsersOnline = new ConcurrentDictionary<string, HubUsers>();
|
||||
|
||||
public static List<string> AdminConnectionIds
|
||||
{
|
||||
get
|
||||
{
|
||||
return UsersOnline.Where(x => x.Value.Roles.Contains(OmbiRoles.Admin)).Select(x => x.Key).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public const string NotificationEvent = "Notification";
|
||||
|
||||
private readonly OmbiUserManager _userManager;
|
||||
|
||||
public override async Task OnConnectedAsync()
|
||||
{
|
||||
var identity = (ClaimsIdentity) Context.User.Identity;
|
||||
var userIdClaim = identity.Claims.FirstOrDefault(x => x.Type.Equals("Id", StringComparison.InvariantCultureIgnoreCase));
|
||||
if (userIdClaim == null)
|
||||
{
|
||||
return base.OnConnectedAsync();
|
||||
await base.OnConnectedAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
UsersOnline.TryAdd(Context.ConnectionId, userIdClaim.Value);
|
||||
return base.OnConnectedAsync();
|
||||
var user = await _userManager.Users.
|
||||
FirstOrDefaultAsync(x => x.Id.Equals(userIdClaim.Value, StringComparison.InvariantCultureIgnoreCase));
|
||||
var claims = await _userManager.GetRolesAsync(user);
|
||||
UsersOnline.TryAdd(Context.ConnectionId, new HubUsers
|
||||
{
|
||||
UserId = userIdClaim.Value,
|
||||
Roles = claims
|
||||
});
|
||||
await base.OnConnectedAsync();
|
||||
}
|
||||
|
||||
public override Task OnDisconnectedAsync(Exception exception)
|
||||
|
@ -33,7 +61,13 @@ namespace Ombi.Hubs
|
|||
|
||||
public Task Notification(string data)
|
||||
{
|
||||
return Clients.All.SendAsync("Notification", data);
|
||||
return Clients.All.SendAsync(NotificationEvent, data);
|
||||
}
|
||||
}
|
||||
|
||||
public class HubUsers
|
||||
{
|
||||
public string UserId { get; set; }
|
||||
public IList<string> Roles { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue