From 71894ba245f5f72e71a4e7b1afb2828388625e69 Mon Sep 17 00:00:00 2001 From: Andrew Camilleri Date: Wed, 21 Oct 2020 09:53:05 +0200 Subject: [PATCH] Override Block Explorer Links (#2000) * Override Block Explorer Links closes #1953 * load overrides after save as well * fix js --- BTCPayServer.Common/BTCPayNetwork.cs | 18 ++++- BTCPayServer/Controllers/ServerController.cs | 6 +- BTCPayServer/Hosting/BTCPayServerServices.cs | 1 + .../Hosting/BlockExplorerLinkStartupTask.cs | 44 ++++++++++++ BTCPayServer/Services/PoliciesSettings.cs | 10 +++ BTCPayServer/Views/Server/Policies.cshtml | 70 +++++++++++++++---- 6 files changed, 134 insertions(+), 15 deletions(-) create mode 100644 BTCPayServer/Hosting/BlockExplorerLinkStartupTask.cs diff --git a/BTCPayServer.Common/BTCPayNetwork.cs b/BTCPayServer.Common/BTCPayNetwork.cs index a1b7ac1c0..b290f017d 100644 --- a/BTCPayServer.Common/BTCPayNetwork.cs +++ b/BTCPayServer.Common/BTCPayNetwork.cs @@ -130,9 +130,25 @@ namespace BTCPayServer public abstract class BTCPayNetworkBase { + private string _blockExplorerLink; public bool ShowSyncSummary { get; set; } = true; public string CryptoCode { get; internal set; } - public string BlockExplorerLink { get; internal set; } + + public string BlockExplorerLink + { + get => _blockExplorerLink; + set + { + if (string.IsNullOrEmpty(BlockExplorerLinkDefault)) + { + BlockExplorerLinkDefault = value; + } + + _blockExplorerLink = value; + } + } + + public string BlockExplorerLinkDefault { get; internal set; } public string DisplayName { get; set; } public int Divisibility { get; set; } = 8; [Obsolete("Should not be needed")] diff --git a/BTCPayServer/Controllers/ServerController.cs b/BTCPayServer/Controllers/ServerController.cs index 525cd48a1..7bb2728a7 100644 --- a/BTCPayServer/Controllers/ServerController.cs +++ b/BTCPayServer/Controllers/ServerController.cs @@ -11,6 +11,7 @@ using BTCPayServer.Configuration; using BTCPayServer.Data; using BTCPayServer.Events; using BTCPayServer.HostedServices; +using BTCPayServer.Hosting; using BTCPayServer.Logging; using BTCPayServer.Models; using BTCPayServer.Models.AccountViewModels; @@ -273,7 +274,7 @@ namespace BTCPayServer.Controllers [Route("server/policies")] [HttpPost] - public async Task Policies(PoliciesSettings settings, string command = "") + public async Task Policies([FromServices] BTCPayNetworkProvider btcPayNetworkProvider,PoliciesSettings settings, string command = "") { ViewBag.UpdateUrlPresent = _Options.UpdateUrl != null; @@ -292,6 +293,8 @@ namespace BTCPayServer.Controllers return View(settings); } + settings.BlockExplorerLinks = settings.BlockExplorerLinks.Where(tuple => btcPayNetworkProvider.GetNetwork(tuple.CryptoCode).BlockExplorerLinkDefault != tuple.Link).ToList(); + if (!ModelState.IsValid) { return View(settings); @@ -323,6 +326,7 @@ namespace BTCPayServer.Controllers } await _SettingsRepository.UpdateSetting(settings); + BlockExplorerLinkStartupTask.SetLinkOnNetworks(settings.BlockExplorerLinks, btcPayNetworkProvider); TempData[WellKnownTempData.SuccessMessage] = "Policies updated successfully"; return RedirectToAction(nameof(Policies)); } diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index d6979d3f6..aba0809e5 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -91,6 +91,7 @@ namespace BTCPayServer.Hosting services.TryAddSingleton(o => o.GetRequiredService>().Value); services.AddStartupTask(); + services.AddStartupTask(); services.TryAddSingleton(o => { var opts = o.GetRequiredService(); diff --git a/BTCPayServer/Hosting/BlockExplorerLinkStartupTask.cs b/BTCPayServer/Hosting/BlockExplorerLinkStartupTask.cs new file mode 100644 index 000000000..8099a3ff7 --- /dev/null +++ b/BTCPayServer/Hosting/BlockExplorerLinkStartupTask.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using BTCPayServer.Services; + +namespace BTCPayServer.Hosting +{ + public class BlockExplorerLinkStartupTask : IStartupTask + { + private readonly SettingsRepository _settingsRepository; + private readonly BTCPayNetworkProvider _btcPayNetworkProvider; + + public BlockExplorerLinkStartupTask(SettingsRepository settingsRepository, + BTCPayNetworkProvider btcPayNetworkProvider) + { + _settingsRepository = settingsRepository; + _btcPayNetworkProvider = btcPayNetworkProvider; + } + + public async Task ExecuteAsync(CancellationToken cancellationToken = default) + { + var settings = await _settingsRepository.GetSettingAsync(); + if (settings?.BlockExplorerLinks?.Any() is true) + { + SetLinkOnNetworks(settings.BlockExplorerLinks, _btcPayNetworkProvider); + } + } + + public static void SetLinkOnNetworks(List links, + BTCPayNetworkProvider networkProvider) + { + var networks = networkProvider.GetAll(); + foreach (var network in networks) + { + var overrideLink = links.SingleOrDefault(item => + item.CryptoCode.Equals(network.CryptoCode, StringComparison.InvariantCultureIgnoreCase)); + network.BlockExplorerLink = overrideLink?.Link ?? network.BlockExplorerLinkDefault; + + } + } + } +} diff --git a/BTCPayServer/Services/PoliciesSettings.cs b/BTCPayServer/Services/PoliciesSettings.cs index ce853f918..abc6f353b 100644 --- a/BTCPayServer/Services/PoliciesSettings.cs +++ b/BTCPayServer/Services/PoliciesSettings.cs @@ -33,8 +33,18 @@ namespace BTCPayServer.Services public string RootAppId { get; set; } public AppType? RootAppType { get; set; } + + [Display(Name = "Override the block explorers used")] + public List BlockExplorerLinks { get; set; } = new List(); + public List DomainToAppMapping { get; set; } = new List(); + public class BlockExplorerOverrideItem + { + public string CryptoCode { get; set; } + public string Link { get; set; } + } + public class DomainToAppMappingItem { [Display(Name = "Domain")] [Required] [HostName] public string Domain { get; set; } diff --git a/BTCPayServer/Views/Server/Policies.cshtml b/BTCPayServer/Views/Server/Policies.cshtml index b8a7cf453..97105ae8d 100644 --- a/BTCPayServer/Views/Server/Policies.cshtml +++ b/BTCPayServer/Views/Server/Policies.cshtml @@ -1,10 +1,11 @@ +@using BTCPayServer.Services @model BTCPayServer.Services.PoliciesSettings @{ ViewData.SetActivePageAndTitle(ServerNavPages.Policies); } - + @if (!this.ViewContext.ModelState.IsValid) {
@@ -49,20 +50,20 @@ @if (ViewBag.UpdateUrlPresent) { -
- - - -
+
+ + + +
}
@if (!Model.DomainToAppMapping.Any()) - { - - } + { + + }
@if (Model.DomainToAppMapping.Any()) @@ -87,10 +88,7 @@
@@ -112,6 +110,40 @@
} +
+
+ + +
+ +
@@ -125,4 +157,16 @@ background-color: #CCCCCC; } + }