jellyfin/MediaBrowser.Server.Implementations/EntryPoints/Notifications/RemoteNotifications.cs

149 lines
5.3 KiB
C#
Raw Normal View History

2013-07-16 19:42:12 +00:00
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
2013-07-16 19:42:12 +00:00
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Notifications;
using MediaBrowser.Model.Serialization;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
2015-10-04 04:23:11 +00:00
using CommonIO;
2013-07-16 19:42:12 +00:00
namespace MediaBrowser.Server.Implementations.EntryPoints.Notifications
{
public class RemoteNotifications : IServerEntryPoint
{
2014-12-16 05:46:54 +00:00
private const string Url = "http://www.mb3admin.com/admin/service/MB3ServerNotifications.json";
2013-07-16 19:42:12 +00:00
private Timer _timer;
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _appPaths;
private readonly ILogger _logger;
private readonly IJsonSerializer _json;
private readonly IUserManager _userManager;
private readonly IFileSystem _fileSystem;
2013-07-16 19:42:12 +00:00
private readonly TimeSpan _frequency = TimeSpan.FromHours(6);
2013-07-16 19:42:12 +00:00
private readonly TimeSpan _maxAge = TimeSpan.FromDays(31);
2014-04-25 20:15:50 +00:00
private readonly INotificationManager _notificationManager;
public RemoteNotifications(IApplicationPaths appPaths, ILogger logger, IHttpClient httpClient, IJsonSerializer json, IUserManager userManager, IFileSystem fileSystem, INotificationManager notificationManager)
2013-07-16 19:42:12 +00:00
{
_appPaths = appPaths;
_logger = logger;
_httpClient = httpClient;
_json = json;
_userManager = userManager;
_fileSystem = fileSystem;
2014-04-25 20:15:50 +00:00
_notificationManager = notificationManager;
2013-07-16 19:42:12 +00:00
}
/// <summary>
/// Runs this instance.
/// </summary>
public void Run()
{
_timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(500), _frequency);
}
/// <summary>
/// Called when [timer fired].
/// </summary>
/// <param name="state">The state.</param>
private async void OnTimerFired(object state)
{
var dataPath = Path.Combine(_appPaths.DataPath, "remotenotifications.json");
2015-09-13 21:32:02 +00:00
var lastRunTime = _fileSystem.FileExists(dataPath) ? _fileSystem.GetLastWriteTimeUtc(dataPath) : DateTime.MinValue;
2013-07-16 19:42:12 +00:00
2013-07-16 21:06:20 +00:00
try
2013-07-16 19:42:12 +00:00
{
2013-07-16 21:06:20 +00:00
await DownloadNotifications(dataPath, lastRunTime).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.ErrorException("Error downloading remote notifications", ex);
2013-07-16 19:42:12 +00:00
}
}
/// <summary>
/// Downloads the notifications.
/// </summary>
/// <param name="dataPath">The data path.</param>
/// <param name="lastRunTime">The last run time.</param>
/// <returns>Task.</returns>
private async Task DownloadNotifications(string dataPath, DateTime lastRunTime)
{
using (var stream = await _httpClient.Get(new HttpRequestOptions
{
Url = Url
}).ConfigureAwait(false))
{
var notifications = _json.DeserializeFromStream<RemoteNotification[]>(stream);
2015-09-13 21:32:02 +00:00
_fileSystem.WriteAllText(dataPath, string.Empty);
2013-07-16 19:42:12 +00:00
await CreateNotifications(notifications, lastRunTime).ConfigureAwait(false);
}
}
/// <summary>
/// Creates the notifications.
/// </summary>
/// <param name="notifications">The notifications.</param>
/// <param name="lastRunTime">The last run time.</param>
/// <returns>Task.</returns>
private async Task CreateNotifications(IEnumerable<RemoteNotification> notifications, DateTime lastRunTime)
{
// Only show notifications that are active, new since last download, and not older than max age
var notificationList = notifications
2013-07-19 18:37:14 +00:00
.Where(i => string.Equals(i.active, "1") && i.date.ToUniversalTime() > lastRunTime && (DateTime.UtcNow - i.date.ToUniversalTime()) <= _maxAge)
2013-07-16 19:42:12 +00:00
.ToList();
2014-04-25 20:15:50 +00:00
var userIds = _userManager.Users.Select(i => i.Id.ToString("N")).ToList();
foreach (var notification in notificationList)
2013-07-16 19:42:12 +00:00
{
2014-04-25 20:15:50 +00:00
await _notificationManager.SendNotification(new NotificationRequest
2013-07-16 19:42:12 +00:00
{
2014-04-25 20:15:50 +00:00
Date = notification.date,
Name = notification.name,
Description = notification.description,
Url = notification.url,
UserIds = userIds
}, CancellationToken.None).ConfigureAwait(false);
2013-07-16 19:42:12 +00:00
}
}
public void Dispose()
{
if (_timer != null)
{
_timer.Dispose();
_timer = null;
}
}
private class RemoteNotification
{
public string id { get; set; }
public DateTime date { get; set; }
public string name { get; set; }
public string description { get; set; }
public string category { get; set; }
public string url { get; set; }
public object imageUrl { get; set; }
public string active { get; set; }
}
}
}