btcpayserver/BTCPayServer/Services/Notifications/NotificationManager.cs

133 lines
5.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
2020-05-28 15:15:24 -05:00
using BTCPayServer.Data;
using BTCPayServer.Models.NotificationViewModels;
2020-06-16 23:29:25 +09:00
using Google.Apis.Storage.v1.Data;
using Microsoft.AspNetCore.Identity;
2020-06-17 11:26:21 +09:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
2020-06-16 23:29:25 +09:00
using Newtonsoft.Json;
namespace BTCPayServer.Services.Notifications
{
public class NotificationManager
{
private readonly ApplicationDbContextFactory _factory;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IMemoryCache _memoryCache;
2020-06-16 23:29:25 +09:00
private readonly Dictionary<string, INotificationHandler> _handlersByNotificationType;
private readonly Dictionary<Type, INotificationHandler> _handlersByBlobType;
public NotificationManager(ApplicationDbContextFactory factory, UserManager<ApplicationUser> userManager,
IMemoryCache memoryCache, IEnumerable<INotificationHandler> handlers)
{
_factory = factory;
_userManager = userManager;
_memoryCache = memoryCache;
2020-06-16 23:29:25 +09:00
_handlersByNotificationType = handlers.ToDictionary(h => h.NotificationType);
_handlersByBlobType = handlers.ToDictionary(h => h.NotificationBlobType);
}
private const int _cacheExpiryMs = 5000;
2020-06-17 11:26:21 +09:00
public async Task<NotificationSummaryViewModel> GetSummaryNotifications(ClaimsPrincipal user)
{
var userId = _userManager.GetUserId(user);
var cacheKey = GetNotificationsCacheId(userId);
if (_memoryCache.TryGetValue<NotificationSummaryViewModel>(cacheKey, out var obj))
return obj;
2020-06-17 11:26:21 +09:00
var resp = await FetchNotificationsFromDb(userId);
_memoryCache.Set(cacheKey, resp,
new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMilliseconds(_cacheExpiryMs)));
return resp;
}
public void InvalidateNotificationCache(string userId)
{
_memoryCache.Remove(GetNotificationsCacheId(userId));
}
private static string GetNotificationsCacheId(string userId)
{
return $"notifications-{userId}";
}
2020-06-17 11:26:21 +09:00
private async Task<NotificationSummaryViewModel> FetchNotificationsFromDb(string userId)
{
var resp = new NotificationSummaryViewModel();
using (var _db = _factory.CreateContext())
{
resp.UnseenCount = _db.Notifications
.Where(a => a.ApplicationUserId == userId && !a.Seen)
.Count();
if (resp.UnseenCount > 0)
{
try
{
2020-06-17 11:26:21 +09:00
resp.Last5 = (await _db.Notifications
.Where(a => a.ApplicationUserId == userId && !a.Seen)
.OrderByDescending(a => a.Created)
.Take(5)
.ToListAsync())
2020-06-16 23:29:25 +09:00
.Select(a => ToViewModel(a))
.ToList();
}
2020-06-14 23:04:36 -05:00
catch (System.IO.InvalidDataException)
{
// invalid notifications that are not pkuzipable, burn them all
var notif = _db.Notifications.Where(a => a.ApplicationUserId == userId);
_db.Notifications.RemoveRange(notif);
_db.SaveChanges();
resp.UnseenCount = 0;
resp.Last5 = new List<NotificationViewModel>();
}
}
else
{
resp.Last5 = new List<NotificationViewModel>();
}
}
return resp;
}
2020-06-16 23:29:25 +09:00
public NotificationViewModel ToViewModel(NotificationData data)
{
var handler = GetHandler(data.NotificationType);
var notification = JsonConvert.DeserializeObject(ZipUtils.Unzip(data.Blob), handler.NotificationBlobType);
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-06-16 23:29:25 +09:00
public INotificationHandler GetHandler(Type blobType)
{
if (_handlersByBlobType.TryGetValue(blobType, out var h))
return h;
throw new InvalidOperationException($"No INotificationHandler found for {blobType.Name}");
}
}
public class NotificationSummaryViewModel
{
public int UnseenCount { get; set; }
public List<NotificationViewModel> Last5 { get; set; }
}
}