diff --git a/PlexRequests.UI/Modules/AdminModule.cs b/PlexRequests.UI/Modules/AdminModule.cs index ee7bcb0b8..c62b7ff46 100644 --- a/PlexRequests.UI/Modules/AdminModule.cs +++ b/PlexRequests.UI/Modules/AdminModule.cs @@ -82,6 +82,8 @@ namespace PlexRequests.UI.Modules private IRepository LogsRepo { get; } private INotificationService NotificationService { get; } private ICacheProvider Cache { get; } + private ISettingsService SlackSettings { get; } + private ISlackApi SlackApi { get; } private static Logger Log = LogManager.GetCurrentClassLogger(); public AdminModule(ISettingsService prService, @@ -102,7 +104,8 @@ namespace PlexRequests.UI.Modules INotificationService notify, ISettingsService headphones, ISettingsService logs, - ICacheProvider cache) : base("admin", prService) + ICacheProvider cache, ISettingsService slackSettings, + ISlackApi slackApi) : base("admin", prService) { PrService = prService; CpService = cpService; @@ -123,8 +126,10 @@ namespace PlexRequests.UI.Modules HeadphonesService = headphones; LogService = logs; Cache = cache; + SlackSettings = slackSettings; + SlackApi = slackApi; - this.RequiresClaims(UserClaims.Admin); + this.RequiresClaims(UserClaims.Admin); Get["/"] = _ => Admin(); @@ -176,6 +181,11 @@ namespace PlexRequests.UI.Modules Post["/createapikey"] = x => CreateApiKey(); Post["/autoupdate"] = x => AutoUpdate(); + + Post["/testslacknotification"] = _ => TestSlackNotification(); + + Get["/slacknotification"] = _ => SlackNotifications(); + Post["/slacknotification"] = _ => SaveSlackNotifications(); } private Negotiator Authentication() @@ -735,5 +745,65 @@ namespace PlexRequests.UI.Modules return Response.AsJson(apiKey); } + + private Response TestSlackNotification() + { + var settings = this.BindAndValidate(); + if (!ModelValidationResult.IsValid) + { + return Response.AsJson(ModelValidationResult.SendJsonError()); + } + var notificationModel = new NotificationModel + { + NotificationType = NotificationType.Test, + DateTime = DateTime.Now + }; + try + { + NotificationService.Subscribe(new SlackNotification(SlackApi, SlackSettings)); + settings.Enabled = true; + NotificationService.Publish(notificationModel, settings); + Log.Info("Sent slack notification test"); + } + catch (Exception e) + { + Log.Error(e, "Failed to subscribe and publish test Slack Notification"); + } + finally + { + NotificationService.UnSubscribe(new SlackNotification(SlackApi, SlackSettings)); + } + return Response.AsJson(new JsonResponseModel { Result = true, Message = "Successfully sent a test Slack Notification! If you do not receive it please check the logs." }); + } + + private Negotiator SlackNotifications() + { + var settings = SlackSettings.GetSettings(); + return View["SlackNotifications", settings]; + } + + private Response SaveSlackNotifications() + { + var settings = this.BindAndValidate(); + if (!ModelValidationResult.IsValid) + { + return Response.AsJson(ModelValidationResult.SendJsonError()); + } + + var result = SlackSettings.SaveSettings(settings); + if (settings.Enabled) + { + NotificationService.Subscribe(new SlackNotification(SlackApi, SlackSettings)); + } + else + { + NotificationService.UnSubscribe(new SlackNotification(SlackApi, SlackSettings)); + } + + Log.Info("Saved slack settings, result: {0}", result); + return Response.AsJson(result + ? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Slack Notifications!" } + : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." }); + } } } \ No newline at end of file diff --git a/PlexRequests.UI/Modules/AdminNotificationsModule.cs b/PlexRequests.UI/Modules/AdminNotificationsModule.cs deleted file mode 100644 index 709759d2e..000000000 --- a/PlexRequests.UI/Modules/AdminNotificationsModule.cs +++ /dev/null @@ -1,130 +0,0 @@ -#region Copyright -// /************************************************************************ -// Copyright (c) 2016 Jamie Rees -// File: AdminNotificationsModule.cs -// Created By: Jamie Rees -// -// Permission is hereby granted, free of charge, to any person obtaining -// a copy of this software and associated documentation files (the -// "Software"), to deal in the Software without restriction, including -// without limitation the rights to use, copy, modify, merge, publish, -// distribute, sublicense, and/or sell copies of the Software, and to -// permit persons to whom the Software is furnished to do so, subject to -// the following conditions: -// -// The above copyright notice and this permission notice shall be -// included in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -// ************************************************************************/ -#endregion -using System; - -using Nancy; -using Nancy.ModelBinding; -using Nancy.Responses.Negotiation; -using Nancy.Security; -using Nancy.Validation; - -using NLog; - -using PlexRequests.Api.Interfaces; -using PlexRequests.Core; -using PlexRequests.Core.SettingModels; -using PlexRequests.Helpers; -using PlexRequests.Services.Interfaces; -using PlexRequests.Services.Notification; -using PlexRequests.UI.Helpers; -using PlexRequests.UI.Models; - -namespace PlexRequests.UI.Modules -{ - public class AdminNotificationsModule : BaseModule - { - public AdminNotificationsModule(ISettingsService prService, ISettingsService slackSettings, - INotificationService notify, ISlackApi slackApi) : base("admin", prService) - { - this.RequiresClaims(UserClaims.Admin); - - SlackSettings = slackSettings; - NotificationService = notify; - SlackApi = slackApi; - - Post["/testslacknotification"] = _ => TestSlackNotification(); - - Get["/slacknotification"] = _ => SlackNotifications(); - Post["/slacknotification"] = _ => SaveSlackNotifications(); - } - private ISettingsService SlackSettings { get; } - private INotificationService NotificationService { get; } - private ISlackApi SlackApi { get; } - - private static Logger Log = LogManager.GetCurrentClassLogger(); - - private Response TestSlackNotification() - { - var settings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) - { - return Response.AsJson(ModelValidationResult.SendJsonError()); - } - var notificationModel = new NotificationModel - { - NotificationType = NotificationType.Test, - DateTime = DateTime.Now - }; - try - { - NotificationService.Subscribe(new SlackNotification(SlackApi,SlackSettings)); - settings.Enabled = true; - NotificationService.Publish(notificationModel, settings); - Log.Info("Sent slack notification test"); - } - catch (Exception e) - { - Log.Error(e,"Failed to subscribe and publish test Slack Notification"); - } - finally - { - NotificationService.UnSubscribe(new SlackNotification(SlackApi, SlackSettings)); - } - return Response.AsJson(new JsonResponseModel { Result = true, Message = "Successfully sent a test Slack Notification! If you do not receive it please check the logs." }); - } - - private Negotiator SlackNotifications() - { - var settings = SlackSettings.GetSettings(); - return View["Admin/SlackNotifications", settings]; - } - - private Response SaveSlackNotifications() - { - var settings = this.BindAndValidate(); - if (!ModelValidationResult.IsValid) - { - return Response.AsJson(ModelValidationResult.SendJsonError()); - } - - var result = SlackSettings.SaveSettings(settings); - if (settings.Enabled) - { - NotificationService.Subscribe(new SlackNotification(SlackApi, SlackSettings)); - } - else - { - NotificationService.UnSubscribe(new SlackNotification(SlackApi, SlackSettings)); - } - - Log.Info("Saved slack settings, result: {0}", result); - return Response.AsJson(result - ? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Slack Notifications!" } - : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." }); - } - } -} \ No newline at end of file diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 563edeb50..0029e9ed6 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -182,7 +182,6 @@ -