NotificationOptions.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Linq;
  5. using Jellyfin.Data.Entities;
  6. using Jellyfin.Data.Enums;
  7. using MediaBrowser.Model.Extensions;
  8. using MediaBrowser.Model.Users;
  9. namespace MediaBrowser.Model.Notifications
  10. {
  11. public class NotificationOptions
  12. {
  13. public NotificationOptions()
  14. {
  15. Options = new[]
  16. {
  17. new NotificationOption(NotificationType.TaskFailed.ToString())
  18. {
  19. Enabled = true,
  20. SendToUserMode = SendToUserType.Admins
  21. },
  22. new NotificationOption(NotificationType.ServerRestartRequired.ToString())
  23. {
  24. Enabled = true,
  25. SendToUserMode = SendToUserType.Admins
  26. },
  27. new NotificationOption(NotificationType.ApplicationUpdateAvailable.ToString())
  28. {
  29. Enabled = true,
  30. SendToUserMode = SendToUserType.Admins
  31. },
  32. new NotificationOption(NotificationType.ApplicationUpdateInstalled.ToString())
  33. {
  34. Enabled = true,
  35. SendToUserMode = SendToUserType.Admins
  36. },
  37. new NotificationOption(NotificationType.PluginUpdateInstalled.ToString())
  38. {
  39. Enabled = true,
  40. SendToUserMode = SendToUserType.Admins
  41. },
  42. new NotificationOption(NotificationType.PluginUninstalled.ToString())
  43. {
  44. Enabled = true,
  45. SendToUserMode = SendToUserType.Admins
  46. },
  47. new NotificationOption(NotificationType.InstallationFailed.ToString())
  48. {
  49. Enabled = true,
  50. SendToUserMode = SendToUserType.Admins
  51. },
  52. new NotificationOption(NotificationType.PluginInstalled.ToString())
  53. {
  54. Enabled = true,
  55. SendToUserMode = SendToUserType.Admins
  56. },
  57. new NotificationOption(NotificationType.PluginError.ToString())
  58. {
  59. Enabled = true,
  60. SendToUserMode = SendToUserType.Admins
  61. },
  62. new NotificationOption(NotificationType.UserLockedOut.ToString())
  63. {
  64. Enabled = true,
  65. SendToUserMode = SendToUserType.Admins
  66. }
  67. };
  68. }
  69. public NotificationOption[] Options { get; set; }
  70. public NotificationOption GetOptions(string type)
  71. {
  72. foreach (NotificationOption i in Options)
  73. {
  74. if (string.Equals(type, i.Type, StringComparison.OrdinalIgnoreCase))
  75. {
  76. return i;
  77. }
  78. }
  79. return null;
  80. }
  81. public bool IsEnabled(string type)
  82. {
  83. NotificationOption opt = GetOptions(type);
  84. return opt != null && opt.Enabled;
  85. }
  86. public bool IsServiceEnabled(string service, string notificationType)
  87. {
  88. NotificationOption opt = GetOptions(notificationType);
  89. return opt == null ||
  90. !opt.DisabledServices.Contains(service, StringComparer.OrdinalIgnoreCase);
  91. }
  92. public bool IsEnabledToMonitorUser(string type, Guid userId)
  93. {
  94. NotificationOption opt = GetOptions(type);
  95. return opt != null && opt.Enabled &&
  96. !opt.DisabledMonitorUsers.Contains(userId.ToString(string.Empty), StringComparer.OrdinalIgnoreCase);
  97. }
  98. public bool IsEnabledToSendToUser(string type, string userId, User user)
  99. {
  100. NotificationOption opt = GetOptions(type);
  101. if (opt != null && opt.Enabled)
  102. {
  103. if (opt.SendToUserMode == SendToUserType.All)
  104. {
  105. return true;
  106. }
  107. if (opt.SendToUserMode == SendToUserType.Admins && user.HasPermission(PermissionKind.IsAdministrator))
  108. {
  109. return true;
  110. }
  111. return opt.SendToUsers.Contains(userId, StringComparer.OrdinalIgnoreCase);
  112. }
  113. return false;
  114. }
  115. }
  116. }