diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 506ac3a62..5f093a928 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -112,6 +112,7 @@ + diff --git a/PlexRequests.Core/RequestHelper.cs b/PlexRequests.Core/RequestHelper.cs new file mode 100644 index 000000000..0e99df6ca --- /dev/null +++ b/PlexRequests.Core/RequestHelper.cs @@ -0,0 +1,82 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: RequestHelper.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using PlexRequests.Core.SettingModels; +using PlexRequests.Store; + +namespace PlexRequests.Core +{ + public static class RequestHelper + { + public static bool ShouldAutoApprove(this RequestType requestType, PlexRequestSettings prSettings, bool isAdmin, string username) + { + // if the user is an admin or they are whitelisted, they go ahead and allow auto-approval + if (isAdmin || prSettings.ApprovalWhiteList.Any(x => x.Equals(username, StringComparison.OrdinalIgnoreCase))) return true; + + // check by request type if the category requires approval or not + switch (requestType) + { + case RequestType.Movie: + return !prSettings.RequireMovieApproval; + case RequestType.TvShow: + return !prSettings.RequireTvShowApproval; + case RequestType.Album: + return !prSettings.RequireMusicApproval; + default: + return false; + } + } + + public static bool ShouldAutoApprove(this RequestType requestType, PlexRequestSettings prSettings, bool isAdmin, List username) + { + // if the user is an admin or they are whitelisted, they go ahead and allow auto-approval + if (isAdmin) return true; + + + if(prSettings.ApprovalWhiteList.Intersect(username).Any()) + { + return true; + } + + // check by request type if the category requires approval or not + switch (requestType) + { + case RequestType.Movie: + return !prSettings.RequireMovieApproval; + case RequestType.TvShow: + return !prSettings.RequireTvShowApproval; + case RequestType.Album: + return !prSettings.RequireMusicApproval; + default: + return false; + } + } + } +} \ No newline at end of file diff --git a/PlexRequests.Services/Jobs/FaultQueueHandler.cs b/PlexRequests.Services/Jobs/FaultQueueHandler.cs index 9238a12ec..f30a4f2d9 100644 --- a/PlexRequests.Services/Jobs/FaultQueueHandler.cs +++ b/PlexRequests.Services/Jobs/FaultQueueHandler.cs @@ -37,6 +37,7 @@ using PlexRequests.Api.Interfaces; using PlexRequests.Core; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; +using PlexRequests.Helpers.Permissions; using PlexRequests.Services.Interfaces; using PlexRequests.Store; using PlexRequests.Store.Models; @@ -53,7 +54,7 @@ namespace PlexRequests.Services.Jobs public FaultQueueHandler(IJobRecord record, IRepository repo, ISonarrApi sonarrApi, ISickRageApi srApi, ISettingsService sonarrSettings, ISettingsService srSettings, ICouchPotatoApi cpApi, ISettingsService cpsettings, IRequestService requestService, - ISettingsService hpSettings, IHeadphonesApi headphonesApi) + ISettingsService hpSettings, IHeadphonesApi headphonesApi, ISettingsService prSettings) { Record = record; Repo = repo; @@ -68,6 +69,7 @@ namespace PlexRequests.Services.Jobs SonarrSettings = sonarrSettings; CpSettings = cpsettings; HeadphoneSettings = hpSettings; + PrSettings = prSettings.GetSettings(); } private IRepository Repo { get; } @@ -77,6 +79,7 @@ namespace PlexRequests.Services.Jobs private ICouchPotatoApi CpApi { get; } private IHeadphonesApi HpApi { get; } private IRequestService RequestService { get; } + private PlexRequestSettings PrSettings { get; } private ISettingsService SonarrSettings { get; } private ISettingsService SickrageSettings { get; } private ISettingsService CpSettings { get; } @@ -87,7 +90,7 @@ namespace PlexRequests.Services.Jobs try { var faultedRequests = Repo.GetAll().ToList(); - + var missingInfo = faultedRequests.Where(x => x.FaultType == FaultType.MissingInformation).ToList(); ProcessMissingInformation(missingInfo); @@ -108,13 +111,14 @@ namespace PlexRequests.Services.Jobs private void ProcessMissingInformation(List requests) { - var sonarrSettings = SonarrSettings.GetSettings(); - var sickrageSettings = SickrageSettings.GetSettings(); - if (!requests.Any()) { return; } + + var sonarrSettings = SonarrSettings.GetSettings(); + var sickrageSettings = SickrageSettings.GetSettings(); + var tv = requests.Where(x => x.Type == RequestType.TvShow); // TV @@ -122,7 +126,7 @@ namespace PlexRequests.Services.Jobs foreach (var t in tv) { var providerId = int.Parse(t.PrimaryIdentifier); - var showInfo = tvApi.ShowLookupByTheTvDbId(providerId); + var showInfo = tvApi.ShowLookup(providerId); if (showInfo.externals?.thetvdb != null) { @@ -227,8 +231,10 @@ namespace PlexRequests.Services.Jobs if (result) { - // Approve it now - model.Approved = true; + + if (model.Type.ShouldAutoApprove(PrSettings, false, model.RequestedUsers)) + // Approve it now + model.Approved = true; RequestService.UpdateRequest(model); }; @@ -258,7 +264,7 @@ namespace PlexRequests.Services.Jobs foreach (var request in requests) { var model = ByteConverterHelper.ReturnObject(request.Content); - var result = false; + bool result; switch (request.Type) { case RequestType.Movie: diff --git a/PlexRequests.UI/Jobs/Scheduler.cs b/PlexRequests.UI/Jobs/Scheduler.cs index 5ea4ba540..8127abf9a 100644 --- a/PlexRequests.UI/Jobs/Scheduler.cs +++ b/PlexRequests.UI/Jobs/Scheduler.cs @@ -226,7 +226,8 @@ namespace PlexRequests.UI.Jobs var fault = TriggerBuilder.Create() .WithIdentity("FaultQueueHandler", "Fault") - .StartAt(DateBuilder.FutureDate(10, IntervalUnit.Minute)) + //.StartAt(DateBuilder.FutureDate(10, IntervalUnit.Minute)) + .StartNow() .WithSimpleSchedule(x => x.WithIntervalInHours(s.FaultQueueHandler).RepeatForever()) .Build(); diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 0e72cf9c9..eb7c13960 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -878,7 +878,7 @@ namespace PlexRequests.UI.Modules try { - if (ShouldAutoApprove(RequestType.TvShow, settings)) + if (RequestType.TvShow.ShouldAutoApprove(settings, IsAdmin, Username)) { model.Approved = true; var s = await sonarrSettings; @@ -974,7 +974,7 @@ namespace PlexRequests.UI.Modules private bool ShouldSendNotification(RequestType type, PlexRequestSettings prSettings) { - var sendNotification = ShouldAutoApprove(type, prSettings) + var sendNotification = type.ShouldAutoApprove(prSettings, IsAdmin, Username) ? !prSettings.IgnoreNotifyForAutoApprovedRequests : true; var claims = Context.CurrentUser?.Claims; @@ -1073,7 +1073,7 @@ namespace PlexRequests.UI.Modules try { - if (ShouldAutoApprove(RequestType.Album, settings)) + if (RequestType.Album.ShouldAutoApprove(settings, IsAdmin, Username)) { model.Approved = true; var hpSettings = HeadphonesService.GetSettings(); @@ -1127,25 +1127,6 @@ namespace PlexRequests.UI.Modules return img; } - private bool ShouldAutoApprove(RequestType requestType, PlexRequestSettings prSettings) - { - // if the user is an admin or they are whitelisted, they go ahead and allow auto-approval - if (IsAdmin || prSettings.ApprovalWhiteList.Any(x => x.Equals(Username, StringComparison.OrdinalIgnoreCase))) return true; - - // check by request type if the category requires approval or not - switch (requestType) - { - case RequestType.Movie: - return !prSettings.RequireMovieApproval; - case RequestType.TvShow: - return !prSettings.RequireTvShowApproval; - case RequestType.Album: - return !prSettings.RequireMusicApproval; - default: - return false; - } - } - private async Task NotifyUser(bool notify) { Analytics.TrackEventAsync(Category.Search, Action.Save, "NotifyUser", Username, CookieHelper.GetAnalyticClientId(Cookies), notify ? 1 : 0);