2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2019-03-07 06:25:09 +01:00
|
|
|
using System.Linq;
|
2018-12-11 16:36:25 +01:00
|
|
|
using System.Threading.Tasks;
|
2021-12-11 04:32:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
|
|
|
using BTCPayServer.Client;
|
2018-12-11 16:36:25 +01:00
|
|
|
using BTCPayServer.Models.AppViewModels;
|
|
|
|
using BTCPayServer.Services.Apps;
|
2021-12-11 04:32:23 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2018-12-11 16:36:25 +01:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
2022-01-07 12:32:00 +09:00
|
|
|
public partial class UIAppsController
|
2018-12-11 16:36:25 +01:00
|
|
|
{
|
2019-02-19 16:01:28 +09:00
|
|
|
public class AppUpdated
|
2018-12-28 17:38:20 +01:00
|
|
|
{
|
|
|
|
public string AppId { get; set; }
|
2019-02-19 16:01:28 +09:00
|
|
|
public object Settings { get; set; }
|
2019-01-05 19:47:39 +01:00
|
|
|
public string StoreId { get; set; }
|
2019-02-19 16:15:14 +09:00
|
|
|
public override string ToString()
|
|
|
|
{
|
2021-04-08 15:32:42 +02:00
|
|
|
return string.Empty;
|
2019-02-19 16:15:14 +09:00
|
|
|
}
|
2018-12-28 17:38:20 +01:00
|
|
|
}
|
2020-01-23 20:19:24 -06:00
|
|
|
|
2021-12-11 04:32:23 +01:00
|
|
|
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Cookie)]
|
2021-04-08 15:32:42 +02:00
|
|
|
[HttpGet("{appId}/settings/crowdfund")]
|
2022-06-19 19:55:47 -07:00
|
|
|
public async Task<IActionResult> UpdateCrowdfund(string appId)
|
2018-12-11 16:36:25 +01:00
|
|
|
{
|
2021-12-20 15:15:32 +01:00
|
|
|
var app = GetCurrentApp();
|
|
|
|
if (app == null)
|
2018-12-11 16:36:25 +01:00
|
|
|
return NotFound();
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
var settings = app.GetSettings<CrowdfundSettings>();
|
2022-06-28 05:03:13 +02:00
|
|
|
var resetEvery = Enum.GetName(typeof(CrowdfundResetEvery), settings.ResetEvery);
|
2021-04-08 15:32:42 +02:00
|
|
|
var vm = new UpdateCrowdfundViewModel
|
2018-12-11 16:36:25 +01:00
|
|
|
{
|
|
|
|
Title = settings.Title,
|
2021-12-20 15:15:32 +01:00
|
|
|
StoreId = app.StoreDataId,
|
|
|
|
StoreName = app.StoreData?.StoreName,
|
2022-06-19 19:55:47 -07:00
|
|
|
StoreDefaultCurrency = await GetStoreDefaultCurrentIfEmpty(app.StoreDataId, settings.TargetCurrency),
|
2021-12-20 15:15:32 +01:00
|
|
|
AppName = app.Name,
|
2018-12-11 16:36:25 +01:00
|
|
|
Enabled = settings.Enabled,
|
|
|
|
EnforceTargetAmount = settings.EnforceTargetAmount,
|
|
|
|
StartDate = settings.StartDate,
|
|
|
|
TargetCurrency = settings.TargetCurrency,
|
|
|
|
Description = settings.Description,
|
2018-12-22 15:02:16 +01:00
|
|
|
MainImageUrl = settings.MainImageUrl,
|
2018-12-22 15:43:40 +01:00
|
|
|
EmbeddedCSS = settings.EmbeddedCSS,
|
2018-12-11 16:36:25 +01:00
|
|
|
EndDate = settings.EndDate,
|
|
|
|
TargetAmount = settings.TargetAmount,
|
2018-12-22 15:02:16 +01:00
|
|
|
CustomCSSLink = settings.CustomCSSLink,
|
|
|
|
NotificationUrl = settings.NotificationUrl,
|
2018-12-28 17:38:20 +01:00
|
|
|
Tagline = settings.Tagline,
|
2018-12-31 11:38:05 +01:00
|
|
|
PerksTemplate = settings.PerksTemplate,
|
|
|
|
DisqusEnabled = settings.DisqusEnabled,
|
|
|
|
SoundsEnabled = settings.SoundsEnabled,
|
|
|
|
DisqusShortname = settings.DisqusShortname,
|
|
|
|
AnimationsEnabled = settings.AnimationsEnabled,
|
2019-01-04 11:42:37 +01:00
|
|
|
ResetEveryAmount = settings.ResetEveryAmount,
|
2022-06-28 05:03:13 +02:00
|
|
|
ResetEvery = resetEvery,
|
|
|
|
IsRecurring = resetEvery != nameof(CrowdfundResetEvery.Never),
|
2021-12-20 15:15:32 +01:00
|
|
|
UseAllStoreInvoices = app.TagAllInvoices,
|
2019-01-09 12:22:36 +01:00
|
|
|
AppId = appId,
|
2022-06-28 07:05:02 +02:00
|
|
|
SearchTerm = app.TagAllInvoices ? $"storeid:{app.StoreDataId}" : $"orderid:{AppService.GetAppOrderId(app)}",
|
2019-01-09 12:22:36 +01:00
|
|
|
DisplayPerksRanking = settings.DisplayPerksRanking,
|
2021-10-04 07:54:06 +02:00
|
|
|
DisplayPerksValue = settings.DisplayPerksValue,
|
2019-03-07 06:25:09 +01:00
|
|
|
SortPerksByPopularity = settings.SortPerksByPopularity,
|
2020-01-23 20:19:24 -06:00
|
|
|
Sounds = string.Join(Environment.NewLine, settings.Sounds),
|
|
|
|
AnimationColors = string.Join(Environment.NewLine, settings.AnimationColors)
|
2018-12-11 16:36:25 +01:00
|
|
|
};
|
|
|
|
return View(vm);
|
|
|
|
}
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2021-12-11 04:32:23 +01:00
|
|
|
[HttpPost("{appId}/settings/crowdfund")]
|
2019-11-14 20:56:28 -06:00
|
|
|
public async Task<IActionResult> UpdateCrowdfund(string appId, UpdateCrowdfundViewModel vm, string command)
|
2018-12-11 16:36:25 +01:00
|
|
|
{
|
2021-12-20 15:15:32 +01:00
|
|
|
var app = GetCurrentApp();
|
|
|
|
if (app == null)
|
2021-10-25 16:54:36 +09:00
|
|
|
return NotFound();
|
2021-12-31 16:59:02 +09:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
vm.TargetCurrency = await GetStoreDefaultCurrentIfEmpty(app.StoreDataId, vm.TargetCurrency);
|
2021-10-25 16:54:36 +09:00
|
|
|
if (_currencies.GetCurrencyData(vm.TargetCurrency, false) == null)
|
2018-12-11 16:36:25 +01:00
|
|
|
ModelState.AddModelError(nameof(vm.TargetCurrency), "Invalid currency");
|
2020-01-23 20:19:24 -06:00
|
|
|
|
2018-12-29 11:52:07 +01:00
|
|
|
try
|
|
|
|
{
|
2021-12-11 04:32:23 +01:00
|
|
|
vm.PerksTemplate = _appService.SerializeTemplate(_appService.Parse(vm.PerksTemplate, vm.TargetCurrency));
|
2018-12-29 11:52:07 +01:00
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(vm.PerksTemplate), "Invalid template");
|
|
|
|
}
|
2022-05-17 14:34:06 +09:00
|
|
|
if (vm.TargetAmount is decimal v && v == 0.0m)
|
|
|
|
{
|
|
|
|
vm.TargetAmount = null;
|
|
|
|
}
|
2019-01-04 13:46:44 +01:00
|
|
|
|
2022-06-28 05:03:13 +02:00
|
|
|
if (!vm.IsRecurring)
|
|
|
|
{
|
|
|
|
vm.ResetEvery = nameof(CrowdfundResetEvery.Never);
|
|
|
|
}
|
|
|
|
|
2019-01-04 13:46:44 +01:00
|
|
|
if (Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery) != CrowdfundResetEvery.Never && !vm.StartDate.HasValue)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(vm.StartDate), "A start date is needed when the goal resets every X amount of time.");
|
|
|
|
}
|
2019-01-06 14:28:53 +01:00
|
|
|
|
|
|
|
if (Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery) != CrowdfundResetEvery.Never && vm.ResetEveryAmount <= 0)
|
|
|
|
{
|
|
|
|
ModelState.AddModelError(nameof(vm.ResetEveryAmount), "You must reset the goal at a minimum of 1 ");
|
|
|
|
}
|
2019-01-10 14:43:47 +01:00
|
|
|
|
2022-05-04 01:34:40 -07:00
|
|
|
if (vm.DisplayPerksRanking)
|
2019-01-10 14:43:47 +01:00
|
|
|
{
|
2022-05-04 01:34:40 -07:00
|
|
|
vm.SortPerksByPopularity = true;
|
2019-01-10 14:43:47 +01:00
|
|
|
}
|
2019-03-07 06:25:09 +01:00
|
|
|
|
2022-05-04 01:34:40 -07:00
|
|
|
var parsedSounds = vm.Sounds?.Split(
|
2020-01-23 20:19:24 -06:00
|
|
|
new[] { "\r\n", "\r", "\n" },
|
2019-03-07 06:25:09 +01:00
|
|
|
StringSplitOptions.None
|
|
|
|
).Select(s => s.Trim()).ToArray();
|
2022-05-04 01:34:40 -07:00
|
|
|
if (vm.SoundsEnabled && (parsedSounds == null || !parsedSounds.Any()))
|
2019-03-07 06:25:09 +01:00
|
|
|
{
|
2022-05-04 01:34:40 -07:00
|
|
|
vm.SoundsEnabled = false;
|
|
|
|
parsedSounds = new CrowdfundSettings().Sounds;
|
2019-03-07 06:25:09 +01:00
|
|
|
}
|
2020-01-23 20:19:24 -06:00
|
|
|
|
2022-05-04 01:34:40 -07:00
|
|
|
var parsedAnimationColors = vm.AnimationColors?.Split(
|
2019-03-07 06:25:09 +01:00
|
|
|
new[] { "\r\n", "\r", "\n" },
|
|
|
|
StringSplitOptions.None
|
|
|
|
).Select(s => s.Trim()).ToArray();
|
2022-05-04 01:34:40 -07:00
|
|
|
if (vm.AnimationsEnabled && (parsedAnimationColors == null || !parsedAnimationColors.Any()))
|
2019-03-07 06:25:09 +01:00
|
|
|
{
|
2022-05-04 01:34:40 -07:00
|
|
|
vm.AnimationsEnabled = false;
|
|
|
|
parsedAnimationColors = new CrowdfundSettings().AnimationColors;
|
2019-03-07 06:25:09 +01:00
|
|
|
}
|
2020-01-23 20:19:24 -06:00
|
|
|
|
2018-12-29 11:52:07 +01:00
|
|
|
if (!ModelState.IsValid)
|
|
|
|
{
|
|
|
|
return View(vm);
|
|
|
|
}
|
2020-01-23 20:19:24 -06:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
app.Name = vm.AppName;
|
2021-12-16 17:37:19 +01:00
|
|
|
var newSettings = new CrowdfundSettings
|
2018-12-11 16:36:25 +01:00
|
|
|
{
|
|
|
|
Title = vm.Title,
|
|
|
|
Enabled = vm.Enabled,
|
|
|
|
EnforceTargetAmount = vm.EnforceTargetAmount,
|
2019-05-15 08:02:39 -05:00
|
|
|
StartDate = vm.StartDate?.ToUniversalTime(),
|
2018-12-11 16:36:25 +01:00
|
|
|
TargetCurrency = vm.TargetCurrency,
|
2019-08-10 14:05:11 +09:00
|
|
|
Description = vm.Description,
|
2019-05-15 08:02:39 -05:00
|
|
|
EndDate = vm.EndDate?.ToUniversalTime(),
|
2018-12-11 16:36:25 +01:00
|
|
|
TargetAmount = vm.TargetAmount,
|
2018-12-22 15:02:16 +01:00
|
|
|
CustomCSSLink = vm.CustomCSSLink,
|
|
|
|
MainImageUrl = vm.MainImageUrl,
|
2018-12-22 15:43:40 +01:00
|
|
|
EmbeddedCSS = vm.EmbeddedCSS,
|
2018-12-22 15:02:16 +01:00
|
|
|
NotificationUrl = vm.NotificationUrl,
|
2018-12-29 11:52:07 +01:00
|
|
|
Tagline = vm.Tagline,
|
2018-12-31 11:38:05 +01:00
|
|
|
PerksTemplate = vm.PerksTemplate,
|
|
|
|
DisqusEnabled = vm.DisqusEnabled,
|
|
|
|
SoundsEnabled = vm.SoundsEnabled,
|
|
|
|
DisqusShortname = vm.DisqusShortname,
|
|
|
|
AnimationsEnabled = vm.AnimationsEnabled,
|
2019-01-04 11:42:37 +01:00
|
|
|
ResetEveryAmount = vm.ResetEveryAmount,
|
|
|
|
ResetEvery = Enum.Parse<CrowdfundResetEvery>(vm.ResetEvery),
|
2021-10-04 07:54:06 +02:00
|
|
|
DisplayPerksValue = vm.DisplayPerksValue,
|
2019-01-09 12:22:36 +01:00
|
|
|
DisplayPerksRanking = vm.DisplayPerksRanking,
|
2019-03-07 06:25:09 +01:00
|
|
|
SortPerksByPopularity = vm.SortPerksByPopularity,
|
|
|
|
Sounds = parsedSounds,
|
|
|
|
AnimationColors = parsedAnimationColors
|
2019-01-05 19:47:39 +01:00
|
|
|
};
|
2019-02-19 12:48:48 +09:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
app.TagAllInvoices = vm.UseAllStoreInvoices;
|
|
|
|
app.SetSettings(newSettings);
|
2019-02-19 12:48:48 +09:00
|
|
|
|
2021-12-20 15:15:32 +01:00
|
|
|
await _appService.UpdateOrCreateApp(app);
|
2020-01-24 15:11:34 -06:00
|
|
|
|
2021-12-11 04:32:23 +01:00
|
|
|
_eventAggregator.Publish(new AppUpdated()
|
2019-11-14 21:13:41 +09:00
|
|
|
{
|
2020-01-24 15:11:34 -06:00
|
|
|
AppId = appId,
|
2021-12-20 15:15:32 +01:00
|
|
|
StoreId = app.StoreDataId,
|
2020-01-24 15:11:34 -06:00
|
|
|
Settings = newSettings
|
|
|
|
});
|
|
|
|
TempData[WellKnownTempData.SuccessMessage] = "App updated";
|
|
|
|
return RedirectToAction(nameof(UpdateCrowdfund), new { appId });
|
2018-12-11 16:36:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|