mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-17 22:14:26 +01:00
Refactoring of Webhooks and Email Rules (#6954)
This commit is contained in:
88
BTCPayServer/Plugins/Webhooks/WebhookDataExtensions.cs
Normal file
88
BTCPayServer/Plugins/Webhooks/WebhookDataExtensions.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using BTCPayServer.Client.Models;
|
||||
using BTCPayServer.Plugins.Webhooks;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace BTCPayServer.Data
|
||||
{
|
||||
public class AuthorizedWebhookEvents
|
||||
{
|
||||
public bool Everything { get; set; }
|
||||
|
||||
public string[] SpecificEvents { get; set; } = Array.Empty<string>();
|
||||
public bool Match(string evt)
|
||||
{
|
||||
return Everything || SpecificEvents.Contains(evt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class WebhookDeliveryBlob
|
||||
{
|
||||
[JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
|
||||
public WebhookDeliveryStatus Status { get; set; }
|
||||
public int? HttpCode { get; set; }
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
public string ErrorMessage { get; set; }
|
||||
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
|
||||
public byte[] Request { get; set; }
|
||||
public void Prune()
|
||||
{
|
||||
var request = JObject.Parse(UTF8Encoding.UTF8.GetString(Request));
|
||||
foreach (var prop in request.Properties().ToList())
|
||||
{
|
||||
if (prop.Name == "type")
|
||||
continue;
|
||||
prop.Remove();
|
||||
}
|
||||
Request = UTF8Encoding.UTF8.GetBytes(request.ToString(Formatting.None));
|
||||
}
|
||||
public T ReadRequestAs<T>()
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(UTF8Encoding.UTF8.GetString(Request), WebhookSender.DefaultSerializerSettings);
|
||||
}
|
||||
|
||||
public bool IsPruned()
|
||||
{
|
||||
return ReadRequestAs<WebhookEvent>().IsPruned();
|
||||
}
|
||||
}
|
||||
public class WebhookBlob
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public bool Active { get; set; } = true;
|
||||
public string Secret { get; set; }
|
||||
public bool AutomaticRedelivery { get; set; }
|
||||
public AuthorizedWebhookEvents AuthorizedEvents { get; set; }
|
||||
public bool ShouldDeliver(string type)
|
||||
=> Active && AuthorizedEvents.Match(type);
|
||||
}
|
||||
public static class WebhookDataExtensions
|
||||
{
|
||||
public static WebhookBlob GetBlob(this WebhookData webhook)
|
||||
{
|
||||
return webhook.HasTypedBlob<WebhookBlob>().GetBlob();
|
||||
}
|
||||
public static void SetBlob(this WebhookData webhook, WebhookBlob blob)
|
||||
{
|
||||
webhook.HasTypedBlob<WebhookBlob>().SetBlob(blob);
|
||||
}
|
||||
public static WebhookDeliveryBlob GetBlob(this WebhookDeliveryData webhook)
|
||||
{
|
||||
if (webhook.Blob is null)
|
||||
return null;
|
||||
else
|
||||
return JsonConvert.DeserializeObject<WebhookDeliveryBlob>(webhook.Blob, WebhookSender.DefaultSerializerSettings);
|
||||
}
|
||||
public static void SetBlob(this WebhookDeliveryData webhook, WebhookDeliveryBlob blob)
|
||||
{
|
||||
if (blob is null)
|
||||
webhook.Blob = null;
|
||||
else
|
||||
webhook.Blob = JsonConvert.SerializeObject(blob, WebhookSender.DefaultSerializerSettings);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user