Merge branch 'refs/heads/cuttingedge-merge-ServerV2' into cuttingedge

This commit is contained in:
Glax 2024-06-01 22:09:18 +02:00
commit 048b165d76

View File

@ -1,4 +1,5 @@
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json; using Newtonsoft.Json;
namespace Tranga.NotificationConnectors; namespace Tranga.NotificationConnectors;
@ -8,7 +9,7 @@ public class Ntfy : NotificationConnector
// ReSharper disable twice MemberCanBePrivate.Global // ReSharper disable twice MemberCanBePrivate.Global
public string endpoint { get; init; } public string endpoint { get; init; }
public string auth { get; init; } public string auth { get; init; }
private const string Topic = "tranga"; private readonly string _topic = "tranga";
private readonly HttpClient _client = new(); private readonly HttpClient _client = new();
[JsonConstructor] [JsonConstructor]
@ -16,19 +17,25 @@ public class Ntfy : NotificationConnector
{ {
if (!baseUrlRex.IsMatch(endpoint)) if (!baseUrlRex.IsMatch(endpoint))
throw new ArgumentException("endpoint does not match pattern"); throw new ArgumentException("endpoint does not match pattern");
this.endpoint = endpoint; Regex rootUriRex = new(@"(https?:\/\/[a-zA-Z0-9-\.]+\.[a-zA-Z0-9]+)(?:\/([a-zA-Z0-9-\.]+))?.*");
Match match = rootUriRex.Match(endpoint);
if(!match.Success)
Log($"Error getting URI from provided endpoint-URI: {endpoint}");
this.endpoint = match.Groups[1].Value;
if (match.Groups[2].Success)
_topic = match.Groups[2].Value;
this.auth = auth; this.auth = auth;
} }
public override string ToString() public override string ToString()
{ {
return $"Ntfy {endpoint} {Topic}"; return $"Ntfy {endpoint} {_topic}";
} }
public override void SendNotification(string title, string notificationText) public override void SendNotification(string title, string notificationText)
{ {
Log($"Sending notification: {title} - {notificationText}"); Log($"Sending notification: {title} - {notificationText}");
MessageData message = new(title, notificationText); MessageData message = new(title, _topic, notificationText);
HttpRequestMessage request = new(HttpMethod.Post, $"{this.endpoint}?auth={this.auth}"); HttpRequestMessage request = new(HttpMethod.Post, $"{this.endpoint}?auth={this.auth}");
request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json"); request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json");
HttpResponseMessage response = _client.Send(request); HttpResponseMessage response = _client.Send(request);
@ -47,9 +54,9 @@ public class Ntfy : NotificationConnector
public string message { get; } public string message { get; }
public int priority { get; } public int priority { get; }
public MessageData(string title, string message) public MessageData(string title, string topic, string message)
{ {
this.topic = Topic; this.topic = topic;
this.title = title; this.title = title;
this.message = message; this.message = message;
this.priority = 3; this.priority = 3;