mirror of
https://github.com/C9Glax/tranga.git
synced 2025-07-03 17:34:17 +02:00
WIP
This commit is contained in:
52
API/Schema/NotificationsContext/Notification.cs
Normal file
52
API/Schema/NotificationsContext/Notification.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Schema.NotificationsContext;
|
||||
|
||||
[PrimaryKey(nameof(Key))]
|
||||
public class Notification : Identifiable
|
||||
{
|
||||
[Required]
|
||||
public NotificationUrgency Urgency { get; init; }
|
||||
|
||||
[StringLength(128)]
|
||||
[Required]
|
||||
public string Title { get; init; }
|
||||
|
||||
[StringLength(512)]
|
||||
[Required]
|
||||
public string Message { get; init; }
|
||||
|
||||
[Required]
|
||||
public DateTime Date { get; init; }
|
||||
|
||||
public Notification(string title, string message = "", NotificationUrgency urgency = NotificationUrgency.Normal, DateTime? date = null)
|
||||
: base(TokenGen.CreateToken("Notification"))
|
||||
{
|
||||
this.Title = title;
|
||||
this.Message = message;
|
||||
this.Urgency = urgency;
|
||||
this.Date = date ?? DateTime.UtcNow;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EF ONLY!!!
|
||||
/// </summary>
|
||||
public Notification(string key, string title, string message, NotificationUrgency urgency, DateTime date)
|
||||
: base(key)
|
||||
{
|
||||
this.Title = title;
|
||||
this.Message = message;
|
||||
this.Urgency = urgency;
|
||||
this.Date = date;
|
||||
}
|
||||
|
||||
public override string ToString() => $"{base.ToString()} {Urgency} {Title}";
|
||||
}
|
||||
|
||||
public enum NotificationUrgency : byte
|
||||
{
|
||||
Low = 1,
|
||||
Normal = 3,
|
||||
High = 5
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text;
|
||||
using log4net;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace API.Schema.NotificationsContext.NotificationConnectors;
|
||||
|
||||
[PrimaryKey("Name")]
|
||||
public class NotificationConnector(string name, string url, Dictionary<string, string> headers, string httpMethod, string body)
|
||||
{
|
||||
[StringLength(64)]
|
||||
[Required]
|
||||
public string Name { get; init; } = name;
|
||||
|
||||
[StringLength(2048)]
|
||||
[Required]
|
||||
[Url]
|
||||
public string Url { get; internal set; } = url;
|
||||
|
||||
[Required]
|
||||
public Dictionary<string, string> Headers { get; internal set; } = headers;
|
||||
|
||||
[StringLength(8)]
|
||||
[Required]
|
||||
public string HttpMethod { get; internal set; } = httpMethod;
|
||||
|
||||
[StringLength(4096)]
|
||||
[Required]
|
||||
public string Body { get; internal set; } = body;
|
||||
|
||||
[JsonIgnore]
|
||||
[NotMapped]
|
||||
private readonly HttpClient Client = new()
|
||||
{
|
||||
DefaultRequestHeaders = { { "User-Agent", TrangaSettings.userAgent } }
|
||||
};
|
||||
|
||||
[JsonIgnore]
|
||||
[NotMapped]
|
||||
protected ILog Log = LogManager.GetLogger(name);
|
||||
|
||||
public void SendNotification(string title, string notificationText)
|
||||
{
|
||||
Log.Info($"Sending notification: {title} - {notificationText}");
|
||||
CustomWebhookFormatProvider formatProvider = new (title, notificationText);
|
||||
string formattedUrl = string.Format(formatProvider, Url);
|
||||
string formattedBody = string.Format(formatProvider, Body, title, notificationText);
|
||||
Dictionary<string, string> formattedHeaders = Headers.ToDictionary(h => h.Key,
|
||||
h => string.Format(formatProvider, h.Value, title, notificationText));
|
||||
|
||||
HttpRequestMessage request = new(System.Net.Http.HttpMethod.Parse(HttpMethod), formattedUrl);
|
||||
foreach (var (key, value) in formattedHeaders)
|
||||
request.Headers.Add(key, value);
|
||||
request.Content = new StringContent(formattedBody);
|
||||
Log.Debug($"Request: {request}");
|
||||
|
||||
HttpResponseMessage response = Client.Send(request);
|
||||
Log.Debug($"Response status code: {response.StatusCode}");
|
||||
}
|
||||
|
||||
private class CustomWebhookFormatProvider(string title, string text) : IFormatProvider
|
||||
{
|
||||
public object? GetFormat(Type? formatType)
|
||||
{
|
||||
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("%text", text);
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
10
API/Schema/NotificationsContext/NotificationsContext.cs
Normal file
10
API/Schema/NotificationsContext/NotificationsContext.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using API.Schema.NotificationsContext.NotificationConnectors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Schema.NotificationsContext;
|
||||
|
||||
public class NotificationsContext(DbContextOptions<NotificationsContext> options) : TrangaBaseContext<NotificationsContext>(options)
|
||||
{
|
||||
public DbSet<NotificationConnector> NotificationConnectors { get; set; }
|
||||
public DbSet<Notification> Notifications { get; set; }
|
||||
}
|
Reference in New Issue
Block a user