From 08dd01942f6ccfc234807f86e86ac04c146ba5ae Mon Sep 17 00:00:00 2001 From: Glax Date: Sun, 2 Jun 2024 00:23:16 +0200 Subject: [PATCH] #183 Fix NTFY not exporting topic to notificationConnectors.json --- Tranga/NotificationConnectors/Ntfy.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Tranga/NotificationConnectors/Ntfy.cs b/Tranga/NotificationConnectors/Ntfy.cs index c725b8a..13d220e 100644 --- a/Tranga/NotificationConnectors/Ntfy.cs +++ b/Tranga/NotificationConnectors/Ntfy.cs @@ -9,10 +9,9 @@ public class Ntfy : NotificationConnector // ReSharper disable twice MemberCanBePrivate.Global public string endpoint { get; init; } public string auth { get; init; } - private readonly string _topic = "tranga"; + public string topic { get; init; } private readonly HttpClient _client = new(); - [JsonConstructor] public Ntfy(GlobalBase clone, string endpoint, string auth) : base(clone, NotificationConnectorType.Ntfy) { if (!baseUrlRex.IsMatch(endpoint)) @@ -23,19 +22,29 @@ public class Ntfy : NotificationConnector 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; + topic = match.Groups[2].Value; + else + topic = "tranga"; + this.auth = auth; + } + + [JsonConstructor] + public Ntfy(GlobalBase clone, string endpoint, string auth, string topic) : base(clone, NotificationConnectorType.Ntfy) + { + this.endpoint = endpoint; + this.topic = topic.Length > 0 ? topic : "tranga"; this.auth = auth; } public override string ToString() { - return $"Ntfy {endpoint} {_topic}"; + return $"Ntfy {endpoint} {topic}"; } public override void SendNotification(string title, string notificationText) { Log($"Sending notification: {title} - {notificationText}"); - MessageData message = new(title, _topic, notificationText); + MessageData message = new(title, topic, notificationText); HttpRequestMessage request = new(HttpMethod.Post, $"{this.endpoint}?auth={this.auth}"); request.Content = new StringContent(JsonConvert.SerializeObject(message, Formatting.None), Encoding.UTF8, "application/json"); HttpResponseMessage response = _client.Send(request);