NotificationOptions.cs 4.6 KB

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