Fixed a bunch of bugs around the newsletter !wip

pull/2098/head
tidusjar 7 years ago
parent 187808b64b
commit ab9cdc856b

@ -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,7 +134,7 @@ 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));
} }
@ -145,28 +142,25 @@ namespace Ombi.Schedule.Jobs.Ombi
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,
{ Type = RecentlyAddedType.Plex,
AddedAt = DateTime.Now, ContentType = ContentType.Parent,
Type = RecentlyAddedType.Plex, ContentId = p.Id
ContentId = p.Id });
});
} }
else
foreach (var p in plexEpisodesToSend)
{
recentlyAddedLog.Add(new RecentlyAddedLog
{ {
// Add the episodes AddedAt = DateTime.Now,
foreach (var ep in p.Episodes) Type = RecentlyAddedType.Plex,
{ ContentType = ContentType.Episode,
recentlyAddedLog.Add(new RecentlyAddedLog ContentId = p.Id
{ });
AddedAt = DateTime.Now,
Type = RecentlyAddedType.Plex,
ContentId = ep.Id
});
}
}
} }
foreach (var e in embyContentMoviesToSend) foreach (var e in embyContentMoviesToSend)
@ -177,22 +171,21 @@ 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 }
foreach (var p in embyEpisodesToSend)
{
recentlyAddedLog.Add(new RecentlyAddedLog
{ {
// Add the episodes AddedAt = DateTime.Now,
foreach (var ep in e.Episodes) Type = RecentlyAddedType.Emby,
{ ContentType = ContentType.Episode,
recentlyAddedLog.Add(new RecentlyAddedLog ContentId = p.Id
{ });
AddedAt = DateTime.Now,
Type = RecentlyAddedType.Plex,
ContentId = ep.Id
});
}
}
} }
await _recentlyAddedLog.AddRange(recentlyAddedLog); await _recentlyAddedLog.AddRange(recentlyAddedLog);
@ -212,7 +205,7 @@ namespace Ombi.Schedule.Jobs.Ombi
var email = new NewsletterTemplate(); var email = new NewsletterTemplate();
var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo); var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo);
await _email.Send( await _email.Send(
new NotificationMessage { Message = html, Subject = messageContent.Subject, To = a.Email }, new NotificationMessage { Message = html, Subject = messageContent.Subject, To = a.Email },
emailSettings); emailSettings);
@ -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 orderedTv = plexContent.OrderByDescending(x => x.AddedAt); var series = new List<PlexServerContent>();
foreach (var plexEpisode in plexContent)
{
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 orderedTv = plexContent.OrderByDescending(x => x.AddedAt); var series = new List<EmbyContent>();
foreach (var episode in embyContent)
{
var alreadyAdded = series.FirstOrDefault(x => x.EmbyId == episode.Series.EmbyId);
if (alreadyAdded != null)
{
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