btcpayserver/BTCPayServer/Models/StoreViewModels/EditWebhookViewModel.cs

86 lines
2.7 KiB
C#
Raw Normal View History

2020-11-06 12:42:26 +01:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Data;
using BTCPayServer.Validation;
namespace BTCPayServer.Models.StoreViewModels
{
public class DeliveryViewModel
{
public DeliveryViewModel()
{
}
2020-11-13 06:01:51 +01:00
public DeliveryViewModel(Data.WebhookDeliveryData s)
2020-11-06 12:42:26 +01:00
{
var blob = s.GetBlob();
Id = s.Id;
Success = blob.Status == WebhookDeliveryStatus.HttpSuccess;
ErrorMessage = blob.ErrorMessage ?? "Success";
Time = s.Timestamp;
2023-05-28 16:44:10 +02:00
var evt = blob.ReadRequestAs<WebhookEvent>();
Type = evt.Type;
Pruned = evt.IsPruned();
2020-11-06 12:42:26 +01:00
WebhookId = s.Id;
PayloadUrl = s.Webhook?.GetBlob().Url;
2020-11-06 12:42:26 +01:00
}
public string Id { get; set; }
public DateTimeOffset Time { get; set; }
public WebhookEventType Type { get; private set; }
2023-05-28 16:44:10 +02:00
public bool Pruned { get; set; }
2020-11-06 12:42:26 +01:00
public string WebhookId { get; set; }
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public string PayloadUrl { get; set; }
2020-11-06 12:42:26 +01:00
}
public class EditWebhookViewModel
{
public EditWebhookViewModel()
{
}
public EditWebhookViewModel(WebhookBlob blob)
{
Active = blob.Active;
AutomaticRedelivery = blob.AutomaticRedelivery;
Everything = blob.AuthorizedEvents.Everything;
Events = blob.AuthorizedEvents.SpecificEvents;
PayloadUrl = blob.Url;
Secret = blob.Secret;
IsNew = false;
}
public bool IsNew { get; set; }
public bool Active { get; set; }
public bool AutomaticRedelivery { get; set; }
public bool Everything { get; set; }
public WebhookEventType[] Events { get; set; } = Array.Empty<WebhookEventType>();
[Uri]
[Required]
public string PayloadUrl { get; set; }
[MaxLength(64)]
public string Secret { get; set; }
2021-12-31 08:59:02 +01:00
public List<DeliveryViewModel> Deliveries { get; set; } = new List<DeliveryViewModel>();
2020-11-06 12:42:26 +01:00
public WebhookBlob CreateBlob()
{
return new WebhookBlob()
{
Active = Active,
Secret = Secret,
AutomaticRedelivery = AutomaticRedelivery,
Url = new Uri(PayloadUrl, UriKind.Absolute).AbsoluteUri,
AuthorizedEvents = new AuthorizedWebhookEvents()
{
Everything = Everything,
SpecificEvents = Events
}
};
}
}
}