From 38317755a6fba12dff35fe4dbbb7f95660aa1132 Mon Sep 17 00:00:00 2001 From: aditya Date: Wed, 5 Mar 2025 17:18:52 +0100 Subject: [PATCH 1/2] chore: flag user, handle errors --- src/Moderation.cs | 5 ++++- tests/ModerationTests.cs | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Moderation.cs b/src/Moderation.cs index e035c1f..c54d459 100644 --- a/src/Moderation.cs +++ b/src/Moderation.cs @@ -45,7 +45,10 @@ public async Task FlagAsync(string entityType, string entityId, st var response = await _client.MakeRequestAsync(request); - return StreamJsonConverter.DeserializeObject(response.Content); + if (response.StatusCode > HttpStatusCode.Accepted) + return StreamJsonConverter.DeserializeObject(response.Content); + + throw StreamException.FromResponse(response); } } } \ No newline at end of file diff --git a/tests/ModerationTests.cs b/tests/ModerationTests.cs index d5744de..1e0204f 100644 --- a/tests/ModerationTests.cs +++ b/tests/ModerationTests.cs @@ -63,7 +63,6 @@ public async Task TestReactionModeration() Assert.AreEqual("complete", response.Status); Assert.AreEqual("remove", response.RecommendedAction); - } [Test] @@ -86,6 +85,12 @@ public async Task TestFlagUser() Assert.NotNull(response); } + [Test] + public async Task TestFlagUserError() + { + Assert.ThrowsAsync(async () => await Client.Moderation.FlagUserAsync(string.Empty, "blood")); + } + [Test] public async Task TestFlagActivity() { From a8460eb2fde79963eabf38edb1b2e10e5bfa9800 Mon Sep 17 00:00:00 2001 From: aditya Date: Wed, 5 Mar 2025 18:32:36 +0100 Subject: [PATCH 2/2] chore: flag user should accept flagging userID, fix tests --- src/IModeration.cs | 2 +- src/Moderation.cs | 6 +++--- tests/ModerationTests.cs | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/IModeration.cs b/src/IModeration.cs index 53dee6a..66f376f 100644 --- a/src/IModeration.cs +++ b/src/IModeration.cs @@ -6,7 +6,7 @@ namespace Stream { public interface IModeration { - Task FlagUserAsync(string flaggedUserId, string reason, IDictionary options = null); + Task FlagUserAsync(string flaggingUserID, string flaggedUserId, string reason, IDictionary options = null); Task FlagActivityAsync(string entityId, string entityCreatorId, string reason, IDictionary options = null); diff --git a/src/Moderation.cs b/src/Moderation.cs index c54d459..82ee975 100644 --- a/src/Moderation.cs +++ b/src/Moderation.cs @@ -19,9 +19,9 @@ public Moderation(StreamClient client) _client = client; } - public async Task FlagUserAsync(string flaggedUserID, string reason, IDictionary options = null) + public async Task FlagUserAsync(string flaggingUserID, string flaggedUserID, string reason, IDictionary options = null) { - return await FlagAsync("stream:user", flaggedUserID, string.Empty, reason, options); + return await FlagAsync("stream:user", flaggedUserID, flaggingUserID, reason, options); } public async Task FlagActivityAsync(string entityId, string entityCreatorID, string reason, IDictionary options = null) @@ -45,7 +45,7 @@ public async Task FlagAsync(string entityType, string entityId, st var response = await _client.MakeRequestAsync(request); - if (response.StatusCode > HttpStatusCode.Accepted) + if (response.StatusCode == HttpStatusCode.Created) return StreamJsonConverter.DeserializeObject(response.Content); throw StreamException.FromResponse(response); diff --git a/tests/ModerationTests.cs b/tests/ModerationTests.cs index 1e0204f..6ac4b22 100644 --- a/tests/ModerationTests.cs +++ b/tests/ModerationTests.cs @@ -68,27 +68,27 @@ public async Task TestReactionModeration() [Test] public async Task TestFlagUser() { - var userId = Guid.NewGuid().ToString(); + var userId = "flagginguser"; var userData = new Dictionary { { "field", "value" }, { "is_admin", true }, }; - var u = await Client.Users.AddAsync(userId, userData); + var u = await Client.Users.AddAsync(userId, userData, true); Assert.NotNull(u); Assert.NotNull(u.CreatedAt); Assert.NotNull(u.UpdatedAt); - var response = await Client.Moderation.FlagUserAsync(userId, "blood"); + var response = await Client.Moderation.FlagUserAsync(userId, "flagged-user", "blood"); Assert.NotNull(response); } [Test] public async Task TestFlagUserError() { - Assert.ThrowsAsync(async () => await Client.Moderation.FlagUserAsync(string.Empty, "blood")); + Assert.ThrowsAsync(async () => await Client.Moderation.FlagUserAsync("", string.Empty, "blood")); } [Test]