2026-10-25 05:59:18 +01:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.Localization;
|
2026-07-22 19:54:55 +00:00
|
|
|
using Heimdall.Application.Services;
|
2026-10-22 19:43:18 +02:00
|
|
|
using Heimdall.Domain.Enums;
|
2026-07-22 19:54:55 +00:00
|
|
|
using MediatR;
|
2026-10-25 05:59:18 +01:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2026-07-22 19:54:55 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Events.Notifications;
|
|
|
|
|
|
|
|
|
|
public class EventCreatedNotificationHandler : INotificationHandler<EventCreatedNotification>
|
|
|
|
|
{
|
|
|
|
|
private readonly IEventBroadcastService _broadcast;
|
2026-10-22 19:43:18 +02:00
|
|
|
private readonly ICameraAccessService _cameraAccess;
|
|
|
|
|
private readonly INotificationService _notifications;
|
2026-10-25 05:59:18 +01:00
|
|
|
private readonly IApplicationDbContext? _context;
|
2026-07-22 19:54:55 +00:00
|
|
|
|
2026-10-22 19:43:18 +02:00
|
|
|
public EventCreatedNotificationHandler(
|
|
|
|
|
IEventBroadcastService broadcast,
|
|
|
|
|
ICameraAccessService cameraAccess,
|
|
|
|
|
INotificationService notifications)
|
2026-10-25 05:59:18 +01:00
|
|
|
: this(broadcast, cameraAccess, notifications, null)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public EventCreatedNotificationHandler(
|
|
|
|
|
IEventBroadcastService broadcast,
|
|
|
|
|
ICameraAccessService cameraAccess,
|
|
|
|
|
INotificationService notifications,
|
|
|
|
|
IApplicationDbContext? context)
|
2026-10-22 19:43:18 +02:00
|
|
|
{
|
|
|
|
|
_broadcast = broadcast;
|
|
|
|
|
_cameraAccess = cameraAccess;
|
|
|
|
|
_notifications = notifications;
|
2026-10-25 05:59:18 +01:00
|
|
|
_context = context;
|
2026-10-22 19:43:18 +02:00
|
|
|
}
|
2026-07-22 19:54:55 +00:00
|
|
|
|
|
|
|
|
public async Task Handle(EventCreatedNotification notification, CancellationToken ct)
|
|
|
|
|
{
|
2026-07-27 18:59:12 +00:00
|
|
|
await _broadcast.NotifyEventCreated(notification.Event, ct).ConfigureAwait(false);
|
2026-10-22 19:43:18 +02:00
|
|
|
|
|
|
|
|
var cameraEvent = notification.Event;
|
2026-10-23 07:20:47 +02:00
|
|
|
if (cameraEvent.Type == EventType.ManualSnapshot) return;
|
2026-10-22 19:43:18 +02:00
|
|
|
var recipients = await _cameraAccess.GetUserIdsWithCameraAccessAsync(cameraEvent.CameraId, ct)
|
|
|
|
|
.ConfigureAwait(false);
|
2026-10-25 05:59:18 +01:00
|
|
|
|
|
|
|
|
var languages = _context is null || recipients.Count == 0
|
|
|
|
|
? new Dictionary<Guid, string>()
|
|
|
|
|
: await _context.Users.AsNoTracking()
|
|
|
|
|
.Where(user => recipients.Contains(user.Id))
|
|
|
|
|
.Select(user => new { user.Id, user.Language })
|
|
|
|
|
.ToDictionaryAsync(user => user.Id, user => user.Language, ct)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var titleKey = cameraEvent.Type switch
|
2026-10-22 19:43:18 +02:00
|
|
|
{
|
2026-10-25 05:59:18 +01:00
|
|
|
EventType.PersonDetected => "Notification.Event.PersonDetected",
|
|
|
|
|
EventType.VehicleDetected => "Notification.Event.VehicleDetected",
|
|
|
|
|
EventType.UnknownPerson => "Notification.Event.UnknownPerson",
|
|
|
|
|
EventType.FaceRecognized => "Notification.Event.FaceRecognized",
|
|
|
|
|
EventType.LicensePlateDetected => "Notification.Event.LicensePlateDetected",
|
|
|
|
|
EventType.CameraOffline => "Notification.Event.CameraOffline",
|
|
|
|
|
EventType.CameraOnline => "Notification.Event.CameraOnline",
|
|
|
|
|
_ => "Notification.Event.MotionDetected"
|
2026-10-22 19:43:18 +02:00
|
|
|
};
|
|
|
|
|
var type = cameraEvent.Type is EventType.UnknownPerson or EventType.CameraOffline
|
|
|
|
|
? NotificationType.Alert
|
|
|
|
|
: cameraEvent.Type == EventType.CameraOnline
|
|
|
|
|
? NotificationType.Info
|
|
|
|
|
: NotificationType.Warning;
|
|
|
|
|
|
|
|
|
|
foreach (var userId in recipients)
|
|
|
|
|
{
|
2026-10-25 05:59:18 +01:00
|
|
|
languages.TryGetValue(userId, out var language);
|
|
|
|
|
var title = ServerText.Get(titleKey, language);
|
|
|
|
|
var message = string.IsNullOrWhiteSpace(cameraEvent.Label)
|
|
|
|
|
? ServerText.Format("Notification.Event.NoLabel", language, title, cameraEvent.CameraId)
|
|
|
|
|
: ServerText.Format("Notification.Event.WithLabel", language, title, cameraEvent.CameraId, cameraEvent.Label);
|
2026-10-22 19:43:18 +02:00
|
|
|
var result = await _notifications.CreateNotificationAsync(
|
|
|
|
|
userId, title, message, type, cameraEvent.Type, cameraEvent.CameraId,
|
|
|
|
|
cameraEvent.SiteId, cameraEvent.SnapshotUrl,
|
|
|
|
|
$"/events?cameraId={cameraEvent.CameraId}&eventId={cameraEvent.Id}",
|
|
|
|
|
sourceEventId: cameraEvent.Id, ct: ct).ConfigureAwait(false);
|
|
|
|
|
if (!result.IsSuccess)
|
|
|
|
|
throw new InvalidOperationException($"Could not persist event notification for user {userId}: {result.Error}");
|
|
|
|
|
}
|
2026-07-22 19:54:55 +00:00
|
|
|
}
|
|
|
|
|
}
|