From 3e2015b7c6cb71937c4a7aa06f857d7f05c0b9e5 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sun, 3 Mar 2024 13:23:22 +0100 Subject: [PATCH] Add `RequestUnreadCount` and `ClearUnread` methods to client --- .../Program.cs | 38 +++++++++---- .../Client/Models/CountReceivedEventArgs.cs | 6 ++ .../Client/NotificationApiClient.cs | 56 ++++++++++++++++--- .../Client/Payloads/CountPayload.cs | 6 ++ ...ivedPayload.cs => NotificationsPayload.cs} | 2 +- .../Utilities/UserIdHasher.cs | 2 +- 6 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 src/StoneRed.NetificationApi/Client/Models/CountReceivedEventArgs.cs create mode 100644 src/StoneRed.NetificationApi/Client/Payloads/CountPayload.cs rename src/StoneRed.NetificationApi/Client/Payloads/{NotificationsReceivedPayload.cs => NotificationsPayload.cs} (80%) diff --git a/src/StoneRed.NetificationApi.Example/Program.cs b/src/StoneRed.NetificationApi.Example/Program.cs index a1c4be4..20a6c06 100644 --- a/src/StoneRed.NetificationApi.Example/Program.cs +++ b/src/StoneRed.NetificationApi.Example/Program.cs @@ -4,18 +4,26 @@ using StoneRed.NetificationApi.Server; using StoneRed.NetificationApi.Server.IdentifyUser; using StoneRed.NetificationApi.Server.Send; -const string userId = ""; -const string notificationId = ""; -const string clientId = ""; -const string clientSecret = ""; +// The ID of the user to send the notification to +string userId = "test"; + +// The ID of the notification to send +string notificationId = "test"; + +// The client ID and secret to use for the API +string clientId = Environment.GetEnvironmentVariable("NOTIFICATION_API_CLIENT_ID", EnvironmentVariableTarget.User) ?? ""; +string clientSecret = Environment.GetEnvironmentVariable("NOTIFICATION_API_SECRET", EnvironmentVariableTarget.User) ?? ""; + +// The client is used to receive notifications +NotificationApiClient notificationApiClient = new NotificationApiClient(userId, clientId); + +// The server is used to send notifications +NotificationApiServer notificationApiServer = new NotificationApiServer(clientId, clientSecret, false); Console.WriteLine("Start client"); -NotificationApiClient notificationApiClient = new NotificationApiClient("test", clientId, clientSecret, false); await notificationApiClient.Start(); Console.WriteLine("Client started"); -notificationApiClient.RequestNotifications(); - notificationApiClient.RequestedNotificationsReceived += (sender, args) => { Console.WriteLine("Requested notifications received"); @@ -34,13 +42,23 @@ notificationApiClient.NewNotificationsReceived += (sender, args) => } }; -NotificationApiServer notificationApiServer = new NotificationApiServer(clientId, clientSecret, false); +notificationApiClient.UnreadCountReceived += (sender, args) => +{ + Console.WriteLine($"Unread count received: {args.Count}"); +}; + +Console.WriteLine("Request unread count"); +notificationApiClient.RequestUnreadCount(); +Console.WriteLine("Unread count requested"); + +Console.WriteLine("Request notifications"); +notificationApiClient.RequestNotifications(5); +Console.WriteLine("Notifications requested"); Console.WriteLine("Identify user"); await notificationApiServer.Identify(new IdentifyUserData { - UserId = userId, - Email = "kek123@stone-red.net" + UserId = userId }); Console.WriteLine("User identified"); diff --git a/src/StoneRed.NetificationApi/Client/Models/CountReceivedEventArgs.cs b/src/StoneRed.NetificationApi/Client/Models/CountReceivedEventArgs.cs new file mode 100644 index 0000000..a1c21b4 --- /dev/null +++ b/src/StoneRed.NetificationApi/Client/Models/CountReceivedEventArgs.cs @@ -0,0 +1,6 @@ +namespace StoneRed.NetificationApi.Client.Models; + +public class CountReceivedEventArgs(int count) : EventArgs +{ + public int Count { get; } = count; +} \ No newline at end of file diff --git a/src/StoneRed.NetificationApi/Client/NotificationApiClient.cs b/src/StoneRed.NetificationApi/Client/NotificationApiClient.cs index 5eeb4d1..97465c6 100644 --- a/src/StoneRed.NetificationApi/Client/NotificationApiClient.cs +++ b/src/StoneRed.NetificationApi/Client/NotificationApiClient.cs @@ -19,9 +19,11 @@ public class NotificationApiClient public event EventHandler? NewNotificationsReceived; + public event EventHandler? UnreadCountReceived; + private readonly WebsocketClient client; - public NotificationApiClient(string userId, string clientId, string clientSecret, bool secureMode, string baseAddress = "wss://ws.notificationapi.com") + public NotificationApiClient(string userId, string clientId, string? userIdHash = null, string baseAddress = "wss://ws.notificationapi.com") { UriBuilder uriBuilder = new UriBuilder(baseAddress); @@ -29,9 +31,9 @@ public class NotificationApiClient query["envId"] = clientId; query["userId"] = userId; - if (secureMode) + if (userIdHash is not null) { - query["userIdHash"] = UserIdHasher.Hash(userId, clientSecret); + query["userIdHash"] = userIdHash; } uriBuilder.Query = query.ToString(); @@ -46,7 +48,7 @@ public class NotificationApiClient _ = client.MessageReceived .Where(msg => msg.Text is not null) .Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/notifications")) - .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage(msg.Text!)) + .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage(msg.Text!)) .Subscribe(msg => { if (msg.Payload is null) @@ -60,7 +62,7 @@ public class NotificationApiClient _ = client.MessageReceived .Where(msg => msg.Text is not null) .Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/new_notifications")) - .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage(msg.Text!)) + .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage(msg.Text!)) .Subscribe(msg => { if (msg.Payload is null) @@ -70,6 +72,20 @@ public class NotificationApiClient NewNotificationsReceived?.Invoke(this, new NotificationsReceivedEventArgs(msg.Payload.Notifications)); }); + + _ = client.MessageReceived + .Where(msg => msg.Text is not null) + .Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/unread_count")) + .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage(msg.Text!)) + .Subscribe(msg => + { + if (msg.Payload is null) + { + return; + } + + UnreadCountReceived?.Invoke(this, new CountReceivedEventArgs(msg.Payload.Count)); + }); } public Task Start() @@ -77,13 +93,37 @@ public class NotificationApiClient return client.StartOrFail(); } - public bool RequestNotifications() + public bool RequestNotifications(int count) { - WebsocketMessage message = new("inapp_web/notifications") + WebsocketMessage message = new("inapp_web/notifications") + { + Payload = new(count) + }; + + return client.Send(JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions)); + } + + public bool RequestUnreadCount() + { + WebsocketMessage message = new("inapp_web/unread_count"); + + return client.Send(JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions)); + } + + public bool ClearUnread() + { + WebsocketMessage message = new("inapp_web/unread_clear"); + + return client.Send(JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions)); + } + + public bool ClearUnread(string notificationId) + { + WebsocketMessage message = new("inapp_web/unread_clear") { Payload = new { - count = 50 + notificationId } }; diff --git a/src/StoneRed.NetificationApi/Client/Payloads/CountPayload.cs b/src/StoneRed.NetificationApi/Client/Payloads/CountPayload.cs new file mode 100644 index 0000000..2d6c9a6 --- /dev/null +++ b/src/StoneRed.NetificationApi/Client/Payloads/CountPayload.cs @@ -0,0 +1,6 @@ +namespace StoneRed.NetificationApi.Client.Payloads; + +internal class CountPayload(int count) +{ + public int Count { get; set; } = count; +} \ No newline at end of file diff --git a/src/StoneRed.NetificationApi/Client/Payloads/NotificationsReceivedPayload.cs b/src/StoneRed.NetificationApi/Client/Payloads/NotificationsPayload.cs similarity index 80% rename from src/StoneRed.NetificationApi/Client/Payloads/NotificationsReceivedPayload.cs rename to src/StoneRed.NetificationApi/Client/Payloads/NotificationsPayload.cs index adb0d1d..ef33ec3 100644 --- a/src/StoneRed.NetificationApi/Client/Payloads/NotificationsReceivedPayload.cs +++ b/src/StoneRed.NetificationApi/Client/Payloads/NotificationsPayload.cs @@ -2,7 +2,7 @@ namespace StoneRed.NetificationApi.Client.Payloads; -internal class NotificationsReceivedPayload +internal class NotificationsPayload { public required List Notifications { get; set; } } \ No newline at end of file diff --git a/src/StoneRed.NetificationApi/Utilities/UserIdHasher.cs b/src/StoneRed.NetificationApi/Utilities/UserIdHasher.cs index 0318524..1fdd103 100644 --- a/src/StoneRed.NetificationApi/Utilities/UserIdHasher.cs +++ b/src/StoneRed.NetificationApi/Utilities/UserIdHasher.cs @@ -3,7 +3,7 @@ using System.Text; namespace StoneRed.NetificationApi.Utilities; -internal static class UserIdHasher +public static class UserIdHasher { public static string Hash(string userId, string clientSecret) {