Add RequestUnreadCount and ClearUnread methods to client

This commit is contained in:
Stone_Red
2024-03-03 13:23:37 +01:00
parent 380c520084
commit 3e2015b7c6
6 changed files with 90 additions and 20 deletions
+28 -10
View File
@@ -4,18 +4,26 @@ using StoneRed.NetificationApi.Server;
using StoneRed.NetificationApi.Server.IdentifyUser; using StoneRed.NetificationApi.Server.IdentifyUser;
using StoneRed.NetificationApi.Server.Send; using StoneRed.NetificationApi.Server.Send;
const string userId = "<UserId>"; // The ID of the user to send the notification to
const string notificationId = "<NotifcationId>"; string userId = "test";
const string clientId = "<ClientId>";
const string clientSecret = "<ClientSecret>"; // 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) ?? "<ClientId>";
string clientSecret = Environment.GetEnvironmentVariable("NOTIFICATION_API_SECRET", EnvironmentVariableTarget.User) ?? "<ClientSecret>";
// 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"); Console.WriteLine("Start client");
NotificationApiClient notificationApiClient = new NotificationApiClient("test", clientId, clientSecret, false);
await notificationApiClient.Start(); await notificationApiClient.Start();
Console.WriteLine("Client started"); Console.WriteLine("Client started");
notificationApiClient.RequestNotifications();
notificationApiClient.RequestedNotificationsReceived += (sender, args) => notificationApiClient.RequestedNotificationsReceived += (sender, args) =>
{ {
Console.WriteLine("Requested notifications received"); 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"); Console.WriteLine("Identify user");
await notificationApiServer.Identify(new IdentifyUserData await notificationApiServer.Identify(new IdentifyUserData
{ {
UserId = userId, UserId = userId
Email = "[email protected]"
}); });
Console.WriteLine("User identified"); Console.WriteLine("User identified");
@@ -0,0 +1,6 @@
namespace StoneRed.NetificationApi.Client.Models;
public class CountReceivedEventArgs(int count) : EventArgs
{
public int Count { get; } = count;
}
@@ -19,9 +19,11 @@ public class NotificationApiClient
public event EventHandler<NotificationsReceivedEventArgs>? NewNotificationsReceived; public event EventHandler<NotificationsReceivedEventArgs>? NewNotificationsReceived;
public event EventHandler<CountReceivedEventArgs>? UnreadCountReceived;
private readonly WebsocketClient client; 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); UriBuilder uriBuilder = new UriBuilder(baseAddress);
@@ -29,9 +31,9 @@ public class NotificationApiClient
query["envId"] = clientId; query["envId"] = clientId;
query["userId"] = userId; query["userId"] = userId;
if (secureMode) if (userIdHash is not null)
{ {
query["userIdHash"] = UserIdHasher.Hash(userId, clientSecret); query["userIdHash"] = userIdHash;
} }
uriBuilder.Query = query.ToString(); uriBuilder.Query = query.ToString();
@@ -46,7 +48,7 @@ public class NotificationApiClient
_ = client.MessageReceived _ = client.MessageReceived
.Where(msg => msg.Text is not null) .Where(msg => msg.Text is not null)
.Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/notifications")) .Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/notifications"))
.Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage<NotificationsReceivedPayload>(msg.Text!)) .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage<NotificationsPayload>(msg.Text!))
.Subscribe(msg => .Subscribe(msg =>
{ {
if (msg.Payload is null) if (msg.Payload is null)
@@ -60,7 +62,7 @@ public class NotificationApiClient
_ = client.MessageReceived _ = client.MessageReceived
.Where(msg => msg.Text is not null) .Where(msg => msg.Text is not null)
.Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/new_notifications")) .Where(msg => WebsocketMessageComparer.Compare(msg.Text, "inapp_web/new_notifications"))
.Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage<NotificationsReceivedPayload>(msg.Text!)) .Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage<NotificationsPayload>(msg.Text!))
.Subscribe(msg => .Subscribe(msg =>
{ {
if (msg.Payload is null) if (msg.Payload is null)
@@ -70,6 +72,20 @@ public class NotificationApiClient
NewNotificationsReceived?.Invoke(this, new NotificationsReceivedEventArgs(msg.Payload.Notifications)); 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<CountPayload>(msg.Text!))
.Subscribe(msg =>
{
if (msg.Payload is null)
{
return;
}
UnreadCountReceived?.Invoke(this, new CountReceivedEventArgs(msg.Payload.Count));
});
} }
public Task Start() public Task Start()
@@ -77,13 +93,37 @@ public class NotificationApiClient
return client.StartOrFail(); return client.StartOrFail();
} }
public bool RequestNotifications() public bool RequestNotifications(int count)
{ {
WebsocketMessage<object> message = new("inapp_web/notifications") WebsocketMessage<CountPayload> 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<object> message = new("inapp_web/unread_clear")
{ {
Payload = new Payload = new
{ {
count = 50 notificationId
} }
}; };
@@ -0,0 +1,6 @@
namespace StoneRed.NetificationApi.Client.Payloads;
internal class CountPayload(int count)
{
public int Count { get; set; } = count;
}
@@ -2,7 +2,7 @@
namespace StoneRed.NetificationApi.Client.Payloads; namespace StoneRed.NetificationApi.Client.Payloads;
internal class NotificationsReceivedPayload internal class NotificationsPayload
{ {
public required List<NotificationReceivedData> Notifications { get; set; } public required List<NotificationReceivedData> Notifications { get; set; }
} }
@@ -3,7 +3,7 @@ using System.Text;
namespace StoneRed.NetificationApi.Utilities; namespace StoneRed.NetificationApi.Utilities;
internal static class UserIdHasher public static class UserIdHasher
{ {
public static string Hash(string userId, string clientSecret) public static string Hash(string userId, string clientSecret)
{ {