RemoteNotifications.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(6);
  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. try
  54. {
  55. await DownloadNotifications(dataPath, lastRunTime).ConfigureAwait(false);
  56. }
  57. catch (Exception ex)
  58. {
  59. _logger.ErrorException("Error downloading remote notifications", ex);
  60. }
  61. }
  62. /// <summary>
  63. /// Downloads the notifications.
  64. /// </summary>
  65. /// <param name="dataPath">The data path.</param>
  66. /// <param name="lastRunTime">The last run time.</param>
  67. /// <returns>Task.</returns>
  68. private async Task DownloadNotifications(string dataPath, DateTime lastRunTime)
  69. {
  70. using (var stream = await _httpClient.Get(new HttpRequestOptions
  71. {
  72. Url = Url
  73. }).ConfigureAwait(false))
  74. {
  75. var notifications = _json.DeserializeFromStream<RemoteNotification[]>(stream);
  76. File.WriteAllText(dataPath, string.Empty);
  77. await CreateNotifications(notifications, lastRunTime).ConfigureAwait(false);
  78. }
  79. }
  80. /// <summary>
  81. /// Creates the notifications.
  82. /// </summary>
  83. /// <param name="notifications">The notifications.</param>
  84. /// <param name="lastRunTime">The last run time.</param>
  85. /// <returns>Task.</returns>
  86. private async Task CreateNotifications(IEnumerable<RemoteNotification> notifications, DateTime lastRunTime)
  87. {
  88. // Only show notifications that are active, new since last download, and not older than max age
  89. var notificationList = notifications
  90. .Where(i => string.Equals(i.active, "1") && i.date.ToUniversalTime() > lastRunTime && (DateTime.UtcNow - i.date.ToUniversalTime()) <= _maxAge)
  91. .ToList();
  92. foreach (var user in _userManager.Users.ToList())
  93. {
  94. foreach (var notification in notificationList)
  95. {
  96. await _notificationsRepo.AddNotification(new Notification
  97. {
  98. Category = notification.category,
  99. Date = notification.date,
  100. Name = notification.name,
  101. Description = notification.description,
  102. Url = notification.url,
  103. UserId = user.Id
  104. }, CancellationToken.None).ConfigureAwait(false);
  105. }
  106. }
  107. }
  108. public void Dispose()
  109. {
  110. if (_timer != null)
  111. {
  112. _timer.Dispose();
  113. _timer = null;
  114. }
  115. }
  116. private class RemoteNotification
  117. {
  118. public string id { get; set; }
  119. public DateTime date { get; set; }
  120. public string name { get; set; }
  121. public string description { get; set; }
  122. public string category { get; set; }
  123. public string url { get; set; }
  124. public object imageUrl { get; set; }
  125. public string active { get; set; }
  126. }
  127. }
  128. }