feature: online services push notifications

This commit is contained in:
Gary Sharp
2025-01-01 19:23:18 +11:00
parent 4006bdbcc6
commit 296f7a13fd
19 changed files with 667 additions and 271 deletions
@@ -19,7 +19,7 @@ namespace Disco.Services.Interop.DiscoServices
public class ActivationService
{
private static readonly byte[] onlineServicesActivationKey;
internal const string baseUrl = "https://activate.discoict.com.au";
internal static readonly Uri BaseUrl = new Uri("https://activate.discoict.com.au");
private readonly DiscoDataContext database;
static ActivationService()
@@ -40,8 +40,8 @@ namespace Disco.Services.Interop.DiscoServices
public string GetDataStoreLocation => Path.Combine(database.DiscoConfiguration.DataStoreLocation, "Activations");
public bool RequiresCleanup => Directory.Exists(GetDataStoreLocation);
public string GetCallbackUrl()
=> $"{baseUrl}/api/callback";
public Uri GetCallbackUrl()
=> new Uri(BaseUrl, "/api/callback");
/// <summary>
/// Begin the activation process
@@ -64,7 +64,7 @@ namespace Disco.Services.Interop.DiscoServices
ChallengeResponse challenge;
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(baseUrl);
httpClient.BaseAddress = BaseUrl;
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var body = new ChallengeRequest()
@@ -124,7 +124,7 @@ namespace Disco.Services.Interop.DiscoServices
TimeStamp = challenge.TimeStamp,
ChallengeResponse = challengeResponse,
ChallengeResponseIv = challengeResponseIv,
RedirectUrl = $"{baseUrl}/",
RedirectUrl = new Uri(BaseUrl, "/").ToString(),
};
// store activation
@@ -174,7 +174,7 @@ namespace Disco.Services.Interop.DiscoServices
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(baseUrl);
httpClient.BaseAddress = BaseUrl;
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var body = new CompleteRequest()
@@ -132,6 +132,8 @@ namespace Disco.Services.Interop.DiscoServices
key = config.ActivationKey;
token = null;
tokenExpires = null;
ThreadPool.QueueUserWorkItem(async _ => await OnlineServicesConnect.StartAsync());
}
else
{
@@ -0,0 +1,92 @@
using Disco.Models.Services.Interop.DiscoServices;
using Disco.Services.Logging;
using Microsoft.AspNetCore.SignalR.Client;
using System;
using System.Threading.Tasks;
namespace Disco.Services.Interop.DiscoServices
{
public static class OnlineServicesConnect
{
private static readonly HubConnection connection;
public static string State => connection.State.ToString();
static OnlineServicesConnect()
{
connection = new HubConnectionBuilder()
.WithUrl(new Uri(ActivationService.BaseUrl, "/connect"), options =>
{
options.AccessTokenProvider = () => OnlineServicesAuthentication.GetTokenAsync();
})
.WithAutomaticReconnect()
.WithStatefulReconnect()
.Build();
}
public static async Task StartAsync()
{
try
{
await connection.StartAsync();
}
catch (Exception ex)
{
SystemLog.LogException("Online Services", ex);
}
}
public static async Task StopAsync()
{
await connection.StopAsync();
}
public static IDisposable SubscribeToNotifications(Action<IConnectNotification> handler, params int[] notificationTypes)
{
return new NotificationSubscription(handler, notificationTypes);
}
private class NotificationSubscription : IDisposable
{
private readonly IDisposable subscription;
private readonly Action<IConnectNotification> handler;
private readonly int[] notificationTypes;
public NotificationSubscription(Action<IConnectNotification> handler, params int[] notificationTypes)
{
if (notificationTypes == null || notificationTypes.Length == 0)
{
handler = null;
subscription = null;
notificationTypes = null;
}
else
{
this.handler = handler;
this.notificationTypes = notificationTypes;
subscription = connection.On<ConnectNotification>("Notify", Handler);
}
}
public void Handler(ConnectNotification notification)
{
if (Array.IndexOf(notificationTypes, notification.Type) >= 0)
handler(notification);
}
public void Dispose()
{
subscription?.Dispose();
}
}
private class ConnectNotification : IConnectNotification
{
public int Version { get; set; }
public int Type { get; set; }
public string Content { get; set; }
}
}
}