diff --git a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs index a22e72e72..7c544f3f3 100644 --- a/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs +++ b/src/Ombi.Schedule/Jobs/Ombi/NewsletterJob.cs @@ -76,8 +76,8 @@ namespace Ombi.Schedule.Jobs.Ombi var customization = await _customizationSettings.GetSettingsAsync(); // Get the Content - var plexContent = _plex.GetAll().Include(x => x.Episodes); - var embyContent = _emby.GetAll().Include(x => x.Episodes); + var plexContent = _plex.GetAll().Include(x => x.Episodes).AsNoTracking(); + var embyContent = _emby.GetAll().Include(x => x.Episodes).AsNoTracking(); var addedLog = _recentlyAddedLog.GetAll(); 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 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 embyContentTvToSend = embyContent.Where(x => x.Type == EmbyMediaType.Series && x.Episodes.Any(e => !addedEmbyEpisodesLogIds.Contains(e.Id))); - - var plexContentToSend = plexContentMoviesToSend.Union(plexContentTvToSend); - var embyContentToSend = embyContentMoviesToSend.Union(embyContentTvToSend); + var plexEpisodesToSend = _plex.GetAllEpisodes().Include(x => x.Series).Where(x => !addedPlexEpisodesLogIds.Contains(x.Id)).AsNoTracking(); + var embyEpisodesToSend = _emby.GetAllEpisodes().Include(x => x.Series).Where(x => !addedEmbyEpisodesLogIds.Contains(x.Id)).AsNoTracking(); var body = string.Empty; if (test) { 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 plext = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Show).OrderByDescending(x => x.AddedAt).Take(10); - var embyt = embyContent.Where(x => x.Type == EmbyMediaType.Series).OrderByDescending(x => x.AddedAt).Take(10); - body = await BuildHtml(plexm.Union(plext), embym.Union(embyt)); + var plext = _plex.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.Series.AddedAt).Take(10); + var embyt = _emby.GetAllEpisodes().Include(x => x.Series).OrderByDescending(x => x.AddedAt).Take(10); + body = await BuildHtml(plexm, embym, plext, embyt); } else { - body = await BuildHtml(plexContentToSend, embyContentToSend); + body = await BuildHtml(plexContentMoviesToSend, embyContentMoviesToSend, plexEpisodesToSend, embyEpisodesToSend); if (body.IsNullOrEmpty()) { return; @@ -137,7 +134,7 @@ namespace Ombi.Schedule.Jobs.Ombi var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo); 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)); } @@ -145,28 +142,25 @@ namespace Ombi.Schedule.Jobs.Ombi var recentlyAddedLog = new HashSet(); foreach (var p in plexContentMoviesToSend) { - if (p.Type == PlexMediaTypeEntity.Movie) + recentlyAddedLog.Add(new RecentlyAddedLog { - recentlyAddedLog.Add(new RecentlyAddedLog - { - AddedAt = DateTime.Now, - Type = RecentlyAddedType.Plex, - ContentId = p.Id - }); - } - else + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentType = ContentType.Parent, + ContentId = p.Id + }); + + } + + foreach (var p in plexEpisodesToSend) + { + recentlyAddedLog.Add(new RecentlyAddedLog { - // Add the episodes - foreach (var ep in p.Episodes) - { - recentlyAddedLog.Add(new RecentlyAddedLog - { - AddedAt = DateTime.Now, - Type = RecentlyAddedType.Plex, - ContentId = ep.Id - }); - } - } + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Plex, + ContentType = ContentType.Episode, + ContentId = p.Id + }); } foreach (var e in embyContentMoviesToSend) @@ -177,22 +171,21 @@ namespace Ombi.Schedule.Jobs.Ombi { AddedAt = DateTime.Now, Type = RecentlyAddedType.Emby, + ContentType = ContentType.Parent, ContentId = e.Id }); } - else + } + + foreach (var p in embyEpisodesToSend) + { + recentlyAddedLog.Add(new RecentlyAddedLog { - // Add the episodes - foreach (var ep in e.Episodes) - { - recentlyAddedLog.Add(new RecentlyAddedLog - { - AddedAt = DateTime.Now, - Type = RecentlyAddedType.Plex, - ContentId = ep.Id - }); - } - } + AddedAt = DateTime.Now, + Type = RecentlyAddedType.Emby, + ContentType = ContentType.Episode, + ContentId = p.Id + }); } await _recentlyAddedLog.AddRange(recentlyAddedLog); @@ -212,7 +205,7 @@ namespace Ombi.Schedule.Jobs.Ombi var email = new NewsletterTemplate(); var html = email.LoadTemplate(messageContent.Subject, messageContent.Message, body, customization.Logo); - + await _email.Send( new NotificationMessage { Message = html, Subject = messageContent.Subject, To = a.Email }, emailSettings); @@ -236,7 +229,7 @@ namespace Ombi.Schedule.Jobs.Ombi return resolver.ParseMessage(template, curlys); } - private async Task BuildHtml(IQueryable plexContentToSend, IQueryable embyContentToSend) + private async Task BuildHtml(IQueryable plexContentToSend, IQueryable embyContentToSend, IQueryable plexEpisodes, IQueryable embyEp) { var sb = new StringBuilder(); @@ -249,13 +242,11 @@ namespace Ombi.Schedule.Jobs.Ombi await ProcessEmbyMovies(embyMovies, sb); } - var plexTv = plexContentToSend.Where(x => x.Type == PlexMediaTypeEntity.Show); - var embyTv = embyContentToSend.Where(x => x.Type == EmbyMediaType.Series); - if (plexTv.Any() || embyTv.Any()) + if (plexEpisodes.Any() || embyEp.Any()) { sb.Append("

New Episodes:



"); - await ProcessPlexTv(plexTv, sb); - await ProcessEmbyMovies(embyTv, sb); + await ProcessPlexTv(plexEpisodes, sb); + await ProcessEmbyTv(embyEp, sb); } return sb.ToString(); @@ -351,9 +342,28 @@ namespace Ombi.Schedule.Jobs.Ombi AddParagraph(sb, info.Overview); } - private async Task ProcessPlexTv(IQueryable plexContent, StringBuilder sb) + private async Task ProcessPlexTv(IQueryable plexContent, StringBuilder sb) { - var orderedTv = plexContent.OrderByDescending(x => x.AddedAt); + var series = new List(); + 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 }; + series.Add(plexEpisode.Series); + } + } + + var orderedTv = series.OrderByDescending(x => x.AddedAt); sb.Append( ""); foreach (var t in orderedTv) @@ -411,7 +421,7 @@ namespace Ombi.Schedule.Jobs.Ombi EndTag(sb, "a"); // Group by the season number - var results = t.Episodes?.GroupBy(p => p.SeasonNumber, + var results = t.Episodes.GroupBy(p => p.SeasonNumber, (key, g) => new { SeasonNumber = key, @@ -460,9 +470,26 @@ namespace Ombi.Schedule.Jobs.Ombi } - private async Task ProcessEmbyTv(IQueryable plexContent, StringBuilder sb) + private async Task ProcessEmbyTv(IQueryable embyContent, StringBuilder sb) { - var orderedTv = plexContent.OrderByDescending(x => x.AddedAt); + var series = new List(); + 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 + { + episode + }; + series.Add(episode.Series); + } + } + var orderedTv = series.OrderByDescending(x => x.AddedAt); sb.Append( "
"); foreach (var t in orderedTv)