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