From da2c306a3185bb323b64cf95b3632c723a4ddda0 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Mon, 14 Feb 2022 20:58:44 +0000 Subject: [PATCH] feat: Applied the feature service on the backend --- .../Engine/V2/MovieRequestEngineTests.cs | 4 +- .../Rule/Request/AutoApproveRuleTests.cs | 17 +++++++- .../Request/ExistingMovieRequestRuleTests.cs | 33 ++++++++++++++- .../Rule/Search/ExistingRequestRuleTests.cs | 2 +- src/Ombi.Core/Engine/MovieRequestEngine.cs | 15 +++++-- .../Rule/Rules/Request/AutoApproveRule.cs | 40 ++++++++++--------- .../Rules/Request/ExistingMovieRequestRule.cs | 22 +++++----- src/Ombi.Core/Services/FeatureService.cs | 28 +++++++++++++ src/Ombi.DependencyInjection/IocExtensions.cs | 1 + 9 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 src/Ombi.Core/Services/FeatureService.cs diff --git a/src/Ombi.Core.Tests/Engine/V2/MovieRequestEngineTests.cs b/src/Ombi.Core.Tests/Engine/V2/MovieRequestEngineTests.cs index 3c651b167..d9f5b11be 100644 --- a/src/Ombi.Core.Tests/Engine/V2/MovieRequestEngineTests.cs +++ b/src/Ombi.Core.Tests/Engine/V2/MovieRequestEngineTests.cs @@ -9,6 +9,7 @@ using Ombi.Api.TheMovieDb; using Ombi.Core.Engine; using Ombi.Core.Models.Requests; using Ombi.Core.Rule.Interfaces; +using Ombi.Core.Services; using Ombi.Core.Settings; using Ombi.Helpers; using Ombi.Settings.Settings.Models; @@ -43,8 +44,9 @@ namespace Ombi.Core.Tests.Engine.V2 var ombiSettings = new Mock>(); var requestSubs = new Mock>(); var mediaCache = new Mock(); + var featureService = new Mock(); _engine = new MovieRequestEngine(movieApi.Object, requestService.Object, user.Object, notificationHelper.Object, rules.Object, movieSender.Object, - logger.Object, userManager.Object, requestLogRepo.Object, cache.Object, ombiSettings.Object, requestSubs.Object, mediaCache.Object); + logger.Object, userManager.Object, requestLogRepo.Object, cache.Object, ombiSettings.Object, requestSubs.Object, mediaCache.Object, featureService.Object); } [Test] diff --git a/src/Ombi.Core.Tests/Rule/Request/AutoApproveRuleTests.cs b/src/Ombi.Core.Tests/Rule/Request/AutoApproveRuleTests.cs index 41a7e14d7..9633249d4 100644 --- a/src/Ombi.Core.Tests/Rule/Request/AutoApproveRuleTests.cs +++ b/src/Ombi.Core.Tests/Rule/Request/AutoApproveRuleTests.cs @@ -10,6 +10,7 @@ using Ombi.Test.Common; using System.Collections.Generic; using Ombi.Store.Entities; using System; +using Ombi.Core.Services; namespace Ombi.Core.Tests.Rule.Request { @@ -28,15 +29,17 @@ namespace Ombi.Core.Tests.Rule.Request PrincipalMock = new Mock(); PrincipalMock.Setup(x => x.Identity.Name).Returns("abc"); + FeatureService = new Mock(); UserManager = MockHelper.MockUserManager(_users); - Rule = new AutoApproveRule(PrincipalMock.Object, UserManager.Object); + Rule = new AutoApproveRule(PrincipalMock.Object, UserManager.Object, FeatureService.Object); } private AutoApproveRule Rule { get; set; } private Mock PrincipalMock { get; set; } private Mock UserManager { get; set; } + private Mock FeatureService { get; set; } [Test] public async Task Should_ReturnSuccess_WhenAdminAndRequestMovie() @@ -137,5 +140,17 @@ namespace Ombi.Core.Tests.Rule.Request Assert.True(result.Success); Assert.False(request.Approved); } + + [Test] + public async Task Should_ReturnFail_When4kRequestAndFeatureNotEnabled() + { + UserManager.Setup(x => x.IsInRoleAsync(It.IsAny(), It.IsAny())).ReturnsAsync(false); + var request = new MovieRequests() { RequestType = Store.Entities.RequestType.Movie, Is4kRequest = true }; + var result = await Rule.Execute(request); + + Assert.True(result.Success); + Assert.False(request.Approved); + Assert.False(request.Approved4K); + } } } diff --git a/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs b/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs index 1d14fb069..682d4bf4d 100644 --- a/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs +++ b/src/Ombi.Core.Tests/Rule/Request/ExistingMovieRequestRuleTests.cs @@ -9,7 +9,9 @@ using NUnit.Framework; using Ombi.Core.Authentication; using Ombi.Core.Rule.Rules; using Ombi.Core.Rule.Rules.Request; +using Ombi.Core.Services; using Ombi.Helpers; +using Ombi.Settings.Settings.Models; using Ombi.Store.Entities; using Ombi.Store.Entities.Requests; using Ombi.Store.Repository.Requests; @@ -24,12 +26,14 @@ namespace Ombi.Core.Tests.Rule.Request public void Setup() { ContextMock = new Mock(); - Rule = new ExistingMovieRequestRule(ContextMock.Object); + FeatureService = new Mock(); + Rule = new ExistingMovieRequestRule(ContextMock.Object, FeatureService.Object); } private ExistingMovieRequestRule Rule { get; set; } private Mock ContextMock { get; set; } + private Mock FeatureService { get; set; } [Test] public async Task ExistingRequestRule_Movie_Has_Been_Requested_With_TheMovieDBId() @@ -125,6 +129,7 @@ namespace Ombi.Core.Tests.Rule.Request [Test] public async Task ExistingRequestRule_Movie_4K_Request() { + FeatureService.Setup(x => x.FeatureEnabled(FeatureNames.Movie4KRequests)).ReturnsAsync(true); ContextMock.Setup(x => x.GetAll()).Returns(new List { new MovieRequests @@ -146,5 +151,31 @@ namespace Ombi.Core.Tests.Rule.Request Assert.That(result.Success, Is.True); Assert.That(result.Message, Is.Null.Or.Empty); } + + [Test] + public async Task ExistingRequestRule_Movie_4K_Request_FeatureNotEnabled() + { + FeatureService.Setup(x => x.FeatureEnabled(FeatureNames.Movie4KRequests)).ReturnsAsync(false); + ContextMock.Setup(x => x.GetAll()).Returns(new List + { + new MovieRequests + { + TheMovieDbId = 2, + ImdbId = "2", + RequestType = RequestType.Movie, + Is4kRequest = false + } + }.AsQueryable().BuildMock().Object); + var o = new MovieRequests + { + TheMovieDbId = 2, + ImdbId = "1", + Is4kRequest = true + }; + var result = await Rule.Execute(o); + + Assert.That(result.Success, Is.False); + Assert.That(result.Message, Is.Not.Null); + } } } diff --git a/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs b/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs index c49b5d829..66601f936 100644 --- a/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs +++ b/src/Ombi.Core.Tests/Rule/Search/ExistingRequestRuleTests.cs @@ -30,7 +30,7 @@ namespace Ombi.Core.Tests.Rule.Search [Test] - public async Task ShouldBe_Requested_WhenExisitngMovie() + public async Task ShouldBe_Requested_WhenExistingMovie() { var list = new MovieRequests { diff --git a/src/Ombi.Core/Engine/MovieRequestEngine.cs b/src/Ombi.Core/Engine/MovieRequestEngine.cs index 39afaec16..2a29196b2 100644 --- a/src/Ombi.Core/Engine/MovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/MovieRequestEngine.cs @@ -22,6 +22,7 @@ using Ombi.Store.Entities.Requests; using Ombi.Store.Repository; using Ombi.Core.Models; using System.Threading; +using Ombi.Core.Services; namespace Ombi.Core.Engine { @@ -30,7 +31,8 @@ namespace Ombi.Core.Engine public MovieRequestEngine(IMovieDbApi movieApi, IRequestServiceMain requestService, IPrincipal user, INotificationHelper helper, IRuleEvaluator r, IMovieSender sender, ILogger log, OmbiUserManager manager, IRepository rl, ICacheService cache, - ISettingsService ombiSettings, IRepository sub, IMediaCacheService mediaCacheService) + ISettingsService ombiSettings, IRepository sub, IMediaCacheService mediaCacheService, + IFeatureService featureService) : base(user, requestService, r, manager, cache, ombiSettings, sub) { MovieApi = movieApi; @@ -39,6 +41,7 @@ namespace Ombi.Core.Engine Logger = log; _requestLog = rl; _mediaCacheService = mediaCacheService; + _featureService = featureService; } private IMovieDbApi MovieApi { get; } @@ -47,6 +50,7 @@ namespace Ombi.Core.Engine private ILogger Logger { get; } private readonly IRepository _requestLog; private readonly IMediaCacheService _mediaCacheService; + private readonly IFeatureService _featureService; /// /// Requests the movie. @@ -94,11 +98,14 @@ namespace Ombi.Core.Engine }; } + var is4kFeatureEnabled = await _featureService.FeatureEnabled(FeatureNames.Movie4KRequests); + var is4kRequest = is4kFeatureEnabled && model.Is4kRequest; + MovieRequests requestModel; bool isExisting = false; // Do we already have a request? 4k or non 4k var existingRequest = await MovieRepository.GetRequestAsync(movieInfo.Id); - if (existingRequest != null) + if (existingRequest != null && is4kFeatureEnabled) { if (model.Is4kRequest) { @@ -156,7 +163,7 @@ namespace Ombi.Core.Engine if (requestModel.Approved) // The rules have auto approved this { - var requestEngineResult = await AddMovieRequest(requestModel, fullMovieName, model.RequestOnBehalf, isExisting, model.Is4kRequest); + var requestEngineResult = await AddMovieRequest(requestModel, fullMovieName, model.RequestOnBehalf, isExisting, is4kRequest); if (requestEngineResult.Result) { var result = await ApproveMovie(requestModel, model.Is4kRequest); @@ -177,7 +184,7 @@ namespace Ombi.Core.Engine // If there are no providers then it's successful but movie has not been sent } - return await AddMovieRequest(requestModel, fullMovieName, model.RequestOnBehalf, isExisting, model.Is4kRequest); + return await AddMovieRequest(requestModel, fullMovieName, model.RequestOnBehalf, isExisting, is4kRequest); } diff --git a/src/Ombi.Core/Rule/Rules/Request/AutoApproveRule.cs b/src/Ombi.Core/Rule/Rules/Request/AutoApproveRule.cs index 30cda9591..92c1b11a7 100644 --- a/src/Ombi.Core/Rule/Rules/Request/AutoApproveRule.cs +++ b/src/Ombi.Core/Rule/Rules/Request/AutoApproveRule.cs @@ -5,7 +5,9 @@ using Microsoft.EntityFrameworkCore; using Ombi.Core.Authentication; using Ombi.Core.Models.Requests; using Ombi.Core.Rule.Interfaces; +using Ombi.Core.Services; using Ombi.Helpers; +using Ombi.Settings.Settings.Models; using Ombi.Store.Entities; using Ombi.Store.Entities.Requests; @@ -13,14 +15,16 @@ namespace Ombi.Core.Rule.Rules.Request { public class AutoApproveRule : BaseRequestRule, IRules { - public AutoApproveRule(IPrincipal principal, OmbiUserManager um) + public AutoApproveRule(IPrincipal principal, OmbiUserManager um, IFeatureService featureService) { User = principal; _manager = um; + _featureService = featureService; } private IPrincipal User { get; } private readonly OmbiUserManager _manager; + private readonly IFeatureService _featureService; public async Task Execute(BaseRequest obj) { @@ -28,17 +32,9 @@ namespace Ombi.Core.Rule.Rules.Request var user = await _manager.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == username); if (await _manager.IsInRoleAsync(user, OmbiRoles.Admin) || user.IsSystemUser) { - if (obj.RequestType == RequestType.Movie) + if (obj is MovieRequests movie) { - var movie = (MovieRequests)obj; - if (movie.Is4kRequest) - { - movie.Approved4K = true; - } - else - { - obj.Approved = true; - } + await Check4K(movie); } else { @@ -50,14 +46,7 @@ namespace Ombi.Core.Rule.Rules.Request if (obj.RequestType == RequestType.Movie && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveMovie)) { var movie = (MovieRequests)obj; - if (movie.Is4kRequest) - { - movie.Approved4K = true; - } - else - { - obj.Approved = true; - } + await Check4K(movie); } if (obj.RequestType == RequestType.TvShow && await _manager.IsInRoleAsync(user, OmbiRoles.AutoApproveTv)) obj.Approved = true; @@ -65,5 +54,18 @@ namespace Ombi.Core.Rule.Rules.Request obj.Approved = true; return Success(); // We don't really care, we just don't set the obj to approve } + + private async Task Check4K(MovieRequests movie) + { + var featureEnabled = await _featureService.FeatureEnabled(FeatureNames.Movie4KRequests); + if (movie.Is4kRequest && featureEnabled) + { + movie.Approved4K = true; + } + else + { + movie.Approved = true; + } + } } } \ No newline at end of file diff --git a/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs b/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs index ed5465c78..17533a570 100644 --- a/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs +++ b/src/Ombi.Core/Rule/Rules/Request/ExistingMovieRequestRule.cs @@ -1,21 +1,24 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Ombi.Core.Rule.Interfaces; using Ombi.Helpers; using Ombi.Store.Entities; using Ombi.Store.Entities.Requests; -using Ombi.Store.Repository; using Ombi.Core.Engine; using Ombi.Store.Repository.Requests; +using Ombi.Core.Services; +using Ombi.Settings.Settings.Models; namespace Ombi.Core.Rule.Rules.Request { public class ExistingMovieRequestRule : BaseRequestRule, IRules { - public ExistingMovieRequestRule(IMovieRequestRepository movie) + private readonly IFeatureService _featureService; + + public ExistingMovieRequestRule(IMovieRequestRepository movie, IFeatureService featureService) { Movie = movie; + _featureService = featureService; } private IMovieRequestRepository Movie { get; } @@ -35,7 +38,7 @@ namespace Ombi.Core.Rule.Rules.Request var existing = await movieRequests.FirstOrDefaultAsync(x => x.TheMovieDbId == movie.TheMovieDbId); if (existing != null) // Do we already have a request for this? { - found = Check4KRequests(movie, existing); + found = await Check4KRequests(movie, existing); } if (!found && movie.ImdbId.HasValue()) @@ -45,7 +48,7 @@ namespace Ombi.Core.Rule.Rules.Request x.ImdbId == movie.ImdbId); if (existing != null) { - found = Check4KRequests(movie, existing); + found = await Check4KRequests(movie, existing); } } if (found) @@ -56,13 +59,14 @@ namespace Ombi.Core.Rule.Rules.Request return Success(); } - private static bool Check4KRequests(MovieRequests movie,MovieRequests existing) + private async Task Check4KRequests(MovieRequests movie, MovieRequests existing) { - if (movie.Is4kRequest && existing.Has4KRequest) + var featureEnabled = await _featureService.FeatureEnabled(FeatureNames.Movie4KRequests); + if (movie.Is4kRequest && existing.Has4KRequest && featureEnabled) { return true; } - if (!movie.Is4kRequest && !existing.Has4KRequest) + if (!movie.Is4kRequest && !existing.Has4KRequest || !featureEnabled) { return true; } diff --git a/src/Ombi.Core/Services/FeatureService.cs b/src/Ombi.Core/Services/FeatureService.cs new file mode 100644 index 000000000..279c5c2c5 --- /dev/null +++ b/src/Ombi.Core/Services/FeatureService.cs @@ -0,0 +1,28 @@ +using Ombi.Core.Settings; +using Ombi.Settings.Settings.Models; +using System.Linq; +using System.Threading.Tasks; + +namespace Ombi.Core.Services +{ + public interface IFeatureService + { + Task FeatureEnabled(string featureName); + } + + public class FeatureService : IFeatureService + { + private readonly ISettingsService _featureSettings; + + public FeatureService(ISettingsService featureSettings) + { + _featureSettings = featureSettings; + } + + public async Task FeatureEnabled(string featureName) + { + var settings = await _featureSettings.GetSettingsAsync(); + return settings.Features?.Where(x => x.Name.Equals(featureName, System.StringComparison.InvariantCultureIgnoreCase)).Select(x => x.Enabled)?.FirstOrDefault() ?? false; + } + } +} diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index f81cd05da..3fb5cd643 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -224,6 +224,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddScoped(); } public static void RegisterJobs(this IServiceCollection services)