2
0

WebSocketNotifier.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using MediaBrowser.Common.Net;
  2. using MediaBrowser.Controller.Notifications;
  3. using MediaBrowser.Controller.Plugins;
  4. using System.Linq;
  5. namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
  6. {
  7. /// <summary>
  8. /// Notifies clients anytime a notification is added or udpated
  9. /// </summary>
  10. public class WebSocketNotifier : IServerEntryPoint
  11. {
  12. private readonly INotificationsRepository _notificationsRepo;
  13. private readonly IServerManager _serverManager;
  14. public WebSocketNotifier(INotificationsRepository notificationsRepo, IServerManager serverManager)
  15. {
  16. _notificationsRepo = notificationsRepo;
  17. _serverManager = serverManager;
  18. }
  19. public void Run()
  20. {
  21. _notificationsRepo.NotificationAdded += _notificationsRepo_NotificationAdded;
  22. _notificationsRepo.NotificationUpdated += _notificationsRepo_NotificationUpdated;
  23. _notificationsRepo.NotificationsMarkedRead += _notificationsRepo_NotificationsMarkedRead;
  24. }
  25. void _notificationsRepo_NotificationsMarkedRead(object sender, NotificationReadEventArgs e)
  26. {
  27. var list = e.IdList.Select(i => i.ToString("N")).ToList();
  28. list.Add(e.UserId.ToString("N"));
  29. list.Add(e.IsRead.ToString().ToLower());
  30. var msg = string.Join("|", list.ToArray());
  31. _serverManager.SendWebSocketMessage("NotificationsMarkedRead", msg);
  32. }
  33. void _notificationsRepo_NotificationUpdated(object sender, NotificationUpdateEventArgs e)
  34. {
  35. var msg = e.Notification.UserId + "|" + e.Notification.Id;
  36. _serverManager.SendWebSocketMessage("NotificationUpdated", msg);
  37. }
  38. void _notificationsRepo_NotificationAdded(object sender, NotificationUpdateEventArgs e)
  39. {
  40. var msg = e.Notification.UserId + "|" + e.Notification.Id;
  41. _serverManager.SendWebSocketMessage("NotificationAdded", msg);
  42. }
  43. public void Dispose()
  44. {
  45. _notificationsRepo.NotificationAdded -= _notificationsRepo_NotificationAdded;
  46. _notificationsRepo.NotificationUpdated -= _notificationsRepo_NotificationUpdated;
  47. }
  48. }
  49. }