| | | 1 | | using System.Text.Json; |
| | | 2 | | using Microsoft.AspNetCore.SignalR; |
| | | 3 | | |
| | | 4 | | namespace Services.SignalRServices; |
| | | 5 | | |
| | | 6 | | public static class NotificationService |
| | | 7 | | { |
| | | 8 | | private static IHubContext<NotificationHub>? _hubContext; |
| | | 9 | | |
| | | 10 | | public static void Initialize(IHubContext<NotificationHub> hubContext) |
| | | 11 | | { |
| | 2 | 12 | | _hubContext = hubContext; |
| | 2 | 13 | | } |
| | | 14 | | |
| | | 15 | | public static async Task NotifyAllAsync(string message) |
| | | 16 | | { |
| | 0 | 17 | | if (_hubContext == null) |
| | 0 | 18 | | throw new InvalidOperationException("NotificationService не инициализирован."); |
| | | 19 | | |
| | 0 | 20 | | await _hubContext.Clients.All.SendAsync("PopupNotification", message); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static async Task NotifyAsync(string source, string color, string text, string icon = "fa-circle-info") |
| | | 24 | | { |
| | 14 | 25 | | if (_hubContext == null) |
| | 9 | 26 | | throw new InvalidOperationException("NotificationService не инициализирован."); |
| | | 27 | | |
| | 5 | 28 | | var payload = JsonSerializer.Serialize(new { source, color, icon, text }); |
| | 5 | 29 | | await _hubContext.Clients.All.SendAsync("PopupNotification", payload); |
| | 1 | 30 | | } |
| | | 31 | | |
| | | 32 | | public static async Task NotifyUsersAsync(IEnumerable<string> userIds, string source, string color, string text, str |
| | | 33 | | { |
| | 305 | 34 | | if (_hubContext == null) |
| | 305 | 35 | | throw new InvalidOperationException("NotificationService не инициализирован."); |
| | | 36 | | |
| | 0 | 37 | | var recipients = userIds.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToArray(); |
| | 0 | 38 | | if (recipients.Length == 0) |
| | 0 | 39 | | return; |
| | 0 | 40 | | var payload = JsonSerializer.Serialize(new { source, color, icon, text }); |
| | 0 | 41 | | await _hubContext.Clients.Users(recipients).SendAsync("PopupNotification", payload); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Отправляет системное уведомление всем активным пользователям через SignalR |
| | | 46 | | /// Используется для важных системных сообщений, которые должны отображаться в модальном окне |
| | | 47 | | /// </summary> |
| | | 48 | | public static async Task NotifySystemAsync(string message) |
| | | 49 | | { |
| | 0 | 50 | | if (_hubContext == null) |
| | 0 | 51 | | throw new InvalidOperationException("NotificationService не инициализирован."); |
| | | 52 | | |
| | 0 | 53 | | await _hubContext.Clients.All.SendAsync("SystemNotification", message); |
| | 0 | 54 | | } |
| | | 55 | | } |