Skip to content

Commit

Permalink
add block user feature
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmeadi committed Jun 11, 2024
1 parent 7c032a2 commit dcab637
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Clients/IUserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,5 +238,23 @@ public interface IUserClient
/// </summary>
/// <remarks>https://getstream.io/chat/docs/dotnet-csharp/query_users/?language=csharp/</remarks>
Task<QueryUsersResponse> QueryAsync(QueryUserOptions opts);

/// <summary>
/// Block user from sending 1x1 messages
/// </summary>
/// <remarks>https://getstream.io/chat/docs/javascript/block_user/</remarks>
Task<BlockUserResponse> BlockUserAsync(string targetId, string userID);

/// <summary>
/// Get list of blocked users by this user
/// </summary>
/// <remarks>https://getstream.io/chat/docs/javascript/block_user/</remarks>
Task<GetBlockedUsersResponse> GetBlockedUsersAsync(string userID);

/// <summary>
/// Unblock user
/// </summary>
/// <remarks>https://getstream.io/chat/docs/javascript/block_user/</remarks>
Task<ApiResponse> UnblockUserAsync(string targetId, string userID);
}
}
27 changes: 27 additions & 0 deletions src/Clients/UserClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,33 @@ public async Task<MuteResponse> MuteAsync(string targetId, string id)
user_id = id,
});

public async Task<BlockUserResponse> BlockUserAsync(string targetId, string userID)
=> await ExecuteRequestAsync<BlockUserResponse>("users/block",
HttpMethod.POST,
HttpStatusCode.OK,
body: new
{
blocked_user_id = targetId,
user_id = userID,
});
public async Task<ApiResponse> UnblockUserAsync(string targetId, string userID)
=> await ExecuteRequestAsync<BlockUserResponse>("users/unblock",
HttpMethod.POST,
HttpStatusCode.OK,
body: new
{
blocked_user_id = targetId,
user_id = userID,
});//$$

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🐶 Reviewdog

Semicolons should be followed by a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🐶 Reviewdog

Single line comment should begin with a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Semicolons should be followed by a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Single line comment should begin with a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Semicolons should be followed by a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Single line comment should begin with a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Semicolons should be followed by a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Single line comment should begin with a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Semicolons should be followed by a space

Check warning on line 167 in src/Clients/UserClient.cs

View workflow job for this annotation

GitHub Actions / 🧪 Run tests

Single line comment should begin with a space
public async Task<GetBlockedUsersResponse> GetBlockedUsersAsync(string userID)
=> await ExecuteRequestAsync<GetBlockedUsersResponse>("users/block",
HttpMethod.GET,
HttpStatusCode.OK,
queryParams: new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("user_id", userID),
});

public async Task<ApiResponse> UnmuteAsync(string targetId, string id)
=> await ExecuteRequestAsync<ApiResponse>("moderation/unmute",
HttpMethod.POST,
Expand Down
37 changes: 37 additions & 0 deletions src/Models/BlockUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace StreamChat.Models
{
public class BlockUserResponse : ApiResponse
{
public string BlockedByUserID { get; set; }
public string BlockedUserID { get; set; }
public string CreatedAt { get; set; }
}

public class GetBlockedUsersResponse : ApiResponse
{
[JsonProperty("blocks")]
public Blocks[] Blocks { get; set; }
}

public class Blocks
{
[JsonProperty("user")]
public User BlockedByUser { get; set; }

[JsonProperty("user_id")]
public string BlockedByUserID { get; set; }

[JsonProperty("blocked_user")]
public User BlockedUser { get; set; }

[JsonProperty("blocked_user_id")]
public string BlockedUserID { get; set; }

[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class User : CustomDataBase
public DateTimeOffset? DeletedAt { get; set; }
public DateTimeOffset? DeactivatedAt { get; set; }
public bool? Online { get; set; }
public string[] BlockedUserIDs { get; set; }
}

public class OwnUser
Expand Down
34 changes: 34 additions & 0 deletions tests/UserClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,40 @@ public async Task TestMuteUserAsync()
muteResp.Mute.User.Id.Should().BeEquivalentTo(_user1.Id);
}

[Test]
public async Task TestBlockUserAsync()
{
var blockResp = await _userClient.BlockUserAsync(_user2.Id, _user1.Id);

blockResp.BlockedByUserID.Should().BeEquivalentTo(_user1.Id);
blockResp.BlockedUserID.Should().BeEquivalentTo(_user2.Id);

var getBlockResp = await _userClient.GetBlockedUsersAsync(_user1.Id);
getBlockResp.Blocks[0].BlockedUserID.Should().BeEquivalentTo(_user2.Id);
getBlockResp.Blocks[0].BlockedByUserID.Should().BeEquivalentTo(_user1.Id);
getBlockResp.Blocks[0].BlockedUser.Id.Should().BeEquivalentTo(_user2.Id);
getBlockResp.Blocks[0].BlockedByUser.Id.Should().BeEquivalentTo(_user1.Id);

var queryUsersResp = await _userClient.QueryAsync(QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
{ "id", _user1.Id },
}));
queryUsersResp.Users.Should().NotBeEmpty();
queryUsersResp.Users[0].BlockedUserIDs[0].Should().BeEquivalentTo(_user2.Id);

await _userClient.UnblockUserAsync(_user2.Id, _user1.Id);

getBlockResp = await _userClient.GetBlockedUsersAsync(_user1.Id);
getBlockResp.Blocks.Length.Should().Be(0);

queryUsersResp = await _userClient.QueryAsync(QueryUserOptions.Default.WithFilter(new Dictionary<string, object>
{
{ "id", _user1.Id },
}));
queryUsersResp.Users.Should().NotBeEmpty();
queryUsersResp.Users[0].BlockedUserIDs.Length.Should().Be(0);
}

[Test]
public async Task TestUnmuteUserAsync()
{
Expand Down

0 comments on commit dcab637

Please sign in to comment.