NotificationOptions.cs 4.2 KB

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