From 48facb797ed912e4ea6b04b17d1ff190ac2daac4 Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 12 Sep 2018 19:26:21 +0200 Subject: Update to 3.5.2 and .net core 2.1 --- .../Session/FirebaseSessionController.cs | 131 +++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 Emby.Server.Implementations/Session/FirebaseSessionController.cs (limited to 'Emby.Server.Implementations/Session/FirebaseSessionController.cs') diff --git a/Emby.Server.Implementations/Session/FirebaseSessionController.cs b/Emby.Server.Implementations/Session/FirebaseSessionController.cs new file mode 100644 index 000000000..cfe513305 --- /dev/null +++ b/Emby.Server.Implementations/Session/FirebaseSessionController.cs @@ -0,0 +1,131 @@ +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Net; +using MediaBrowser.Common.Net; +using MediaBrowser.Model.Serialization; +using System; +using System.Threading; +using System.Threading.Tasks; +using System.Text; +using MediaBrowser.Common; + +namespace Emby.Server.Implementations.Session +{ + public class FirebaseSessionController : ISessionController + { + private readonly IHttpClient _httpClient; + private readonly IJsonSerializer _json; + private readonly ISessionManager _sessionManager; + + public SessionInfo Session { get; private set; } + + private readonly string _token; + + private IApplicationHost _appHost; + private string _senderId; + private string _applicationId; + + public FirebaseSessionController(IHttpClient httpClient, + IApplicationHost appHost, + IJsonSerializer json, + SessionInfo session, + string token, ISessionManager sessionManager) + { + _httpClient = httpClient; + _json = json; + _appHost = appHost; + Session = session; + _token = token; + _sessionManager = sessionManager; + + _applicationId = _appHost.GetValue("firebase_applicationid"); + _senderId = _appHost.GetValue("firebase_senderid"); + } + + public static bool IsSupported(IApplicationHost appHost) + { + return !string.IsNullOrEmpty(appHost.GetValue("firebase_applicationid")) && !string.IsNullOrEmpty(appHost.GetValue("firebase_senderid")); + } + + public bool IsSessionActive + { + get + { + return (DateTime.UtcNow - Session.LastActivityDate).TotalDays <= 3; + } + } + + public bool SupportsMediaControl + { + get { return true; } + } + + public async Task SendMessage(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken) + { + if (!IsSessionActive) + { + return; + } + + if (string.IsNullOrEmpty(_senderId) || string.IsNullOrEmpty(_applicationId)) + { + return; + } + + foreach (var controller in allControllers) + { + // Don't send if there's an active web socket connection + if ((controller is WebSocketController) && controller.IsSessionActive) + { + return; + } + } + + var msg = new WebSocketMessage + { + Data = data, + MessageType = name, + MessageId = messageId, + ServerId = _appHost.SystemId + }; + + var req = new FirebaseBody + { + to = _token, + data = msg + }; + + var byteArray = Encoding.UTF8.GetBytes(_json.SerializeToString(req)); + + var enableLogging = false; + +#if DEBUG + enableLogging = true; +#endif + + var options = new HttpRequestOptions + { + Url = "https://fcm.googleapis.com/fcm/send", + RequestContentType = "application/json", + RequestContentBytes = byteArray, + CancellationToken = cancellationToken, + LogRequest = enableLogging, + LogResponse = enableLogging, + LogErrors = enableLogging + }; + + options.RequestHeaders["Authorization"] = string.Format("key={0}", _applicationId); + options.RequestHeaders["Sender"] = string.Format("id={0}", _senderId); + + using (var response = await _httpClient.Post(options).ConfigureAwait(false)) + { + + } + } + } + + internal class FirebaseBody + { + public string to { get; set; } + public WebSocketMessage data { get; set; } + } +} -- cgit v1.2.3