From 0e5f45a4578b48e0a890031b70051f6cef20f032 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 2 Sep 2021 21:12:13 +0100 Subject: [PATCH] Send signalr message for episode monitored flag changes (cherry picked from commit 9e81d41f262fb1f9d798374673b0a0427bf1a6e3) Closes #1049 --- .../Books/Repositories/BookRepository.cs | 2 ++ src/NzbDrone.Core/Datastore/BasicRepository.cs | 16 ++++++++-------- src/NzbDrone.Core/Datastore/Events/ModelEvent.cs | 11 ++++++++++- src/Readarr.Api.V1/Books/BookController.cs | 9 +++++++++ .../Books/BookControllerWithSignalR.cs | 7 +++++++ .../REST/RestControllerWithSignalR.cs | 9 +++++++-- 6 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/NzbDrone.Core/Books/Repositories/BookRepository.cs b/src/NzbDrone.Core/Books/Repositories/BookRepository.cs index d70c69eb8..a7b8f79ce 100644 --- a/src/NzbDrone.Core/Books/Repositories/BookRepository.cs +++ b/src/NzbDrone.Core/Books/Repositories/BookRepository.cs @@ -176,6 +176,8 @@ public void SetMonitoredFlat(Book book, bool monitored) { book.Monitored = monitored; SetFields(book, p => p.Monitored); + + ModelUpdated(book, true); } public void SetMonitored(IEnumerable ids, bool monitored) diff --git a/src/NzbDrone.Core/Datastore/BasicRepository.cs b/src/NzbDrone.Core/Datastore/BasicRepository.cs index dc6f62c70..b9d3ea400 100644 --- a/src/NzbDrone.Core/Datastore/BasicRepository.cs +++ b/src/NzbDrone.Core/Datastore/BasicRepository.cs @@ -473,24 +473,24 @@ protected int GetPagedRecordCount(SqlBuilder builder, PagingSpec pagingS } } - protected void ModelCreated(TModel model) + protected void ModelCreated(TModel model, bool forcePublish = false) { - PublishModelEvent(model, ModelAction.Created); + PublishModelEvent(model, ModelAction.Created, forcePublish); } - protected void ModelUpdated(TModel model) + protected void ModelUpdated(TModel model, bool forcePublish = false) { - PublishModelEvent(model, ModelAction.Updated); + PublishModelEvent(model, ModelAction.Updated, forcePublish); } - protected void ModelDeleted(TModel model) + protected void ModelDeleted(TModel model, bool forcePublish = false) { - PublishModelEvent(model, ModelAction.Deleted); + PublishModelEvent(model, ModelAction.Deleted, forcePublish); } - private void PublishModelEvent(TModel model, ModelAction action) + private void PublishModelEvent(TModel model, ModelAction action, bool forcePublish) { - if (PublishModelEvents) + if (PublishModelEvents || forcePublish) { _eventAggregator.PublishEvent(new ModelEvent(model, action)); } diff --git a/src/NzbDrone.Core/Datastore/Events/ModelEvent.cs b/src/NzbDrone.Core/Datastore/Events/ModelEvent.cs index d5b02c8cb..1fecff945 100644 --- a/src/NzbDrone.Core/Datastore/Events/ModelEvent.cs +++ b/src/NzbDrone.Core/Datastore/Events/ModelEvent.cs @@ -1,14 +1,23 @@ -using NzbDrone.Common.Messaging; +using NzbDrone.Common.Messaging; namespace NzbDrone.Core.Datastore.Events { public class ModelEvent : IEvent + where TModel : ModelBase { + public int ModelId { get; set; } public TModel Model { get; set; } public ModelAction Action { get; set; } + public ModelEvent(int modelId, ModelAction action) + { + ModelId = modelId; + Action = action; + } + public ModelEvent(TModel model, ModelAction action) { + ModelId = model.Id; Model = model; Action = action; } diff --git a/src/Readarr.Api.V1/Books/BookController.cs b/src/Readarr.Api.V1/Books/BookController.cs index bfc82429b..213d99532 100644 --- a/src/Readarr.Api.V1/Books/BookController.cs +++ b/src/Readarr.Api.V1/Books/BookController.cs @@ -174,6 +174,15 @@ public IActionResult SetBooksMonitored([FromBody]BooksMonitoredResource resource { _bookService.SetMonitored(resource.BookIds, resource.Monitored); + if (resource.BookIds.Count == 1) + { + _bookService.SetBookMonitored(resource.BookIds.First(), resource.Monitored); + } + else + { + _bookService.SetMonitored(resource.BookIds, resource.Monitored); + } + return Accepted(MapToResource(_bookService.GetBooks(resource.BookIds), false)); } diff --git a/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs b/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs index 8b20ee27d..ca2643137 100644 --- a/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs +++ b/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs @@ -40,6 +40,13 @@ public override BookResource GetResourceById(int id) return resource; } + protected override BookResource GetResourceByIdForBroadcast(int id) + { + var book = _bookService.GetBook(id); + var resource = MapToResource(book, false); + return resource; + } + protected BookResource MapToResource(Book book, bool includeAuthor) { var resource = book.ToResource(); diff --git a/src/Readarr.Http/REST/RestControllerWithSignalR.cs b/src/Readarr.Http/REST/RestControllerWithSignalR.cs index 0ba3ab343..bc56b42e7 100644 --- a/src/Readarr.Http/REST/RestControllerWithSignalR.cs +++ b/src/Readarr.Http/REST/RestControllerWithSignalR.cs @@ -29,6 +29,11 @@ protected RestControllerWithSignalR(IBroadcastSignalRMessage signalRBroadcaster) } } + protected virtual TResource GetResourceByIdForBroadcast(int id) + { + return GetResourceById(id); + } + [NonAction] public void Handle(ModelEvent message) { @@ -42,7 +47,7 @@ public void Handle(ModelEvent message) BroadcastResourceChange(message.Action); } - BroadcastResourceChange(message.Action, message.Model.Id); + BroadcastResourceChange(message.Action, message.ModelId); } protected void BroadcastResourceChange(ModelAction action, int id) @@ -58,7 +63,7 @@ protected void BroadcastResourceChange(ModelAction action, int id) } else { - var resource = GetResourceById(id); + var resource = GetResourceByIdForBroadcast(id); BroadcastResourceChange(action, resource); } }