mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 18:47:15 -07:00
Initial impliementation of #739
This commit is contained in:
parent
88c0651b1e
commit
973688393f
18 changed files with 624 additions and 21 deletions
|
@ -93,6 +93,8 @@ namespace Ombi.UI.Modules.Admin
|
|||
private IAnalytics Analytics { get; }
|
||||
private IRecentlyAdded RecentlyAdded { get; }
|
||||
private ISettingsService<NotificationSettingsV2> NotifySettings { get; }
|
||||
private ISettingsService<DiscordNotificationSettings> DiscordSettings { get; }
|
||||
private IDiscordApi DiscordApi { get; }
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public AdminModule(ISettingsService<PlexRequestSettings> prService,
|
||||
|
@ -118,7 +120,9 @@ namespace Ombi.UI.Modules.Admin
|
|||
ISlackApi slackApi, ISettingsService<LandingPageSettings> lp,
|
||||
ISettingsService<ScheduledJobsSettings> scheduler, IJobRecord rec, IAnalytics analytics,
|
||||
ISettingsService<NotificationSettingsV2> notifyService, IRecentlyAdded recentlyAdded,
|
||||
ISettingsService<WatcherSettings> watcherSettings
|
||||
ISettingsService<WatcherSettings> watcherSettings ,
|
||||
ISettingsService<DiscordNotificationSettings> discord,
|
||||
IDiscordApi discordapi
|
||||
, ISecurityExtensions security) : base("admin", prService, security)
|
||||
{
|
||||
PrService = prService;
|
||||
|
@ -150,6 +154,8 @@ namespace Ombi.UI.Modules.Admin
|
|||
NotifySettings = notifyService;
|
||||
RecentlyAdded = recentlyAdded;
|
||||
WatcherSettings = watcherSettings;
|
||||
DiscordSettings = discord;
|
||||
DiscordApi = discordapi;
|
||||
|
||||
Before += (ctx) => Security.AdminLoginRedirect(Permissions.Administrator, ctx);
|
||||
|
||||
|
@ -208,10 +214,13 @@ namespace Ombi.UI.Modules.Admin
|
|||
|
||||
|
||||
Post["/testslacknotification", true] = async (x, ct) => await TestSlackNotification();
|
||||
|
||||
Get["/slacknotification"] = _ => SlackNotifications();
|
||||
Post["/slacknotification"] = _ => SaveSlackNotifications();
|
||||
|
||||
Post["/testdiscordnotification", true] = async (x, ct) => await TestDiscordNotification();
|
||||
Get["/discordnotification", true] = async (x, ct) => await DiscordNotification();
|
||||
Post["/discordnotification", true] = async (x, ct) => await SaveDiscordNotifications();
|
||||
|
||||
Get["/landingpage", true] = async (x, ct) => await LandingPage();
|
||||
Post["/landingpage", true] = async (x, ct) => await SaveLandingPage();
|
||||
|
||||
|
@ -918,6 +927,71 @@ namespace Ombi.UI.Modules.Admin
|
|||
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
|
||||
}
|
||||
|
||||
private async Task<Negotiator> DiscordNotification()
|
||||
{
|
||||
var settings = await DiscordSettings.GetSettingsAsync();
|
||||
return View["DiscordNotification", settings];
|
||||
}
|
||||
|
||||
private async Task<Response> TestDiscordNotification()
|
||||
{
|
||||
var settings = this.BindAndValidate<DiscordNotificationSettings>();
|
||||
if (!ModelValidationResult.IsValid)
|
||||
{
|
||||
return Response.AsJson(ModelValidationResult.SendJsonError());
|
||||
}
|
||||
var notificationModel = new NotificationModel
|
||||
{
|
||||
NotificationType = NotificationType.Test,
|
||||
DateTime = DateTime.Now
|
||||
};
|
||||
|
||||
var currentDicordSettings = await DiscordSettings.GetSettingsAsync();
|
||||
try
|
||||
{
|
||||
NotificationService.Subscribe(new DiscordNotification(DiscordApi, DiscordSettings));
|
||||
settings.Enabled = true;
|
||||
await NotificationService.Publish(notificationModel, settings);
|
||||
Log.Info("Sent Discord notification test");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e, "Failed to subscribe and publish test Discord Notification");
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!currentDicordSettings.Enabled)
|
||||
{
|
||||
NotificationService.UnSubscribe(new DiscordNotification(DiscordApi, DiscordSettings));
|
||||
}
|
||||
}
|
||||
return Response.AsJson(new JsonResponseModel { Result = true, Message = "Successfully sent a test Discord Notification! If you do not receive it please check the logs." });
|
||||
}
|
||||
|
||||
private async Task<Response> SaveDiscordNotifications()
|
||||
{
|
||||
var settings = this.BindAndValidate<DiscordNotificationSettings>();
|
||||
if (!ModelValidationResult.IsValid)
|
||||
{
|
||||
return Response.AsJson(ModelValidationResult.SendJsonError());
|
||||
}
|
||||
|
||||
var result = await DiscordSettings.SaveSettingsAsync(settings);
|
||||
if (settings.Enabled)
|
||||
{
|
||||
NotificationService.Subscribe(new DiscordNotification(DiscordApi, DiscordSettings));
|
||||
}
|
||||
else
|
||||
{
|
||||
NotificationService.UnSubscribe(new DiscordNotification(DiscordApi, DiscordSettings));
|
||||
}
|
||||
|
||||
Log.Info("Saved discord settings, result: {0}", result);
|
||||
return Response.AsJson(result
|
||||
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Discord Notifications!" }
|
||||
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
|
||||
}
|
||||
|
||||
private async Task<Negotiator> LandingPage()
|
||||
{
|
||||
var settings = await LandingSettings.GetSettingsAsync();
|
||||
|
|
|
@ -115,7 +115,7 @@ namespace Ombi.UI.Modules
|
|||
var username = Security.GetUsername(User.UserName, Session);
|
||||
if (string.IsNullOrEmpty(username))
|
||||
{
|
||||
return Session[SessionKeys.UsernameKey].ToString();
|
||||
return "Unknown User";
|
||||
}
|
||||
_username = username;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue