Displaying last 5 notifications in Layout

This commit is contained in:
rockstardev
2020-06-10 18:55:31 -05:00
parent 359e761922
commit 95a751c505
2 changed files with 58 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ using System.Threading.Tasks;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Events.Notifications;
using BTCPayServer.Models.NotificationViewModels;
using Microsoft.AspNetCore.Identity;
namespace BTCPayServer.HostedServices
@@ -64,15 +65,37 @@ namespace BTCPayServer.HostedServices
_db = db;
}
public int GetNotificationCount(ClaimsPrincipal user)
public NotificationSummaryViewModel GetSummaryNotifications(ClaimsPrincipal user)
{
var resp = new NotificationSummaryViewModel();
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
resp.UnseenCount = _db.Notifications
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
.Count();
return count;
if (resp.UnseenCount > 0)
{
resp.Last5 = _db.Notifications
.Where(a => a.ApplicationUserId == claimWithId.Value && !a.Seen)
.OrderByDescending(a => a.Created)
.Take(5)
.Select(a => a.ViewModel())
.ToList();
}
else
{
resp.Last5 = new List<NotificationViewModel>();
}
return resp;
}
}
public class NotificationSummaryViewModel
{
public int UnseenCount { get; set; }
public List<NotificationViewModel> Last5 { get; set; }
}
}

View File

@@ -79,14 +79,38 @@
<li class="nav-item">
<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="Notifications" asp-action="Index" title="Notifications" class="nav-link js-scroll-trigger" id="Notifications"><i class="fa fa-bell"></i></a>
@{ var notifications = notificationManager.GetNotificationCount(User); }
@if (notifications > 0)
{
<span class="alerts-badge badge badge-pill badge-danger">@notifications</span>
}
</li>
var notificationModel = notificationManager.GetSummaryNotifications(User);
@if (notificationModel.UnseenCount > 0)
{
<li class="nav-item dropdown">
<a class="nav-link js-scroll-trigger" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" id="Notifications">
<i class="fa fa-bell"></i>
</a>
<span class="alerts-badge badge badge-pill badge-danger">@notificationModel.UnseenCount</span>
<div class="dropdown-menu dropdown-menu-right text-center" aria-labelledby="navbarDropdown">
@foreach (var notif in notificationModel.Last5)
{
<a href="@(notif.ActionLink)" class="dropdown-item border-bottom">
<div class="text-left" style="width: 200px; white-space:normal;">
@notif.Body
</div>
<div class="text-left">
<small class="text-muted">@notif.Created.ToBrowserDate()</small>
</div>
</a>
}
<a class="dropdown-item text-info" asp-controller="Notifications" asp-action="Index">See All</a>
</div>
</li>
}
else
{
<li class="nav-item">
<a asp-controller="Notifications" asp-action="Index" title="Notifications" class="nav-link js-scroll-trigger" id="Notifications"><i class="fa fa-bell"></i></a>
</li>
}
<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>
</li>