@ -1,4 +1,5 @@
using System ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Linq ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.SignalR ;
using Microsoft.AspNetCore.SignalR ;
@ -8,6 +9,7 @@ using Ombi.Core;
using Ombi.Helpers ;
using Ombi.Helpers ;
using Ombi.Hubs ;
using Ombi.Hubs ;
using Ombi.Notifications.Models ;
using Ombi.Notifications.Models ;
using Ombi.Schedule.Jobs.Plex.Models ;
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 ;
@ -60,13 +62,13 @@ namespace Ombi.Schedule.Jobs.Plex
private Task ProcessTv ( )
private Task ProcessTv ( )
{
{
var tv = _tvRepo . GetChild ( ) . Where ( x = > ! x . Available ) ;
var tv = _tvRepo . GetChild ( ) . Where ( x = > ! x . Available ) .AsNoTracking ( ) ;
return ProcessTv ( tv ) ;
return ProcessTv ( tv ) ;
}
}
private async Task ProcessTv ( IQueryable < ChildRequests > tv )
private async Task ProcessTv ( IQueryable < ChildRequests > tv )
{
{
var plexEpisodes = _repo . GetAllEpisodes ( ) . Include ( x = > x . Series ) ;
var plexEpisodes = _repo . GetAllEpisodes ( ) . Include ( x = > x . Series ) .AsNoTracking ( ) ;
foreach ( var child in tv )
foreach ( var child in tv )
{
{
@ -108,6 +110,7 @@ namespace Ombi.Schedule.Jobs.Plex
}
}
var availableEpisode = new List < AvailabilityModel > ( ) ;
foreach ( var season in child . SeasonRequests )
foreach ( var season in child . SeasonRequests )
{
{
foreach ( var episode in season . Episodes )
foreach ( var episode in season . Episodes )
@ -122,20 +125,28 @@ namespace Ombi.Schedule.Jobs.Plex
if ( foundEp ! = null )
if ( foundEp ! = null )
{
{
availableEpisode . Add ( new AvailabilityModel
{
Id = episode . Id
} ) ;
episode . Available = true ;
episode . Available = true ;
}
}
}
}
}
}
//TODO Partial avilability notifications here
foreach ( var c in availableEpisode )
{
await _tvRepo . MarkEpisodeAsAvailable ( c . Id ) ;
}
// Check to see if all of the episodes in all seasons are available for this request
// Check to see if all of the episodes in all seasons are available for this request
var allAvailable = child . SeasonRequests . All ( x = > x . Episodes . All ( c = > c . Available ) ) ;
var allAvailable = child . SeasonRequests . All ( x = > x . Episodes . All ( c = > c . Available ) ) ;
if ( allAvailable )
if ( allAvailable )
{
{
_log . LogInformation ( "[PAC] - Child request {0} is now available, sending notification" , $"{child.Title} - {child.Id}" ) ;
_log . LogInformation ( "[PAC] - Child request {0} is now available, sending notification" , $"{child.Title} - {child.Id}" ) ;
// We have ful-fulled this request!
// We have ful-fulled this request!
child . Available = true ;
await _tvRepo . MarkChildAsAvailable ( child . Id ) ;
child . MarkedAsAvailable = DateTime . Now ;
await _notificationService . Notify ( new NotificationOptions
await _notificationService . Notify ( new NotificationOptions
{
{
DateTime = DateTime . Now ,
DateTime = DateTime . Now ,
@ -153,10 +164,16 @@ namespace Ombi.Schedule.Jobs.Plex
private async Task ProcessMovies ( )
private async Task ProcessMovies ( )
{
{
// Get all non available
// Get all non available
var movies = _movieRepo . GetAll ( ) . Include ( x = > x . RequestedUser ) . Where ( x = > ! x . Available ) ;
var movies = _movieRepo . GetAll ( ) . Include ( x = > x . RequestedUser ) . Where ( x = > ! x . Available ) . AsNoTracking ( ) ;
var itemsForAvailbility = new List < AvailabilityModel > ( ) ;
foreach ( var movie in movies )
foreach ( var movie in movies )
{
{
if ( movie . Available )
{
return ;
}
PlexServerContent item = null ;
PlexServerContent item = null ;
if ( movie . ImdbId . HasValue ( ) )
if ( movie . ImdbId . HasValue ( ) )
{
{
@ -174,24 +191,29 @@ namespace Ombi.Schedule.Jobs.Plex
// We don't yet have this
// We don't yet have this
continue ;
continue ;
}
}
_log . LogInformation ( "[PAC] - Movie request {0} is now available, sending notification" , $"{movie.Title} - {movie.Id}" ) ;
itemsForAvailbility . Add ( new AvailabilityModel
{
Id = movie . Id ,
RequestedUser = movie . RequestedUser ! = null ? movie . RequestedUser . Email : string . Empty
} ) ;
}
movie . Available = true ;
foreach ( var i in itemsForAvailbility )
movie . MarkedAsAvailable = DateTime . Now ;
{
item . RequestId = movie . Id ;
await _movieRepo . MarkAsAvailable ( i . Id ) ;
_log . LogInformation ( "[PAC] - Movie request {0} is now available, sending notification" , $"{movie.Title} - {movie.Id}" ) ;
await _notificationService . Notify ( new NotificationOptions
await _notificationService . Notify ( new NotificationOptions
{
{
DateTime = DateTime . Now ,
DateTime = DateTime . Now ,
NotificationType = NotificationType . RequestAvailable ,
NotificationType = NotificationType . RequestAvailable ,
RequestId = mov ie . Id ,
RequestId = i. Id ,
RequestType = RequestType . Movie ,
RequestType = RequestType . Movie ,
Recipient = mov ie . RequestedUser ! = null ? movie . RequestedUser . Email : string . Empty
Recipient = i. RequestedUser
} ) ;
} ) ;
}
}
await _movieRepo . Save ( ) ;
await _repo . SaveChangesAsync ( ) ;
await _repo . SaveChangesAsync ( ) ;
}
}