mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 02:26:55 -07:00
Added the actual notification part of #27
This commit is contained in:
parent
3ea708aca5
commit
c50e2bb644
9 changed files with 135 additions and 113 deletions
|
@ -71,8 +71,8 @@ namespace PlexRequests.Services.Notification
|
|||
await EmailIssue(model, emailSettings);
|
||||
break;
|
||||
case NotificationType.RequestAvailable:
|
||||
throw new NotImplementedException();
|
||||
|
||||
await EmailAvailableRequest(model, emailSettings);
|
||||
break;
|
||||
case NotificationType.RequestApproved:
|
||||
throw new NotImplementedException();
|
||||
|
||||
|
@ -120,23 +120,7 @@ namespace PlexRequests.Services.Notification
|
|||
Subject = $"Plex Requests: New request for {model.Title}!"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (var smtp = new SmtpClient(settings.EmailHost, settings.EmailPort))
|
||||
{
|
||||
smtp.Credentials = new NetworkCredential(settings.EmailUsername, settings.EmailPassword);
|
||||
smtp.EnableSsl = settings.Ssl;
|
||||
await smtp.SendMailAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (SmtpException smtp)
|
||||
{
|
||||
Log.Error(smtp);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
await Send(message, settings);
|
||||
}
|
||||
|
||||
private async Task EmailIssue(NotificationModel model, EmailNotificationSettings settings)
|
||||
|
@ -146,10 +130,34 @@ namespace PlexRequests.Services.Notification
|
|||
IsBodyHtml = true,
|
||||
To = { new MailAddress(settings.RecipientEmail) },
|
||||
Body = $"Hello! The user '{model.User}' has reported a new issue {model.Body} for the title {model.Title}!",
|
||||
From = new MailAddress(settings.RecipientEmail),
|
||||
From = new MailAddress(settings.EmailSender),
|
||||
Subject = $"Plex Requests: New issue for {model.Title}!"
|
||||
};
|
||||
|
||||
await Send(message, settings);
|
||||
}
|
||||
|
||||
private async Task EmailAvailableRequest(NotificationModel model, EmailNotificationSettings settings)
|
||||
{
|
||||
if (!settings.EnableUserEmailNotifications)
|
||||
{
|
||||
await Task.FromResult(false);
|
||||
}
|
||||
|
||||
var message = new MailMessage
|
||||
{
|
||||
IsBodyHtml = true,
|
||||
To = { new MailAddress(model.UserEmail) },
|
||||
Body = $"Hello! You requested {model.Title} on PlexRequests! This is now available on Plex! :)",
|
||||
From = new MailAddress(settings.EmailSender),
|
||||
Subject = $"Plex Requests: {model.Title} is now available!"
|
||||
};
|
||||
|
||||
await Send(message, settings);
|
||||
}
|
||||
|
||||
private async Task Send(MailMessage message, EmailNotificationSettings settings)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var smtp = new SmtpClient(settings.EmailHost, settings.EmailPort))
|
||||
|
@ -176,27 +184,11 @@ namespace PlexRequests.Services.Notification
|
|||
IsBodyHtml = true,
|
||||
To = { new MailAddress(settings.RecipientEmail) },
|
||||
Body = "This is just a test! Success!",
|
||||
From = new MailAddress(settings.RecipientEmail),
|
||||
From = new MailAddress(settings.EmailSender),
|
||||
Subject = "Plex Requests: Test Message!"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
using (var smtp = new SmtpClient(settings.EmailHost, settings.EmailPort))
|
||||
{
|
||||
smtp.Credentials = new NetworkCredential(settings.EmailUsername, settings.EmailPassword);
|
||||
smtp.EnableSsl = settings.Ssl;
|
||||
await smtp.SendMailAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (SmtpException smtp)
|
||||
{
|
||||
Log.Error(smtp);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
await Send(message, settings);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -35,5 +35,6 @@ namespace PlexRequests.Services.Notification
|
|||
public DateTime DateTime { get; set; }
|
||||
public NotificationType NotificationType { get; set; }
|
||||
public string User { get; set; }
|
||||
public string UserEmail { get; set; }
|
||||
}
|
||||
}
|
|
@ -41,6 +41,11 @@ namespace PlexRequests.Services.Notification
|
|||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public ConcurrentDictionary<string, INotification> Observers { get; } = new ConcurrentDictionary<string, INotification>();
|
||||
|
||||
/// <summary>
|
||||
/// Sends a notification to the user. This one is used in normal notification scenarios
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <returns></returns>
|
||||
public async Task Publish(NotificationModel model)
|
||||
{
|
||||
var notificationTasks = Observers.Values.Select(notification => NotifyAsync(notification, model));
|
||||
|
@ -48,6 +53,12 @@ namespace PlexRequests.Services.Notification
|
|||
await Task.WhenAll(notificationTasks).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sends a notification to the user, this is usually for testing the settings.
|
||||
/// </summary>
|
||||
/// <param name="model">The model.</param>
|
||||
/// <param name="settings">The settings.</param>
|
||||
/// <returns></returns>
|
||||
public async Task Publish(NotificationModel model, Settings settings)
|
||||
{
|
||||
var notificationTasks = Observers.Values.Select(notification => NotifyAsync(notification, model, settings));
|
||||
|
|
|
@ -33,6 +33,7 @@ namespace PlexRequests.Services.Notification
|
|||
RequestAvailable,
|
||||
RequestApproved,
|
||||
AdminNote,
|
||||
Test
|
||||
Test,
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,6 @@ namespace PlexRequests.Services.Notification
|
|||
}
|
||||
private IPushbulletApi PushbulletApi { get; }
|
||||
private ISettingsService<PushbulletNotificationSettings> SettingsService { get; }
|
||||
private PushbulletNotificationSettings Settings => GetSettings();
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public string NotificationName => "PushbulletNotification";
|
||||
|
@ -107,45 +106,28 @@ namespace PlexRequests.Services.Notification
|
|||
{
|
||||
var message = $"{model.Title} has been requested by user: {model.User}";
|
||||
var pushTitle = $"Plex Requests: {model.Title} has been requested!";
|
||||
try
|
||||
{
|
||||
var result = await PushbulletApi.PushAsync(settings.AccessToken, pushTitle, message, settings.DeviceIdentifier);
|
||||
if (result == null)
|
||||
{
|
||||
Log.Error("Pushbullet api returned a null value, the notification did not get pushed");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
await Push(settings, message, pushTitle);
|
||||
}
|
||||
|
||||
private async Task PushIssueAsync(NotificationModel model, PushbulletNotificationSettings settings)
|
||||
{
|
||||
var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
|
||||
var pushTitle = $"Plex Requests: A new issue has been reported for {model.Title}";
|
||||
try
|
||||
{
|
||||
var result = await PushbulletApi.PushAsync(settings.AccessToken, pushTitle, message, settings.DeviceIdentifier);
|
||||
if (result != null)
|
||||
{
|
||||
Log.Error("Pushbullet api returned a null value, the notification did not get pushed");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
await Push(settings, message, pushTitle);
|
||||
}
|
||||
|
||||
private async Task PushTestAsync(NotificationModel model, PushbulletNotificationSettings settings)
|
||||
{
|
||||
var message = "This is just a test! Success!";
|
||||
var pushTitle = "Plex Requests: Test Message!";
|
||||
await Push(settings, message, pushTitle);
|
||||
}
|
||||
|
||||
private async Task Push(PushbulletNotificationSettings settings, string message, string title)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await PushbulletApi.PushAsync(settings.AccessToken, pushTitle, message, settings.DeviceIdentifier);
|
||||
var result = await PushbulletApi.PushAsync(settings.AccessToken, title, message, settings.DeviceIdentifier);
|
||||
if (result != null)
|
||||
{
|
||||
Log.Error("Pushbullet api returned a null value, the notification did not get pushed");
|
||||
|
|
|
@ -45,7 +45,6 @@ namespace PlexRequests.Services.Notification
|
|||
}
|
||||
private IPushoverApi PushoverApi { get; }
|
||||
private ISettingsService<PushoverNotificationSettings> SettingsService { get; }
|
||||
private PushoverNotificationSettings Settings => GetSettings();
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public string NotificationName => "PushoverNotification";
|
||||
|
@ -106,40 +105,23 @@ namespace PlexRequests.Services.Notification
|
|||
private async Task PushNewRequestAsync(NotificationModel model, PushoverNotificationSettings settings)
|
||||
{
|
||||
var message = $"Plex Requests: {model.Title} has been requested by user: {model.User}";
|
||||
try
|
||||
{
|
||||
var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken);
|
||||
if (result?.status != 1)
|
||||
{
|
||||
Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
await Push(settings, message);
|
||||
}
|
||||
|
||||
private async Task PushIssueAsync(NotificationModel model, PushoverNotificationSettings settings)
|
||||
{
|
||||
var message = $"Plex Requests: A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
|
||||
try
|
||||
{
|
||||
var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken);
|
||||
if (result?.status != 1)
|
||||
{
|
||||
Log.Error("Pushover api returned a status that was not 1, the notification did not get pushed");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
await Push(settings, message);
|
||||
}
|
||||
|
||||
private async Task PushTestAsync(NotificationModel model, PushoverNotificationSettings settings)
|
||||
{
|
||||
var message = $"Plex Requests: Test Message!";
|
||||
await Push(settings, message);
|
||||
}
|
||||
|
||||
private async Task Push(PushoverNotificationSettings settings, string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await PushoverApi.PushAsync(settings.AccessToken, message, settings.UserToken);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue