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] 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;
}