Skip to content

Commit

Permalink
GetStream#130 Remove and add members can now pass initiator
Browse files Browse the repository at this point in the history
  • Loading branch information
erdtsieck committed Sep 28, 2023
1 parent 910df4d commit 558e9ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Clients/ChannelClient.Members.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ namespace StreamChat.Clients
{
public partial class ChannelClient
{
public async Task<ApiResponse> AddMembersAsync(string channelType, string channelId, AddMemberOptions options, params string[] userIds)
=> await AddMembersAsync(channelType, channelId, userIds, null, options);

public async Task<ApiResponse> AddMembersAsync(string channelType, string channelId, params string[] userIds)
=> await AddMembersAsync(channelType, channelId, userIds, null, null);

Expand All @@ -20,16 +23,18 @@ public async Task<ApiResponse> AddMembersAsync(string channelType, string channe
AddMembers = userIds,
HideHistory = options?.HideHistory,
Message = msg,
UserId = options?.InitiatorUserId,
});

public async Task<ApiResponse> RemoveMembersAsync(string channelType, string channelId, IEnumerable<string> userIds, MessageRequest msg = null)
public async Task<ApiResponse> RemoveMembersAsync(string channelType, string channelId, IEnumerable<string> userIds, MessageRequest msg = null, RemoveMemberOptions options = null)
=> await ExecuteRequestAsync<ApiResponse>($"channels/{channelType}/{channelId}",
HttpMethod.POST,
HttpStatusCode.Created,
new ChannelUpdateRequest
{
RemoveMembers = userIds,
Message = msg,
UserId = options?.InitiatorUserId,
});

public async Task<QueryMembersResponse> QueryMembersAsync(QueryMembersRequest queryMembersRequest)
Expand Down
8 changes: 7 additions & 1 deletion src/Clients/IChannelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace StreamChat.Clients
/// <remarks>https://getstream.io/chat/docs/dotnet-csharp/creating_channels/?language=csharp</remarks>
public interface IChannelClient
{
/// <summary>
/// Adds members to a channel.
/// </summary>
/// <remarks>https://getstream.io/chat/docs/dotnet-csharp/channel_members/?language=csharp</remarks>
Task<ApiResponse> AddMembersAsync(string channelType, string channelId, AddMemberOptions options, params string[] userIds);

/// <summary>
/// Adds members to a channel.
/// </summary>
Expand Down Expand Up @@ -162,7 +168,7 @@ public interface IChannelClient
/// Removes member(s) from a channel.
/// </summary>
/// <remarks>https://getstream.io/chat/docs/dotnet-csharp/channel_members/?language=csharp</remarks>
Task<ApiResponse> RemoveMembersAsync(string channelType, string channelId, IEnumerable<string> userIds, MessageRequest msg = null);
Task<ApiResponse> RemoveMembersAsync(string channelType, string channelId, IEnumerable<string> userIds, MessageRequest msg = null, RemoveMemberOptions options = null);

/// <summary>
/// <para>Removes all of the messages but not affect the channel data or channel members.</para>
Expand Down
6 changes: 6 additions & 0 deletions src/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public class OwnUser
public class AddMemberOptions
{
public bool? HideHistory { get; set; }
public string InitiatorUserId { get; set; }
}

public class RemoveMemberOptions
{
public string InitiatorUserId { get; set; }
}

public class UserPartialRequest : CustomDataBase
Expand Down
25 changes: 25 additions & 0 deletions tests/ChannelClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,19 @@ await WaitForAsync(async () =>
}, timeout: 10000);
}

[Test]
public async Task TestAddMembersWithInitiatorAsync()
{
var initiatingUser = await UpsertNewUserAsync();
var newUser = await UpsertNewUserAsync();

await _channelClient.AddMembersAsync(_channel.Type, _channel.Id, new AddMemberOptions{ InitiatorUserId = initiatingUser.Id }, newUser.Id);

var channel = await _channelClient.GetOrCreateAsync(_channel.Type, _channel.Id, ChannelGetRequest.WithoutWatching());
channel.Members.Should().Contain(x => x.UserId == newUser.Id);
await TryDeleteUsersAsync(new[] { newUser.Id });
}

[Test]
public async Task TestAddMembersAsync()
{
Expand All @@ -227,6 +240,18 @@ public async Task TestAddMembersAsync()
await TryDeleteUsersAsync(new[] { newUser.Id });
}

[Test]
public async Task TestRemoveMembersWithInitiatorAsync()
{
await _channelClient.RemoveMembersAsync(_channel.Type, _channel.Id, new[] { _user3.Id }, options: new RemoveMemberOptions
{
InitiatorUserId = _user1.Id
});

var channel = await _channelClient.GetOrCreateAsync(_channel.Type, _channel.Id, ChannelGetRequest.WithoutWatching());
channel.Members.Should().NotContain(x => x.UserId == _user3.Id);
}

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

0 comments on commit 558e9ba

Please sign in to comment.