mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-08 06:00:50 -07:00
feat(request-limits): ✨ Updated the RequestLimit Rules to use the new refactored service
This commit is contained in:
parent
6adf75d0ec
commit
e31ee8d892
1 changed files with 26 additions and 45 deletions
|
@ -31,6 +31,7 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Ombi.Core.Authentication;
|
using Ombi.Core.Authentication;
|
||||||
using Ombi.Core.Rule.Interfaces;
|
using Ombi.Core.Rule.Interfaces;
|
||||||
|
using Ombi.Core.Services;
|
||||||
using Ombi.Store.Entities;
|
using Ombi.Store.Entities;
|
||||||
using Ombi.Store.Entities.Requests;
|
using Ombi.Store.Entities.Requests;
|
||||||
using Ombi.Store.Repository;
|
using Ombi.Store.Repository;
|
||||||
|
@ -39,45 +40,37 @@ namespace Ombi.Core.Rule.Rules.Request
|
||||||
{
|
{
|
||||||
public class RequestLimitRule : BaseRequestRule, IRules<BaseRequest>
|
public class RequestLimitRule : BaseRequestRule, IRules<BaseRequest>
|
||||||
{
|
{
|
||||||
public RequestLimitRule(IRepository<RequestLog> rl, OmbiUserManager um)
|
public RequestLimitRule(IRequestLimitService requestLimitService)
|
||||||
{
|
{
|
||||||
_requestLog = rl;
|
_requestLimitService = requestLimitService;
|
||||||
_userManager = um;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly IRepository<RequestLog> _requestLog;
|
private readonly IRequestLimitService _requestLimitService;
|
||||||
private readonly OmbiUserManager _userManager;
|
|
||||||
|
|
||||||
public async Task<RuleResult> Execute(BaseRequest obj)
|
public async Task<RuleResult> Execute(BaseRequest obj)
|
||||||
{
|
{
|
||||||
var user = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == obj.RequestedUserId);
|
|
||||||
|
|
||||||
var movieLimit = user.MovieRequestLimit;
|
|
||||||
var episodeLimit = user.EpisodeRequestLimit;
|
|
||||||
var musicLimit = user.MusicRequestLimit;
|
|
||||||
|
|
||||||
var requestLog = _requestLog.GetAll().Where(x => x.UserId == obj.RequestedUserId);
|
|
||||||
if (obj.RequestType == RequestType.Movie)
|
if (obj.RequestType == RequestType.Movie)
|
||||||
{
|
{
|
||||||
if (movieLimit <= 0)
|
var remainingLimitsModel = await _requestLimitService.GetRemainingMovieRequests();
|
||||||
|
if (!remainingLimitsModel.HasLimit)
|
||||||
|
{
|
||||||
return Success();
|
return Success();
|
||||||
|
}
|
||||||
|
|
||||||
var movieLogs = requestLog.Where(x => x.RequestType == RequestType.Movie);
|
if (remainingLimitsModel.Remaining < 1)
|
||||||
|
|
||||||
// Count how many requests in the past 7 days
|
|
||||||
var count = await movieLogs.CountAsync(x => x.RequestDate >= DateTime.UtcNow.AddDays(-7));
|
|
||||||
count += 1; // Since we are including this request
|
|
||||||
if (count > movieLimit)
|
|
||||||
{
|
{
|
||||||
return Fail("You have exceeded your Movie request quota!");
|
return Fail("You have exceeded your Movie request quota!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (obj.RequestType == RequestType.TvShow)
|
if (obj.RequestType == RequestType.TvShow)
|
||||||
|
{
|
||||||
|
var remainingLimitsModel = await _requestLimitService.GetRemainingTvRequests();
|
||||||
|
if (!remainingLimitsModel.HasLimit)
|
||||||
{
|
{
|
||||||
if (episodeLimit <= 0)
|
|
||||||
return Success();
|
return Success();
|
||||||
|
}
|
||||||
|
|
||||||
var child = (ChildRequests) obj;
|
var child = (ChildRequests)obj;
|
||||||
var requestCount = 0;
|
var requestCount = 0;
|
||||||
// Get the count of requests to be made
|
// Get the count of requests to be made
|
||||||
foreach (var s in child.SeasonRequests)
|
foreach (var s in child.SeasonRequests)
|
||||||
|
@ -85,32 +78,20 @@ namespace Ombi.Core.Rule.Rules.Request
|
||||||
requestCount += s.Episodes.Count;
|
requestCount += s.Episodes.Count;
|
||||||
}
|
}
|
||||||
|
|
||||||
var tvLogs = requestLog.Where(x => x.RequestType == RequestType.TvShow);
|
if ((remainingLimitsModel.Remaining - requestCount) < 0)
|
||||||
|
|
||||||
// Count how many requests in the past 7 days
|
|
||||||
var tv = tvLogs.Where(x => x.RequestDate >= DateTime.UtcNow.AddDays(-7));
|
|
||||||
|
|
||||||
// Needed, due to a bug which would cause all episode counts to be 0
|
|
||||||
var zeroEpisodeCount = await tv.Where(x => x.EpisodeCount == 0).Select(x => x.EpisodeCount).CountAsync();
|
|
||||||
|
|
||||||
var episodeCount = await tv.Where(x => x.EpisodeCount != 0).Select(x => x.EpisodeCount).SumAsync();
|
|
||||||
|
|
||||||
var count = requestCount + episodeCount + zeroEpisodeCount; // Add the amount of requests in
|
|
||||||
if (count > episodeLimit)
|
|
||||||
{
|
{
|
||||||
return Fail("You have exceeded your Episode request quota!");
|
return Fail("You have exceeded your Episode request quota!");
|
||||||
}
|
}
|
||||||
} else if (obj.RequestType == RequestType.Album)
|
}
|
||||||
|
if (obj.RequestType == RequestType.Album)
|
||||||
|
{
|
||||||
|
var remainingLimitsModel = await _requestLimitService.GetRemainingMusicRequests();
|
||||||
|
if (!remainingLimitsModel.HasLimit)
|
||||||
{
|
{
|
||||||
if (musicLimit <= 0)
|
|
||||||
return Success();
|
return Success();
|
||||||
|
}
|
||||||
|
|
||||||
var albumLogs = requestLog.Where(x => x.RequestType == RequestType.Album);
|
if (remainingLimitsModel.Remaining < 1)
|
||||||
|
|
||||||
// Count how many requests in the past 7 days
|
|
||||||
var count = await albumLogs.CountAsync(x => x.RequestDate >= DateTime.UtcNow.AddDays(-7));
|
|
||||||
count += 1; // Since we are including this request
|
|
||||||
if (count > musicLimit)
|
|
||||||
{
|
{
|
||||||
return Fail("You have exceeded your Album request quota!");
|
return Fail("You have exceeded your Album request quota!");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue