From d2149081f675882d8df3e2428e819dc52e809fb3 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 11 Mar 2024 20:01:58 +0100 Subject: [PATCH 1/3] Fix inconsistencies and fix `Identify` method not sending all data to the server --- .../Server/IdentifyUser/IdentifyUserData.cs | 26 +------------------ .../Server/NotificationApiServer.cs | 24 +++++++---------- .../SetUserPreferencesData.cs | 11 +------- 3 files changed, 12 insertions(+), 49 deletions(-) diff --git a/src/StoneRed.NetificationApi/Server/IdentifyUser/IdentifyUserData.cs b/src/StoneRed.NetificationApi/Server/IdentifyUser/IdentifyUserData.cs index 92fbefa..795cc04 100644 --- a/src/StoneRed.NetificationApi/Server/IdentifyUser/IdentifyUserData.cs +++ b/src/StoneRed.NetificationApi/Server/IdentifyUser/IdentifyUserData.cs @@ -1,5 +1,4 @@ -using System.Diagnostics.CodeAnalysis; -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace StoneRed.NetificationApi.Server.IdentifyUser; @@ -8,12 +7,6 @@ namespace StoneRed.NetificationApi.Server.IdentifyUser; /// public class IdentifyUserData { - /// - /// Gets or sets the user ID. - /// - [JsonIgnore] - public required string UserId { get; set; } - /// /// Gets or sets the email address of the user. /// @@ -34,21 +27,4 @@ public class IdentifyUserData /// Gets or sets the list of web push tokens for the user. /// public List? WebPushTokens { get; set; } - - /// - /// Initializes a new instance of the class with the specified user ID. - /// - /// The user ID. - [SetsRequiredMembers] - public IdentifyUserData(string userId) - { - UserId = userId; - } - - /// - /// Initializes a new instance of the class. - /// - public IdentifyUserData() - { - } } \ No newline at end of file diff --git a/src/StoneRed.NetificationApi/Server/NotificationApiServer.cs b/src/StoneRed.NetificationApi/Server/NotificationApiServer.cs index 803ac97..34538dd 100644 --- a/src/StoneRed.NetificationApi/Server/NotificationApiServer.cs +++ b/src/StoneRed.NetificationApi/Server/NotificationApiServer.cs @@ -76,31 +76,26 @@ public class NotificationApiServer /// /// Identifies a user. /// + /// The ID of the user. /// The data for identifying the user. /// The HTTP response message. - public async Task Identify(IdentifyUserData identifyUserData) + public async Task Identify(string userId, IdentifyUserData identifyUserData) { string authToken; if (secureMode) { - string hashedUserId = UserIdHasher.Hash(identifyUserData.UserId, clientSecret); - authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{identifyUserData.UserId}:{hashedUserId}")); + string hashedUserId = UserIdHasher.Hash(userId, clientSecret); + authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{userId}:{hashedUserId}")); } else { - authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{identifyUserData.UserId}")); + authToken = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{clientId}:{userId}")); } - var requestData = new + HttpRequestMessage request = new(HttpMethod.Post, $"users/{userId}") { - email = identifyUserData.Email, - number = identifyUserData.TelephoneNumber, - }; - - HttpRequestMessage request = new(HttpMethod.Post, $"users/{identifyUserData.UserId}") - { - Content = JsonContent.Create(requestData, options: Configuration.JsonSerializerOptions), + Content = JsonContent.Create(identifyUserData, options: Configuration.JsonSerializerOptions), }; request.Headers.Add("Authorization", $"Basic {authToken}"); @@ -111,10 +106,11 @@ public class NotificationApiServer /// /// Sets user preferences. /// + /// The ID of the user. /// The data for setting user preferences. /// The HTTP response message. - public async Task SetUserPreferences(SetUserPreferencesData setUserPreferencesData) + public async Task SetUserPreferences(string userId, SetUserPreferencesData setUserPreferencesData) { - return await httpClient.PostAsJsonAsync($"user_preferences/{setUserPreferencesData.UserId}", setUserPreferencesData, Configuration.JsonSerializerOptions); + return await httpClient.PostAsJsonAsync($"user_preferences/{userId}", setUserPreferencesData, Configuration.JsonSerializerOptions); } } \ No newline at end of file diff --git a/src/StoneRed.NetificationApi/Server/SetUserPreferences/SetUserPreferencesData.cs b/src/StoneRed.NetificationApi/Server/SetUserPreferences/SetUserPreferencesData.cs index 5e7be5f..42eb949 100644 --- a/src/StoneRed.NetificationApi/Server/SetUserPreferences/SetUserPreferencesData.cs +++ b/src/StoneRed.NetificationApi/Server/SetUserPreferences/SetUserPreferencesData.cs @@ -1,5 +1,4 @@ using System.Diagnostics.CodeAnalysis; -using System.Text.Json.Serialization; namespace StoneRed.NetificationApi.Server.SetUserPreferences; @@ -8,12 +7,6 @@ namespace StoneRed.NetificationApi.Server.SetUserPreferences; /// public class SetUserPreferencesData { - /// - /// Gets or sets the user ID. - /// - [JsonIgnore] - public required string UserId { get; set; } - /// /// Gets or sets the list of notification preferences. /// @@ -22,12 +15,10 @@ public class SetUserPreferencesData /// /// Initializes a new instance of the class. /// - /// The user ID. /// The list of notification preferences. [SetsRequiredMembers] - public SetUserPreferencesData(string userId, List preferences) + public SetUserPreferencesData(List preferences) { - UserId = userId; Preferences = preferences; } From e6f7f88785fd1a728381a0b861387032b8b2a827 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 11 Mar 2024 20:03:14 +0100 Subject: [PATCH 2/3] Update example project --- src/StoneRed.NetificationApi.Example/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StoneRed.NetificationApi.Example/Program.cs b/src/StoneRed.NetificationApi.Example/Program.cs index ef22d51..48316ff 100644 --- a/src/StoneRed.NetificationApi.Example/Program.cs +++ b/src/StoneRed.NetificationApi.Example/Program.cs @@ -73,9 +73,9 @@ notificationApiClient.RequestNotifications(5); Console.WriteLine("Notifications requested"); Console.WriteLine("Identify user"); -await notificationApiServer.Identify(new IdentifyUserData +await notificationApiServer.Identify(userId, new IdentifyUserData { - UserId = userId + Email = "example@example.com" }); Console.WriteLine("User identified"); From d4b9ed769db7f5c754cc6be1703a5970fd2948f7 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 11 Mar 2024 20:03:25 +0100 Subject: [PATCH 3/3] Update package version --- src/StoneRed.NetificationApi/StoneRed.NetificationApi.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StoneRed.NetificationApi/StoneRed.NetificationApi.csproj b/src/StoneRed.NetificationApi/StoneRed.NetificationApi.csproj index d558442..b32e408 100644 --- a/src/StoneRed.NetificationApi/StoneRed.NetificationApi.csproj +++ b/src/StoneRed.NetificationApi/StoneRed.NetificationApi.csproj @@ -5,7 +5,7 @@ enable enable True - 1.0.3.0 + 1.0.4.0 https://github.com/Stone-Red-Software/StoneRed.NetificationApi MIT A .NET library for NotificationAPI