btcpayserver/BTCPayServer/Data/WalletTransactionInfo.cs
Nicolas Dorier a2fa688cde
Refactor labels (#4179)
* Create new tables

* wip

* wip

* Refactor LegacyLabel

* Remove LabelFactory

* Add migration

* wip

* wip

* Add pull-payment attachment to tx

* Address kukks points
2022-10-11 17:34:29 +09:00

87 lines
3.2 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using BTCPayServer.Client.Models;
using BTCPayServer.Services;
using BTCPayServer.Services.Labels;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
namespace BTCPayServer.Data
{
public class WalletTransactionInfo
{
public WalletTransactionInfo(WalletId walletId)
{
WalletId = walletId;
}
[JsonIgnore]
public WalletId WalletId { get; }
public string Comment { get; set; } = string.Empty;
[JsonIgnore]
public List<Attachment> Attachments { get; set; } = new List<Attachment>();
[JsonIgnore]
public Dictionary<string, string> LabelColors { get; set; } = new Dictionary<string, string>();
[Obsolete]
Dictionary<string, LabelData>? _LegacyLabels;
[JsonIgnore]
[Obsolete]
public Dictionary<string, LabelData> LegacyLabels
{
get
{
if (_LegacyLabels is null)
{
var legacyLabels = new Dictionary<string, LabelData>();
foreach (var tag in Attachments)
{
switch (tag.Type)
{
case "payout":
PayoutLabel legacyPayoutLabel;
if (legacyLabels.TryGetValue(tag.Type, out var existing) &&
existing is PayoutLabel)
{
legacyPayoutLabel = (PayoutLabel)existing;
}
else
{
legacyPayoutLabel = new PayoutLabel();
legacyLabels.Add(tag.Type, legacyPayoutLabel);
}
var ppid = tag.Data?["pullPaymentId"]?.Value<string>() ?? "";
if (!legacyPayoutLabel.PullPaymentPayouts.TryGetValue(ppid, out var payouts))
{
payouts = new List<string>();
legacyPayoutLabel.PullPaymentPayouts.Add(ppid, payouts);
}
payouts.Add(tag.Id);
break;
case "payjoin":
case "payment-request":
case "app":
case "pj-exposed":
case "invoice":
legacyLabels.TryAdd(tag.Type, new ReferenceLabel(tag.Type, tag.Id));
break;
default:
continue;
}
}
foreach (var label in LabelColors)
{
legacyLabels.TryAdd(label.Key, new RawLabel(label.Key));
}
_LegacyLabels = legacyLabels;
}
return _LegacyLabels;
}
}
}
}