Merge pull request #2098 from tidusjar/feature/recently-added

Fixed a bunch of bugs around the newsletter !wip
pull/2093/head
Jamie 7 years ago committed by GitHub
commit 4063461a9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -76,8 +76,8 @@ namespace Ombi.Schedule.Jobs.Ombi
var customization = await _customizationSettings.GetSettingsAsync(); var customization = await _customizationSettings.GetSettingsAsync();
// Get the Content // Get the Content
var plexContent = _plex.GetAll().Include(x => x.Episodes); var plexContent = _plex.GetAll().Include(x => x.Episodes).AsNoTracking();
var embyContent = _emby.GetAll().Include(x => x.Episodes); var embyContent = _emby.GetAll().Include(x => x.Episodes).AsNoTracking();
var addedLog = _recentlyAddedLog.GetAll(); var addedLog = _recentlyAddedLog.GetAll();
var addedPlexMovieLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId); var addedPlexMovieLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
@ -90,24 +90,21 @@ namespace Ombi.Schedule.Jobs.Ombi
var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(x.Id)); var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(x.Id));
var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(x.Id)); var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(x.Id));
var plexContentTvToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Show && x.Episodes.Any(e => !addedPlexEpisodesLogIds.Contains(e.Id))); var plexEpisodesToSend = _plex.GetAllEpisodes().Include(x => x.Series).Where(x => !addedPlexEpisodesLogIds.Contains(x.Id)).AsNoTracking();
var embyContentTvToSend = embyContent.Where(x => x.Type == EmbyMediaType.Series && x.Episodes.Any(e => !addedEmbyEpisodesLogIds.Contains(e.Id))); var embyEpisodesToSend = _emby.GetAllEpisodes().Include(x => x.Series).Where(x => !addedEmbyEpisodesLogIds.Contains(x.Id)).AsNoTracking();
var plexContentToSend = plexContentMoviesToSend.Union(plexContentTvToSend);
var embyContentToSend = embyContentMoviesToSend.Union(embyContentTvToSend);
var body = string.Empty; var body = string.Empty;
if (test) if (test)
{ {
var plexm = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie).OrderByDescending(x => x.AddedAt).Take(10); var plexm = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie).OrderByDescending(x => x.AddedAt).Take(10);
var embym = embyContent.Where(x => x.Type == EmbyMediaType.Movie).OrderByDescending(x => x.AddedAt).Take(10); var embym = embyContent.Where(x => x.Type == EmbyMediaType.Movie).OrderByDescending(x => x.AddedAt).Take(10);
var plext = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Show).OrderByDescending(x => x.AddedAt).Take(10); var plext = _plex.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.Series.AddedAt).Take(10);
var embyt = embyContent.Where(x => x.Type == EmbyMediaType.Series).OrderByDescending(x => x.AddedAt).Take(10); var embyt = _emby.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.AddedAt).Take(10);
body = await BuildHtml(plexm.Union(plext), embym.Union(embyt)); body = await BuildHtml(plexm, embym, plext, embyt);
} }
else else
{ {
body = await BuildHtml(plexContentToSend, embyContentToSend); body = await BuildHtml(plexContentMoviesToSend, embyContentMoviesToSend, plexEpisodesToSend, embyEpisodesToSend);
if (body.IsNullOrEmpty()) if (body.IsNullOrEmpty())
{ {
return; return;
@ -137,37 +134,34 @@ namespace Ombi.Schedule.Jobs.Ombi
var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo); var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo);
emailTasks.Add(_email.Send( emailTasks.Add(_email.Send(
new NotificationMessage {Message = html, Subject = messageContent.Subject, To = user.Email}, new NotificationMessage { Message = html, Subject = messageContent.Subject, To = user.Email },
emailSettings)); emailSettings));
} }
// Now add all of this to the Recently Added log // Now add all of this to the Recently Added log
var recentlyAddedLog = new HashSet<RecentlyAddedLog>(); var recentlyAddedLog = new HashSet<RecentlyAddedLog>();
foreach (var p in plexContentMoviesToSend) foreach (var p in plexContentMoviesToSend)
{
if (p.Type == PlexMediaTypeEntity.Movie)
{ {
recentlyAddedLog.Add(new RecentlyAddedLog recentlyAddedLog.Add(new RecentlyAddedLog
{ {
AddedAt = DateTime.Now, AddedAt = DateTime.Now,
Type = RecentlyAddedType.Plex, Type = RecentlyAddedType.Plex,
ContentType = ContentType.Parent,
ContentId = p.Id ContentId = p.Id
}); });
} }
else
{ foreach (var p in plexEpisodesToSend)
// Add the episodes
foreach (var ep in p.Episodes)
{ {
recentlyAddedLog.Add(new RecentlyAddedLog recentlyAddedLog.Add(new RecentlyAddedLog
{ {
AddedAt = DateTime.Now, AddedAt = DateTime.Now,
Type = RecentlyAddedType.Plex, Type = RecentlyAddedType.Plex,
ContentId = ep.Id ContentType = ContentType.Episode,
ContentId = p.Id
}); });
} }
}
}
foreach (var e in embyContentMoviesToSend) foreach (var e in embyContentMoviesToSend)
{ {
@ -177,23 +171,22 @@ namespace Ombi.Schedule.Jobs.Ombi
{ {
AddedAt = DateTime.Now, AddedAt = DateTime.Now,
Type = RecentlyAddedType.Emby, Type = RecentlyAddedType.Emby,
ContentType = ContentType.Parent,
ContentId = e.Id ContentId = e.Id
}); });
} }
else }
{
// Add the episodes foreach (var p in embyEpisodesToSend)
foreach (var ep in e.Episodes)
{ {
recentlyAddedLog.Add(new RecentlyAddedLog recentlyAddedLog.Add(new RecentlyAddedLog
{ {
AddedAt = DateTime.Now, AddedAt = DateTime.Now,
Type = RecentlyAddedType.Plex, Type = RecentlyAddedType.Emby,
ContentId = ep.Id ContentType = ContentType.Episode,
ContentId = p.Id
}); });
} }
}
}
await _recentlyAddedLog.AddRange(recentlyAddedLog); await _recentlyAddedLog.AddRange(recentlyAddedLog);
await Task.WhenAll(emailTasks.ToArray()); await Task.WhenAll(emailTasks.ToArray());
@ -236,7 +229,7 @@ namespace Ombi.Schedule.Jobs.Ombi
return resolver.ParseMessage(template, curlys); return resolver.ParseMessage(template, curlys);
} }
private async Task<string> BuildHtml(IQueryable<PlexServerContent> plexContentToSend, IQueryable<EmbyContent> embyContentToSend) private async Task<string> BuildHtml(IQueryable<PlexServerContent> plexContentToSend, IQueryable<EmbyContent> embyContentToSend, IQueryable<PlexEpisode> plexEpisodes, IQueryable<EmbyEpisode> embyEp)
{ {
var sb = new StringBuilder(); var sb = new StringBuilder();
@ -249,13 +242,11 @@ namespace Ombi.Schedule.Jobs.Ombi
await ProcessEmbyMovies(embyMovies, sb); await ProcessEmbyMovies(embyMovies, sb);
} }
var plexTv = plexContentToSend.Where(x => x.Type == PlexMediaTypeEntity.Show); if (plexEpisodes.Any() || embyEp.Any())
var embyTv = embyContentToSend.Where(x => x.Type == EmbyMediaType.Series);
if (plexTv.Any() || embyTv.Any())
{ {
sb.Append("<h1>New Episodes:</h1><br /><br />"); sb.Append("<h1>New Episodes:</h1><br /><br />");
await ProcessPlexTv(plexTv, sb); await ProcessPlexTv(plexEpisodes, sb);
await ProcessEmbyMovies(embyTv, sb); await ProcessEmbyTv(embyEp, sb);
} }
return sb.ToString(); return sb.ToString();
@ -351,9 +342,28 @@ namespace Ombi.Schedule.Jobs.Ombi
AddParagraph(sb, info.Overview); AddParagraph(sb, info.Overview);
} }
private async Task ProcessPlexTv(IQueryable<PlexServerContent> plexContent, StringBuilder sb) private async Task ProcessPlexTv(IQueryable<PlexEpisode> plexContent, StringBuilder sb)
{
var series = new List<PlexServerContent>();
foreach (var plexEpisode in plexContent)
{ {
var orderedTv = plexContent.OrderByDescending(x => x.AddedAt); var alreadyAdded = series.FirstOrDefault(x => x.Key == plexEpisode.Series.Key);
if (alreadyAdded != null)
{
var episodeExists = alreadyAdded.Episodes.Any(x => x.Key == plexEpisode.Key);
if (!episodeExists)
{
alreadyAdded.Episodes.Add(plexEpisode);
}
}
else
{
plexEpisode.Series.Episodes = new List<PlexEpisode> { plexEpisode };
series.Add(plexEpisode.Series);
}
}
var orderedTv = series.OrderByDescending(x => x.AddedAt);
sb.Append( sb.Append(
"<table border=\"0\" cellpadding=\"0\" align=\"center\" cellspacing=\"0\" style=\"border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;\" width=\"100%\">"); "<table border=\"0\" cellpadding=\"0\" align=\"center\" cellspacing=\"0\" style=\"border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;\" width=\"100%\">");
foreach (var t in orderedTv) foreach (var t in orderedTv)
@ -411,7 +421,7 @@ namespace Ombi.Schedule.Jobs.Ombi
EndTag(sb, "a"); EndTag(sb, "a");
// Group by the season number // Group by the season number
var results = t.Episodes?.GroupBy(p => p.SeasonNumber, var results = t.Episodes.GroupBy(p => p.SeasonNumber,
(key, g) => new (key, g) => new
{ {
SeasonNumber = key, SeasonNumber = key,
@ -460,9 +470,26 @@ namespace Ombi.Schedule.Jobs.Ombi
} }
private async Task ProcessEmbyTv(IQueryable<EmbyContent> plexContent, StringBuilder sb) private async Task ProcessEmbyTv(IQueryable<EmbyEpisode> embyContent, StringBuilder sb)
{
var series = new List<EmbyContent>();
foreach (var episode in embyContent)
{
var alreadyAdded = series.FirstOrDefault(x => x.EmbyId == episode.Series.EmbyId);
if (alreadyAdded != null)
{ {
var orderedTv = plexContent.OrderByDescending(x => x.AddedAt); alreadyAdded.Episodes.Add(episode);
}
else
{
episode.Series.Episodes = new List<EmbyEpisode>
{
episode
};
series.Add(episode.Series);
}
}
var orderedTv = series.OrderByDescending(x => x.AddedAt);
sb.Append( sb.Append(
"<table border=\"0\" cellpadding=\"0\" align=\"center\" cellspacing=\"0\" style=\"border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;\" width=\"100%\">"); "<table border=\"0\" cellpadding=\"0\" align=\"center\" cellspacing=\"0\" style=\"border-collapse: separate; mso-table-lspace: 0pt; mso-table-rspace: 0pt; width: 100%;\" width=\"100%\">");
foreach (var t in orderedTv) foreach (var t in orderedTv)

Loading…
Cancel
Save