Merge pull request #1243 from anonaut/master

Added Mattermost Notifications
This commit is contained in:
Jamie 2017-04-10 15:43:53 +01:00 committed by GitHub
commit f7c25b0e15
21 changed files with 579 additions and 2 deletions

View file

@ -0,0 +1,37 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: IMattermostApi.cs
// Created By: Michel Zaleski
//
// 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 Ombi.Api.Models.Notifications;
namespace Ombi.Api.Interfaces
{
public interface IMattermostApi
{
Task<string> PushAsync(string webhook, MattermostNotificationBody message);
}
}

View file

@ -65,6 +65,7 @@
<Compile Include="IPushbulletApi.cs" /> <Compile Include="IPushbulletApi.cs" />
<Compile Include="IRadarrApi.cs" /> <Compile Include="IRadarrApi.cs" />
<Compile Include="ISlackApi.cs" /> <Compile Include="ISlackApi.cs" />
<Compile Include="IMattermostApi.cs" />
<Compile Include="IPushoverApi.cs" /> <Compile Include="IPushoverApi.cs" />
<Compile Include="ISickRageApi.cs" /> <Compile Include="ISickRageApi.cs" />
<Compile Include="ISonarrApi.cs" /> <Compile Include="ISonarrApi.cs" />

View file

@ -0,0 +1,45 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: MattermostNotificationBody.cs
// Created By: Michel Zaleski
//
// 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 Ombi.Api.Models.Notifications
{
public class MattermostNotificationBody
{
[JsonConstructor]
public MattermostNotificationBody()
{
username = "Ombi";
}
public string username { get; set; } = "Ombi";
public string channel { get; set; }
public string text { get; set; }
public string icon_url { get; set; }
}
}

View file

@ -96,6 +96,7 @@
<Compile Include="Notifications\PushbulletResponse.cs" /> <Compile Include="Notifications\PushbulletResponse.cs" />
<Compile Include="Notifications\PushoverResponse.cs" /> <Compile Include="Notifications\PushoverResponse.cs" />
<Compile Include="Notifications\SlackNotificationBody.cs" /> <Compile Include="Notifications\SlackNotificationBody.cs" />
<Compile Include="Notifications\MattermostNotificationBody.cs" />
<Compile Include="Plex\PlexAccount.cs" /> <Compile Include="Plex\PlexAccount.cs" />
<Compile Include="Plex\PlexAuthentication.cs" /> <Compile Include="Plex\PlexAuthentication.cs" />
<Compile Include="Plex\PlexEpisodeMetadata.cs" /> <Compile Include="Plex\PlexEpisodeMetadata.cs" />

58
Ombi.Api/MattermostApi.cs Normal file
View file

@ -0,0 +1,58 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: PlexApi.cs
// Created By: Michel Zaleski
//
// 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 Ombi.Api.Interfaces;
using Ombi.Api.Models.Notifications;
using RestSharp;
namespace Ombi.Api
{
public class MattermostApi : IMattermostApi
{
public async Task<string> PushAsync(string webhook, MattermostNotificationBody message)
{
var request = new RestRequest
{
Method = Method.POST,
Resource = "/"
};
request.AddJsonBody(message);
var api = new ApiRequest();
return await Task.Run(
() =>
{
var result = api.Execute(request, new Uri(webhook));
return result.Content;
});
}
}
}

View file

@ -85,6 +85,7 @@
<Compile Include="WatcherApi.cs" /> <Compile Include="WatcherApi.cs" />
<Compile Include="MusicBrainzApi.cs" /> <Compile Include="MusicBrainzApi.cs" />
<Compile Include="SlackApi.cs" /> <Compile Include="SlackApi.cs" />
<Compile Include="MattermostApi.cs" />
<Compile Include="PushoverApi.cs" /> <Compile Include="PushoverApi.cs" />
<Compile Include="PushbulletApi.cs" /> <Compile Include="PushbulletApi.cs" />
<Compile Include="SickrageApi.cs" /> <Compile Include="SickrageApi.cs" />

View file

@ -70,6 +70,9 @@ namespace Ombi.Core.Notification
case TransportType.Slack: case TransportType.Slack:
content = notification.SlackNotification; content = notification.SlackNotification;
break; break;
case TransportType.Mattermost:
content = notification.MattermostNotification;
break;
default: default:
throw new ArgumentOutOfRangeException(nameof(transportType), transportType, null); throw new ArgumentOutOfRangeException(nameof(transportType), transportType, null);
} }

