diff --git a/src/SignalRServiceExtension/Bindings/SignalRAsyncCollector.cs b/src/SignalRServiceExtension/Bindings/SignalRAsyncCollector.cs index 92f3183b..b434853c 100644 --- a/src/SignalRServiceExtension/Bindings/SignalRAsyncCollector.cs +++ b/src/SignalRServiceExtension/Bindings/SignalRAsyncCollector.cs @@ -59,24 +59,29 @@ internal SignalRAsyncCollector(IAzureSignalRSender client) if (!string.IsNullOrEmpty(groupAction.ConnectionId)) { - if (groupAction.Action == GroupAction.Add) + switch(groupAction.Action) { - await client.AddConnectionToGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false); - } - else - { - await client.RemoveConnectionFromGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false); + case GroupAction.Add: + await client.AddConnectionToGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false); + break; + case GroupAction.Remove: + await client.RemoveConnectionFromGroup(groupAction.ConnectionId, groupAction.GroupName).ConfigureAwait(false); + break; } } else if (!string.IsNullOrEmpty(groupAction.UserId)) { - if (groupAction.Action == GroupAction.Add) - { - await client.AddUserToGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false); - } - else + switch (groupAction.Action) { - await client.RemoveUserFromGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false); + case GroupAction.Add: + await client.AddUserToGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false); + break; + case GroupAction.Remove: + await client.RemoveUserFromGroup(groupAction.UserId, groupAction.GroupName).ConfigureAwait(false); + break; + case GroupAction.RemoveAll: + await client.RemoveUserFromAllGroups(groupAction.UserId).ConfigureAwait(false); + break; } } else @@ -95,4 +100,4 @@ internal SignalRAsyncCollector(IAzureSignalRSender client) return Task.CompletedTask; } } -} \ No newline at end of file +} diff --git a/src/SignalRServiceExtension/Bindings/SignalRGroupAction.cs b/src/SignalRServiceExtension/Bindings/SignalRGroupAction.cs index dff7b900..1bf611bf 100644 --- a/src/SignalRServiceExtension/Bindings/SignalRGroupAction.cs +++ b/src/SignalRServiceExtension/Bindings/SignalRGroupAction.cs @@ -33,6 +33,8 @@ public enum GroupAction [EnumMember(Value = "add")] Add, [EnumMember(Value = "remove")] - Remove + Remove, + [EnumMember(Value = "removeAll")] + RemoveAll } } \ No newline at end of file diff --git a/src/SignalRServiceExtension/Client/AzureSignalRClient.cs b/src/SignalRServiceExtension/Client/AzureSignalRClient.cs index ec062216..385a5a4d 100644 --- a/src/SignalRServiceExtension/Client/AzureSignalRClient.cs +++ b/src/SignalRServiceExtension/Client/AzureSignalRClient.cs @@ -117,6 +117,16 @@ public async Task RemoveUserFromGroup(string userId, string groupName) await serviceHubContext.UserGroups.RemoveFromGroupAsync(userId, groupName); } + public async Task RemoveUserFromAllGroups(string userId) + { + if (string.IsNullOrEmpty(userId)) + { + throw new ArgumentException($"{nameof(userId)} cannot be null or empty"); + } + var serviceHubContext = await serviceManagerStore.GetOrAddByConnectionString(connectionString).GetAsync(hubName); + await serviceHubContext.UserGroups.RemoveFromAllGroupsAsync(userId); + } + public async Task AddConnectionToGroup(string connectionId, string groupName) { if (string.IsNullOrEmpty(connectionId)) @@ -165,4 +175,4 @@ private static IEnumerable BuildJwtClaims(IEnumerable customerClai } } } -} \ No newline at end of file +} diff --git a/src/SignalRServiceExtension/Client/IAzureSignalRSender.cs b/src/SignalRServiceExtension/Client/IAzureSignalRSender.cs index 47534104..287e7877 100644 --- a/src/SignalRServiceExtension/Client/IAzureSignalRSender.cs +++ b/src/SignalRServiceExtension/Client/IAzureSignalRSender.cs @@ -14,7 +14,8 @@ internal interface IAzureSignalRSender Task SendToGroup(string group, SignalRData data); Task AddUserToGroup(string userId, string groupName); Task RemoveUserFromGroup(string userId, string groupName); + Task RemoveUserFromAllGroups(string userId); Task AddConnectionToGroup(string connectionId, string groupName); Task RemoveConnectionFromGroup(string connectionId, string groupName); } -} \ No newline at end of file +} diff --git a/test/SignalRServiceExtension.Tests/SignalRAsyncCollectorTests.cs b/test/SignalRServiceExtension.Tests/SignalRAsyncCollectorTests.cs index 255ec57b..6e8a668c 100644 --- a/test/SignalRServiceExtension.Tests/SignalRAsyncCollectorTests.cs +++ b/test/SignalRServiceExtension.Tests/SignalRAsyncCollectorTests.cs @@ -123,6 +123,26 @@ await collector.AddAsync(new SignalRGroupAction Assert.Equal("group1", actualData.Arguments[1]); } + [Fact] + public async Task AddAsync_WithUserId_CallsRemoveUserFromAllGroups() + { + var signalRSenderMock = new Mock(); + var collector = new SignalRAsyncCollector(signalRSenderMock.Object); + + await collector.AddAsync(new SignalRGroupAction + { + UserId = "userId1", + Action = GroupAction.RemoveAll + }); + + signalRSenderMock.Verify( + c => c.RemoveUserFromAllGroups("userId1"), + Times.Once); + signalRSenderMock.VerifyNoOtherCalls(); + var actualData = signalRSenderMock.Invocations[0]; + Assert.Equal("userId1", actualData.Arguments[0]); + } + [Fact] public async Task AddAsync_InvalidTypeThrowException() { @@ -240,4 +260,4 @@ public async Task AddAsync_GroupOperation_WithoutParametersThrowException() await Assert.ThrowsAsync(() => collector.AddAsync(item)); } } -} \ No newline at end of file +}