|
|
|
@ -31,6 +31,7 @@ using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Ombi.Core.Authentication;
|
|
|
|
|
using Ombi.Core.Rule.Interfaces;
|
|
|
|
|
using Ombi.Core.Services;
|
|
|
|
|
using Ombi.Store.Entities;
|
|
|
|
|
using Ombi.Store.Entities.Requests;
|
|
|
|
|
using Ombi.Store.Repository;
|
|
|
|
@ -39,45 +40,37 @@ namespace Ombi.Core.Rule.Rules.Request
|
|
|
|
|
{
|
|
|
|
|
public class RequestLimitRule : BaseRequestRule, IRules<BaseRequest>
|
|
|
|
|
{
|
|
|
|
|
public RequestLimitRule(IRepository<RequestLog> rl, OmbiUserManager um)
|
|
|
|
|
public RequestLimitRule(IRequestLimitService requestLimitService)
|
|
|
|
|
{
|
|
|
|
|
_requestLog = rl;
|
|
|
|
|
_userManager = um;
|
|
|
|
|
_requestLimitService = requestLimitService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly IRepository<RequestLog> _requestLog;
|
|
|
|
|
private readonly OmbiUserManager _userManager;
|
|
|
|
|
private readonly IRequestLimitService _requestLimitService;
|
|
|
|
|
|
|
|
|
|
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 (movieLimit <= 0)
|
|
|
|
|
var remainingLimitsModel = await _requestLimitService.GetRemainingMovieRequests();
|
|
|
|
|
if (!remainingLimitsModel.HasLimit)
|
|
|
|
|
{
|
|
|
|
|
return Success();
|
|
|
|
|
|
|
|
|
|
var movieLogs = requestLog.Where(x => x.RequestType == RequestType.Movie);
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (remainingLimitsModel.Remaining < 1)
|
|
|
|
|
{
|
|
|
|
|
return Fail("You have exceeded your Movie request quota!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (obj.RequestType == RequestType.TvShow)
|
|
|
|
|
if (obj.RequestType == RequestType.TvShow)
|
|
|
|
|
{
|
|
|
|
|
if (episodeLimit <= 0)
|
|
|
|
|
var remainingLimitsModel = await _requestLimitService.GetRemainingTvRequests();
|
|
|
|
|
if (!remainingLimitsModel.HasLimit)
|
|
|
|
|
{
|
|
|
|
|
return Success();
|
|
|
|
|
|
|
|
|
|
var child = (ChildRequests) obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var child = (ChildRequests)obj;
|
|
|
|
|
var requestCount = 0;
|
|
|
|
|
// Get the count of requests to be made
|
|
|
|
|
foreach (var s in child.SeasonRequests)
|
|
|
|
@ -85,37 +78,25 @@ namespace Ombi.Core.Rule.Rules.Request
|
|
|
|
|
requestCount += s.Episodes.Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tvLogs = requestLog.Where(x => x.RequestType == RequestType.TvShow);
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
if ((remainingLimitsModel.Remaining - requestCount) < 0)
|
|
|
|
|
{
|
|
|
|
|
return Fail("You have exceeded your Episode request quota!");
|
|
|
|
|
}
|
|
|
|
|
} else if (obj.RequestType == RequestType.Album)
|
|
|
|
|
}
|
|
|
|
|
if (obj.RequestType == RequestType.Album)
|
|
|
|
|
{
|
|
|
|
|
if (musicLimit <= 0)
|
|
|
|
|
var remainingLimitsModel = await _requestLimitService.GetRemainingMusicRequests();
|
|
|
|
|
if (!remainingLimitsModel.HasLimit)
|
|
|
|
|
{
|
|
|
|
|
return Success();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var albumLogs = requestLog.Where(x => x.RequestType == RequestType.Album);
|
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
if (remainingLimitsModel.Remaining < 1)
|
|
|
|
|
{
|
|
|
|
|
return Fail("You have exceeded your Album request quota!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Success();
|
|
|
|
|
return Success();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|