2
0

NotificationOptions.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Linq;
  3. namespace MediaBrowser.Model.Configuration
  4. {
  5. public class NotificationOptions
  6. {
  7. public NotificationOption[] Options { get; set; }
  8. public NotificationOptions()
  9. {
  10. Options = new[]
  11. {
  12. new NotificationOption
  13. {
  14. Type = NotificationType.TaskFailed.ToString(),
  15. Enabled = true,
  16. SendToUserMode = SendToUserType.Admins
  17. },
  18. new NotificationOption
  19. {
  20. Type = NotificationType.ServerRestartRequired.ToString(),
  21. Enabled = true,
  22. SendToUserMode = SendToUserType.Admins
  23. },
  24. new NotificationOption
  25. {
  26. Type = NotificationType.ApplicationUpdateAvailable.ToString(),
  27. Enabled = true,
  28. SendToUserMode = SendToUserType.Admins
  29. },
  30. new NotificationOption
  31. {
  32. Type = NotificationType.ApplicationUpdateInstalled.ToString(),
  33. Enabled = true,
  34. SendToUserMode = SendToUserType.Admins
  35. },
  36. new NotificationOption
  37. {
  38. Type = NotificationType.PluginUpdateInstalled.ToString(),
  39. Enabled = true,
  40. SendToUserMode = SendToUserType.Admins
  41. },
  42. new NotificationOption
  43. {
  44. Type = NotificationType.PluginUninstalled.ToString(),
  45. Enabled = true,
  46. SendToUserMode = SendToUserType.Admins
  47. },
  48. new NotificationOption
  49. {
  50. Type = NotificationType.InstallationFailed.ToString(),
  51. Enabled = true,
  52. SendToUserMode = SendToUserType.Admins
  53. },
  54. new NotificationOption
  55. {
  56. Type = NotificationType.PluginInstalled.ToString(),
  57. Enabled = true,
  58. SendToUserMode = SendToUserType.Admins
  59. }
  60. };
  61. }
  62. public NotificationOption GetOptions(string type)
  63. {
  64. return Options.FirstOrDefault(i => string.Equals(type, i.Type, StringComparison.OrdinalIgnoreCase));
  65. }
  66. public bool IsEnabled(string type)
  67. {
  68. var opt = GetOptions(type);
  69. return opt != null && opt.Enabled;
  70. }
  71. public bool IsServiceEnabled(string service, string notificationType)
  72. {
  73. var opt = GetOptions(notificationType);
  74. return opt == null ||
  75. !opt.DisabledServices.Contains(service, StringComparer.OrdinalIgnoreCase);
  76. }
  77. public bool IsEnabledToMonitorUser(string type, string userId)
  78. {
  79. var opt = GetOptions(type);
  80. return opt != null && opt.Enabled &&
  81. !opt.DisabledMonitorUsers.Contains(userId, StringComparer.OrdinalIgnoreCase);
  82. }
  83. public bool IsEnabledToSendToUser(string type, string userId, UserConfiguration userConfig)
  84. {
  85. var opt = GetOptions(type);
  86. if (opt != null && opt.Enabled)
  87. {
  88. if (opt.SendToUserMode == SendToUserType.All)
  89. {
  90. return true;
  91. }
  92. if (opt.SendToUserMode == SendToUserType.Admins && userConfig.IsAdministrator)
  93. {
  94. return true;
  95. }
  96. return opt.SendToUsers.Contains(userId, StringComparer.OrdinalIgnoreCase);
  97. }
  98. return false;
  99. }
  100. }
  101. public class NotificationOption
  102. {
  103. public string Type { get; set; }
  104. /// <summary>
  105. /// User Ids to not monitor (it's opt out)
  106. /// </summary>
  107. public string[] DisabledMonitorUsers { get; set; }
  108. /// <summary>
  109. /// User Ids to send to (if SendToUserMode == Custom)
  110. /// </summary>
  111. public string[] SendToUsers { get; set; }
  112. /// <summary>
  113. /// Gets or sets a value indicating whether this <see cref="NotificationOption"/> is enabled.
  114. /// </summary>
  115. /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
  116. public bool Enabled { get; set; }
  117. /// <summary>
  118. /// Gets or sets the title format string.
  119. /// </summary>
  120. /// <value>The title format string.</value>
  121. public string Title { get; set; }
  122. /// <summary>
  123. /// Gets or sets the disabled services.
  124. /// </summary>
  125. /// <value>The disabled services.</value>
  126. public string[] DisabledServices { get; set; }
  127. /// <summary>
  128. /// Gets or sets the send to user mode.
  129. /// </summary>
  130. /// <value>The send to user mode.</value>
  131. public SendToUserType SendToUserMode { get; set; }
  132. public NotificationOption()
  133. {
  134. DisabledServices = new string[] { };
  135. DisabledMonitorUsers = new string[] { };
  136. SendToUsers = new string[] { };
  137. }
  138. }
  139. public enum NotificationType
  140. {
  141. ApplicationUpdateAvailable,
  142. ApplicationUpdateInstalled,
  143. AudioPlayback,
  144. GamePlayback,
  145. InstallationFailed,
  146. PluginInstalled,
  147. PluginUpdateInstalled,
  148. PluginUninstalled,
  149. NewLibraryContent,
  150. ServerRestartRequired,
  151. TaskFailed,
  152. VideoPlayback
  153. }
  154. public enum SendToUserType
  155. {
  156. All = 0,
  157. Admins = 1,
  158. Custom = 2
  159. }
  160. }