mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-31 03:50:08 -07:00
#164 has been resolved
This commit is contained in:
parent
8cce80654b
commit
5a6863456f
22 changed files with 663 additions and 8 deletions
37
PlexRequests.Api.Interfaces/ISlackApi.cs
Normal file
37
PlexRequests.Api.Interfaces/ISlackApi.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ISlackApi.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.Threading.Tasks;
|
||||
|
||||
using PlexRequests.Api.Models.Notifications;
|
||||
|
||||
namespace PlexRequests.Api.Interfaces
|
||||
{
|
||||
public interface ISlackApi
|
||||
{
|
||||
Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message);
|
||||
}
|
||||
}
|
|
@ -51,6 +51,7 @@
|
|||
<Compile Include="IMusicBrainzApi.cs" />
|
||||
<Compile Include="IPlexApi.cs" />
|
||||
<Compile Include="IPushbulletApi.cs" />
|
||||
<Compile Include="ISlackApi.cs" />
|
||||
<Compile Include="IPushoverApi.cs" />
|
||||
<Compile Include="ISickRageApi.cs" />
|
||||
<Compile Include="ISonarrApi.cs" />
|
||||
|
|
|
@ -0,0 +1,56 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: SlackNotificationBody.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 Newtonsoft.Json;
|
||||
|
||||
namespace PlexRequests.Api.Models.Notifications
|
||||
{
|
||||
public class SlackNotificationBody
|
||||
{
|
||||
[JsonConstructor]
|
||||
public SlackNotificationBody()
|
||||
{
|
||||
username = "Plex Requests";
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
private string _username;
|
||||
public string username
|
||||
{
|
||||
get { return _username; }
|
||||
set
|
||||
{
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
_username = value;
|
||||
}
|
||||
}
|
||||
public string channel { get; set; }
|
||||
public string text { get; set; }
|
||||
|
||||
public string icon_url { get; set; }
|
||||
public string icon_emoji { get; set; }
|
||||
}
|
||||
}
|
|
@ -58,6 +58,7 @@
|
|||
<Compile Include="Notifications\PushbulletPush.cs" />
|
||||
<Compile Include="Notifications\PushbulletResponse.cs" />
|
||||
<Compile Include="Notifications\PushoverResponse.cs" />
|
||||
<Compile Include="Notifications\SlackNotificationBody.cs" />
|
||||
<Compile Include="Plex\PlexAccount.cs" />
|
||||
<Compile Include="Plex\PlexAuthentication.cs" />
|
||||
<Compile Include="Plex\PlexError.cs" />
|
||||
|
|
|
@ -109,7 +109,6 @@ namespace PlexRequests.Api
|
|||
public T ExecuteJson<T>(IRestRequest request, Uri baseUri) where T : new()
|
||||
{
|
||||
var client = new RestClient { BaseUrl = baseUri };
|
||||
|
||||
var response = client.Execute(request);
|
||||
Log.Trace("Api Content Response:");
|
||||
Log.Trace(response.Content);
|
||||
|
|
|
@ -71,6 +71,7 @@
|
|||
<DependentUpon>MockApiData.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Mocks\MockSonarrApi.cs" />
|
||||
<Compile Include="SlackApi.cs" />
|
||||
<Compile Include="PushoverApi.cs" />
|
||||
<Compile Include="PushbulletApi.cs" />
|
||||
<Compile Include="SickrageApi.cs" />
|
||||
|
|
62
PlexRequests.Api/SlackApi.cs
Normal file
62
PlexRequests.Api/SlackApi.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PlexApi.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 System.Threading.Tasks;
|
||||
|
||||
using PlexRequests.Api.Interfaces;
|
||||
using PlexRequests.Api.Models.Notifications;
|
||||
|
||||
using RestSharp;
|
||||
|
||||
namespace PlexRequests.Api
|
||||
{
|
||||
public class SlackApi : ISlackApi
|
||||
{
|
||||
public async Task<string> PushAsync(string team, string token, string service, SlackNotificationBody message)
|
||||
{
|
||||
var request = new RestRequest
|
||||
{
|
||||
Method = Method.POST,
|
||||
Resource = "/services/{team}/{service}/{token}"
|
||||
};
|
||||
|
||||
request.AddUrlSegment("team", team);
|
||||
request.AddUrlSegment("service", service);
|
||||
request.AddUrlSegment("token", token);
|
||||
request.AddJsonBody(message);
|
||||
|
||||
var api = new ApiRequest();
|
||||
return await Task.Run(
|
||||
() =>
|
||||
{
|
||||
var result = api.Execute(request, new Uri("https://hooks.slack.com/"));
|
||||
return result.Content;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -73,6 +73,7 @@
|
|||
<Compile Include="Models\UserProperties.cs" />
|
||||
<Compile Include="SettingModels\AuthenticationSettings.cs" />
|
||||
<Compile Include="SettingModels\HeadphonesSettings.cs" />
|
||||
<Compile Include="SettingModels\SlackNotificationSettings.cs" />
|
||||
<Compile Include="SettingModels\PushoverNotificationSettings.cs" />
|
||||
<Compile Include="SettingModels\PushBulletNotificationSettings.cs" />
|
||||
<Compile Include="SettingModels\EmailNotificationSettings.cs" />
|
||||
|
|
37
PlexRequests.Core/SettingModels/SlackNotificationSettings.cs
Normal file
37
PlexRequests.Core/SettingModels/SlackNotificationSettings.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using System;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace PlexRequests.Core.SettingModels
|
||||
{
|
||||
public class SlackNotificationSettings : Settings
|
||||
{
|
||||
public bool Enabled { get; set; }
|
||||
public string WebhookUrl { get; set; }
|
||||
public string Channel { get; set; }
|
||||
public string Username { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public string Team => SplitWebUrl(3);
|
||||
|
||||
[JsonIgnore]
|
||||
public string Service => SplitWebUrl(4);
|
||||
|
||||
[JsonIgnore]
|
||||
public string Token => SplitWebUrl(5);
|
||||
|
||||
private string SplitWebUrl(int index)
|
||||
{
|
||||
if (!WebhookUrl.StartsWith("http", StringComparison.InvariantCulture))
|
||||
{
|
||||
WebhookUrl = "https://" + WebhookUrl;
|
||||
}
|
||||
var split = WebhookUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return split.Length < index
|
||||
? string.Empty
|
||||
: split[index];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -77,7 +77,7 @@ namespace PlexRequests.Services.Notification
|
|||
case NotificationType.AdminNote:
|
||||
break;
|
||||
case NotificationType.Test:
|
||||
await PushTestAsync(model, pushSettings);
|
||||
await PushTestAsync(pushSettings);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
@ -116,7 +116,7 @@ namespace PlexRequests.Services.Notification
|
|||
await Push(settings, message, pushTitle);
|
||||
}
|
||||
|
||||
private async Task PushTestAsync(NotificationModel model, PushbulletNotificationSettings settings)
|
||||
private async Task PushTestAsync(PushbulletNotificationSettings settings)
|
||||
{
|
||||
var message = "This is just a test! Success!";
|
||||
var pushTitle = "Plex Requests: Test Message!";
|
||||
|
|
155
PlexRequests.Services/Notification/SlackNotification.cs
Normal file
155
PlexRequests.Services/Notification/SlackNotification.cs
Normal file
|
@ -0,0 +1,155 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: SlackNotification.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 System.Threading.Tasks;
|
||||
|
||||
using NLog;
|
||||
|
||||
using PlexRequests.Api.Interfaces;
|
||||
using PlexRequests.Api.Models.Notifications;
|
||||
using PlexRequests.Core;
|
||||
using PlexRequests.Core.SettingModels;
|
||||
using PlexRequests.Services.Interfaces;
|
||||
|
||||
namespace PlexRequests.Services.Notification
|
||||
{
|
||||
public class SlackNotification : INotification
|
||||
{
|
||||
public SlackNotification(ISlackApi api, ISettingsService<SlackNotificationSettings> sn)
|
||||
{
|
||||
Api = api;
|
||||
Settings = sn;
|
||||
}
|
||||
|
||||
public string NotificationName => "SlackNotification";
|
||||
|
||||
private ISlackApi Api { get; }
|
||||
private ISettingsService<SlackNotificationSettings> Settings { get; }
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
|
||||
public async Task NotifyAsync(NotificationModel model)
|
||||
{
|
||||
var settings = Settings.GetSettings();
|
||||
|
||||
await NotifyAsync(model, settings);
|
||||
}
|
||||
|
||||
public async Task NotifyAsync(NotificationModel model, Settings settings)
|
||||
{
|
||||
if (settings == null) await NotifyAsync(model);
|
||||
|
||||
var pushSettings = (SlackNotificationSettings)settings;
|
||||
if (!ValidateConfiguration(pushSettings))
|
||||
{
|
||||
Log.Error("Settings for Slack was not correct, we cannot push a notification");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (model.NotificationType)
|
||||
{
|
||||
case NotificationType.NewRequest:
|
||||
await PushNewRequestAsync(model, pushSettings);
|
||||
break;
|
||||
case NotificationType.Issue:
|
||||
await PushIssueAsync(model, pushSettings);
|
||||
break;
|
||||
case NotificationType.RequestAvailable:
|
||||
break;
|
||||
case NotificationType.RequestApproved:
|
||||
break;
|
||||
case NotificationType.AdminNote:
|
||||
break;
|
||||
case NotificationType.Test:
|
||||
await PushTest(pushSettings);
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PushNewRequestAsync(NotificationModel model, SlackNotificationSettings settings)
|
||||
{
|
||||
var message = $"{model.Title} has been requested by user: {model.User}";
|
||||
await Push(settings, message);
|
||||
}
|
||||
|
||||
private async Task PushIssueAsync(NotificationModel model, SlackNotificationSettings settings)
|
||||
{
|
||||
var message = $"A new issue: {model.Body} has been reported by user: {model.User} for the title: {model.Title}";
|
||||
await Push(settings, message);
|
||||
}
|
||||
|
||||
private async Task PushTest(SlackNotificationSettings settings)
|
||||
{
|
||||
var message = $"This is a test from Plex Requests, if you can see this then we have successfully pushed a notification!";
|
||||
await Push(settings, message);
|
||||
}
|
||||
|
||||
private async Task Push(SlackNotificationSettings config, string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
var notification = new SlackNotificationBody { username = config.Username, channel = config.Channel ?? string.Empty, text = message };
|
||||
|
||||
var result = await Api.PushAsync(config.Team, config.Token, config.Service, notification);
|
||||
if (!result.Equals("ok"))
|
||||
{
|
||||
Log.Error("Slack returned a message that was not 'ok', the notification did not get pushed");
|
||||
Log.Error($"Message that slack returned: {result}");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
}
|
||||
}
|
||||
|
||||
private bool ValidateConfiguration(SlackNotificationSettings settings)
|
||||
{
|
||||
if (!settings.Enabled)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (string.IsNullOrEmpty(settings.WebhookUrl))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try
|
||||
{
|
||||
var a = settings.Team;
|
||||
var b = settings.Service;
|
||||
var c = settings.Token;
|
||||
}
|
||||
catch (IndexOutOfRangeException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -93,6 +93,7 @@
|
|||
<Compile Include="Notification\NotificationType.cs" />
|
||||
<Compile Include="Notification\PushoverNotification.cs" />
|
||||
<Compile Include="Notification\PushbulletNotification.cs" />
|
||||
<Compile Include="Notification\SlackNotification.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -83,6 +83,7 @@ namespace PlexRequests.UI
|
|||
container.Register<ISettingsService<PushoverNotificationSettings>, SettingsServiceV2<PushoverNotificationSettings>>();
|
||||
container.Register<ISettingsService<HeadphonesSettings>, SettingsServiceV2<HeadphonesSettings>>();
|
||||
container.Register<ISettingsService<LogSettings>, SettingsServiceV2<LogSettings>>();
|
||||
container.Register<ISettingsService<SlackNotificationSettings>, SettingsServiceV2<SlackNotificationSettings>>();
|
||||
|
||||
// Repo's
|
||||
container.Register<IRepository<LogEntity>, GenericRepository<LogEntity>>();
|
||||
|
@ -108,6 +109,7 @@ namespace PlexRequests.UI
|
|||
container.Register<IPlexApi, PlexApi>();
|
||||
container.Register<IMusicBrainzApi, MusicBrainzApi>();
|
||||
container.Register<IHeadphonesApi, HeadphonesApi>();
|
||||
container.Register<ISlackApi, SlackApi>();
|
||||
|
||||
// NotificationService
|
||||
container.Register<INotificationService, NotificationService>().AsSingleton();
|
||||
|
@ -193,6 +195,13 @@ namespace PlexRequests.UI
|
|||
{
|
||||
notificationService.Subscribe(new PushoverNotification(container.Resolve<IPushoverApi>(), pushoverService));
|
||||
}
|
||||
|
||||
var slackService = container.Resolve<ISettingsService<SlackNotificationSettings>>();
|
||||
var slackSettings = slackService.GetSettings();
|
||||
if (slackSettings.Enabled)
|
||||
{
|
||||
notificationService.Subscribe(new SlackNotification(container.Resolve<ISlackApi>(), slackService));
|
||||
}
|
||||
}
|
||||
|
||||
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
|
||||
|
|
130
PlexRequests.UI/Modules/AdminNotificationsModule.cs
Normal file
130
PlexRequests.UI/Modules/AdminNotificationsModule.cs
Normal file
|
@ -0,0 +1,130 @@
|
|||
#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<PlexRequestSettings> prService, ISettingsService<SlackNotificationSettings> 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<SlackNotificationSettings> SlackSettings { get; }
|
||||
private INotificationService NotificationService { get; }
|
||||
private ISlackApi SlackApi { get; }
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
private Response TestSlackNotification()
|
||||
{
|
||||
var settings = this.BindAndValidate<SlackNotificationSettings>();
|
||||
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<SlackNotificationSettings>();
|
||||
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." });
|
||||
}
|
||||
}
|
||||
}
|
|
@ -175,6 +175,7 @@
|
|||
<Compile Include="Models\SearchMusicViewModel.cs" />
|
||||
<Compile Include="Models\SearchMovieViewModel.cs" />
|
||||
<Compile Include="Models\UserUpdateViewModel.cs" />
|
||||
<Compile Include="Modules\AdminNotificationsModule.cs" />
|
||||
<Compile Include="Modules\ApiDocsModule.cs" />
|
||||
<Compile Include="Modules\ApiSettingsMetadataModule.cs" />
|
||||
<Compile Include="Modules\ApiUserMetadataModule.cs" />
|
||||
|
@ -186,6 +187,7 @@
|
|||
<Compile Include="Modules\UpdateCheckerModule.cs" />
|
||||
<Compile Include="Start\StartupOptions.cs" />
|
||||
<Compile Include="Start\UpdateValue.cs" />
|
||||
<Compile Include="Validators\SlackSettingsValidator.cs" />
|
||||
<Compile Include="Validators\UserViewModelValidator.cs" />
|
||||
<Compile Include="Validators\HeadphonesValidator.cs" />
|
||||
<Compile Include="Validators\PushoverSettingsValidator.cs" />
|
||||
|
@ -479,6 +481,9 @@
|
|||
<Content Include="Views\Admin\Headphones.cshtml">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<Content Include="Views\Admin\SlackNotifications.cshtml">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Content>
|
||||
<None Include="Web.Debug.config">
|
||||
<DependentUpon>web.config</DependentUpon>
|
||||
</None>
|
||||
|
|
40
PlexRequests.UI/Validators/SlackSettingsValidator.cs
Normal file
40
PlexRequests.UI/Validators/SlackSettingsValidator.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: SlackSettingsValidator.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 FluentValidation;
|
||||
|
||||
using PlexRequests.Core.SettingModels;
|
||||
|
||||
namespace PlexRequests.UI.Validators
|
||||
{
|
||||
public class SlackSettingsValidator : AbstractValidator<SlackNotificationSettings>
|
||||
{
|
||||
public SlackSettingsValidator()
|
||||
{
|
||||
RuleFor(request => request.WebhookUrl).NotEmpty().WithMessage("You must specify a Webhook Url");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -58,7 +58,7 @@
|
|||
<div class="form-group">
|
||||
<label for="username" class="control-label">Username and Password</label>
|
||||
<div>
|
||||
<input type="text" class="form-control form-control-custom" id="username" name="Username" placeholder="Username">
|
||||
<input type="text" class="form-control form-control-custom" id="username" name="Username" placeholder="username">
|
||||
</div>
|
||||
<br/>
|
||||
<div>
|
||||
|
|
119
PlexRequests.UI/Views/Admin/SlackNotifications.cshtml
Normal file
119
PlexRequests.UI/Views/Admin/SlackNotifications.cshtml
Normal file
|
@ -0,0 +1,119 @@
|
|||
@using PlexRequests.UI.Helpers
|
||||
@Html.Partial("_Sidebar")
|
||||
|
||||
<div class="col-sm-8 col-sm-push-1">
|
||||
<form class="form-horizontal" method="POST" id="mainForm">
|
||||
<fieldset>
|
||||
<legend>Slack Notifications</legend>
|
||||
|
||||
<div class="form-group">
|
||||
<div class="checkbox">
|
||||
|
||||
@if (Model.Enabled)
|
||||
{
|
||||
<input type="checkbox" id="Enabled" name="Enabled" checked="checked"><label for="Enabled">Enabled</label>
|
||||
}
|
||||
else
|
||||
{
|
||||
<input type="checkbox" id="Enabled" name="Enabled"><label for="Enabled">Enabled</label>
|
||||
}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="WebhookUrl" class="control-label">Incoming Webhook Url</label>
|
||||
<small class="control-label">This is the full webhook url.</small>
|
||||
<small class="control-label"> Slack > Settings > Add app or integration > Build > Make a Custom Integration > Incoming Webhooks > Add Incoming Webhook. You will then have a Webhook Url</small>
|
||||
<div class="">
|
||||
<input type="text" class="form-control form-control-custom " id="WebhookUrl" name="WebhookUrl" value="@Model.WebhookUrl">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="Channel" class="control-label">Channel Override</label>
|
||||
<small class="control-label">You can override the default channel here</small>
|
||||
<div class="">
|
||||
<input type="text" class="form-control form-control-custom " id="Channel" name="Channel" value="@Model.Channel">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="Username" class="control-label">Username Override</label>
|
||||
<small class="control-label">You can override the default username (Plex Requests) here</small>
|
||||
<div class="">
|
||||
<input type="text" class="form-control form-control-custom " id="Username" name="Username" value="@Model.Username">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<button id="testSlack" type="submit" class="btn btn-primary-outline">Test <div id="spinner"></div></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<div>
|
||||
<button id="save" type="submit" class="btn btn-primary-outline">Submit</button>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
$(function () {
|
||||
|
||||
var base = '@Html.GetBaseUrl()';
|
||||
$('#save').click(function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
$('#spinner').attr("class", "fa fa-spinner fa-spin");
|
||||
var $form = $("#mainForm");
|
||||
$.ajax({
|
||||
type: $form.prop("method"),
|
||||
data: $form.serialize(),
|
||||
url: $form.prop("action"),
|
||||
dataType: "json",
|
||||
success: function (response) {
|
||||
if (response.result === true) {
|
||||
generateNotify(response.message, "success");
|
||||
$('#spinner').attr("class", "fa fa-check");
|
||||
} else {
|
||||
generateNotify(response.message, "warning");
|
||||
$('#spinner').attr("class", "fa fa-times");
|
||||
}
|
||||
},
|
||||
error: function (e) {
|
||||
console.log(e);
|
||||
generateNotify("Something went wrong!", "danger");
|
||||
$('#spinner').attr("class", "fa fa-times");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$('#testSlack').click(function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
var url = createBaseUrl(base, '/admin/testslacknotification');
|
||||
var $form = $("#mainForm");
|
||||
$.ajax({
|
||||
type: $form.prop("method"),
|
||||
data: $form.serialize(),
|
||||
url: url,
|
||||
dataType: "json",
|
||||
success: function (response) {
|
||||
if (response.result === true) {
|
||||
generateNotify(response.message, "success");
|
||||
} else {
|
||||
generateNotify(response.message, "warning");
|
||||
}
|
||||
},
|
||||
error: function (e) {
|
||||
console.log(e);
|
||||
generateNotify("Something went wrong!", "danger");
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
|
@ -11,6 +11,7 @@
|
|||
@Html.GetSidebarUrl(Context, "/admin/emailnotification", "Email Notifications")
|
||||
@Html.GetSidebarUrl(Context, "/admin/pushbulletnotification", "Pushbullet Notifications")
|
||||
@Html.GetSidebarUrl(Context, "/admin/pushovernotification", "Pushover Notifications")
|
||||
@Html.GetSidebarUrl(Context, "/admin/slacknotification", "Slack Notifications")
|
||||
@Html.GetSidebarUrl(Context, "/admin/logs", "Logs")
|
||||
@Html.GetSidebarUrl(Context, "/admin/status", "Status")
|
||||
</div>
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
}
|
||||
}
|
||||
<form method="POST">
|
||||
Username <input class="form-control form-control-custom" type="text" name="Username" />
|
||||
Username <input class="form-control form-control-custom" type="text" name="username" />
|
||||
<br />
|
||||
Password <input class="form-control form-control-custom" name="Password" type="password" />
|
||||
<div class="checkbox">
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<form method="POST">
|
||||
Username <input class="form-control form-control-custom" type="text" name="Username" />
|
||||
Username <input class="form-control form-control-custom" type="text" name="username" />
|
||||
<br />
|
||||
Password <input class="form-control form-control-custom" name="Password" type="password" />
|
||||
<br />
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<label>Plex.tv Username </label>
|
||||
</div>
|
||||
<div>
|
||||
<input class="form-control form-control-custom" style="width: 50%" type="text" name="Username" placeholder="Username" />
|
||||
<input class="form-control form-control-custom" style="width: 50%" type="text" name="Username" placeholder="username" />
|
||||
</div>
|
||||
</div>
|
||||
<br />
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue