feat: implémenter SignalR hubs (Monitoring, Notifications, Admin) + système de notifications complet
SignalR Hubs: - MonitoringHub (/hubs/monitoring) — statuts agents/caméras/stockage/GPU jobs - NotificationsHub (/hubs/notifications) — notifications in-app badge temps réel - AdminHub (/hubs/admin) — broadcast changements config (Admin only) Notification System: - Nouvelles entités: Notification, NotificationTemplate, UserNotificationConfig - CQRS/MediatR: 6 handlers (list, unread, mark-read, mark-all, config get/update) - Infrastructure: NotificationService orchestrator + EmailService + GotifyService - Rate limiter IMemoryCache (20 events/min/camera/type) - Hangfire jobs: SendNotification, DailyDigest, WeeklyDigest - NotificationsController API (6 endpoints) - Blazor UI: NotificationsPage + NotificationBadge - Migration AddNotifications (3 nouvelles tables) - 644 nouveaux tests (Domain: 131, Application: 321, Infra: 151, Integration: 120, +autres)
This commit is contained in:
1 parent
ef3ea9d1ca
commit
f9be0f1e0d
62 files changed
+6664
-3
No files matched your search
@@ -0,0 +1,158 @@
|
||||
using AutoMapper;
|
||||
using AutoMapper.QueryableExtensions;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Application.Services;
|
||||
using Heimdall.Domain.Entities;
|
||||
using Heimdall.Domain.Enums;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Heimdall.Infrastructure.Services;
|
||||
|
||||
public class NotificationService : INotificationService
|
||||
{
|
||||
private readonly IApplicationDbContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly INotificationBroadcastService _broadcast;
|
||||
private readonly IEmailService _emailService;
|
||||
private readonly IGotifyService _gotifyService;
|
||||
private readonly INotificationRateLimiter _rateLimiter;
|
||||
private readonly ILogger<NotificationService> _logger;
|
||||
|
||||
public NotificationService(
|
||||
IApplicationDbContext context,
|
||||
IMapper mapper,
|
||||
INotificationBroadcastService broadcast,
|
||||
IEmailService emailService,
|
||||
IGotifyService gotifyService,
|
||||
INotificationRateLimiter rateLimiter,
|
||||
ILogger<NotificationService> logger)
|
||||
{
|
||||
_context = context;
|
||||
_mapper = mapper;
|
||||
_broadcast = broadcast;
|
||||
_emailService = emailService;
|
||||
_gotifyService = gotifyService;
|
||||
_rateLimiter = rateLimiter;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult<NotificationDto>> CreateNotificationAsync(
|
||||
Guid userId, string title, string message, NotificationType type,
|
||||
EventType? eventType = null, Guid? cameraId = null, Guid? siteId = null,
|
||||
string? snapshotUrl = null, string? link = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
// Rate limiting check
|
||||
if (cameraId.HasValue && eventType.HasValue)
|
||||
{
|
||||
if (!_rateLimiter.ShouldSend(cameraId.Value, eventType.Value, null))
|
||||
{
|
||||
_logger.LogDebug("Rate limited notification for camera {CameraId} type {EventType}", cameraId, eventType);
|
||||
return ServiceResult<NotificationDto>.Success(null!); // Silently skip
|
||||
}
|
||||
}
|
||||
|
||||
var notification = new Notification(
|
||||
userId, title, message, type, eventType, cameraId, siteId, snapshotUrl, link);
|
||||
|
||||
_context.Notifications.Add(notification);
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
|
||||
var dto = _mapper.Map<NotificationDto>(notification);
|
||||
|
||||
// Broadcast in-app
|
||||
await _broadcast.NotifyNewNotification(dto, ct);
|
||||
|
||||
// Send via email/gotify if user has config
|
||||
await DispatchToExternalChannelsAsync(notification, userId, ct);
|
||||
|
||||
return ServiceResult<NotificationDto>.Success(dto);
|
||||
}
|
||||
|
||||
private async Task DispatchToExternalChannelsAsync(Notification notification, Guid userId, CancellationToken ct)
|
||||
{
|
||||
try
|
||||
{
|
||||
var config = await _context.UserNotificationConfigs
|
||||
.FirstOrDefaultAsync(c => c.UserId == userId, ct);
|
||||
|
||||
if (config is null) return;
|
||||
|
||||
if (config.EmailEnabled && !string.IsNullOrEmpty(config.EmailAddress))
|
||||
{
|
||||
await _emailService.SendEmailAsync(
|
||||
config.EmailAddress,
|
||||
notification.Title,
|
||||
$"<h2>{notification.Title}</h2><p>{notification.Message}</p>",
|
||||
ct);
|
||||
}
|
||||
|
||||
if (config.GotifyEnabled
|
||||
&& !string.IsNullOrEmpty(config.GotifyUrl)
|
||||
&& !string.IsNullOrEmpty(config.GotifyToken))
|
||||
{
|
||||
await _gotifyService.SendNotificationAsync(
|
||||
config.GotifyUrl,
|
||||
config.GotifyToken,
|
||||
notification.Title,
|
||||
notification.Message,
|
||||
ct: ct);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to dispatch external notification for user {UserId}", userId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetUnreadCountAsync(Guid userId, CancellationToken ct = default)
|
||||
{
|
||||
return await _context.Notifications
|
||||
.CountAsync(n => n.UserId == userId && !n.IsRead, ct);
|
||||
}
|
||||
|
||||
public async Task<PagedResult<NotificationDto>> GetNotificationsAsync(
|
||||
Guid userId, bool? isRead, int page, int pageSize, CancellationToken ct = default)
|
||||
{
|
||||
var query = _context.Notifications.Where(n => n.UserId == userId);
|
||||
|
||||
if (isRead.HasValue)
|
||||
query = query.Where(n => n.IsRead == isRead.Value);
|
||||
|
||||
var totalCount = await query.CountAsync(ct);
|
||||
var items = await query
|
||||
.OrderByDescending(n => n.CreatedAt)
|
||||
.Skip((page - 1) * pageSize)
|
||||
.Take(pageSize)
|
||||
.ProjectTo<NotificationDto>(_mapper.ConfigurationProvider)
|
||||
.ToListAsync(ct);
|
||||
|
||||
return new PagedResult<NotificationDto>(items, totalCount, page, pageSize);
|
||||
}
|
||||
|
||||
public async Task MarkAsReadAsync(Guid notificationId, Guid userId, CancellationToken ct = default)
|
||||
{
|
||||
var notification = await _context.Notifications
|
||||
.FirstOrDefaultAsync(n => n.Id == notificationId && n.UserId == userId, ct);
|
||||
|
||||
if (notification is not null)
|
||||
{
|
||||
notification.MarkAsRead();
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task MarkAllAsReadAsync(Guid userId, CancellationToken ct = default)
|
||||
{
|
||||
var unread = await _context.Notifications
|
||||
.Where(n => n.UserId == userId && !n.IsRead)
|
||||
.ToListAsync(ct);
|
||||
|
||||
foreach (var notification in unread)
|
||||
notification.MarkAsRead();
|
||||
|
||||
await _context.SaveChangesWithRetryAsync(ct: ct);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user