2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2020-05-27 18:41:00 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2020-05-28 22:48:09 -05:00
|
|
|
using System.Security.Claims;
|
2020-05-27 18:41:00 -05:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
2020-08-12 16:02:13 +09:00
|
|
|
using BTCPayServer.Components.NotificationsDropdown;
|
2020-05-28 15:15:24 -05:00
|
|
|
using BTCPayServer.Data;
|
2020-05-28 16:19:02 -05:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-06-17 11:26:21 +09:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-06-12 00:01:45 -05:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2020-06-16 23:29:25 +09:00
|
|
|
using Newtonsoft.Json;
|
2020-05-27 18:41:00 -05:00
|
|
|
|
2020-06-15 01:22:09 -05:00
|
|
|
namespace BTCPayServer.Services.Notifications
|
2020-05-27 18:41:00 -05:00
|
|
|
{
|
2020-05-28 22:48:09 -05:00
|
|
|
public class NotificationManager
|
|
|
|
{
|
2020-06-12 00:01:45 -05:00
|
|
|
private readonly ApplicationDbContextFactory _factory;
|
2020-06-11 17:58:50 -05:00
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
2020-06-14 23:26:04 -05:00
|
|
|
private readonly IMemoryCache _memoryCache;
|
2020-06-24 10:23:16 +02:00
|
|
|
private readonly EventAggregator _eventAggregator;
|
2020-06-16 23:29:25 +09:00
|
|
|
private readonly Dictionary<string, INotificationHandler> _handlersByNotificationType;
|
2020-05-28 22:48:09 -05:00
|
|
|
|
2020-06-23 03:06:02 +02:00
|
|
|
public NotificationManager(ApplicationDbContextFactory factory, UserManager<ApplicationUser> userManager,
|
2020-06-24 10:23:16 +02:00
|
|
|
IMemoryCache memoryCache, IEnumerable<INotificationHandler> handlers, EventAggregator eventAggregator)
|
2020-05-28 22:48:09 -05:00
|
|
|
{
|
2020-06-12 00:01:45 -05:00
|
|
|
_factory = factory;
|
2020-06-11 17:58:50 -05:00
|
|
|
_userManager = userManager;
|
2020-06-14 23:26:04 -05:00
|
|
|
_memoryCache = memoryCache;
|
2020-06-24 10:23:16 +02:00
|
|
|
_eventAggregator = eventAggregator;
|
2020-06-16 23:29:25 +09:00
|
|
|
_handlersByNotificationType = handlers.ToDictionary(h => h.NotificationType);
|
2020-05-28 22:48:09 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 00:01:45 -05:00
|
|
|
private const int _cacheExpiryMs = 5000;
|
2020-06-23 03:06:02 +02:00
|
|
|
|
2020-06-17 11:26:21 +09:00
|
|
|
public async Task<NotificationSummaryViewModel> GetSummaryNotifications(ClaimsPrincipal user)
|
2020-05-28 22:48:09 -05:00
|
|
|
{
|
2020-06-11 17:58:50 -05:00
|
|
|
var userId = _userManager.GetUserId(user);
|
2020-06-23 03:06:02 +02:00
|
|
|
var cacheKey = GetNotificationsCacheId(userId);
|
2020-06-15 01:22:09 -05:00
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
return await _memoryCache.GetOrCreateAsync(cacheKey, async entry =>
|
|
|
|
{
|
|
|
|
var resp = await GetNotifications(new NotificationsQuery()
|
|
|
|
{
|
|
|
|
Seen = false, Skip = 0, Take = 5, UserId = userId
|
|
|
|
});
|
|
|
|
entry.SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs));
|
|
|
|
var res = new NotificationSummaryViewModel() {Last5 = resp.Items, UnseenCount = resp.Count};
|
|
|
|
entry.Value = res;
|
|
|
|
return res;
|
|
|
|
});
|
2020-06-12 00:01:45 -05:00
|
|
|
}
|
2020-12-11 15:11:08 +01:00
|
|
|
|
|
|
|
public void InvalidateNotificationCache(params string[] userIds)
|
2020-06-23 03:06:02 +02:00
|
|
|
{
|
2020-12-11 15:11:08 +01:00
|
|
|
foreach (var userId in userIds)
|
|
|
|
{
|
|
|
|
_memoryCache.Remove(GetNotificationsCacheId(userId));
|
|
|
|
_eventAggregator.Publish(new UserNotificationsUpdatedEvent() {UserId = userId});
|
|
|
|
}
|
2020-06-23 03:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetNotificationsCacheId(string userId)
|
|
|
|
{
|
|
|
|
return $"notifications-{userId}";
|
|
|
|
}
|
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
public async Task<(List<NotificationViewModel> Items, int Count)> GetNotifications(NotificationsQuery query)
|
|
|
|
{
|
|
|
|
await using var dbContext = _factory.CreateContext();
|
|
|
|
|
|
|
|
var queryables = GetNotificationsQueryable(dbContext, query);
|
|
|
|
|
|
|
|
return (Items: (await queryables.withPaging.ToListAsync()).Select(ToViewModel).ToList(),
|
|
|
|
Count: await queryables.withoutPaging.CountAsync());
|
|
|
|
}
|
|
|
|
|
|
|
|
private ( IQueryable<NotificationData> withoutPaging, IQueryable<NotificationData> withPaging)
|
|
|
|
GetNotificationsQueryable(ApplicationDbContext dbContext, NotificationsQuery query)
|
2020-06-12 00:01:45 -05:00
|
|
|
{
|
2020-12-11 15:11:08 +01:00
|
|
|
var queryable = dbContext.Notifications.AsQueryable();
|
|
|
|
if (query.Ids?.Any() is true)
|
2020-06-10 18:55:31 -05:00
|
|
|
{
|
2020-12-11 15:11:08 +01:00
|
|
|
queryable = queryable.Where(data => query.Ids.Contains(data.Id));
|
|
|
|
}
|
2020-06-12 00:01:45 -05:00
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
if (!string.IsNullOrEmpty(query.UserId))
|
|
|
|
{
|
|
|
|
queryable = queryable.Where(data => data.ApplicationUserId == query.UserId);
|
2020-06-10 18:55:31 -05:00
|
|
|
}
|
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
if (query.Seen.HasValue)
|
|
|
|
{
|
|
|
|
queryable = queryable.Where(data => data.Seen == query.Seen);
|
|
|
|
}
|
|
|
|
|
|
|
|
queryable = queryable.OrderByDescending(a => a.Created);
|
|
|
|
|
|
|
|
var queryable2 = queryable;
|
|
|
|
if (query.Skip.HasValue)
|
|
|
|
{
|
|
|
|
queryable2 = queryable.Skip(query.Skip.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (query.Take.HasValue)
|
|
|
|
{
|
|
|
|
queryable2 = queryable.Take(query.Take.Value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (queryable, queryable2);
|
2020-05-28 22:48:09 -05:00
|
|
|
}
|
2020-06-16 23:29:25 +09:00
|
|
|
|
2020-12-11 15:11:08 +01:00
|
|
|
|
|
|
|
public async Task<List<NotificationViewModel>> ToggleSeen(NotificationsQuery notificationsQuery, bool? setSeen)
|
|
|
|
{
|
|
|
|
await using var dbContext = _factory.CreateContext();
|
|
|
|
|
|
|
|
var queryables = GetNotificationsQueryable(dbContext, notificationsQuery);
|
|
|
|
var items = await queryables.withPaging.ToListAsync();
|
|
|
|
var userIds = items.Select(data => data.ApplicationUserId).Distinct();
|
|
|
|
foreach (var notificationData in items)
|
|
|
|
{
|
|
|
|
notificationData.Seen = setSeen.GetValueOrDefault(!notificationData.Seen);
|
|
|
|
}
|
|
|
|
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
InvalidateNotificationCache(userIds.ToArray());
|
|
|
|
return items.Select(ToViewModel).ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Remove(NotificationsQuery notificationsQuery)
|
|
|
|
{
|
|
|
|
await using var dbContext = _factory.CreateContext();
|
|
|
|
|
|
|
|
var queryables = GetNotificationsQueryable(dbContext, notificationsQuery);
|
|
|
|
dbContext.RemoveRange(queryables.withPaging);
|
|
|
|
await dbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(notificationsQuery.UserId))
|
|
|
|
InvalidateNotificationCache(notificationsQuery.UserId);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private NotificationViewModel ToViewModel(NotificationData data)
|
2020-06-16 23:29:25 +09:00
|
|
|
{
|
|
|
|
var handler = GetHandler(data.NotificationType);
|
|
|
|
var notification = JsonConvert.DeserializeObject(ZipUtils.Unzip(data.Blob), handler.NotificationBlobType);
|
2020-12-11 15:11:08 +01:00
|
|
|
var obj = new NotificationViewModel {Id = data.Id, Created = data.Created, Seen = data.Seen};
|
2020-06-16 23:29:25 +09:00
|
|
|
handler.FillViewModel(notification, obj);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
public INotificationHandler GetHandler(string notificationId)
|
|
|
|
{
|
|
|
|
if (_handlersByNotificationType.TryGetValue(notificationId, out var h))
|
|
|
|
return h;
|
|
|
|
throw new InvalidOperationException($"No INotificationHandler found for {notificationId}");
|
|
|
|
}
|
2020-05-28 22:48:09 -05:00
|
|
|
}
|
2020-12-11 15:11:08 +01:00
|
|
|
|
|
|
|
public class NotificationsQuery
|
|
|
|
{
|
|
|
|
public string[] Ids { get; set; }
|
|
|
|
public string UserId { get; set; }
|
|
|
|
public int? Skip { get; set; }
|
|
|
|
public int? Take { get; set; }
|
|
|
|
public bool? Seen { get; set; }
|
|
|
|
}
|
2020-05-27 18:41:00 -05:00
|
|
|
}
|