Prune webhook data from database

This commit is contained in:
nicolas.dorier
2023-05-28 23:44:10 +09:00
committed by Andrew Camilleri
parent 418b476725
commit 4e03c2523a
21 changed files with 515 additions and 161 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -29,12 +30,30 @@ namespace BTCPayServer.Data
[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), HostedServices.WebhookSender.DefaultSerializerSettings);
}
public bool IsPruned()
{
return ReadRequestAs<WebhookEvent>().IsPruned();
}
}
public class WebhookBlob
{
@@ -56,11 +75,17 @@ namespace BTCPayServer.Data
}
public static WebhookDeliveryBlob GetBlob(this WebhookDeliveryData webhook)
{
return webhook.HasTypedBlob<WebhookDeliveryBlob>().GetBlob(HostedServices.WebhookSender.DefaultSerializerSettings);
if (webhook.Blob is null)
return null;
else
return JsonConvert.DeserializeObject<WebhookDeliveryBlob>(webhook.Blob, HostedServices.WebhookSender.DefaultSerializerSettings);
}
public static void SetBlob(this WebhookDeliveryData webhook, WebhookDeliveryBlob blob)
{
webhook.HasTypedBlob<WebhookDeliveryBlob>().SetBlob(blob, HostedServices.WebhookSender.DefaultSerializerSettings);
if (blob is null)
webhook.Blob = null;
else
webhook.Blob = JsonConvert.SerializeObject(blob, HostedServices.WebhookSender.DefaultSerializerSettings);
}
}
}