From 8cb59c35fbc76db1b3b9f713041e77c512b03c8a Mon Sep 17 00:00:00 2001 From: Bogdan Date: Sat, 12 Oct 2024 01:43:51 +0300 Subject: [PATCH] New: Sync UI updates for providers --- frontend/src/Components/SignalRConnector.js | 53 ++++++++++++++++--- .../Applications/ApplicationController.cs | 5 +- .../DownloadClientController.cs | 5 +- .../IndexerProxies/IndexerProxyController.cs | 5 +- .../Indexers/IndexerController.cs | 6 ++- .../Notifications/NotificationController.cs | 5 +- src/Prowlarr.Api.V1/ProviderControllerBase.cs | 31 ++++++++++- 7 files changed, 91 insertions(+), 19 deletions(-) diff --git a/frontend/src/Components/SignalRConnector.js b/frontend/src/Components/SignalRConnector.js index 28c12df12..d39c05e10 100644 --- a/frontend/src/Components/SignalRConnector.js +++ b/frontend/src/Components/SignalRConnector.js @@ -141,6 +141,16 @@ class SignalRConnector extends Component { console.error(`signalR: Unable to find handler for ${name}`); }; + handleApplications = ({ action, resource }) => { + const section = 'settings.applications'; + + if (action === 'created' || action === 'updated') { + this.props.dispatchUpdateItem({ section, ...resource }); + } else if (action === 'deleted') { + this.props.dispatchRemoveItem({ section, id: resource.id }); + } + }; + handleCommand = (body) => { if (body.action === 'sync') { this.props.dispatchFetchCommands(); @@ -150,8 +160,8 @@ class SignalRConnector extends Component { const resource = body.resource; const status = resource.status; - // Both sucessful and failed commands need to be - // completed, otherwise they spin until they timeout. + // Both successful and failed commands need to be + // completed, otherwise they spin until they time out. if (status === 'completed' || status === 'failed') { this.props.dispatchFinishCommand(resource); @@ -160,6 +170,16 @@ class SignalRConnector extends Component { } }; + handleDownloadclient = ({ action, resource }) => { + const section = 'settings.downloadClients'; + + if (action === 'created' || action === 'updated') { + this.props.dispatchUpdateItem({ section, ...resource }); + } else if (action === 'deleted') { + this.props.dispatchRemoveItem({ section, id: resource.id }); + } + }; + handleHealth = () => { this.props.dispatchFetchHealth(); }; @@ -168,14 +188,33 @@ class SignalRConnector extends Component { this.props.dispatchFetchIndexerStatus(); }; - handleIndexer = (body) => { - const action = body.action; + handleIndexer = ({ action, resource }) => { const section = 'indexers'; - if (action === 'updated') { - this.props.dispatchUpdateItem({ section, ...body.resource }); + if (action === 'created' || action === 'updated') { + this.props.dispatchUpdateItem({ section, ...resource }); } else if (action === 'deleted') { - this.props.dispatchRemoveItem({ section, id: body.resource.id }); + this.props.dispatchRemoveItem({ section, id: resource.id }); + } + }; + + handleIndexerproxy = ({ action, resource }) => { + const section = 'settings.indexerProxies'; + + if (action === 'created' || action === 'updated') { + this.props.dispatchUpdateItem({ section, ...resource }); + } else if (action === 'deleted') { + this.props.dispatchRemoveItem({ section, id: resource.id }); + } + }; + + handleNotification = ({ action, resource }) => { + const section = 'settings.notifications'; + + if (action === 'created' || action === 'updated') { + this.props.dispatchUpdateItem({ section, ...resource }); + } else if (action === 'deleted') { + this.props.dispatchRemoveItem({ section, id: resource.id }); } }; diff --git a/src/Prowlarr.Api.V1/Applications/ApplicationController.cs b/src/Prowlarr.Api.V1/Applications/ApplicationController.cs index c3819d8f9..a63beedcf 100644 --- a/src/Prowlarr.Api.V1/Applications/ApplicationController.cs +++ b/src/Prowlarr.Api.V1/Applications/ApplicationController.cs @@ -1,4 +1,5 @@ using NzbDrone.Core.Applications; +using NzbDrone.SignalR; using Prowlarr.Http; namespace Prowlarr.Api.V1.Applications @@ -9,8 +10,8 @@ public class ApplicationController : ProviderControllerBase { - public IndexerController(IndexerFactory indexerFactory, + public IndexerController(IBroadcastSignalRMessage signalRBroadcaster, + IndexerFactory indexerFactory, IndexerResourceMapper resourceMapper, IndexerBulkResourceMapper bulkResourceMapper, AppProfileExistsValidator appProfileExistsValidator, DownloadClientExistsValidator downloadClientExistsValidator) - : base(indexerFactory, "indexer", resourceMapper, bulkResourceMapper) + : base(signalRBroadcaster, indexerFactory, "indexer", resourceMapper, bulkResourceMapper) { SharedValidator.RuleFor(c => c.AppProfileId).Cascade(CascadeMode.Stop) .ValidId() diff --git a/src/Prowlarr.Api.V1/Notifications/NotificationController.cs b/src/Prowlarr.Api.V1/Notifications/NotificationController.cs index b6aa8d99e..1520a8dd4 100644 --- a/src/Prowlarr.Api.V1/Notifications/NotificationController.cs +++ b/src/Prowlarr.Api.V1/Notifications/NotificationController.cs @@ -1,6 +1,7 @@ using System; using Microsoft.AspNetCore.Mvc; using NzbDrone.Core.Notifications; +using NzbDrone.SignalR; using Prowlarr.Http; namespace Prowlarr.Api.V1.Notifications @@ -11,8 +12,8 @@ public class NotificationController : ProviderControllerBase : RestController + public abstract class ProviderControllerBase : RestControllerWithSignalR, + IHandle>, + IHandle>, + IHandle> where TProviderDefinition : ProviderDefinition, new() where TBulkProviderResource : ProviderBulkResource, new() where TProvider : IProvider @@ -21,11 +28,13 @@ public abstract class ProviderControllerBase _resourceMapper; private readonly ProviderBulkResourceMapper _bulkResourceMapper; - protected ProviderControllerBase(IProviderFactory providerFactory, string resource, ProviderResourceMapper resourceMapper, ProviderBulkResourceMapper bulkResourceMapper) + : base(signalRBroadcaster) { _providerFactory = providerFactory; _resourceMapper = resourceMapper; @@ -244,6 +253,24 @@ public IActionResult RequestAction([FromRoute] string name, [FromBody] TProvider return Json(data); } + [NonAction] + public void Handle(ProviderAddedEvent message) + { + BroadcastResourceChange(ModelAction.Created, message.Definition.Id); + } + + [NonAction] + public void Handle(ProviderUpdatedEvent message) + { + BroadcastResourceChange(ModelAction.Updated, message.Definition.Id); + } + + [NonAction] + public void Handle(ProviderDeletedEvent message) + { + BroadcastResourceChange(ModelAction.Deleted, message.ProviderId); + } + private void Validate(TProviderDefinition definition, bool includeWarnings) { var validationResult = definition.Settings.Validate();