diff --git a/src/Ombi.Hubs/NotificationHub.cs b/src/Ombi.Hubs/NotificationHub.cs index 6fb4f4ea3..7fac9851e 100644 --- a/src/Ombi.Hubs/NotificationHub.cs +++ b/src/Ombi.Hubs/NotificationHub.cs @@ -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 UsersOnline = new ConcurrentDictionary(); + public NotificationHub(OmbiUserManager um) + { + _userManager = um; + } - public override Task OnConnectedAsync() + public static ConcurrentDictionary UsersOnline = new ConcurrentDictionary(); + + public static List 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 Roles { get; set; } + } } diff --git a/src/Ombi.Hubs/Ombi.Hubs.csproj b/src/Ombi.Hubs/Ombi.Hubs.csproj index 3331096d0..64d92ec89 100644 --- a/src/Ombi.Hubs/Ombi.Hubs.csproj +++ b/src/Ombi.Hubs/Ombi.Hubs.csproj @@ -1,13 +1,17 @@ - + - netcoreapp2.2 + netstandard2.0 + + + + ..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.signalr.core\1.1.0\lib\netstandard2.0\Microsoft.AspNetCore.SignalR.Core.dll diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs index 964832b44..87acccf58 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexContentSync.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Hangfire; +using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Ombi.Api.Plex; @@ -37,6 +38,7 @@ using Ombi.Api.Plex.Models; using Ombi.Core.Settings; using Ombi.Core.Settings.Models.External; using Ombi.Helpers; +using Ombi.Hubs; using Ombi.Schedule.Jobs.Ombi; using Ombi.Schedule.Jobs.Plex.Interfaces; using Ombi.Schedule.Jobs.Plex.Models; @@ -48,7 +50,7 @@ namespace Ombi.Schedule.Jobs.Plex public class PlexContentSync : IPlexContentSync { public PlexContentSync(ISettingsService plex, IPlexApi plexApi, ILogger logger, IPlexContentRepository repo, - IPlexEpisodeSync epsiodeSync, IRefreshMetadata metadataRefresh, IPlexAvailabilityChecker checker) + IPlexEpisodeSync epsiodeSync, IRefreshMetadata metadataRefresh, IPlexAvailabilityChecker checker, IHubContext hub) { Plex = plex; PlexApi = plexApi; @@ -57,6 +59,7 @@ namespace Ombi.Schedule.Jobs.Plex EpisodeSync = epsiodeSync; Metadata = metadataRefresh; Checker = checker; + Notification = hub; } private ISettingsService Plex { get; } @@ -66,17 +69,23 @@ namespace Ombi.Schedule.Jobs.Plex private IPlexEpisodeSync EpisodeSync { get; } private IRefreshMetadata Metadata { get; } private IPlexAvailabilityChecker Checker { get; } + private IHubContext Notification { get; set; } public async Task CacheContent(bool recentlyAddedSearch = false) { + var plexSettings = await Plex.GetSettingsAsync(); if (!plexSettings.Enable) { return; } + await Notification.Clients.Clients(NotificationHub.AdminConnectionIds) + .SendAsync(NotificationHub.NotificationEvent, recentlyAddedSearch ? "Plex Recently Added Sync Started" : "Plex Content Sync Started"); if (!ValidateSettings(plexSettings)) { Logger.LogError("Plex Settings are not valid"); + await Notification.Clients.Clients(NotificationHub.AdminConnectionIds) + .SendAsync(NotificationHub.NotificationEvent, recentlyAddedSearch ? "Plex Recently Added Sync, Settings Not Valid" : "Plex Content, Settings Not Valid"); return; } var processedContent = new ProcessedContent(); @@ -94,6 +103,8 @@ namespace Ombi.Schedule.Jobs.Plex } catch (Exception e) { + await Notification.Clients.Clients(NotificationHub.AdminConnectionIds) + .SendAsync(NotificationHub.NotificationEvent, recentlyAddedSearch ? "Plex Recently Added Sync Errored" : "Plex Content Sync Errored"); Logger.LogWarning(LoggingEvents.PlexContentCacher, e, "Exception thrown when attempting to cache the Plex Content"); } @@ -113,6 +124,9 @@ namespace Ombi.Schedule.Jobs.Plex { BackgroundJob.Enqueue(() => Checker.Start()); } + + await Notification.Clients.Clients(NotificationHub.AdminConnectionIds) + .SendAsync(NotificationHub.NotificationEvent, recentlyAddedSearch ? "Plex Recently Added Sync Finished" : "Plex Content Sync Finished"); } private async Task StartTheCache(PlexSettings plexSettings, bool recentlyAddedSearch) diff --git a/src/Ombi.Schedule/Ombi.Schedule.csproj b/src/Ombi.Schedule/Ombi.Schedule.csproj index 827db0d9a..6e69cd5b6 100644 --- a/src/Ombi.Schedule/Ombi.Schedule.csproj +++ b/src/Ombi.Schedule/Ombi.Schedule.csproj @@ -35,9 +35,16 @@ + + + + ..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.signalr.core\1.1.0\lib\netstandard2.0\Microsoft.AspNetCore.SignalR.Core.dll + + + \ No newline at end of file diff --git a/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts b/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts index ec417003f..e44b1d2b2 100644 --- a/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts +++ b/src/Ombi/ClientApp/src/app/services/signlarnotification.service.ts @@ -17,7 +17,7 @@ export class SignalRNotificationService { public initialize(): void { this.stopConnection(); - + this.hubConnection = new signalR.HubConnectionBuilder().withUrl("/hubs/notification", { accessTokenFactory: () => { return this.authService.getToken(); @@ -26,6 +26,7 @@ export class SignalRNotificationService { this.hubConnection.on("Notification", (data: any) => { + debugger; this.Notification.emit(data); }); diff --git a/src/Ombi/Controllers/V2/HubController.cs b/src/Ombi/Controllers/V2/HubController.cs index f2eb7adf7..8fb5f864b 100644 --- a/src/Ombi/Controllers/V2/HubController.cs +++ b/src/Ombi/Controllers/V2/HubController.cs @@ -4,12 +4,14 @@ using Microsoft.AspNetCore.Mvc; using Ombi.Api.TheMovieDb.Models; using Ombi.Core.Engine.V2; using System.Collections.Generic; +using System.Linq; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.SignalR; using Ombi.Core; using Ombi.Core.Engine.Interfaces; using Ombi.Core.Models.Search; using Ombi.Core.Models.Search.V2; +using Ombi.Helpers; using Ombi.Hubs; using Ombi.Models; @@ -36,5 +38,16 @@ namespace Ombi.Controllers.V2 { await _hub.Clients.All.SendAsync("Notification", searchTerm); } + + /// + /// Returns search results for both TV and Movies + /// + /// + [HttpGet("admin/{searchTerm}")] + public async Task Admin(string searchTerm) + { + var admins = NotificationHub.UsersOnline.Where(x => x.Value.Roles.Contains(OmbiRoles.Admin)).Select(x => x.Key).ToList(); + await _hub.Clients.Clients(admins).SendAsync("Notification", searchTerm); + } } } \ No newline at end of file diff --git a/src/Ombi/Startup.cs b/src/Ombi/Startup.cs index 0b6aca4cb..390fc9087 100644 --- a/src/Ombi/Startup.cs +++ b/src/Ombi/Startup.cs @@ -110,9 +110,10 @@ namespace Ombi services.AddCors(o => o.AddPolicy("MyPolicy", builder => { - builder.AllowAnyOrigin() + builder.AllowAnyHeader() .AllowAnyMethod() - .AllowAnyHeader().AllowCredentials(); + .SetIsOriginAllowed(isOriginAllowed: _ => true) + .AllowCredentials(); })); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);