2
0

NotificationOptions.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. new NotificationOption
  61. {
  62. Type = NotificationType.PluginError.ToString(),
  63. Enabled = true,
  64. SendToUserMode = SendToUserType.Admins
  65. }
  66. };
  67. }
  68. public NotificationOption GetOptions(string type)
  69. {
  70. return Options.FirstOrDefault(i => string.Equals(type, i.Type, StringComparison.OrdinalIgnoreCase));
  71. }
  72. public bool IsEnabled(string type)
  73. {
  74. var opt = GetOptions(type);
  75. return opt != null && opt.Enabled;
  76. }
  77. public bool IsServiceEnabled(string service, string notificationType)
  78. {
  79. var opt = GetOptions(notificationType);
  80. return opt == null ||
  81. !opt.DisabledServices.Contains(service, StringComparer.OrdinalIgnoreCase);
  82. }
  83. public bool IsEnabledToMonitorUser(string type, string userId)
  84. {
  85. var opt = GetOptions(type);
  86. return opt != null && opt.Enabled &&
  87. !opt.DisabledMonitorUsers.Contains(userId, StringComparer.OrdinalIgnoreCase);
  88. }
  89. public bool IsEnabledToSendToUser(string type, string userId, UserConfiguration userConfig)
  90. {
  91. var opt = GetOptions(type);
  92. if (opt != null && opt.Enabled)
  93. {
  94. if (opt.SendToUserMode == SendToUserType.All)
  95. {
  96. return true;
  97. }
  98. if (opt.SendToUserMode == SendToUserType.Admins && userConfig.IsAdministrator)
  99. {
  100. return true;
  101. }
  102. return opt.SendToUsers.Contains(userId, StringComparer.OrdinalIgnoreCase);
  103. }
  104. return false;
  105. }
  106. }
  107. public class NotificationOption
  108. {
  109. public string Type { get; set; }
  110. /// <summary>
  111. /// User Ids to not monitor (it's opt out)
  112. /// </summary>
  113. public string[] DisabledMonitorUsers { get; set; }
  114. /// <summary>
  115. /// User Ids to send to (if SendToUserMode == Custom)
  116. /// </summary>
  117. public string[] SendToUsers { get; set; }
  118. /// <summary>
  119. /// Gets or sets a value indicating whether this <see cref="NotificationOption"/> is enabled.
  120. /// </summary>
  121. /// <value><c>true</c> if enabled; otherwise, <c>false</c>.</value>
  122. public bool Enabled { get; set; }
  123. /// <summary>
  124. /// Gets or sets the title format string.
  125. /// </summary>
  126. /// <value>The title format string.</value>
  127. public string Title { get; set; }
  128. /// <summary>
  129. /// Gets or sets the description.
  130. /// </summary>
  131. /// <value>The description.</value>
  132. public string Description { get; set; }
  133. /// <summary>
  134. /// Gets or sets the disabled services.
  135. /// </summary>
  136. /// <value>The disabled services.</value>
  137. public string[] DisabledServices { get; set; }
  138. /// <summary>
  139. /// Gets or sets the send to user mode.
  140. /// </summary>
  141. /// <value>The send to user mode.</value>
  142. public SendToUserType SendToUserMode { get; set; }
  143. public NotificationOption()
  144. {
  145. DisabledServices = new string[] { };
  146. DisabledMonitorUsers = new string[] { };
  147. SendToUsers = new string[] { };
  148. }
  149. }
  150. public enum NotificationType
  151. {
  152. ApplicationUpdateAvailable,
  153. ApplicationUpdateInstalled,
  154. AudioPlayback,
  155. GamePlayback,
  156. InstallationFailed,
  157. PluginError,
  158. PluginInstalled,
  159. PluginUpdateInstalled,
  160. PluginUninstalled,
  161. NewLibraryContent,
  162. ServerRestartRequired,
  163. TaskFailed,
  164. VideoPlayback
  165. }
  166. public enum SendToUserType
  167. {
  168. All = 0,
  169. Admins = 1,
  170. Custom = 2
  171. }
  172. }