RemoteNotifications.cs 5.3 KB

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