pull/2947/head^2
Jamie Rees 6 years ago
parent bc026e7e72
commit 396740b2c4

@ -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 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";
public override Task OnConnectedAsync()
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; }
}
}

@ -1,13 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.SignalR" Version="2.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Ombi.Core\Ombi.Core.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.SignalR.Core">
<HintPath>..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.signalr.core\1.1.0\lib\netstandard2.0\Microsoft.AspNetCore.SignalR.Core.dll</HintPath>

@ -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<PlexSettings> plex, IPlexApi plexApi, ILogger<PlexContentSync> logger, IPlexContentRepository repo,
IPlexEpisodeSync epsiodeSync, IRefreshMetadata metadataRefresh, IPlexAvailabilityChecker checker)
IPlexEpisodeSync epsiodeSync, IRefreshMetadata metadataRefresh, IPlexAvailabilityChecker checker, IHubContext<NotificationHub> hub)
{
Plex = plex;
PlexApi = plexApi;
@ -57,6 +59,7 @@ namespace Ombi.Schedule.Jobs.Plex
EpisodeSync = epsiodeSync;
Metadata = metadataRefresh;
Checker = checker;
Notification = hub;
}
private ISettingsService<PlexSettings> 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<NotificationHub> 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<ProcessedContent> StartTheCache(PlexSettings plexSettings, bool recentlyAddedSearch)

@ -35,9 +35,16 @@
<ProjectReference Include="..\Ombi.Api.Sonarr\Ombi.Api.Sonarr.csproj" />
<ProjectReference Include="..\Ombi.Api.TvMaze\Ombi.Api.TvMaze.csproj" />
<ProjectReference Include="..\Ombi.Core\Ombi.Core.csproj" />
<ProjectReference Include="..\Ombi.Hubs\Ombi.Hubs.csproj" />
<ProjectReference Include="..\Ombi.Notifications\Ombi.Notifications.csproj" />
<ProjectReference Include="..\Ombi.Settings\Ombi.Settings.csproj" />
<ProjectReference Include="..\Ombi.TheMovieDbApi\Ombi.Api.TheMovieDb.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.SignalR.Core">
<HintPath>..\..\..\..\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.signalr.core\1.1.0\lib\netstandard2.0\Microsoft.AspNetCore.SignalR.Core.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

@ -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);
});

@ -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);
}
/// <summary>
/// Returns search results for both TV and Movies
/// </summary>
/// <returns></returns>
[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);
}
}
}

@ -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);

Loading…
Cancel
Save