Displaying number of unread messages in _Layout.cshtml

This commit is contained in:
rockstardev 2020-05-28 22:48:09 -05:00
parent 3eb503b13b
commit 654bb0c8ca
3 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Claims;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -53,4 +54,25 @@ namespace BTCPayServer.HostedServices
} }
} }
} }
public class NotificationManager
{
private readonly ApplicationDbContext _db;
public NotificationManager(ApplicationDbContext db)
{
_db = db;
}
public int GetNotificationCount(ClaimsPrincipal user)
{
var claimWithId = user.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
// TODO: Soft caching in order not to pound database too much
var count = _db.Notifications
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
.Count();
return count;
}
}
} }

View File

@ -201,6 +201,7 @@ namespace BTCPayServer.Hosting
services.AddSingleton<ChangellyClientProvider>(); services.AddSingleton<ChangellyClientProvider>();
services.AddSingleton<IHostedService, NotificationDbSaver>(); services.AddSingleton<IHostedService, NotificationDbSaver>();
services.AddScoped<NotificationManager>();
services.AddSingleton<IHostedService, NBXplorerWaiters>(); services.AddSingleton<IHostedService, NBXplorerWaiters>();
services.AddSingleton<IHostedService, InvoiceNotificationManager>(); services.AddSingleton<IHostedService, InvoiceNotificationManager>();

View File

@ -4,6 +4,7 @@
@inject BTCPayServer.Services.BTCPayServerEnvironment env @inject BTCPayServer.Services.BTCPayServerEnvironment env
@inject BTCPayServer.HostedServices.NBXplorerDashboard dashboard @inject BTCPayServer.HostedServices.NBXplorerDashboard dashboard
@inject BTCPayServer.HostedServices.CssThemeManager themeManager @inject BTCPayServer.HostedServices.CssThemeManager themeManager
@inject BTCPayServer.HostedServices.NotificationManager notificationManager
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
@ -80,7 +81,11 @@
</li> </li>
<li class="nav-item"> <li class="nav-item">
<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> <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> @{ var notifications = notificationManager.GetNotificationCount(User); }
@if (notifications > 0)
{
<span class="alerts-badge badge badge-pill badge-danger">@notifications</span>
}
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a asp-area="" asp-controller="Account" asp- asp-action="Logout" title="Logout" class="nav-link js-scroll-trigger" id="Logout"><i class="fa fa-sign-out"></i></a> <a asp-area="" asp-controller="Account" asp- asp-action="Logout" title="Logout" class="nav-link js-scroll-trigger" id="Logout"><i class="fa fa-sign-out"></i></a>