mirror of
https://github.com/Stone-Red-Software/StoneRed.NetificationApi.git
synced 2026-09-04 08:46:05 +02:00
Add RequestUserPreferences and PatchUserPreferences methods to client
This commit is contained in:
@@ -47,6 +47,23 @@ notificationApiClient.UnreadCountReceived += (sender, args) =>
|
|||||||
Console.WriteLine($"Unread count received: {args.Count}");
|
Console.WriteLine($"Unread count received: {args.Count}");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
notificationApiClient.UserPreferencesReceived += (sender, args) =>
|
||||||
|
{
|
||||||
|
Console.WriteLine("User preferences received");
|
||||||
|
foreach (NotificationUserPreference notificationPreference in args.UserPreferences)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Notification preference received: {notificationPreference.Title}");
|
||||||
|
foreach (NotificationUserPreferenceSetting preference in notificationPreference.Settings)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Notification preference: {preference.Channel} {preference.ChannelName} {preference.State}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Console.WriteLine("Request user preferences");
|
||||||
|
notificationApiClient.RequestUserPreferences();
|
||||||
|
Console.WriteLine("User preferences requested");
|
||||||
|
|
||||||
Console.WriteLine("Request unread count");
|
Console.WriteLine("Request unread count");
|
||||||
notificationApiClient.RequestUnreadCount();
|
notificationApiClient.RequestUnreadCount();
|
||||||
Console.WriteLine("Unread count requested");
|
Console.WriteLine("Unread count requested");
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using StoneRed.NetificationApi.Shared;
|
||||||
|
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace StoneRed.NetificationApi.Client.Models;
|
||||||
|
|
||||||
|
public class NotificationChannelPreference
|
||||||
|
{
|
||||||
|
public NotificationChannel Channel { get; set; }
|
||||||
|
|
||||||
|
public bool State { get; set; }
|
||||||
|
public string? SubNotificationId { get; set; }
|
||||||
|
|
||||||
|
[SetsRequiredMembers]
|
||||||
|
public NotificationChannelPreference(NotificationChannel channel, bool state, string? subNotificationId = null)
|
||||||
|
{
|
||||||
|
Channel = channel;
|
||||||
|
State = state;
|
||||||
|
SubNotificationId = subNotificationId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NotificationChannelPreference()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace StoneRed.NetificationApi.Client.Models;
|
||||||
|
|
||||||
|
public class NotificationUserPreference
|
||||||
|
{
|
||||||
|
public required string NotificationId { get; set; }
|
||||||
|
public required string Title { get; set; }
|
||||||
|
public required List<NotificationUserPreferenceSetting> Settings { get; set; }
|
||||||
|
public required List<object> SubNotificationPreferences { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
using StoneRed.NetificationApi.Shared;
|
||||||
|
|
||||||
|
namespace StoneRed.NetificationApi.Client.Models;
|
||||||
|
|
||||||
|
public class NotificationUserPreferenceSetting
|
||||||
|
{
|
||||||
|
public required NotificationChannel Channel { get; set; }
|
||||||
|
public bool State { get; set; }
|
||||||
|
public required string ChannelName { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace StoneRed.NetificationApi.Client.Models;
|
||||||
|
|
||||||
|
public class UserPreferencesReceivedEventArgs(List<NotificationUserPreference> userPreferences)
|
||||||
|
{
|
||||||
|
public List<NotificationUserPreference> UserPreferences { get; set; } = userPreferences;
|
||||||
|
}
|
||||||
@@ -21,6 +21,8 @@ public class NotificationApiClient
|
|||||||
|
|
||||||
public event EventHandler<CountReceivedEventArgs>? UnreadCountReceived;
|
public event EventHandler<CountReceivedEventArgs>? UnreadCountReceived;
|
||||||
|
|
||||||
|
public event EventHandler<UserPreferencesReceivedEventArgs>? UserPreferencesReceived;
|
||||||
|
|
||||||
private readonly WebsocketClient client;
|
private readonly WebsocketClient client;
|
||||||
|
|
||||||
public NotificationApiClient(string userId, string clientId, string? userIdHash = null, string baseAddress = "wss://ws.notificationapi.com")
|
public NotificationApiClient(string userId, string clientId, string? userIdHash = null, string baseAddress = "wss://ws.notificationapi.com")
|
||||||
@@ -86,6 +88,20 @@ public class NotificationApiClient
|
|||||||
|
|
||||||
UnreadCountReceived?.Invoke(this, new CountReceivedEventArgs(msg.Payload.Count));
|
UnreadCountReceived?.Invoke(this, new CountReceivedEventArgs(msg.Payload.Count));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
_ = client.MessageReceived
|
||||||
|
.Where(msg => msg.Text is not null)
|
||||||
|
.Where(msg => WebsocketMessageComparer.Compare(msg.Text, "user_preferences/preferences"))
|
||||||
|
.Select(msg => WebsocketMessageConverter.ConvertWebsocketMessage<UserPreferencesPayload>(msg.Text!))
|
||||||
|
.Subscribe(msg =>
|
||||||
|
{
|
||||||
|
if (msg.Payload is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
UserPreferencesReceived?.Invoke(this, new UserPreferencesReceivedEventArgs(msg.Payload.UserPreferences));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task Start()
|
public Task Start()
|
||||||
@@ -130,6 +146,32 @@ public class NotificationApiClient
|
|||||||
return client.Send(JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions));
|
return client.Send(JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool RequestUserPreferences()
|
||||||
|
{
|
||||||
|
WebsocketMessage message = new("user_preferences/get_preferences");
|
||||||
|
|
||||||
|
return client.Send(JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool PatchUserPreferences(string notificationId, params NotificationChannelPreference[] channelPreferences)
|
||||||
|
{
|
||||||
|
WebsocketMessage<object> message = new("user_preferences/patch_preferences")
|
||||||
|
{
|
||||||
|
Payload = new object[]
|
||||||
|
{
|
||||||
|
new
|
||||||
|
{
|
||||||
|
notificationId,
|
||||||
|
channelPreferences
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
string msg = JsonSerializer.Serialize(message, Configuration.JsonSerializerOptions);
|
||||||
|
|
||||||
|
return client.Send(msg);
|
||||||
|
}
|
||||||
|
|
||||||
public Task Stop()
|
public Task Stop()
|
||||||
{
|
{
|
||||||
return client.StopOrFail(WebSocketCloseStatus.NormalClosure, "Normal closure");
|
return client.StopOrFail(WebSocketCloseStatus.NormalClosure, "Normal closure");
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
using StoneRed.NetificationApi.Client.Models;
|
||||||
|
|
||||||
|
namespace StoneRed.NetificationApi.Client.Payloads;
|
||||||
|
|
||||||
|
internal class UserPreferencesPayload
|
||||||
|
{
|
||||||
|
public required List<NotificationUserPreference> UserPreferences { get; set; }
|
||||||
|
}
|
||||||
@@ -12,8 +12,16 @@ internal static class WebsocketMessageComparer
|
|||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
WebsocketMessage? websocketMessage;
|
||||||
|
|
||||||
WebsocketMessage? websocketMessage = JsonSerializer.Deserialize<WebsocketMessage>(message, Configuration.JsonSerializerOptions);
|
try
|
||||||
|
{
|
||||||
|
websocketMessage = JsonSerializer.Deserialize<WebsocketMessage>(message, Configuration.JsonSerializerOptions);
|
||||||
|
}
|
||||||
|
catch (JsonException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return websocketMessage?.Route == route;
|
return websocketMessage?.Route == route;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user