NotificationOptions.cs 4.5 KB

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