-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
492 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using Ombi.Api.Ntfy.Models; | ||
|
||
namespace Ombi.Api.Ntfy; | ||
|
||
public interface INtfyApi | ||
{ | ||
Task PushAsync(string endpoint, string authorizationHeader, NtfyNotificationBody body); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Ombi.Api.Ntfy.Models; | ||
|
||
public class NtfyNotificationBody | ||
{ | ||
[JsonConstructor] | ||
public NtfyNotificationBody() | ||
{ | ||
} | ||
|
||
public string topic { get; set; } | ||
public string message { get; set; } | ||
public string title { get; set; } | ||
public List<string> tags { get; set; } | ||
public sbyte priority { get; set; } | ||
public string click { get; set; } | ||
public string attach { get; set; } | ||
public string filename { get; set; } | ||
public string delay { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Ombi.Api.Ntfy.Models; | ||
|
||
namespace Ombi.Api.Ntfy; | ||
|
||
public class NtfyApi: INtfyApi | ||
{ | ||
public NtfyApi(IApi api) | ||
{ | ||
_api = api; | ||
} | ||
|
||
private readonly IApi _api; | ||
|
||
public async Task PushAsync(string endpoint, string authorizationHeader, NtfyNotificationBody body) | ||
{ | ||
var request = new Request("/", endpoint, HttpMethod.Post); | ||
if(!String.IsNullOrEmpty(authorizationHeader)) request.AddHeader("Authorization", authorizationHeader); | ||
request.ApplicationJsonContentType(); | ||
request.AddJsonBody(body); | ||
|
||
Console.WriteLine(endpoint); | ||
Console.WriteLine(request.JsonBody); | ||
|
||
await _api.Request(request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Ombi.Api\Ombi.Api.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
using System.Collections.Generic; | ||
using Ombi.Settings.Settings.Models.Notifications; | ||
using Ombi.Store.Entities; | ||
|
||
namespace Ombi.Core.Models.UI | ||
{ | ||
/// <summary> | ||
/// The view model for the notification settings page | ||
/// </summary> | ||
/// <seealso cref="NtfyNotificationViewModel" /> | ||
public class NtfyNotificationViewModel : NtfySettings | ||
{ | ||
/// <summary> | ||
/// Gets or sets the notification templates. | ||
/// </summary> | ||
/// <value> | ||
/// The notification templates. | ||
/// </value> | ||
public List<NotificationTemplates> NotificationTemplates { get; set; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ public enum NotificationAgent | |
Mobile = 7, | ||
Gotify = 8, | ||
Webhook = 9, | ||
WhatsApp = 10 | ||
WhatsApp = 10, | ||
Ntfy = 11 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Ombi.Notifications/Agents/Interfaces/INtfyNotification.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Ombi.Notifications.Agents | ||
{ | ||
public interface INtfyNotification : INotification | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Logging; | ||
using Ombi.Api.Ntfy; | ||
using Ombi.Api.Ntfy; | ||
using Ombi.Api.Ntfy.Models; | ||
using Ombi.Core.Settings; | ||
using Ombi.Helpers; | ||
using Ombi.Notifications.Models; | ||
using Ombi.Settings.Settings.Models; | ||
using Ombi.Settings.Settings.Models.Notifications; | ||
using Ombi.Store.Entities; | ||
using Ombi.Store.Repository; | ||
using Ombi.Store.Repository.Requests; | ||
|
||
namespace Ombi.Notifications.Agents | ||
{ | ||
public class NtfyNotification : BaseNotification<NtfySettings>, INtfyNotification | ||
{ | ||
public NtfyNotification(INtfyApi api, ISettingsService<NtfySettings> sn, ILogger<NtfyNotification> log, INotificationTemplatesRepository r, IMovieRequestRepository m, ITvRequestRepository t, | ||
ISettingsService<CustomizationSettings> s, IRepository<RequestSubscription> sub, IMusicRequestRepository music, | ||
IRepository<UserNotificationPreferences> userPref, UserManager<OmbiUser> um) : base(sn, r, m, t, s, log, sub, music, userPref, um) | ||
{ | ||
Api = api; | ||
Logger = log; | ||
} | ||
|
||
public override string NotificationName => "NtfyNotification"; | ||
|
||
private INtfyApi Api { get; } | ||
private ILogger<NtfyNotification> Logger { get; } | ||
|
||
protected override bool ValidateConfiguration(NtfySettings settings) | ||
{ | ||
return settings.Enabled && !string.IsNullOrEmpty(settings.BaseUrl); | ||
} | ||
|
||
protected override async Task NewRequest(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.NewRequest); | ||
} | ||
|
||
|
||
protected override async Task NewIssue(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.Issue); | ||
} | ||
|
||
protected override async Task IssueComment(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.IssueComment); | ||
} | ||
|
||
protected override async Task IssueResolved(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.IssueResolved); | ||
} | ||
|
||
protected override async Task AddedToRequestQueue(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.ItemAddedToFaultQueue); | ||
} | ||
|
||
protected override async Task RequestDeclined(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.RequestDeclined); | ||
} | ||
|
||
protected override async Task RequestApproved(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.RequestApproved); | ||
} | ||
|
||
protected override async Task AvailableRequest(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.RequestAvailable); | ||
} | ||
|
||
protected override async Task Send(NotificationMessage model, NtfySettings settings) | ||
{ | ||
try | ||
{ | ||
await Api.PushAsync(settings.BaseUrl, settings.AuthorizationHeader, new NtfyNotificationBody() | ||
{ | ||
topic = settings.Topic, // To change | ||
title = model.Subject, | ||
message = model.Message, | ||
priority = settings.Priority | ||
}); | ||
} | ||
catch (Exception e) | ||
{ | ||
Logger.LogError(LoggingEvents.NtfyNotification, e, "Failed to send Ntfy notification"); | ||
} | ||
} | ||
|
||
protected override async Task Test(NotificationOptions model, NtfySettings settings) | ||
{ | ||
var message = $"This is a test from Ombi, if you can see this then we have successfully pushed a notification!"; | ||
var notification = new NotificationMessage | ||
{ | ||
Message = message, | ||
}; | ||
await Send(notification, settings); | ||
} | ||
|
||
private async Task Run(NotificationOptions model, NtfySettings settings, NotificationType type) | ||
{ | ||
var parsed = await LoadTemplate(NotificationAgent.Ntfy, type, model); | ||
if (parsed.Disabled) | ||
{ | ||
Logger.LogInformation($"Template {type} is disabled for {NotificationAgent.Ntfy}"); | ||
return; | ||
} | ||
|
||
var notification = new NotificationMessage | ||
{ | ||
Message = parsed.Message, | ||
}; | ||
|
||
await Send(notification, settings); | ||
} | ||
|
||
protected override async Task PartiallyAvailable(NotificationOptions model, NtfySettings settings) | ||
{ | ||
await Run(model, settings, NotificationType.PartiallyAvailable); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/Ombi.Settings/Settings/Models/Notifications/NtfySettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Ombi.Settings.Settings.Models.Notifications | ||
{ | ||
public class NtfySettings : Settings | ||
{ | ||
public bool Enabled { get; set; } | ||
public string BaseUrl { get; set; } | ||
public string AuthorizationHeader { get; set; } | ||
public string Topic { get; set; } | ||
public sbyte Priority { get; set; } = 4; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.