2
0

NotificationOptions.cs 4.6 KB

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