Fixed the newsletter not sending #2134

pull/2183/head^2
Jamie Rees 7 years ago
parent ad41ea2086
commit 04af799efb

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using MailKit; using MailKit;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Ombi.Api.TheMovieDb; using Ombi.Api.TheMovieDb;
using Ombi.Api.TheMovieDb.Models; using Ombi.Api.TheMovieDb.Models;
using Ombi.Api.TvMaze; using Ombi.Api.TvMaze;
@ -26,7 +27,7 @@ namespace Ombi.Schedule.Jobs.Ombi
public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, IRepository<RecentlyAddedLog> addedLog, public NewsletterJob(IPlexContentRepository plex, IEmbyContentRepository emby, IRepository<RecentlyAddedLog> addedLog,
IMovieDbApi movieApi, ITvMazeApi tvApi, IEmailProvider email, ISettingsService<CustomizationSettings> custom, IMovieDbApi movieApi, ITvMazeApi tvApi, IEmailProvider email, ISettingsService<CustomizationSettings> custom,
ISettingsService<EmailNotificationSettings> emailSettings, INotificationTemplatesRepository templateRepo, ISettingsService<EmailNotificationSettings> emailSettings, INotificationTemplatesRepository templateRepo,
UserManager<OmbiUser> um, ISettingsService<NewsletterSettings> newsletter) UserManager<OmbiUser> um, ISettingsService<NewsletterSettings> newsletter, ILogger<NewsletterJob> log)
{ {
_plex = plex; _plex = plex;
_emby = emby; _emby = emby;
@ -42,6 +43,7 @@ namespace Ombi.Schedule.Jobs.Ombi
_emailSettings.ClearCache(); _emailSettings.ClearCache();
_customizationSettings.ClearCache(); _customizationSettings.ClearCache();
_newsletterSettings.ClearCache(); _newsletterSettings.ClearCache();
_log = log;
} }
private readonly IPlexContentRepository _plex; private readonly IPlexContentRepository _plex;
@ -55,6 +57,7 @@ namespace Ombi.Schedule.Jobs.Ombi
private readonly ISettingsService<EmailNotificationSettings> _emailSettings; private readonly ISettingsService<EmailNotificationSettings> _emailSettings;
private readonly ISettingsService<NewsletterSettings> _newsletterSettings; private readonly ISettingsService<NewsletterSettings> _newsletterSettings;
private readonly UserManager<OmbiUser> _userManager; private readonly UserManager<OmbiUser> _userManager;
private readonly ILogger _log;
public async Task Start(NewsletterSettings settings, bool test) public async Task Start(NewsletterSettings settings, bool test)
{ {
@ -74,152 +77,167 @@ namespace Ombi.Schedule.Jobs.Ombi
return; return;
} }
var customization = await _customizationSettings.GetSettingsAsync(); try
{
// Get the Content
var plexContent = _plex.GetAll().Include(x => x.Episodes).AsNoTracking();
var embyContent = _emby.GetAll().Include(x => x.Episodes).AsNoTracking();
var addedLog = _recentlyAddedLog.GetAll(); var customization = await _customizationSettings.GetSettingsAsync();
var addedPlexMovieLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
var addedEmbyMoviesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
var addedPlexEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode).Select(x => x.ContentId); // Get the Content
var addedEmbyEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Episode).Select(x => x.ContentId); var plexContent = _plex.GetAll().Include(x => x.Episodes).AsNoTracking();
var embyContent = _emby.GetAll().Include(x => x.Episodes).AsNoTracking();
// Filter out the ones that we haven't sent yet var addedLog = _recentlyAddedLog.GetAll();
var plexContentMoviesToSend = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie && !addedPlexMovieLogIds.Contains(x.Id)); var addedPlexMovieLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
var embyContentMoviesToSend = embyContent.Where(x => x.Type == EmbyMediaType.Movie && !addedEmbyMoviesLogIds.Contains(x.Id)); var addedEmbyMoviesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Parent).Select(x => x.ContentId);
var plexEpisodesToSend = _plex.GetAllEpisodes().Include(x => x.Series).Where(x => !addedPlexEpisodesLogIds.Contains(x.Id)).AsNoTracking(); var addedPlexEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Plex && x.ContentType == ContentType.Episode).Select(x => x.ContentId);
var embyEpisodesToSend = _emby.GetAllEpisodes().Include(x => x.Series).Where(x => !addedEmbyEpisodesLogIds.Contains(x.Id)).AsNoTracking(); var addedEmbyEpisodesLogIds = addedLog.Where(x => x.Type == RecentlyAddedType.Emby && x.ContentType == ContentType.Episode).Select(x => x.ContentId);
var body = string.Empty; // Filter out the ones that we haven't sent yet
if (test) 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 plexm = plexContent.Where(x => x.Type == PlexMediaTypeEntity.Movie).OrderByDescending(x => x.AddedAt).Take(10); _log.LogInformation("Plex Movies to send: {0}", plexContentMoviesToSend.Count());
var embym = embyContent.Where(x => x.Type == EmbyMediaType.Movie).OrderByDescending(x => x.AddedAt).Take(10); _log.LogInformation("Emby Movies to send: {0}", embyContentMoviesToSend.Count());
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, settings);
}
else
{
body = await BuildHtml(plexContentMoviesToSend, embyContentMoviesToSend, plexEpisodesToSend, embyEpisodesToSend, settings);
if (body.IsNullOrEmpty())
{
return;
}
} 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();
if (!test) _log.LogInformation("Plex Episodes to send: {0}", plexEpisodesToSend.Count());
{ _log.LogInformation("Emby Episodes to send: {0}", embyEpisodesToSend.Count());
// Get the users to send it to
var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.RecievesNewsletter); var body = string.Empty;
if (!users.Any()) if (test)
{ {
return; 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 = _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, settings);
} }
else
foreach (var emails in settings.ExternalEmails)
{ {
users.Add(new OmbiUser body = await BuildHtml(plexContentMoviesToSend, embyContentMoviesToSend, plexEpisodesToSend, embyEpisodesToSend, settings);
if (body.IsNullOrEmpty())
{ {
UserName = emails, return;
Email = emails }
});
} }
var emailTasks = new List<Task>();
foreach (var user in users) if (!test)
{ {
if (user.Email.IsNullOrEmpty()) // Get the users to send it to
var users = await _userManager.GetUsersInRoleAsync(OmbiRoles.RecievesNewsletter);
if (!users.Any())
{ {
continue; return;
}
foreach (var emails in settings.ExternalEmails)
{
users.Add(new OmbiUser
{
UserName = emails,
Email = emails
});
} }
var emailTasks = new List<Task>();
foreach (var user in users)
{
if (user.Email.IsNullOrEmpty())
{
continue;
}
var messageContent = ParseTemplate(template, customization, user); var messageContent = ParseTemplate(template, customization, user);
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);
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)
{
recentlyAddedLog.Add(new RecentlyAddedLog
{ {
AddedAt = DateTime.Now, recentlyAddedLog.Add(new RecentlyAddedLog
Type = RecentlyAddedType.Plex, {
ContentType = ContentType.Parent, AddedAt = DateTime.Now,
ContentId = p.Id Type = RecentlyAddedType.Plex,
}); ContentType = ContentType.Parent,
ContentId = p.Id
});
} }
foreach (var p in plexEpisodesToSend) foreach (var p in plexEpisodesToSend)
{
recentlyAddedLog.Add(new RecentlyAddedLog
{ {
AddedAt = DateTime.Now, recentlyAddedLog.Add(new RecentlyAddedLog
Type = RecentlyAddedType.Plex, {
ContentType = ContentType.Episode, AddedAt = DateTime.Now,
ContentId = p.Id Type = RecentlyAddedType.Plex,
}); ContentType = ContentType.Episode,
} ContentId = p.Id
});
}
foreach (var e in embyContentMoviesToSend) foreach (var e in embyContentMoviesToSend)
{ {
if (e.Type == EmbyMediaType.Movie) if (e.Type == EmbyMediaType.Movie)
{
recentlyAddedLog.Add(new RecentlyAddedLog
{
AddedAt = DateTime.Now,
Type = RecentlyAddedType.Emby,
ContentType = ContentType.Parent,
ContentId = e.Id
});
}
}
foreach (var p in embyEpisodesToSend)
{ {
recentlyAddedLog.Add(new RecentlyAddedLog recentlyAddedLog.Add(new RecentlyAddedLog
{ {
AddedAt = DateTime.Now, AddedAt = DateTime.Now,
Type = RecentlyAddedType.Emby, Type = RecentlyAddedType.Emby,
ContentType = ContentType.Parent, ContentType = ContentType.Episode,
ContentId = e.Id ContentId = p.Id
}); });
} }
} await _recentlyAddedLog.AddRange(recentlyAddedLog);
foreach (var p in embyEpisodesToSend) await Task.WhenAll(emailTasks.ToArray());
{
recentlyAddedLog.Add(new RecentlyAddedLog
{
AddedAt = DateTime.Now,
Type = RecentlyAddedType.Emby,
ContentType = ContentType.Episode,
ContentId = p.Id
});
} }
await _recentlyAddedLog.AddRange(recentlyAddedLog); else
await Task.WhenAll(emailTasks.ToArray());
}
else
{
var admins = await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin);
foreach (var a in admins)
{ {
if (a.Email.IsNullOrEmpty()) var admins = await _userManager.GetUsersInRoleAsync(OmbiRoles.Admin);
foreach (var a in admins)
{ {
continue; if (a.Email.IsNullOrEmpty())
} {
var messageContent = ParseTemplate(template, customization, a); continue;
}
var messageContent = ParseTemplate(template, customization, a);
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);
}
} }
}
catch (Exception e)
{
_log.LogError(e, "Error when attempting to create newsletter");
throw;
} }
} }
@ -285,8 +303,7 @@ namespace Ombi.Schedule.Jobs.Ombi
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); _log.LogError(e, "Error when Processing Plex Movies {0}", info.Title);
throw;
} }
finally finally
{ {
@ -327,8 +344,7 @@ namespace Ombi.Schedule.Jobs.Ombi
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e); _log.LogError(e, "Error when processing Emby Movies {0}", info.Title);
throw;
} }
finally finally
{ {
@ -483,7 +499,7 @@ namespace Ombi.Schedule.Jobs.Ombi
} }
catch (Exception e) catch (Exception e)
{ {
//Log.Error(e); _log.LogError(e, "Error when processing Plex TV {0}", t.Title);
} }
finally finally
{ {
@ -584,7 +600,7 @@ namespace Ombi.Schedule.Jobs.Ombi
} }
catch (Exception e) catch (Exception e)
{ {
//Log.Error(e); _log.LogError(e, "Error when processing Emby TV {0}", t.Title);
} }
finally finally
{ {

@ -7,6 +7,6 @@ namespace Ombi.Settings.Settings.Models.Notifications
public bool DisableTv { get; set; } public bool DisableTv { get; set; }
public bool DisableMovies { get; set; } public bool DisableMovies { get; set; }
public bool Enabled { get; set; } public bool Enabled { get; set; }
public List<string> ExternalEmails { get; set; } public List<string> ExternalEmails { get; set; } = new List<string>();
} }
} }
Loading…
Cancel
Save