2020-06-12 00:19:30 -05:00
|
|
|
|
using System.Linq;
|
2020-05-27 18:41:00 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2020-06-11 23:52:46 -05:00
|
|
|
|
using BTCPayServer.Data;
|
2020-06-14 23:47:11 -05:00
|
|
|
|
using BTCPayServer.Events;
|
|
|
|
|
using BTCPayServer.Services.Notifications.Blobs;
|
2020-06-11 23:52:46 -05:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2020-05-27 18:41:00 -05:00
|
|
|
|
|
2020-06-14 23:47:11 -05:00
|
|
|
|
namespace BTCPayServer.Services.Notifications
|
2020-05-27 18:41:00 -05:00
|
|
|
|
{
|
2020-06-11 23:52:46 -05:00
|
|
|
|
public class NotificationSender
|
2020-05-27 18:41:00 -05:00
|
|
|
|
{
|
2020-06-15 01:25:55 -05:00
|
|
|
|
private readonly ApplicationDbContextFactory _contextFactory;
|
2020-06-11 23:52:46 -05:00
|
|
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
|
private readonly EventAggregator _eventAggregator;
|
|
|
|
|
|
2020-06-15 01:25:55 -05:00
|
|
|
|
public NotificationSender(ApplicationDbContextFactory contextFactory, UserManager<ApplicationUser> userManager, EventAggregator eventAggregator)
|
2020-05-27 18:41:00 -05:00
|
|
|
|
{
|
2020-06-15 01:25:55 -05:00
|
|
|
|
_contextFactory = contextFactory;
|
2020-06-11 23:52:46 -05:00
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_eventAggregator = eventAggregator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal async Task NoticeNewVersionAsync(string version)
|
|
|
|
|
{
|
|
|
|
|
var admins = await _userManager.GetUsersInRoleAsync(Roles.ServerAdmin);
|
|
|
|
|
var adminUids = admins.Select(a => a.Id).ToArray();
|
2020-06-15 01:22:09 -05:00
|
|
|
|
|
|
|
|
|
var notif = new NewVersionNotification
|
2020-05-27 18:41:00 -05:00
|
|
|
|
{
|
2020-06-15 01:22:09 -05:00
|
|
|
|
Version = version
|
|
|
|
|
};
|
2020-06-15 01:25:55 -05:00
|
|
|
|
using (var db = _contextFactory.CreateContext())
|
2020-06-15 01:22:09 -05:00
|
|
|
|
{
|
|
|
|
|
foreach (var uid in adminUids)
|
2020-06-11 23:52:46 -05:00
|
|
|
|
{
|
2020-06-15 01:22:09 -05:00
|
|
|
|
var data = notif.ToData(uid);
|
|
|
|
|
db.Notifications.Add(data);
|
2020-06-11 23:52:46 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-15 01:22:09 -05:00
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// propagate notification
|
|
|
|
|
_eventAggregator.Publish(new NotificationEvent
|
|
|
|
|
{
|
|
|
|
|
ApplicationUserIds = adminUids,
|
|
|
|
|
Notification = notif
|
|
|
|
|
});
|
2020-05-27 18:41:00 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|