using NzbDrone.Core.Datastore; using NzbDrone.Core.Datastore.Events; using NzbDrone.Core.Messaging.Events; using NzbDrone.SignalR; using Lidarr.Http.REST; namespace Lidarr.Http { public abstract class LidarrRestModuleWithSignalR : LidarrRestModule, IHandle> where TResource : RestResource, new() where TModel : ModelBase, new() { private readonly IBroadcastSignalRMessage _signalRBroadcaster; protected LidarrRestModuleWithSignalR(IBroadcastSignalRMessage signalRBroadcaster) { _signalRBroadcaster = signalRBroadcaster; } protected LidarrRestModuleWithSignalR(IBroadcastSignalRMessage signalRBroadcaster, string resource) : base(resource) { _signalRBroadcaster = signalRBroadcaster; } public void Handle(ModelEvent message) { if (message.Action == ModelAction.Deleted || message.Action == ModelAction.Sync) { BroadcastResourceChange(message.Action); } BroadcastResourceChange(message.Action, message.Model.Id); } protected void BroadcastResourceChange(ModelAction action, int id) { if (action == ModelAction.Deleted) { BroadcastResourceChange(action, new TResource { Id = id }); } else { var resource = GetResourceById(id); BroadcastResourceChange(action, resource); } } protected void BroadcastResourceChange(ModelAction action, TResource resource) { if (GetType().Namespace.Contains("V1")) { var signalRMessage = new SignalRMessage { Name = Resource, Body = new ResourceChangeMessage(resource, action), Action = action }; _signalRBroadcaster.BroadcastMessage(signalRMessage); } } protected void BroadcastResourceChange(ModelAction action) { if (GetType().Namespace.Contains("V1")) { var signalRMessage = new SignalRMessage { Name = Resource, Body = new ResourceChangeMessage(action), Action = action }; _signalRBroadcaster.BroadcastMessage(signalRMessage); } } } }