@ -1,62 +1,58 @@
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.Linq ;
using System.Security.Principal ;
using System.Threading.Tasks ;
using Hangfire ;
using Hangfire ;
using Ombi.Api.TheMovieDb ;
using Ombi.Api.TvMaze ;
using Ombi.Core.Models.Requests ;
using Ombi.Core.Models.Requests.Movie ;
using Ombi.Core.Models.Search ;
using Ombi.Core.Requests.Models ;
using Ombi.Store.Entities ;
using Ombi.Core.Rules ;
using Ombi.Helpers ;
using Ombi.Notifications ;
using Ombi.Notifications.Models ;
using Ombi.Store.Entities ;
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.Linq ;
using System.Security.Principal ;
using System.Threading.Tasks ;
namespace Ombi.Core.Engine
{
public class MovieRequestEngine : BaseMediaEngine , IMovieRequestEngine
{
public MovieRequestEngine ( IMovieDbApi movieApi , IRequestServiceMain requestService , IPrincipal user , INotificationService notificationService ) : base ( user , requestService )
public MovieRequestEngine ( IMovieDbApi movieApi , IRequestServiceMain requestService , IPrincipal user ,
INotificationService notificationService , IRuleEvaluator r ) : base ( user , requestService , r )
{
MovieApi = movieApi ;
NotificationService = notificationService ;
}
private IMovieDbApi MovieApi { get ; }
private INotificationService NotificationService { get ; }
public async Task < RequestEngineResult > RequestMovie ( SearchMovieViewModel model )
{
var movieInfo = await MovieApi . GetMovieInformation ( model . Id ) ;
if ( movieInfo = = null )
{
return new RequestEngineResult
{
RequestAdded = false ,
Message = "There was an issue adding this movie!" ,
ErrorMessage = $"TheMovieDb didn't have any information for ID {model.Id}"
} ;
}
var fullMovieName =
$"{movieInfo.Title}{(!string.IsNullOrEmpty(movieInfo.ReleaseDate) ? $" ( { DateTime . Parse ( movieInfo . ReleaseDate ) . Year } ) " : string.Empty)}" ;
var existingRequest = await MovieRequestService . CheckRequestAsync ( model . Id ) ;
if ( existingRequest ! = null )
{
return new RequestEngineResult
{
RequestAdded = false ,
Message = $"{fullMovieName} has already been requested"
} ;
}
// TODO
//try
//{
// var content = PlexContentRepository.GetAll();
// var movies = PlexChecker.GetPlexMovies(content);
// if (PlexChecker.IsMovieAvailable(movies.ToArray(), movieInfo.Title, movieInfo.ReleaseDate?.Year.ToString()))
@ -88,20 +84,27 @@ namespace Ombi.Core.Engine
ImdbId = movieInfo . ImdbId ,
PosterPath = movieInfo . PosterPath ,
Title = movieInfo . Title ,
ReleaseDate = ! string . IsNullOrEmpty ( movieInfo . ReleaseDate ) ? DateTime . Parse ( movieInfo . ReleaseDate ) : DateTime . MinValue ,
ReleaseDate = ! string . IsNullOrEmpty ( movieInfo . ReleaseDate )
? DateTime . Parse ( movieInfo . ReleaseDate )
: DateTime . MinValue ,
Status = movieInfo . Status ,
RequestedDate = DateTime . UtcNow ,
Approved = false ,
RequestedUsers = new List < string > { Username } ,
Issues = IssueState . None ,
RequestedUsers = new List < string > { Username } ,
Issues = IssueState . None
} ;
try
{
if ( ShouldAutoApprove ( RequestType . Movie ) )
{
model . Approved = true ;
var ruleResults = RunRules ( requestModel ) . ToList ( ) ;
if ( ruleResults . Any ( x = > ! x . Success ) )
return new RequestEngineResult
{
ErrorMessage = ruleResults . FirstOrDefault ( x = > ! string . IsNullOrEmpty ( x . Message ) ) . Message
} ;
if ( requestModel . Approved ) // The rules have auto approved this
{
// var result = await MovieSender.Send(model);
// if (result.Result)
// {
@ -120,7 +123,6 @@ namespace Ombi.Core.Engine
// }
// if (!result.MovieSendingEnabled)
// {
// return await AddRequest(model, settings, $"{fullMovieName} {Resources.UI.Search_SuccessfullyAdded}");
// }
@ -131,10 +133,8 @@ namespace Ombi.Core.Engine
// });
}
return await AddMovieRequest ( requestModel , /*settings,*/
$"{fullMovieName} has been successfully added!" ) ;
$"{fullMovieName} has been successfully added!" ) ;
}
catch ( Exception e )
{
@ -160,8 +160,48 @@ namespace Ombi.Core.Engine
return null ;
}
private IEnumerable < EpisodesModel > GetListDifferences ( IEnumerable < EpisodesModel > existing , IEnumerable < EpisodesModel > request )
public async Task < IEnumerable < MovieRequestModel > > GetMovieRequests ( int count , int position )
{
var allRequests = await MovieRequestService . GetAllAsync ( count , position ) ;
return allRequests ;
}
public async Task < IEnumerable < MovieRequestModel > > SearchMovieRequest ( string search )
{
var allRequests = await MovieRequestService . GetAllAsync ( ) ;
var results = allRequests . Where ( x = > x . Title . Contains ( search , CompareOptions . IgnoreCase ) ) ;
return results ;
}
public async Task < MovieRequestModel > UpdateMovieRequest ( MovieRequestModel request )
{
var allRequests = await MovieRequestService . GetAllAsync ( ) ;
var results = allRequests . FirstOrDefault ( x = > x . Id = = request . Id ) ;
results . Approved = request . Approved ;
results . Available = request . Available ;
results . Denied = request . Denied ;
results . DeniedReason = request . DeniedReason ;
results . AdminNote = request . AdminNote ;
results . ImdbId = request . ImdbId ;
results . IssueId = request . IssueId ;
results . Issues = request . Issues ;
results . OtherMessage = request . OtherMessage ;
results . Overview = request . Overview ;
results . PosterPath = request . PosterPath ;
results . RequestedUsers = request . RequestedUsers ? . ToList ( ) ? ? new List < string > ( ) ;
var model = MovieRequestService . UpdateRequest ( results ) ;
return model ;
}
public async Task RemoveMovieRequest ( int requestId )
{
await MovieRequestService . DeleteRequestAsync ( requestId ) ;
}
private IEnumerable < EpisodesModel > GetListDifferences ( IEnumerable < EpisodesModel > existing ,
IEnumerable < EpisodesModel > request )
{
var newRequest = request
. Select ( r = >
@ -169,12 +209,12 @@ namespace Ombi.Core.Engine
{
SeasonNumber = r . SeasonNumber ,
EpisodeNumber = r . EpisodeNumber
} ) . ToList ( ) ;
} )
. ToList ( ) ;
return newRequest . Except ( existing ) ;
}
private async Task < RequestEngineResult > AddMovieRequest ( MovieRequestModel model , string message )
{
await MovieRequestService . AddRequestAsync ( model ) ;
@ -188,7 +228,9 @@ namespace Ombi.Core.Engine
DateTime = DateTime . Now ,
NotificationType = NotificationType . NewRequest ,
RequestType = model . Type ,
ImgSrc = model . Type = = RequestType . Movie ? $"https://image.tmdb.org/t/p/w300/{model.PosterPath}" : model . PosterPath
ImgSrc = model . Type = = RequestType . Movie
? $"https://image.tmdb.org/t/p/w300/{model.PosterPath}"
: model . PosterPath
} ;
BackgroundJob . Enqueue ( ( ) = > NotificationService . Publish ( notificationModel ) . Wait ( ) ) ;
@ -212,46 +254,7 @@ namespace Ombi.Core.Engine
// await RequestLimitRepo.UpdateAsync(usersLimit);
//}
return new RequestEngineResult { RequestAdded = true } ;
}
public async Task < IEnumerable < MovieRequestModel > > GetMovieRequests ( int count , int position )
{
var allRequests = await MovieRequestService . GetAllAsync ( count , position ) ;
return allRequests ;
}
public async Task < IEnumerable < MovieRequestModel > > SearchMovieRequest ( string search )
{
var allRequests = await MovieRequestService . GetAllAsync ( ) ;
var results = allRequests . Where ( x = > x . Title . Contains ( search , CompareOptions . IgnoreCase ) ) ;
return results ;
}
public async Task < MovieRequestModel > UpdateMovieRequest ( MovieRequestModel request )
{
var allRequests = await MovieRequestService . GetAllAsync ( ) ;
var results = allRequests . FirstOrDefault ( x = > x . Id = = request . Id ) ;
results . Approved = request . Approved ;
results . Available = request . Available ;
results . Denied = request . Denied ;
results . DeniedReason = request . DeniedReason ;
results . AdminNote = request . AdminNote ;
results . ImdbId = request . ImdbId ;
results . IssueId = request . IssueId ;
results . Issues = request . Issues ;
results . OtherMessage = request . OtherMessage ;
results . Overview = request . Overview ;
results . PosterPath = request . PosterPath ;
results . RequestedUsers = request . RequestedUsers ? . ToList ( ) ? ? new List < string > ( ) ;
var model = MovieRequestService . UpdateRequest ( results ) ;
return model ;
}
public async Task RemoveMovieRequest ( int requestId )
{
await MovieRequestService . DeleteRequestAsync ( requestId ) ;
return new RequestEngineResult { RequestAdded = true } ;
}
}
}