mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-15 01:32:55 -07:00
Rules #865
This commit is contained in:
parent
fc3ed1105d
commit
7f2cfdafd1
10 changed files with 149 additions and 10 deletions
|
@ -32,5 +32,7 @@ namespace Ombi.Core.Claims
|
||||||
public const string AutoApproveMovie = nameof(AutoApproveMovie);
|
public const string AutoApproveMovie = nameof(AutoApproveMovie);
|
||||||
public const string AutoApproveTv = nameof(AutoApproveTv);
|
public const string AutoApproveTv = nameof(AutoApproveTv);
|
||||||
public const string PowerUser = nameof(PowerUser);
|
public const string PowerUser = nameof(PowerUser);
|
||||||
|
public const string RequestTv = nameof(RequestTv);
|
||||||
|
public const string RequestMovie = nameof(RequestMovie);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -13,24 +13,27 @@ using Ombi.Store.Entities;
|
||||||
using Ombi.Helpers;
|
using Ombi.Helpers;
|
||||||
using Ombi.Notifications;
|
using Ombi.Notifications;
|
||||||
using Ombi.Notifications.Models;
|
using Ombi.Notifications.Models;
|
||||||
|
using Ombi.Core.Rules;
|
||||||
|
|
||||||
namespace Ombi.Core.Engine
|
namespace Ombi.Core.Engine
|
||||||
{
|
{
|
||||||
public class TvRequestEngine : BaseMediaEngine, ITvRequestEngine
|
public class TvRequestEngine : BaseMediaEngine, ITvRequestEngine
|
||||||
{
|
{
|
||||||
public TvRequestEngine(ITvMazeApi tvApi, IRequestServiceMain requestService, IPrincipal user, INotificationService notificationService, IMapper map) : base(user, requestService)
|
public TvRequestEngine(ITvMazeApi tvApi, IRequestServiceMain requestService, IPrincipal user, INotificationService notificationService, IMapper map,
|
||||||
|
IRuleEvaluator rule) : base(user, requestService)
|
||||||
{
|
{
|
||||||
TvApi = tvApi;
|
TvApi = tvApi;
|
||||||
NotificationService = notificationService;
|
NotificationService = notificationService;
|
||||||
Mapper = map;
|
Mapper = map;
|
||||||
|
Rules = rule;
|
||||||
}
|
}
|
||||||
private INotificationService NotificationService { get; }
|
private INotificationService NotificationService { get; }
|
||||||
private ITvMazeApi TvApi { get; }
|
private ITvMazeApi TvApi { get; }
|
||||||
private IMapper Mapper { get; }
|
private IMapper Mapper { get; }
|
||||||
|
private IRuleEvaluator Rules { get; }
|
||||||
|
|
||||||
public async Task<RequestEngineResult> RequestTvShow(SearchTvShowViewModel tv)
|
public async Task<RequestEngineResult> RequestTvShow(SearchTvShowViewModel tv)
|
||||||
{
|
{
|
||||||
|
|
||||||
var showInfo = await TvApi.ShowLookupByTheTvDbId(tv.Id);
|
var showInfo = await TvApi.ShowLookupByTheTvDbId(tv.Id);
|
||||||
DateTime.TryParse(showInfo.premiered, out DateTime firstAir);
|
DateTime.TryParse(showInfo.premiered, out DateTime firstAir);
|
||||||
|
|
||||||
|
@ -68,7 +71,7 @@ namespace Ombi.Core.Engine
|
||||||
ImdbId = showInfo.externals?.imdb ?? string.Empty,
|
ImdbId = showInfo.externals?.imdb ?? string.Empty,
|
||||||
TvDbId = tv.Id.ToString(),
|
TvDbId = tv.Id.ToString(),
|
||||||
ProviderId = tv.Id,
|
ProviderId = tv.Id,
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
model.ChildRequests.Add(childRequest);
|
model.ChildRequests.Add(childRequest);
|
||||||
|
@ -120,6 +123,15 @@ namespace Ombi.Core.Engine
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var ruleResults = Rules.StartRequestRules(model);
|
||||||
|
if (ruleResults.Any(x => !x.Success))
|
||||||
|
{
|
||||||
|
return new RequestEngineResult()
|
||||||
|
{
|
||||||
|
ErrorMessage = ruleResults.FirstOrDefault(x => !string.IsNullOrEmpty(x.Message)).Message,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
var existingRequest = await TvRequestService.CheckRequestAsync(model.Id);
|
var existingRequest = await TvRequestService.CheckRequestAsync(model.Id);
|
||||||
if (existingRequest != null)
|
if (existingRequest != null)
|
||||||
{
|
{
|
||||||
|
@ -146,7 +158,7 @@ namespace Ombi.Core.Engine
|
||||||
var allRequests = await TvRequestService.GetAllAsync();
|
var allRequests = await TvRequestService.GetAllAsync();
|
||||||
var results = allRequests.FirstOrDefault(x => x.Id == request.Id);
|
var results = allRequests.FirstOrDefault(x => x.Id == request.Id);
|
||||||
results = Mapper.Map<TvRequestModel>(request);
|
results = Mapper.Map<TvRequestModel>(request);
|
||||||
|
|
||||||
var model = TvRequestService.UpdateRequest(results);
|
var model = TvRequestService.UpdateRequest(results);
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,11 +7,7 @@ using Ombi.Store.Entities;
|
||||||
namespace Ombi.Core.Models.Requests
|
namespace Ombi.Core.Models.Requests
|
||||||
{
|
{
|
||||||
public class BaseRequestModel : Entity
|
public class BaseRequestModel : Entity
|
||||||
{
|
{
|
||||||
public BaseRequestModel()
|
|
||||||
{
|
|
||||||
RequestedUsers = new List<string>();
|
|
||||||
}
|
|
||||||
public int ProviderId { get; set; }
|
public int ProviderId { get; set; }
|
||||||
public string Overview { get; set; }
|
public string Overview { get; set; }
|
||||||
public string Title { get; set; }
|
public string Title { get; set; }
|
||||||
|
@ -26,7 +22,7 @@ namespace Ombi.Core.Models.Requests
|
||||||
public IssueState Issues { get; set; }
|
public IssueState Issues { get; set; }
|
||||||
public string OtherMessage { get; set; }
|
public string OtherMessage { get; set; }
|
||||||
public string AdminNote { get; set; }
|
public string AdminNote { get; set; }
|
||||||
public List<string> RequestedUsers { get; set; }
|
public List<string> RequestedUsers { get; set; } = new List<string>();
|
||||||
public int IssueId { get; set; }
|
public int IssueId { get; set; }
|
||||||
public bool Denied { get; set; }
|
public bool Denied { get; set; }
|
||||||
public string DeniedReason { get; set; }
|
public string DeniedReason { get; set; }
|
||||||
|
|
17
src/Ombi.Core/Rule/BaseRule.cs
Normal file
17
src/Ombi.Core/Rule/BaseRule.cs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
using Ombi.Core.Rules;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Rule
|
||||||
|
{
|
||||||
|
public abstract class BaseRule
|
||||||
|
{
|
||||||
|
public RuleResult Success()
|
||||||
|
{
|
||||||
|
return new RuleResult { Success = true };
|
||||||
|
}
|
||||||
|
|
||||||
|
public RuleResult Fail(string message)
|
||||||
|
{
|
||||||
|
return new RuleResult { Message = message };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
src/Ombi.Core/Rule/IRequestRules.cs
Normal file
9
src/Ombi.Core/Rule/IRequestRules.cs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
using Ombi.Core.Models.Requests;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Rules
|
||||||
|
{
|
||||||
|
public interface IRequestRules<T> where T : BaseRequestModel
|
||||||
|
{
|
||||||
|
RuleResult Execute(T obj);
|
||||||
|
}
|
||||||
|
}
|
10
src/Ombi.Core/Rule/IRuleEvaluator.cs
Normal file
10
src/Ombi.Core/Rule/IRuleEvaluator.cs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Ombi.Core.Models.Requests;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Rules
|
||||||
|
{
|
||||||
|
public interface IRuleEvaluator
|
||||||
|
{
|
||||||
|
IEnumerable<RuleResult> StartRequestRules(BaseRequestModel obj);
|
||||||
|
}
|
||||||
|
}
|
43
src/Ombi.Core/Rule/RuleEvaluator.cs
Normal file
43
src/Ombi.Core/Rule/RuleEvaluator.cs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
using Ombi.Core.Models.Requests;
|
||||||
|
using Ombi.Core.Rule;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Rules
|
||||||
|
{
|
||||||
|
public class RuleEvaluator : IRuleEvaluator
|
||||||
|
{
|
||||||
|
public RuleEvaluator(IServiceProvider provider)
|
||||||
|
{
|
||||||
|
RequestRules = new List<IRequestRules<BaseRequestModel>>();
|
||||||
|
var baseType = typeof(BaseRule).FullName;
|
||||||
|
|
||||||
|
System.Reflection.Assembly ass = typeof(RuleEvaluator).GetTypeInfo().Assembly;
|
||||||
|
|
||||||
|
foreach (TypeInfo ti in ass.DefinedTypes)
|
||||||
|
{
|
||||||
|
if (ti?.BaseType?.FullName == baseType)
|
||||||
|
{
|
||||||
|
var type = ti.GetType();
|
||||||
|
var item = Activator.CreateInstance(ti.GetType(), provider.GetService(type));// ti.GetType is wrong
|
||||||
|
RequestRules.Add((IRequestRules<BaseRequestModel>)item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<IRequestRules<BaseRequestModel>> RequestRules { get; }
|
||||||
|
|
||||||
|
public IEnumerable<RuleResult> StartRequestRules(BaseRequestModel obj)
|
||||||
|
{
|
||||||
|
var results = new List<RuleResult>();
|
||||||
|
foreach (var rule in RequestRules)
|
||||||
|
{
|
||||||
|
var result = rule.Execute(obj);
|
||||||
|
results.Add(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
src/Ombi.Core/Rule/RuleResult.cs
Normal file
12
src/Ombi.Core/Rule/RuleResult.cs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Rules
|
||||||
|
{
|
||||||
|
public class RuleResult
|
||||||
|
{
|
||||||
|
public bool Success { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
}
|
||||||
|
}
|
36
src/Ombi.Core/Rule/Rules/CanRequestRule.cs
Normal file
36
src/Ombi.Core/Rule/Rules/CanRequestRule.cs
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
using Ombi.Core.Claims;
|
||||||
|
using Ombi.Core.Models.Requests;
|
||||||
|
using Ombi.Core.Rules;
|
||||||
|
using System.Security.Principal;
|
||||||
|
|
||||||
|
namespace Ombi.Core.Rule.Rules
|
||||||
|
{
|
||||||
|
public class CanRequestRule<T> : BaseRule where T : BaseRequestModel, IRequestRules<T>
|
||||||
|
{
|
||||||
|
public CanRequestRule(IPrincipal principal)
|
||||||
|
{
|
||||||
|
User = principal;
|
||||||
|
}
|
||||||
|
|
||||||
|
private IPrincipal User { get; }
|
||||||
|
public RuleResult Execute(T obj)
|
||||||
|
{
|
||||||
|
if (obj.Type == Store.Entities.RequestType.Movie)
|
||||||
|
{
|
||||||
|
if (User.IsInRole(OmbiClaims.RequestMovie))
|
||||||
|
{
|
||||||
|
return Success();
|
||||||
|
}
|
||||||
|
return Fail("You do not have permissions to Request a Movie");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (User.IsInRole(OmbiClaims.RequestTv))
|
||||||
|
{
|
||||||
|
return Success();
|
||||||
|
}
|
||||||
|
return Fail("You do not have permissions to Request a Movie");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,7 @@ using Ombi.Schedule.Jobs;
|
||||||
using Ombi.Settings.Settings;
|
using Ombi.Settings.Settings;
|
||||||
using Ombi.Store.Context;
|
using Ombi.Store.Context;
|
||||||
using Ombi.Store.Repository;
|
using Ombi.Store.Repository;
|
||||||
|
using Ombi.Core.Rules;
|
||||||
|
|
||||||
namespace Ombi.DependencyInjection
|
namespace Ombi.DependencyInjection
|
||||||
{
|
{
|
||||||
|
@ -43,6 +44,7 @@ namespace Ombi.DependencyInjection
|
||||||
services.AddTransient<IMovieRequestEngine, MovieRequestEngine>();
|
services.AddTransient<IMovieRequestEngine, MovieRequestEngine>();
|
||||||
services.AddTransient<ITvRequestEngine, TvRequestEngine>();
|
services.AddTransient<ITvRequestEngine, TvRequestEngine>();
|
||||||
services.AddTransient<ITvSearchEngine, TvSearchEngine>();
|
services.AddTransient<ITvSearchEngine, TvSearchEngine>();
|
||||||
|
services.AddTransient<IRuleEvaluator, RuleEvaluator>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void RegisterApi(this IServiceCollection services)
|
public static void RegisterApi(this IServiceCollection services)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue