Added user request limits, We can now set the limit for a user.

This commit is contained in:
tidusjar 2017-12-08 23:50:47 +00:00
commit 0008018080
19 changed files with 1161 additions and 15 deletions

View file

@ -15,6 +15,7 @@ using Ombi.Core.Authentication;
using Ombi.Core.Engine.Interfaces;
using Ombi.Core.Rule.Interfaces;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
namespace Ombi.Core.Engine
{
@ -22,18 +23,20 @@ namespace Ombi.Core.Engine
{
public MovieRequestEngine(IMovieDbApi movieApi, IRequestServiceMain requestService, IPrincipal user,
INotificationHelper helper, IRuleEvaluator r, IMovieSender sender, ILogger<MovieRequestEngine> log,
OmbiUserManager manager) : base(user, requestService, r, manager)
OmbiUserManager manager, IRepository<RequestLog> rl) : base(user, requestService, r, manager)
{
MovieApi = movieApi;
NotificationHelper = helper;
Sender = sender;
Logger = log;
_requestLog = rl;
}
private IMovieDbApi MovieApi { get; }
private INotificationHelper NotificationHelper { get; }
private IMovieSender Sender { get; }
private ILogger<MovieRequestEngine> Logger { get; }
private readonly IRepository<RequestLog> _requestLog;
/// <summary>
/// Requests the movie.
@ -321,6 +324,14 @@ namespace Ombi.Core.Engine
NotificationHelper.NewRequest(model);
}
await _requestLog.Add(new RequestLog
{
UserId = (await GetUser()).Id,
RequestDate = DateTime.UtcNow,
RequestId = model.Id,
RequestType = RequestType.Movie,
});
return new RequestEngineResult { Result = true, Message = $"{movieName} has been successfully added!" };
}

View file

@ -1,4 +1,5 @@
using AutoMapper;
using System;
using AutoMapper;
using Ombi.Api.TvMaze;
using Ombi.Core.Models.Requests;
using Ombi.Core.Models.Search;
@ -25,18 +26,20 @@ namespace Ombi.Core.Engine
{
public TvRequestEngine(ITvMazeApi tvApi, IRequestServiceMain requestService, IPrincipal user,
INotificationHelper helper, IRuleEvaluator rule, OmbiUserManager manager,
ITvSender sender, IAuditRepository audit) : base(user, requestService, rule, manager)
ITvSender sender, IAuditRepository audit, IRepository<RequestLog> rl) : base(user, requestService, rule, manager)
{
TvApi = tvApi;
NotificationHelper = helper;
TvSender = sender;
Audit = audit;
_requestLog = rl;
}
private INotificationHelper NotificationHelper { get; }
private ITvMazeApi TvApi { get; }
private ITvSender TvSender { get; }
private IAuditRepository Audit { get; }
private readonly IRepository<RequestLog> _requestLog;
public async Task<RequestEngineResult> RequestTvShow(SearchTvShowViewModel tv)
{
@ -401,6 +404,14 @@ namespace Ombi.Core.Engine
};
}
await _requestLog.Add(new RequestLog
{
UserId = (await GetUser()).Id,
RequestDate = DateTime.UtcNow,
RequestId = model.Id,
RequestType = RequestType.TvShow,
});
return new RequestEngineResult {Result = true};
}
}

View file

@ -14,6 +14,8 @@ namespace Ombi.Core.Models.UI
public DateTime? LastLoggedIn { get; set; }
public bool HasLoggedIn { get; set; }
public UserType UserType { get; set; }
public int MovieRequestLimit { get; set; }
public int EpisodeRequestLimit { get; set; }
}
public class ClaimCheckboxes

View file

@ -0,0 +1,95 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2017 Jamie Rees
// File: RequestLimitRule.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.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Ombi.Core.Authentication;
using Ombi.Core.Rule.Interfaces;
using Ombi.Store.Entities;
using Ombi.Store.Entities.Requests;
using Ombi.Store.Repository;
namespace Ombi.Core.Rule.Rules.Request
{
public class RequestLimitRule : BaseRequestRule, IRules<BaseRequest>
{
public RequestLimitRule(IRepository<RequestLog> rl, OmbiUserManager um)
{
_requestLog = rl;
_userManager = um;
}
private readonly IRepository<RequestLog> _requestLog;
private readonly OmbiUserManager _userManager;
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 requestLog = _requestLog.GetAll().Where(x => x.UserId == obj.RequestedUserId);
if (obj.RequestType == RequestType.Movie)
{
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)
{
return Fail("You have exceeded your Movie request quota!");
}
}
else
{
var child = (ChildRequests) obj;
var requestCount = 0;
// Get the count of requests to be made
foreach (var s in child.SeasonRequests)
{
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));
var count = await tv.Select(x => x.EpisodeCount).CountAsync();
count += requestCount; // Add the amount of requests in
if (count > episodeLimit)
{
return Fail("You have exceeded your Episode request quota!");
}
}
return Success();
}
}
}