View file

@ -31,6 +31,7 @@ namespace Ombi.Core.Notification
Email, Email,
Pushbullet, Pushbullet,
Pushover, Pushover,
Slack Slack,
Mattermost
} }
} }

View file

@ -138,6 +138,7 @@
<Compile Include="SettingModels\RequestSettings.cs" /> <Compile Include="SettingModels\RequestSettings.cs" />
<Compile Include="SettingModels\ScheduledJobsSettings.cs" /> <Compile Include="SettingModels\ScheduledJobsSettings.cs" />
<Compile Include="SettingModels\SlackNotificationSettings.cs" /> <Compile Include="SettingModels\SlackNotificationSettings.cs" />
<Compile Include="SettingModels\MattermostNotificationSettings.cs" />
<Compile Include="SettingModels\PushoverNotificationSettings.cs" /> <Compile Include="SettingModels\PushoverNotificationSettings.cs" />
<Compile Include="SettingModels\PushBulletNotificationSettings.cs" /> <Compile Include="SettingModels\PushBulletNotificationSettings.cs" />
<Compile Include="SettingModels\EmailNotificationSettings.cs" /> <Compile Include="SettingModels\EmailNotificationSettings.cs" />

View file

@ -0,0 +1,13 @@
using System;
using Newtonsoft.Json;
namespace Ombi.Core.SettingModels
{
public sealed class MattermostNotificationSettings : NotificationSettings
{
public string WebhookUrl { get; set; }
public string Channel { get; set; }
public string Username { get; set; }
public string IconUrl { get; set; }
}
}

View file

@ -51,11 +51,13 @@ namespace Ombi.Core.SettingModels
} }
}; };
SlackNotification = new List<NotificationMessage>(); SlackNotification = new List<NotificationMessage>();
MattermostNotification = new List<NotificationMessage>();
PushoverNotification = new List<NotificationMessage>(); PushoverNotification = new List<NotificationMessage>();
PushbulletNotification = new List<NotificationMessage>(); PushbulletNotification = new List<NotificationMessage>();
} }
public List<NotificationMessage> EmailNotification { get; set; } public List<NotificationMessage> EmailNotification { get; set; }
public List<NotificationMessage> SlackNotification { get; set; } public List<NotificationMessage> SlackNotification { get; set; }
public List<NotificationMessage> MattermostNotification { get; set; }
public List<NotificationMessage> PushbulletNotification { get; set; } public List<NotificationMessage> PushbulletNotification { get; set; }
public List<NotificationMessage> PushoverNotification { get; set; } public List<NotificationMessage> PushoverNotification { get; set; }
} }

View file

