mirror of
https://github.com/C9Glax/tranga.git
synced 2025-09-10 11:58:19 +02:00
Fix NotificationConnectors
This commit is contained in:
@@ -48,30 +48,29 @@ public class NotificationConnectorController(NotificationsContext context) : Con
|
|||||||
/// Creates a new <see cref="NotificationConnector"/>
|
/// Creates a new <see cref="NotificationConnector"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>Formatting placeholders: "%title" and "%text" can be placed in url, header-values and body and will be replaced when notifications are sent</remarks>
|
/// <remarks>Formatting placeholders: "%title" and "%text" can be placed in url, header-values and body and will be replaced when notifications are sent</remarks>
|
||||||
/// <response code="201"></response>
|
/// <response code="200">ID of the new <see cref="NotificationConnector"/></response>
|
||||||
/// <response code="500">Error during Database Operation</response>
|
/// <response code="500">Error during Database Operation</response>
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
[ProducesResponseType(Status201Created)]
|
[ProducesResponseType<string>(Status200OK, "text/plain")]
|
||||||
[ProducesResponseType(Status409Conflict)]
|
|
||||||
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
||||||
public IActionResult CreateConnector([FromBody]NotificationConnector notificationConnector)
|
public IActionResult CreateConnector([FromBody]NotificationConnector notificationConnector)
|
||||||
{
|
{
|
||||||
|
|
||||||
context.NotificationConnectors.Add(notificationConnector);
|
context.NotificationConnectors.Add(notificationConnector);
|
||||||
|
context.Notifications.Add(new ("Added new Notification Connector!", notificationConnector.Name, NotificationUrgency.High));
|
||||||
|
|
||||||
if(context.Sync() is { success: false } result)
|
if(context.Sync() is { success: false } result)
|
||||||
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
||||||
return Created();
|
return Ok(notificationConnector.Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new Gotify-<see cref="NotificationConnector"/>
|
/// Creates a new Gotify-<see cref="NotificationConnector"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>Priority needs to be between 0 and 10</remarks>
|
/// <remarks>Priority needs to be between 0 and 10</remarks>
|
||||||
/// <response code="201"></response>
|
/// <response code="200">ID of the new <see cref="NotificationConnector"/></response>
|
||||||
/// <response code="500">Error during Database Operation</response>
|
/// <response code="500">Error during Database Operation</response>
|
||||||
[HttpPut("Gotify")]
|
[HttpPut("Gotify")]
|
||||||
[ProducesResponseType<string>(Status201Created, "application/json")]
|
[ProducesResponseType<string>(Status200OK, "text/plain")]
|
||||||
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
||||||
public IActionResult CreateGotifyConnector([FromBody]GotifyRecord gotifyData)
|
public IActionResult CreateGotifyConnector([FromBody]GotifyRecord gotifyData)
|
||||||
{
|
{
|
||||||
@@ -79,7 +78,7 @@ public class NotificationConnectorController(NotificationsContext context) : Con
|
|||||||
|
|
||||||
NotificationConnector gotifyConnector = new (gotifyData.Name,
|
NotificationConnector gotifyConnector = new (gotifyData.Name,
|
||||||
gotifyData.Endpoint,
|
gotifyData.Endpoint,
|
||||||
new Dictionary<string, string>() { { "X-Gotify-IDOnConnector", gotifyData.AppToken } },
|
new Dictionary<string, string>() { { "X-Gotify-Key", gotifyData.AppToken } },
|
||||||
"POST",
|
"POST",
|
||||||
$"{{\"message\": \"%text\", \"title\": \"%title\", \"Priority\": {gotifyData.Priority}}}");
|
$"{{\"message\": \"%text\", \"title\": \"%title\", \"Priority\": {gotifyData.Priority}}}");
|
||||||
return CreateConnector(gotifyConnector);
|
return CreateConnector(gotifyConnector);
|
||||||
@@ -89,10 +88,10 @@ public class NotificationConnectorController(NotificationsContext context) : Con
|
|||||||
/// Creates a new Ntfy-<see cref="NotificationConnector"/>
|
/// Creates a new Ntfy-<see cref="NotificationConnector"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>Priority needs to be between 1 and 5</remarks>
|
/// <remarks>Priority needs to be between 1 and 5</remarks>
|
||||||
/// <response code="201"></response>
|
/// <response code="200">ID of the new <see cref="NotificationConnector"/></response>
|
||||||
/// <response code="500">Error during Database Operation</response>
|
/// <response code="500">Error during Database Operation</response>
|
||||||
[HttpPut("Ntfy")]
|
[HttpPut("Ntfy")]
|
||||||
[ProducesResponseType<string>(Status201Created, "application/json")]
|
[ProducesResponseType<string>(Status200OK, "text/plain")]
|
||||||
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
||||||
public IActionResult CreateNtfyConnector([FromBody]NtfyRecord ntfyRecord)
|
public IActionResult CreateNtfyConnector([FromBody]NtfyRecord ntfyRecord)
|
||||||
{
|
{
|
||||||
@@ -102,14 +101,13 @@ public class NotificationConnectorController(NotificationsContext context) : Con
|
|||||||
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(authHeader)).Replace("=","");
|
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(authHeader)).Replace("=","");
|
||||||
|
|
||||||
NotificationConnector ntfyConnector = new (ntfyRecord.Name,
|
NotificationConnector ntfyConnector = new (ntfyRecord.Name,
|
||||||
$"{ntfyRecord.Endpoint}/{ntfyRecord.Topic}?auth={auth}",
|
$"{ntfyRecord.Endpoint}?auth={auth}",
|
||||||
new Dictionary<string, string>()
|
new Dictionary<string, string>()
|
||||||
{
|
{
|
||||||
{"Title", "%title"},
|
{"Authorization", auth}
|
||||||
{"Priority", ntfyRecord.Priority.ToString()},
|
|
||||||
},
|
},
|
||||||
"POST",
|
"POST",
|
||||||
"%text");
|
$"{{\"message\": \"%text\", \"title\": \"%title\", \"Priority\": {ntfyRecord.Priority} \"Topic\": \"{ntfyRecord.Topic}\"}}");
|
||||||
return CreateConnector(ntfyConnector);
|
return CreateConnector(ntfyConnector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,10 +115,10 @@ public class NotificationConnectorController(NotificationsContext context) : Con
|
|||||||
/// Creates a new Pushover-<see cref="NotificationConnector"/>
|
/// Creates a new Pushover-<see cref="NotificationConnector"/>
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>https://pushover.net/api</remarks>
|
/// <remarks>https://pushover.net/api</remarks>
|
||||||
/// <response code="201">ID of new connector</response>
|
/// <response code="200">ID of the new <see cref="NotificationConnector"/></response>
|
||||||
/// <response code="500">Error during Database Operation</response>
|
/// <response code="500">Error during Database Operation</response>
|
||||||
[HttpPut("Pushover")]
|
[HttpPut("Pushover")]
|
||||||
[ProducesResponseType<string>(Status201Created, "application/json")]
|
[ProducesResponseType<string>(Status200OK, "text/plain")]
|
||||||
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
[ProducesResponseType<string>(Status500InternalServerError, "text/plain")]
|
||||||
public IActionResult CreatePushoverConnector([FromBody]PushoverRecord pushoverRecord)
|
public IActionResult CreatePushoverConnector([FromBody]PushoverRecord pushoverRecord)
|
||||||
{
|
{
|
||||||
@@ -154,6 +152,6 @@ public class NotificationConnectorController(NotificationsContext context) : Con
|
|||||||
|
|
||||||
if(context.Sync() is { success: false } result)
|
if(context.Sync() is { success: false } result)
|
||||||
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
return StatusCode(Status500InternalServerError, result.exceptionMessage);
|
||||||
return Created();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -44,41 +44,30 @@ public class NotificationConnector(string name, string url, Dictionary<string, s
|
|||||||
public void SendNotification(string title, string notificationText)
|
public void SendNotification(string title, string notificationText)
|
||||||
{
|
{
|
||||||
Log.Info($"Sending notification: {title} - {notificationText}");
|
Log.Info($"Sending notification: {title} - {notificationText}");
|
||||||
CustomWebhookFormatProvider formatProvider = new (title, notificationText);
|
string formattedUrl = FormatStr(Url, title, notificationText);
|
||||||
string formattedUrl = string.Format(formatProvider, Url);
|
string formattedBody = FormatStr(Body, title, notificationText);
|
||||||
string formattedBody = string.Format(formatProvider, Body, title, notificationText);
|
|
||||||
Dictionary<string, string> formattedHeaders = Headers.ToDictionary(h => h.Key,
|
Dictionary<string, string> formattedHeaders = Headers.ToDictionary(h => h.Key,
|
||||||
h => string.Format(formatProvider, h.Value, title, notificationText));
|
h => FormatStr(h.Value, title, notificationText));
|
||||||
|
|
||||||
HttpRequestMessage request = new(System.Net.Http.HttpMethod.Parse(HttpMethod), formattedUrl);
|
HttpRequestMessage request = new(System.Net.Http.HttpMethod.Parse(HttpMethod), formattedUrl);
|
||||||
foreach (var (key, value) in formattedHeaders)
|
foreach (var (key, value) in formattedHeaders)
|
||||||
request.Headers.Add(key, value);
|
request.Headers.Add(key, value);
|
||||||
request.Content = new StringContent(formattedBody);
|
request.Content = new StringContent(formattedBody);
|
||||||
|
request.Content.Headers.ContentType = new ("application/json");
|
||||||
Log.Debug($"Request: {request}");
|
Log.Debug($"Request: {request}");
|
||||||
|
|
||||||
HttpResponseMessage response = Client.Send(request);
|
HttpResponseMessage response = Client.Send(request);
|
||||||
Log.Debug($"Response status code: {response.StatusCode}");
|
Log.Debug($"Response status code: {response.StatusCode} {response.Content.ReadAsStringAsync().Result}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private class CustomWebhookFormatProvider(string title, string text) : IFormatProvider
|
private string FormatStr(string str, string title, string text)
|
||||||
{
|
{
|
||||||
public object? GetFormat(Type? formatType)
|
StringBuilder sb = new (str);
|
||||||
{
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Format(string fmt, object arg, IFormatProvider provider)
|
|
||||||
{
|
|
||||||
if(arg.GetType() != typeof(string))
|
|
||||||
return arg.ToString() ?? string.Empty;
|
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder(fmt);
|
|
||||||
sb.Replace("%title", title);
|
sb.Replace("%title", title);
|
||||||
sb.Replace("%text", text);
|
sb.Replace("%text", text);
|
||||||
|
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public override string ToString() => $"{GetType().Name} {Name}";
|
public override string ToString() => $"{GetType().Name} {Name}";
|
||||||
}
|
}
|
@@ -71,7 +71,7 @@ public static class Tranga
|
|||||||
return mangaConnector != null;
|
return mangaConnector != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static readonly Dictionary<IPeriodic, Task> PeriodicWorkers = new ();
|
internal static readonly ConcurrentDictionary<IPeriodic, Task> PeriodicWorkers = new ();
|
||||||
|
|
||||||
public static void AddWorker(BaseWorker worker)
|
public static void AddWorker(BaseWorker worker)
|
||||||
{
|
{
|
||||||
@@ -84,8 +84,9 @@ public static class Tranga
|
|||||||
private static void AddPeriodicWorker(BaseWorker worker, IPeriodic periodic)
|
private static void AddPeriodicWorker(BaseWorker worker, IPeriodic periodic)
|
||||||
{
|
{
|
||||||
Log.Debug($"Adding Periodic {worker}");
|
Log.Debug($"Adding Periodic {worker}");
|
||||||
PeriodicWorkers.Add((worker as IPeriodic)!, PeriodicTask(worker, periodic));
|
Task periodicTask = PeriodicTask(worker, periodic);
|
||||||
PeriodicWorkers[(worker as IPeriodic)!].Start();
|
PeriodicWorkers.TryAdd((worker as IPeriodic)!, periodicTask);
|
||||||
|
periodicTask.Start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Task PeriodicTask(BaseWorker worker, IPeriodic periodic) => new (() =>
|
private static Task PeriodicTask(BaseWorker worker, IPeriodic periodic) => new (() =>
|
||||||
@@ -100,8 +101,9 @@ public static class Tranga
|
|||||||
if (worker.State < WorkerExecutionState.Created) //Failed
|
if (worker.State < WorkerExecutionState.Created) //Failed
|
||||||
return;
|
return;
|
||||||
Log.Debug($"Refreshing {worker}");
|
Log.Debug($"Refreshing {worker}");
|
||||||
PeriodicWorkers[(worker as IPeriodic)!] = PeriodicTask(worker, periodic);
|
Task periodicTask = PeriodicTask(worker, periodic);
|
||||||
PeriodicWorkers[(worker as IPeriodic)!].Start();
|
PeriodicWorkers.AddOrUpdate((worker as IPeriodic)!, periodicTask, (_, _) => periodicTask);
|
||||||
|
periodicTask.Start();
|
||||||
};
|
};
|
||||||
|
|
||||||
public static void AddWorkers(IEnumerable<BaseWorker> workers)
|
public static void AddWorkers(IEnumerable<BaseWorker> workers)
|
||||||
@@ -149,7 +151,7 @@ public static class Tranga
|
|||||||
{
|
{
|
||||||
Log.Debug($"Stopping {worker}");
|
Log.Debug($"Stopping {worker}");
|
||||||
if(worker is IPeriodic periodicWorker)
|
if(worker is IPeriodic periodicWorker)
|
||||||
PeriodicWorkers.Remove(periodicWorker);
|
PeriodicWorkers.Remove(periodicWorker, out _);
|
||||||
worker.Cancel();
|
worker.Cancel();
|
||||||
RunningWorkers.Remove(worker, out _);
|
RunningWorkers.Remove(worker, out _);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user