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

@ -87,9 +87,11 @@ namespace Ombi.UI.Modules.Admin
private INotificationService NotificationService { get; }
private ICacheProvider Cache { get; }
private ISettingsService<SlackNotificationSettings> SlackSettings { get; }
private ISettingsService<MattermostNotificationSettings> MattermostSettings { get; }
private ISettingsService<LandingPageSettings> LandingSettings { get; }
private ISettingsService<ScheduledJobsSettings> ScheduledJobSettings { get; }
private ISlackApi SlackApi { get; }
private IMattermostApi MattermostApi { get; }
private IJobRecord JobRecorder { get; }
private IAnalytics Analytics { get; }
private IRecentlyAdded RecentlyAdded { get; }
@ -123,7 +125,8 @@ namespace Ombi.UI.Modules.Admin
ISettingsService<HeadphonesSettings> headphones,
ISettingsService<LogSettings> logs,
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<NotificationSettingsV2> notifyService, IRecentlyAdded recentlyAdded, IMassEmail massEmail,
ISettingsService<WatcherSettings> watcherSettings,
@ -154,6 +157,8 @@ namespace Ombi.UI.Modules.Admin
Cache = cache;
SlackSettings = slackSettings;
SlackApi = slackApi;
MattermostSettings = mattermostSettings;
MattermostApi = mattermostApi;
LandingSettings = lp;
ScheduledJobSettings = scheduler;
JobRecorder = rec;
@ -239,6 +244,10 @@ namespace Ombi.UI.Modules.Admin
Get["/slacknotification"] = _ => SlackNotifications();
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();
Get["/discordnotification", true] = async (x, ct) => await DiscordNotification();
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." });
}
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()
{
var settings = await DiscordSettings.GetSettingsAsync();

View file

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

View file

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

View file

@ -127,6 +127,10 @@ namespace Ombi.UI
var slackSettings = slackService.GetSettings();
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 discordService = discordSettings.GetSettings();
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/pushovernotification", "Pushover Notifications", "fa fa-bell-o")
@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")
</div>