@ -0,0 +1,156 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: MattermostNotification.cs
// Created By: Michel Zaleski
//
// 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 Ombi.Api.Interfaces;
using Ombi.Api.Models.Notifications;
using Ombi.Core;
using Ombi.Core.Models;
using Ombi.Core.SettingModels;
using Ombi.Services.Interfaces;
namespace Ombi.Services.Notification
{
public class MattermostNotification : INotification
{
public MattermostNotification(IMattermostApi api, ISettingsService<MattermostNotificationSettings> sn)
{
Api = api;
Settings = sn;
}
public string NotificationName => "MattermostNotification";
private IMattermostApi Api { get; }
private ISettingsService<MattermostNotificationSettings> 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 = (MattermostNotificationSettings)settings;
if (!ValidateConfiguration(pushSettings))
{
Log.Error("Settings for Mattermost 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;
case NotificationType.RequestDeclined:
break;
case NotificationType.ItemAddedToFaultQueue:
await PushFaultQueue(model, pushSettings);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private async Task PushNewRequestAsync(NotificationModel model, MattermostNotificationSettings settings)
{
var message = $"{model.Title} has been requested by user: {model.User}";
await Push(settings, message);
}
private async Task PushIssueAsync(NotificationModel model, MattermostNotificationSettings 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(MattermostNotificationSettings settings)
{
var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!";
await Push(settings, message);
}
private async Task PushFaultQueue(NotificationModel model, MattermostNotificationSettings settings)
{
var message = $"Hello! The user '{model.User}' has requested {model.Title} but it could not be added. This has been added into the requests queue and will keep retrying";
await Push(settings, message);
}
private async Task Push(MattermostNotificationSettings config, string message)
{
try
{
var notification = new MattermostNotificationBody { username = config.Username, channel = config.Channel ?? string.Empty, icon_url = config.IconUrl ?? string.Empty, text = message };
var result = await Api.PushAsync(config.WebhookUrl, notification);
if (!result.Equals("ok"))
{
Log.Error("Mattermost returned a message that was not 'ok', the notification did not get pushed");
Log.Error($"Message that mattermost returned: {result}");
}
}
catch (Exception e)
{
Log.Error(e);
}
}
private bool ValidateConfiguration(MattermostNotificationSettings settings)
{
if (!settings.Enabled)
{
return false;
}
if (string.IsNullOrEmpty(settings.WebhookUrl))
{
return false;
}
return true;
}
}
}

View file

@ -157,6 +157,7 @@
<Compile Include="Notification\PushbulletNotification.cs" /> <Compile Include="Notification\PushbulletNotification.cs" />
<Compile Include="Notification\DiscordNotification.cs" /> <Compile Include="Notification\DiscordNotification.cs" />
<Compile Include="Notification\SlackNotification.cs" /> <Compile Include="Notification\SlackNotification.cs" />
<Compile Include="Notification\MattermostNotification.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -73,8 +73,10 @@ namespace Ombi.UI.Tests
private Mock<ICacheProvider> Cache { get; set; } private Mock<ICacheProvider> Cache { get; set; }
private Mock<ISettingsService<LogSettings>> Log { get; set; } private Mock<ISettingsService<LogSettings>> Log { get; set; }
private Mock<ISettingsService<SlackNotificationSettings>> SlackSettings { get; set; } private Mock<ISettingsService<SlackNotificationSettings>> SlackSettings { get; set; }
private Mock<ISettingsService<MattermostNotificationSettings>> MattermostSettings { get; set; }
private Mock<ISettingsService<LandingPageSettings>> LandingPageSettings { get; set; } private Mock<ISettingsService<LandingPageSettings>> LandingPageSettings { get; set; }
private Mock<ISlackApi> SlackApi { get; set; } private Mock<ISlackApi> SlackApi { get; set; }
private Mock<IMattermostApi> MattermostApi { get; set; }
private Mock<IAnalytics> Analytics { get; set; } private Mock<IAnalytics> Analytics { get; set; }
private Mock<ISettingsService<NotificationSettingsV2>> NotifyV2 { get; set; } private Mock<ISettingsService<NotificationSettingsV2>> NotifyV2 { get; set; }
private Mock<IRecentlyAdded> RecentlyAdded { get; set; } private Mock<IRecentlyAdded> RecentlyAdded { get; set; }
@ -114,6 +116,8 @@ namespace Ombi.UI.Tests
Log = new Mock<ISettingsService<LogSettings>>(); Log = new Mock<ISettingsService<LogSettings>>();
SlackApi = new Mock<ISlackApi>(); SlackApi = new Mock<ISlackApi>();
SlackSettings = new Mock<ISettingsService<SlackNotificationSettings>>(); SlackSettings = new Mock<ISettingsService<SlackNotificationSettings>>();
MattermostApi = new Mock<IMattermostApi>();
MattermostSettings = new Mock<ISettingsService<MattermostNotificationSettings>>();
LandingPageSettings = new Mock<ISettingsService<LandingPageSettings>>(); LandingPageSettings = new Mock<ISettingsService<LandingPageSettings>>();
ScheduledJobsSettingsMock = new Mock<ISettingsService<ScheduledJobsSettings>>(); ScheduledJobsSettingsMock = new Mock<ISettingsService<ScheduledJobsSettings>>();
RecorderMock = new Mock<IJobRecord>(); RecorderMock = new Mock<IJobRecord>();
@ -147,8 +151,10 @@ namespace Ombi.UI.Tests
with.Dependency(Cache.Object); with.Dependency(Cache.Object);
with.Dependency(Log.Object); with.Dependency(Log.Object);
with.Dependency(SlackApi.Object); with.Dependency(SlackApi.Object);
with.Dependency(MattermostApi.Object);
with.Dependency(LandingPageSettings.Object); with.Dependency(LandingPageSettings.Object);
with.Dependency(SlackSettings.Object); with.Dependency(SlackSettings.Object);
with.Dependency(MattermostSettings.Object);
with.Dependency(ScheduledJobsSettingsMock.Object); with.Dependency(ScheduledJobsSettingsMock.Object);
with.Dependency(RecorderMock.Object); with.Dependency(RecorderMock.Object);
with.Dependency(RecentlyAdded.Object); with.Dependency(RecentlyAdded.Object);

View file

@ -87,9 +87,11 @@ namespace Ombi.UI.Modules.Admin
private INotificationService NotificationService { get; } private INotificationService NotificationService { get; }
private ICacheProvider Cache { get; } private ICacheProvider Cache { get; }
private ISettingsService<SlackNotificationSettings> SlackSettings { get; } private ISettingsService<SlackNotificationSettings> SlackSettings { get; }
private ISettingsService<MattermostNotificationSettings> MattermostSettings { get; }
private ISettingsService<LandingPageSettings> LandingSettings { get; } private ISettingsService<LandingPageSettings> LandingSettings { get; }
private ISettingsService<ScheduledJobsSettings> ScheduledJobSettings { get; } private ISettingsService<ScheduledJobsSettings> ScheduledJobSettings { get; }
private ISlackApi SlackApi { get; } private ISlackApi SlackApi { get; }
private IMattermostApi MattermostApi { get; }
private IJobRecord JobRecorder { get; } private IJobRecord JobRecorder { get; }
private IAnalytics Analytics { get; } private IAnalytics Analytics { get; }
private IRecentlyAdded RecentlyAdded { get; } private IRecentlyAdded RecentlyAdded { get; }
@ -123,7 +125,8 @@ namespace Ombi.UI.Modules.Admin
ISettingsService<HeadphonesSettings> headphones, ISettingsService<HeadphonesSettings> headphones,
ISettingsService<LogSettings> logs, ISettingsService<LogSettings> logs,
ICacheProvider cache, ISettingsService<SlackNotificationSettings> slackSettings, ICacheProvider cache, ISettingsService<SlackNotificationSettings> slackSettings,
ISlackApi slackApi, ISettingsService<LandingPageSettings> lp, ISlackApi slackApi, ISettingsService<MattermostNotificationSettings> mattermostSettings,
IMattermostApi mattermostApi, ISettingsService<LandingPageSettings> lp,
ISettingsService<ScheduledJobsSettings> scheduler, IJobRecord rec, IAnalytics analytics, ISettingsService<ScheduledJobsSettings> scheduler, IJobRecord rec, IAnalytics analytics,
ISettingsService<NotificationSettingsV2> notifyService, IRecentlyAdded recentlyAdded, IMassEmail massEmail, ISettingsService<NotificationSettingsV2> notifyService, IRecentlyAdded recentlyAdded, IMassEmail massEmail,
ISettingsService<WatcherSettings> watcherSettings, ISettingsService<WatcherSettings> watcherSettings,
@ -154,6 +157,8 @@ namespace Ombi.UI.Modules.Admin
Cache = cache; Cache = cache;
SlackSettings = slackSettings; SlackSettings = slackSettings;
SlackApi = slackApi; SlackApi = slackApi;
MattermostSettings = mattermostSettings;
MattermostApi = mattermostApi;
LandingSettings = lp; LandingSettings = lp;
ScheduledJobSettings = scheduler; ScheduledJobSettings = scheduler;
JobRecorder = rec; JobRecorder = rec;
@ -239,6 +244,10 @@ namespace Ombi.UI.Modules.Admin
Get["/slacknotification"] = _ => SlackNotifications(); Get["/slacknotification"] = _ => SlackNotifications();
Post["/slacknotification"] = _ => SaveSlackNotifications(); Post["/slacknotification"] = _ => SaveSlackNotifications();
Post["/testmattermostnotification", true] = async (x, ct) => await TestMattermostNotification();
Get["/mattermostnotification"] = _ => MattermostNotifications();
Post["/mattermostnotification"] = _ => SaveMattermostNotifications();
Post["/testdiscordnotification", true] = async (x, ct) => await TestDiscordNotification(); Post["/testdiscordnotification", true] = async (x, ct) => await TestDiscordNotification();
Get["/discordnotification", true] = async (x, ct) => await DiscordNotification(); Get["/discordnotification", true] = async (x, ct) => await DiscordNotification();
Post["/discordnotification", true] = async (x, ct) => await SaveDiscordNotifications(); Post["/discordnotification", true] = async (x, ct) => await SaveDiscordNotifications();
@ -1051,6 +1060,71 @@ namespace Ombi.UI.Modules.Admin
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." }); : new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
} }
private async Task<Response> TestMattermostNotification()
{
var settings = this.BindAndValidate<MattermostNotificationSettings>();
if (!ModelValidationResult.IsValid)
{
return Response.AsJson(ModelValidationResult.SendJsonError());
}
var notificationModel = new NotificationModel
{
NotificationType = NotificationType.Test,
DateTime = DateTime.Now
};
var currentMattermostSettings = await MattermostSettings.GetSettingsAsync();
try
{
NotificationService.Subscribe(new MattermostNotification(MattermostApi, MattermostSettings));
settings.Enabled = true;
await NotificationService.Publish(notificationModel, settings);
Log.Info("Sent mattermost notification test");
}
catch (Exception e)
{
Log.Error(e, "Failed to subscribe and publish test Mattermost Notification");
}
finally
{
if (!currentMattermostSettings.Enabled)
{
NotificationService.UnSubscribe(new MattermostNotification(MattermostApi, MattermostSettings));
}
}
return Response.AsJson(new JsonResponseModel { Result = true, Message = "Successfully sent a test Mattermost Notification! If you do not receive it please check the logs." });
}
private Negotiator MattermostNotifications()
{
var settings = MattermostSettings.GetSettings();
return View["MattermostNotifications", settings];
}
private Response SaveMattermostNotifications()
{
var settings = this.BindAndValidate<MattermostNotificationSettings>();
if (!ModelValidationResult.IsValid)
{
return Response.AsJson(ModelValidationResult.SendJsonError());
}
var result = MattermostSettings.SaveSettings(settings);
if (settings.Enabled)
{
NotificationService.Subscribe(new MattermostNotification(MattermostApi, MattermostSettings));
}
else
{
NotificationService.UnSubscribe(new MattermostNotification(MattermostApi, MattermostSettings));
}
Log.Info("Saved mattermost settings, result: {0}", result);
return Response.AsJson(result
? new JsonResponseModel { Result = true, Message = "Successfully Updated the Settings for Mattermost Notifications!" }
: new JsonResponseModel { Result = false, Message = "Could not update the settings, take a look at the logs." });
}
private async Task<Negotiator> DiscordNotification() private async Task<Negotiator> DiscordNotification()
{ {
var settings = await DiscordSettings.GetSettingsAsync(); var settings = await DiscordSettings.GetSettingsAsync();

View file

@ -44,6 +44,7 @@ namespace Ombi.UI.NinjectModules
Bind<IMusicBrainzApi>().To<MusicBrainzApi>(); Bind<IMusicBrainzApi>().To<MusicBrainzApi>();
Bind<IHeadphonesApi>().To<HeadphonesApi>(); Bind<IHeadphonesApi>().To<HeadphonesApi>();
Bind<ISlackApi>().To<SlackApi>(); Bind<ISlackApi>().To<SlackApi>();
Bind<IMattermostApi>().To<MattermostApi>();
Bind<IApiRequest>().To<ApiRequest>(); Bind<IApiRequest>().To<ApiRequest>();
Bind<IWatcherApi>().To<WatcherApi>(); Bind<IWatcherApi>().To<WatcherApi>();
Bind<INetflixApi>().To<NetflixRouletteApi>(); Bind<INetflixApi>().To<NetflixRouletteApi>();

View file

@ -299,6 +299,7 @@
<Compile Include="Validators\RadarrValidator.cs" /> <Compile Include="Validators\RadarrValidator.cs" />
<Compile Include="Validators\WatcherValidator.cs" /> <Compile Include="Validators\WatcherValidator.cs" />
<Compile Include="Validators\SlackSettingsValidator.cs" /> <Compile Include="Validators\SlackSettingsValidator.cs" />
<Compile Include="Validators\MattermostSettingsValidator.cs" />
<Compile Include="Validators\UserViewModelValidator.cs" /> <Compile Include="Validators\UserViewModelValidator.cs" />
<Compile Include="Validators\HeadphonesValidator.cs" /> <Compile Include="Validators\HeadphonesValidator.cs" />
<Compile Include="Validators\PushoverSettingsValidator.cs" /> <Compile Include="Validators\PushoverSettingsValidator.cs" />
@ -834,6 +835,9 @@
<Content Include="Views\Admin\SlackNotifications.cshtml"> <Content Include="Views\Admin\SlackNotifications.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>
<Content Include="Views\Admin\MattermostNotifications.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Views\Issues\Index.cshtml"> <Content Include="Views\Issues\Index.cshtml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content> </Content>

View file

@ -127,6 +127,10 @@ namespace Ombi.UI
var slackSettings = slackService.GetSettings(); var slackSettings = slackService.GetSettings();
SubScribeOvserver(slackSettings, notificationService, new SlackNotification(container.Get<ISlackApi>(), slackService)); SubScribeOvserver(slackSettings, notificationService, new SlackNotification(container.Get<ISlackApi>(), slackService));
var mattermostService = container.Get<ISettingsService<MattermostNotificationSettings>>();
var mattermostSettings = mattermostService.GetSettings();
SubScribeOvserver(mattermostSettings, notificationService, new MattermostNotification(container.Get<IMattermostApi>(), mattermostService));
var discordSettings = container.Get<ISettingsService<DiscordNotificationSettings>>(); var discordSettings = container.Get<ISettingsService<DiscordNotificationSettings>>();
var discordService = discordSettings.GetSettings(); var discordService = discordSettings.GetSettings();
SubScribeOvserver(discordService, notificationService, new DiscordNotification(container.Get<IDiscordApi>(), discordSettings)); SubScribeOvserver(discordService, notificationService, new DiscordNotification(container.Get<IDiscordApi>(), discordSettings));

View file

@ -0,0 +1,40 @@
#region Copyright
// /************************************************************************
// Copyright (c) 2016 Jamie Rees
// File: MattermostSettingsValidator.cs
// Created By: Michel Zaleski
//
// 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 Ombi.Core.SettingModels;
namespace Ombi.UI.Validators
{
public class MattermostSettingsValidator : AbstractValidator<MattermostNotificationSettings>
{
public MattermostSettingsValidator()
{
RuleFor(request => request.WebhookUrl).NotEmpty().WithMessage("You must specify a Webhook Url");
}
}
}

View file

@ -0,0 +1,127 @@
@using Ombi.UI.Helpers
@Html.Partial("Shared/Partial/_Sidebar")
<div class="col-sm-8 col-sm-push-1">
<form class="form-horizontal" method="POST" id="mainForm">
<fieldset>
<legend>Mattermost 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"> Mattermost > Integrations > Incoming Webhook > 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 (Ombi) 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">
<label for="IconUrl" class="control-label">Icon Override</label>
<small class="control-label">You can override the default icon here</small>
<div class="">
<input type="text" class="form-control form-control-custom " id="IconUrl" name="IconUrl" value="@Model.IconUrl">
</div>
</div>
<div class="form-group">
<div>
<button id="testMattermost" 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");
}
});
});
$('#testMattermost').click(function (e) {
e.preventDefault();
var url = createBaseUrl(base, '/admin/testMattermostnotification');
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>

View file

@ -33,6 +33,7 @@
@Html.GetSidebarUrl(Context, "/admin/pushbulletnotification", "Pushbullet Notifications","fa fa-bell-o") @Html.GetSidebarUrl(Context, "/admin/pushbulletnotification", "Pushbullet Notifications","fa fa-bell-o")
@Html.GetSidebarUrl(Context, "/admin/pushovernotification", "Pushover Notifications", "fa fa-bell-o") @Html.GetSidebarUrl(Context, "/admin/pushovernotification", "Pushover Notifications", "fa fa-bell-o")
@Html.GetSidebarUrl(Context, "/admin/slacknotification", "Slack Notifications", "fa fa-slack") @Html.GetSidebarUrl(Context, "/admin/slacknotification", "Slack Notifications", "fa fa-slack")
@Html.GetSidebarUrl(Context, "/admin/mattermostnotification", "Mattermost Notifications", "fa fa-bell-o")
@Html.GetSidebarUrl(Context, "/admin/discordnotification", "Discord Notifications", "fa fa-bell-o") @Html.GetSidebarUrl(Context, "/admin/discordnotification", "Discord Notifications", "fa fa-bell-o")
</div> </div>