Files
Heimdall/src/Heimdall.Application/Events/Notifications/EventCreatedNotificationHandler.cs
T

87 lines
3.9 KiB
C#
Raw Normal View History

using Heimdall.Application.Common;
using Heimdall.Application.Localization;
using Heimdall.Application.Services;
using Heimdall.Domain.Enums;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Heimdall.Application.Events.Notifications;
public class EventCreatedNotificationHandler : INotificationHandler<EventCreatedNotification>
{
private readonly IEventBroadcastService _broadcast;
private readonly ICameraAccessService _cameraAccess;
private readonly INotificationService _notifications;
private readonly IApplicationDbContext? _context;
public EventCreatedNotificationHandler(
IEventBroadcastService broadcast,
ICameraAccessService cameraAccess,
INotificationService notifications)
: this(broadcast, cameraAccess, notifications, null)
{
}
public EventCreatedNotificationHandler(
IEventBroadcastService broadcast,
ICameraAccessService cameraAccess,
INotificationService notifications,
IApplicationDbContext? context)
{
_broadcast = broadcast;
_cameraAccess = cameraAccess;
_notifications = notifications;
_context = context;
}
public async Task Handle(EventCreatedNotification notification, CancellationToken ct)
{
await _broadcast.NotifyEventCreated(notification.Event, ct).ConfigureAwait(false);
var cameraEvent = notification.Event;
if (cameraEvent.Type == EventType.ManualSnapshot) return;
var recipients = await _cameraAccess.GetUserIdsWithCameraAccessAsync(cameraEvent.CameraId, ct)
.ConfigureAwait(false);
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
{
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"
};
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)
{
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);
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}");
}
}
}