2
0

RemoteNotifications.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Notifications;
  5. using MediaBrowser.Controller.Plugins;
  6. using MediaBrowser.Model.Logging;
  7. using MediaBrowser.Model.Notifications;
  8. using MediaBrowser.Model.Serialization;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
  16. {
  17. public class RemoteNotifications : IServerEntryPoint
  18. {
  19. private const string Url = "http://www.mb3admin.com/admin/service/MB3ServerNotifications.json";
  20. private Timer _timer;
  21. private readonly IHttpClient _httpClient;
  22. private readonly IApplicationPaths _appPaths;
  23. private readonly ILogger _logger;
  24. private readonly IJsonSerializer _json;
  25. private readonly INotificationsRepository _notificationsRepo;
  26. private readonly IUserManager _userManager;
  27. private readonly TimeSpan _frequency = TimeSpan.FromHours(1);
  28. private readonly TimeSpan _maxAge = TimeSpan.FromDays(31);
  29. public RemoteNotifications(IApplicationPaths appPaths, ILogger logger, IHttpClient httpClient, IJsonSerializer json, INotificationsRepository notificationsRepo, IUserManager userManager)
  30. {
  31. _appPaths = appPaths;
  32. _logger = logger;
  33. _httpClient = httpClient;
  34. _json = json;
  35. _notificationsRepo = notificationsRepo;
  36. _userManager = userManager;
  37. }
  38. /// <summary>
  39. /// Runs this instance.
  40. /// </summary>
  41. public void Run()
  42. {
  43. _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(500), _frequency);
  44. }
  45. /// <summary>
  46. /// Called when [timer fired].
  47. /// </summary>
  48. /// <param name="state">The state.</param>
  49. private async void OnTimerFired(object state)
  50. {
  51. var dataPath = Path.Combine(_appPaths.DataPath, "remotenotifications.json");
  52. var lastRunTime = File.Exists(dataPath) ? File.GetLastWriteTimeUtc(dataPath) : DateTime.MinValue;
  53. if ((DateTime.UtcNow - lastRunTime) >= _frequency)
  54. {
  55. try
  56. {
  57. await DownloadNotifications(dataPath, lastRunTime).ConfigureAwait(false);
  58. }
  59. catch (Exception ex)
  60. {
  61. _logger.ErrorException("Error downloading remote notifications", ex);
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Downloads the notifications.
  67. /// </summary>
  68. /// <param name="dataPath">The data path.</param>
  69. /// <param name="lastRunTime">The last run time.</param>
  70. /// <returns>Task.</returns>
  71. private async Task DownloadNotifications(string dataPath, DateTime lastRunTime)
  72. {
  73. using (var stream = await _httpClient.Get(new HttpRequestOptions
  74. {
  75. Url = Url
  76. }).ConfigureAwait(false))
  77. {
  78. var notifications = _json.DeserializeFromStream<RemoteNotification[]>(stream);
  79. File.WriteAllText(dataPath, string.Empty);
  80. await CreateNotifications(notifications, lastRunTime).ConfigureAwait(false);
  81. }
  82. }
  83. /// <summary>
  84. /// Creates the notifications.
  85. /// </summary>
  86. /// <param name="notifications">The notifications.</param>
  87. /// <param name="lastRunTime">The last run time.</param>
  88. /// <returns>Task.</returns>
  89. private async Task CreateNotifications(IEnumerable<RemoteNotification> notifications, DateTime lastRunTime)
  90. {
  91. // Only show notifications that are active, new since last download, and not older than max age
  92. var notificationList = notifications
  93. .Where(i => string.Equals(i.active, "1") && i.date > lastRunTime && (DateTime.Now - i.date) <= _maxAge)
  94. .ToList();
  95. foreach (var user in _userManager.Users.ToList())
  96. {
  97. foreach (var notification in notificationList)
  98. {
  99. await _notificationsRepo.AddNotification(new Notification
  100. {
  101. Category = notification.category,
  102. Date = notification.date,
  103. Name = notification.name,
  104. Description = notification.description,
  105. Url = notification.url,
  106. UserId = user.Id
  107. }, CancellationToken.None).ConfigureAwait(false);
  108. }
  109. }
  110. }
  111. public void Dispose()
  112. {
  113. if (_timer != null)
  114. {
  115. _timer.Dispose();
  116. _timer = null;
  117. }
  118. }
  119. private class RemoteNotification
  120. {
  121. public string id { get; set; }
  122. public DateTime date { get; set; }
  123. public string name { get; set; }
  124. public string description { get; set; }
  125. public string category { get; set; }
  126. public string url { get; set; }
  127. public object imageUrl { get; set; }
  128. public string active { get; set; }
  129. }
  130. }
  131. }