mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-22 22:25:28 +01:00
Override Block Explorer Links (#2000)
* Override Block Explorer Links closes #1953 * load overrides after save as well * fix js
This commit is contained in:
parent
2b19e0fbc6
commit
71894ba245
6 changed files with 134 additions and 15 deletions
|
@ -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")]
|
||||
|
|
|
@ -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<IActionResult> Policies(PoliciesSettings settings, string command = "")
|
||||
public async Task<IActionResult> 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));
|
||||
}
|
||||
|
|
|
@ -91,6 +91,7 @@ namespace BTCPayServer.Hosting
|
|||
services.TryAddSingleton<BTCPayServerOptions>(o =>
|
||||
o.GetRequiredService<IOptions<BTCPayServerOptions>>().Value);
|
||||
services.AddStartupTask<MigrationStartupTask>();
|
||||
services.AddStartupTask<BlockExplorerLinkStartupTask>();
|
||||
services.TryAddSingleton<InvoiceRepository>(o =>
|
||||
{
|
||||
var opts = o.GetRequiredService<BTCPayServerOptions>();
|
||||
|
|
44
BTCPayServer/Hosting/BlockExplorerLinkStartupTask.cs
Normal file
44
BTCPayServer/Hosting/BlockExplorerLinkStartupTask.cs
Normal file
|
@ -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<PoliciesSettings>();
|
||||
if (settings?.BlockExplorerLinks?.Any() is true)
|
||||
{
|
||||
SetLinkOnNetworks(settings.BlockExplorerLinks, _btcPayNetworkProvider);
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetLinkOnNetworks(List<PoliciesSettings.BlockExplorerOverrideItem> 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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<BlockExplorerOverrideItem> BlockExplorerLinks { get; set; } = new List<BlockExplorerOverrideItem>();
|
||||
|
||||
public List<DomainToAppMappingItem> DomainToAppMapping { get; set; } = new List<DomainToAppMappingItem>();
|
||||
|
||||
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; }
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
@using BTCPayServer.Services
|
||||
@model BTCPayServer.Services.PoliciesSettings
|
||||
@{
|
||||
ViewData.SetActivePageAndTitle(ServerNavPages.Policies);
|
||||
|
@ -87,10 +88,7 @@
|
|||
<div class="form-group">
|
||||
<label asp-for="DomainToAppMapping[index].AppId"></label>
|
||||
<select asp-for="DomainToAppMapping[index].AppId"
|
||||
asp-items="@(new SelectList(ViewBag.AppsList,
|
||||
nameof(SelectListItem.Value),
|
||||
nameof(SelectListItem.Text),
|
||||
Model.DomainToAppMapping[index].AppId))"
|
||||
asp-items="@(new SelectList(ViewBag.AppsList, nameof(SelectListItem.Value), nameof(SelectListItem.Text), Model.DomainToAppMapping[index].AppId))"
|
||||
class="form-control">
|
||||
</select>
|
||||
|
||||
|
@ -112,6 +110,40 @@
|
|||
|
||||
</div>
|
||||
}
|
||||
<div class="form-group card">
|
||||
<div class="cursor-pointer p-2" data-target="#explorer-links" data-toggle="collapse">
|
||||
<label asp-for="BlockExplorerLinks" class="pb-0 cursor-pointer"></label>
|
||||
<span class="fa fa-chevron-down float-right pt-1"></span>
|
||||
</div>
|
||||
<ul class="list-group list-group-flush collapse show collapse-on-js" id="explorer-links">
|
||||
@inject BTCPayNetworkProvider BTCPayNetworkProvider
|
||||
@{
|
||||
var networks = BTCPayNetworkProvider.GetAll().ToArray();
|
||||
}
|
||||
|
||||
@for (int i = 0; i < networks.Count(); i++)
|
||||
{
|
||||
var network = networks.ElementAt(i);
|
||||
var existingOverride = Model.BlockExplorerLinks?.SingleOrDefault(tuple => tuple.CryptoCode.Equals(network.CryptoCode, StringComparison.InvariantCultureIgnoreCase));
|
||||
var linkValue = existingOverride?.Link ?? network.BlockExplorerLinkDefault;
|
||||
if (Model.BlockExplorerLinks.Count < i + 1)
|
||||
{
|
||||
Model.BlockExplorerLinks.Add(new PoliciesSettings.BlockExplorerOverrideItem() {CryptoCode = network.CryptoCode, Link = network.BlockExplorerLinkDefault});
|
||||
}
|
||||
<li class="list-group-item">
|
||||
<label > @network.DisplayName (@network.CryptoCode)</label>
|
||||
<input type="hidden" asp-for="BlockExplorerLinks[i].CryptoCode" value="@network.CryptoCode"/>
|
||||
<div class="input-group">
|
||||
<input type="text" class="form-control" asp-for="BlockExplorerLinks[i].Link" value="@linkValue" data-default-link="@network.BlockExplorerLinkDefault"/>
|
||||
<div class="input-group-btn only-for-js">
|
||||
<button type="button" class="text-decoration-none btn btn-link revert-default fa fa-refresh " title="Revert to default"></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary" name="command" value="Save">Save</button>
|
||||
</form>
|
||||
|
||||
|
@ -125,4 +157,16 @@
|
|||
background-color: #CCCCCC;
|
||||
}
|
||||
</style>
|
||||
<script >
|
||||
for (let element of document.getElementsByClassName("revert-default")) {
|
||||
element.addEventListener("click", function (){
|
||||
element.parentElement.previousElementSibling.value = element.parentElement.previousElementSibling.getAttribute("data-default-link")
|
||||
});
|
||||
}
|
||||
for (let element of document.getElementsByClassName("collapse-on-js")){
|
||||
if (element.classList.contains("show")){
|
||||
element.classList.remove("show");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue