Handling notifications event and rendering them through controller

This commit is contained in:
rockstardev
2020-05-28 16:19:02 -05:00
parent e834cd7595
commit 9206f0cd2e
7 changed files with 125 additions and 112 deletions

View File

@@ -1,88 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Events.Notifications;
using BTCPayServer.Filters;
using BTCPayServer.Models.NotificationViewModels;
using BTCPayServer.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
[BitpayAPIConstraint(false)]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public class NotificationController : Controller
{
private readonly EventAggregator _eventAggregator;
public NotificationController(EventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
[HttpGet]
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
{
var model = new IndexViewModel()
{
Items = new List<IndexViewModel.NoticeDataHolder>
{
new IndexViewModel.NoticeDataHolder
{
Id = 1,
Body = "Hello world",
Level = "Admin",
Created = new DateTime(2020, 01, 05)
},
new IndexViewModel.NoticeDataHolder
{
Id = 1,
Body = "Notification number 2",
Level = "Store",
Created = new DateTime(2020, 02, 07)
},
new IndexViewModel.NoticeDataHolder
{
Id = 1,
Body = "Up unpacked friendly ecstatic so possible humoured do. Ample end might folly quiet one set spoke her. We no am former valley assure. Four need spot ye said we find mile.",
Level = "User",
Created = new DateTime(2020, 03, 01)
},
new IndexViewModel.NoticeDataHolder
{
Id = 1,
Body = "New version of BTCPayServer is detected.",
Level = "Admin",
Created = new DateTime(2020, 06, 22)
},
new IndexViewModel.NoticeDataHolder
{
Id = 1,
Body = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.",
Level = "Store",
Created = new DateTime(2020, 04, 17)
},
new IndexViewModel.NoticeDataHolder
{
Id = 1,
Body = "New invoice paid",
Level = "User",
Created = new DateTime(2020, 05, 07)
}
}
};
model.Items = model.Items.OrderByDescending(a => a.Created).ToList();
return View(model);
}
[HttpGet]
public async Task<IActionResult> Generate()
{
_eventAggregator.NoticeNewVersion("1.1.1");
return RedirectToAction(nameof(Index));
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Events.Notifications;
using BTCPayServer.Filters;
using BTCPayServer.HostedServices;
using BTCPayServer.Models.NotificationViewModels;
using BTCPayServer.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
[BitpayAPIConstraint(false)]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Cookie)]
public class NotificationsController : Controller
{
private readonly EventAggregator _eventAggregator;
public NotificationsController(EventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
[HttpGet]
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
{
var claimWithId = User.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
if (claimWithId == null)
return RedirectToAction("Index", "Home");
var userId = claimWithId.Value;
var model = new IndexViewModel()
{
Items = NotificationDbSaver.Notif
.Skip(skip).Take(count)
.Where(a => a.ApplicationUserId == userId)
.Select(a => a.ViewModel())
.ToList()
};
model.Items = model.Items.OrderByDescending(a => a.Created).ToList();
return View(model);
}
[HttpGet]
public async Task<IActionResult> Generate()
{
_eventAggregator.NoticeNewVersion("1.0.4.4");
// waiting for event handler to catch up
await Task.Delay(1000);
return RedirectToAction(nameof(Index));
}
}
}

View File

@@ -7,12 +7,20 @@ using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Events.Notifications;
using Microsoft.AspNetCore.Identity;
namespace BTCPayServer.HostedServices
{
public class NotificationDbSaver : EventHostedServiceBase
{
public NotificationDbSaver(EventAggregator eventAggregator) : base(eventAggregator) { }
private readonly UserManager<ApplicationUser> _userManager;
public NotificationDbSaver(UserManager<ApplicationUser> userManager,
EventAggregator eventAggregator) : base(eventAggregator) {
_userManager = userManager;
}
public static List<NotificationData> Notif = new List<NotificationData>();
protected override void SubscribeToEvents()
{
@@ -20,21 +28,21 @@ namespace BTCPayServer.HostedServices
base.SubscribeToEvents();
}
public static List<NotificationData> Notif = new List<NotificationData>();
protected override Task ProcessEvent(object evt, CancellationToken cancellationToken)
protected override async Task ProcessEvent(object evt, CancellationToken cancellationToken)
{
if (evt is NewVersionNotification)
{
var data = (evt as NewVersionNotification).ToData();
//var userIds = new[] { "rockstar", "nicolas", "kukkie", "pavel" };
//foreach (var uid in userIds)
data.Id = Guid.NewGuid().ToString();
Notif.Add(data);
}
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
foreach (var admin in admins)
{
data.Id = Guid.NewGuid().ToString();
data.ApplicationUserId = admin.Id;
return Task.CompletedTask;
Notif.Add(data);
}
}
}
}
}

View File

@@ -3,19 +3,46 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Events.Notifications;
using ExchangeSharp;
using Newtonsoft.Json;
namespace BTCPayServer.Models.NotificationViewModels
{
public class IndexViewModel
{
public List<NoticeDataHolder> Items { get; set; }
public List<NotificationViewModel> Items { get; set; }
}
public class NoticeDataHolder
public class NotificationViewModel
{
public string Id { get; set; }
public DateTimeOffset Created { get; set; }
public string Body { get; set; }
public string ActionLink { get; set; }
}
public static class NotificationViewModelExt
{
public static NotificationViewModel ViewModel(this NotificationData data)
{
public int Id { get; set; }
public string Body { get; set; }
public string Level { get; set; }
public DateTime Created { get; set; }
if (data.NotificationType == nameof(NewVersionNotification))
{
var casted = JsonConvert.DeserializeObject<NewVersionNotification>(data.Blob.ToStringFromUTF8());
var obj = new NotificationViewModel
{
Id = data.Id,
Created = data.Created,
Body = $"New version {casted.Version} released!",
ActionLink = "https://github.com/btcpayserver/btcpayserver/releases/tag/v" + casted.Version
};
return obj;
}
return null;
}
}
}

View File

@@ -1,6 +1,6 @@
@model BTCPayServer.Models.NotificationViewModels.IndexViewModel
@{
ViewData["Title"] = "Notices";
ViewData["Title"] = "Notifications";
}
@section HeadScripts {
<script src="~/modal/btcpay.js" asp-append-version="true"></script>
@@ -26,20 +26,29 @@
<table class="table table-sm table-responsive-md">
<thead>
<tr>
<th width="160px">Date</th>
<th>Message</th>
<th>Level</th>
<th>Date</th>
<th width="70px">&nbsp;</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr>
<td>@item.Created.ToBrowserDate()</td>
<td>@item.Body</td>
<td>@item.Level</td>
<td width="100px">@item.Created.ToString("yyyy-MM-dd")</td>
</tr>
}
<td>
@if (!String.IsNullOrEmpty(item.ActionLink))
{
<a href="@item.ActionLink">Action</a>
}
else
{
<span>&nbsp;</span>
}
</td>
</tr>
}
</tbody>
</table>

View File

@@ -79,7 +79,7 @@
<a asp-area="" asp-controller="Manage" asp-action="Index" title="My settings" class="nav-link js-scroll-trigger" id="MySettings"><i class="fa fa-user"></i></a>
</li>
<li class="nav-item">
<a asp-area="" asp-controller="Notification" asp-action="Index" title="Notifications" class="nav-link js-scroll-trigger" id="Notifications"><i class="fa fa-bell"></i></a>
<a asp-area="" asp-controller="Notifications" asp-action="Index" title="Notifications" class="nav-link js-scroll-trigger" id="Notifications"><i class="fa fa-bell"></i></a>
<span class="alerts-badge badge badge-pill badge-danger">6</span>
</li>
<li class="nav-item">