From 68de054ba4da0f23047d5924e3f880b820947ec7 Mon Sep 17 00:00:00 2001 From: Mark Lopez Date: Tue, 1 Sep 2020 10:01:20 -0500 Subject: [PATCH] Updated FetchConversations to fetch all conversations, not just the defaults. --- SlackNet.Bot/SlackBot.cs | 10 +++++- SlackNet.Tests/SlackBotTests.cs | 63 +++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/SlackNet.Bot/SlackBot.cs b/SlackNet.Bot/SlackBot.cs index 5fd7d97..4ffcf1e 100644 --- a/SlackNet.Bot/SlackBot.cs +++ b/SlackNet.Bot/SlackBot.cs @@ -382,7 +382,15 @@ private async Task FetchConversations() do { - var response = await _api.Conversations.List(cursor: cursor).ConfigureAwait(false); + var response = await _api.Conversations.List( + cursor: cursor, + types: new[] + { + ConversationType.PublicChannel, + ConversationType.PrivateChannel, + ConversationType.Im, + ConversationType.Mpim + }).ConfigureAwait(false); foreach (var conversation in response.Channels) _conversations[conversation.Id] = Task.FromResult(conversation); diff --git a/SlackNet.Tests/SlackBotTests.cs b/SlackNet.Tests/SlackBotTests.cs index 6b71a33..1c74b2a 100644 --- a/SlackNet.Tests/SlackBotTests.cs +++ b/SlackNet.Tests/SlackBotTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Reactive.Subjects; @@ -207,7 +208,7 @@ public void GetConversationByName_FindsChannelWithMatchingName_AndCaches() { var expectedConversation = new Conversation { Id = "C1", Name = "foo"}; var otherConversation = new Conversation { Id = "C2", Name = "bar" }; - _api.Conversations.List().Returns(ConversationList(otherConversation, expectedConversation)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(otherConversation, expectedConversation)); _sut.GetConversationByName("#foo") .ShouldComplete() @@ -215,7 +216,7 @@ public void GetConversationByName_FindsChannelWithMatchingName_AndCaches() _sut.GetConversationByName("foo") .ShouldComplete() .And.ShouldBe(expectedConversation); - _api.Conversations.Received(1).List(); + _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -225,7 +226,7 @@ public void GetConversationByName_UserName_OpensImWithUser_AndCaches() var otherUser = new User { Id = "U2", Name = "bar" }; _api.Users.List().Returns(new UserListResponse { Members = { otherUser, matchingUser } }); var expectedIm = new Conversation { Id = "D123", User = matchingUser.Id, IsIm = true }; - _api.Conversations.List().Returns(ConversationList()); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList()); _api.Conversations.OpenAndReturnInfo(UserIds(matchingUser.Id)).Returns(new ConversationOpenResponse { Channel = expectedIm }); _sut.GetConversationByName("@foo") @@ -242,7 +243,7 @@ public void GetConversationByName_UserName_OpensImWithUser_AndCaches() public void GetConversationByUserId_OpensImWithUser_AndCaches() { var expectedIm = new Conversation { Id = "D123", User = "U123", IsIm = true }; - _api.Conversations.List().Returns(ConversationList()); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList()); _api.Conversations.OpenAndReturnInfo(UserIds(expectedIm.User)).Returns(new ConversationOpenResponse { Channel = expectedIm }); _sut.GetConversationByUserId(expectedIm.User) @@ -259,7 +260,7 @@ public void GetConversations_FetchesConversationList_AndCaches() { var conversation1 = new Conversation { Id = "C1" }; var conversation2 = new Conversation { Id = "C2" }; - _api.Conversations.List().Returns(ConversationList(conversation1, conversation2)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(conversation1, conversation2)); _sut.GetConversations() .ShouldComplete() @@ -267,7 +268,7 @@ public void GetConversations_FetchesConversationList_AndCaches() _sut.GetConversations() .ShouldComplete() .And.ShouldOnlyContain(new[] { conversation1, conversation2 }); - _api.Conversations.Received(1).List(); + _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -276,8 +277,8 @@ public void GetConversations_FetchesAllPages() var conversation1 = new Conversation { Id = "C1" }; var conversation2 = new Conversation { Id = "C2" }; var page2Cursor = "next cursor"; - _api.Conversations.List().Returns(new ConversationListResponse { Channels = new[] { conversation1 }, ResponseMetadata = new ResponseMetadata { NextCursor = page2Cursor } }); - _api.Conversations.List(cursor: page2Cursor).Returns(new ConversationListResponse { Channels = new[] { conversation2 }, ResponseMetadata = new ResponseMetadata() }); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(new ConversationListResponse { Channels = new[] { conversation1 }, ResponseMetadata = new ResponseMetadata { NextCursor = page2Cursor } }); + _api.Conversations.List(cursor: page2Cursor, types: IsOfAllConversationTypes()).Returns(new ConversationListResponse { Channels = new[] { conversation2 }, ResponseMetadata = new ResponseMetadata() }); _sut.GetConversations() .ShouldComplete() @@ -285,7 +286,7 @@ public void GetConversations_FetchesAllPages() _sut.GetConversations() .ShouldComplete() .And.ShouldOnlyContain(new[] { conversation1, conversation2 }); - _api.Conversations.Received(1).List(); + _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -545,7 +546,7 @@ public void GetHubByName_ChannelName_FindsChannelWithMatchingName() { var expectedChannel = new Conversation { Id = "C1", Name = "foo", IsChannel = true }; var otherChannel = new Conversation { Id = "C2", Name = "bar", IsChannel = true }; - _api.Conversations.List().Returns(ConversationList(otherChannel, expectedChannel)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(otherChannel, expectedChannel)); _sut.GetHubByName("#foo") .ShouldComplete() @@ -577,7 +578,7 @@ public void GetHubByName_GroupName_FindsGroupWithMatchingName() { var expectedGroup = new Conversation { Id = "G1", Name = "foo", IsGroup = true }; var otherGroup = new Conversation { Id = "G2", Name = "bar", IsGroup = true }; - _api.Conversations.List().Returns(ConversationList(otherGroup, expectedGroup)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(otherGroup, expectedGroup)); _sut.GetHubByName("foo") .ShouldComplete() @@ -590,7 +591,7 @@ public async Task GetChannelByName_FindsChannelWithMatchingName_AndCaches() { var expectedChannel = new Conversation { Id = "C1", Name = "foo", IsChannel = true }; var otherChannel = new Conversation { Id = "C2", Name = "bar", IsChannel = true }; - _api.Conversations.List().Returns(ConversationList(otherChannel, expectedChannel)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(otherChannel, expectedChannel)); var result = await _sut.GetChannelByName("#foo"); @@ -599,7 +600,7 @@ public async Task GetChannelByName_FindsChannelWithMatchingName_AndCaches() _sut.GetChannelByName("foo") .ShouldComplete() .And.ShouldBe(result); - await _api.Conversations.Received(1).List(); + await _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -631,7 +632,7 @@ public async Task GetGroupByName_FindsGroupWithMatchingName_AndCaches() { var expectedGroup = new Conversation { Id = "G1", Name = "foo", IsGroup = true }; var otherGroup = new Conversation { Id = "G2", Name = "bar", IsGroup = true }; - _api.Conversations.List().Returns(ConversationList(otherGroup, expectedGroup)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(otherGroup, expectedGroup)); var result = await _sut.GetGroupByName("foo"); @@ -640,7 +641,7 @@ public async Task GetGroupByName_FindsGroupWithMatchingName_AndCaches() _sut.GetGroupByName("foo") .ShouldComplete() .And.ShouldBe(result); - await _api.Conversations.Received(1).List(); + await _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -665,7 +666,7 @@ public async Task GetChannels_FetchesChannelList_AndCaches() var channel1 = new Conversation { Id = "C1", IsChannel = true }; var channel2 = new Conversation { Id = "C2", IsChannel = true }; var notAChannel = new Conversation { Id = "D1", IsIm = true }; - _api.Conversations.List().Returns(ConversationList(channel1, channel2, notAChannel)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(channel1, channel2, notAChannel)); var results = await _sut.GetChannels(); @@ -674,7 +675,7 @@ public async Task GetChannels_FetchesChannelList_AndCaches() _sut.GetChannels() .ShouldComplete() .And.ShouldOnlyContain(results); - await _api.Conversations.Received(1).List(); + await _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -683,7 +684,7 @@ public async Task GetGroups_FetchesGroupList_AndCaches() var group1 = new Conversation { Id = "G1", IsGroup = true }; var group2 = new Conversation { Id = "G2", IsGroup = true }; var notAGroup = new Conversation { Id = "C1", IsChannel = true }; - _api.Conversations.List().Returns(ConversationList(group1, group2, notAGroup)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(group1, group2, notAGroup)); var results = await _sut.GetGroups(); @@ -692,7 +693,7 @@ public async Task GetGroups_FetchesGroupList_AndCaches() _sut.GetGroups() .ShouldComplete() .And.ShouldOnlyContain(results); - await _api.Conversations.Received(1).List(); + await _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -701,7 +702,7 @@ public async Task GetMpIms_FetchesMpImList_AndCaches() var mpim1 = new Conversation { Id = "G1", IsMpim = true }; var mpim2 = new Conversation { Id = "G2", IsMpim = true }; var notAnMpim = new Conversation { Id = "C1", IsChannel = true }; - _api.Conversations.List().Returns(ConversationList(mpim1, mpim2, notAnMpim)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(mpim1, mpim2, notAnMpim)); var results = await _sut.GetMpIms(); @@ -710,7 +711,7 @@ public async Task GetMpIms_FetchesMpImList_AndCaches() _sut.GetMpIms() .ShouldComplete() .And.ShouldOnlyContain(results); - await _api.Conversations.Received(1).List(); + await _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } [Test] @@ -719,7 +720,7 @@ public async Task GetIms_FetchesOpenIms_AndCaches() var im1 = new Conversation { Id = "D1", IsIm = true }; var im2 = new Conversation { Id = "D2", IsIm = true }; var notAnIm = new Conversation { Id = "G1", IsMpim = true }; - _api.Conversations.List().Returns(ConversationList(im1, im2, notAnIm)); + _api.Conversations.List(types: IsOfAllConversationTypes()).Returns(ConversationList(im1, im2, notAnIm)); var results = await _sut.GetIms(); @@ -728,7 +729,7 @@ public async Task GetIms_FetchesOpenIms_AndCaches() _sut.GetIms() .ShouldComplete() .And.ShouldOnlyContain(results); - await _api.Conversations.Received(1).List(); + await _api.Conversations.Received(1).List(types: IsOfAllConversationTypes()); } #endregion @@ -751,6 +752,22 @@ private static ConversationListResponse ConversationList(params Conversation[] c private static string[] UserIds(params string[] userIds) => Arg.Is(us => us.SequenceEqual(userIds)); + + private static IEnumerable IsOfAllConversationTypes(params ConversationType[] conversationTypes) + { + if (conversationTypes == null) + { + conversationTypes = new[] + { + ConversationType.PrivateChannel, + ConversationType.Mpim, + ConversationType.PublicChannel, + ConversationType.Im + }; + } + + return Arg.Is>(types => conversationTypes.All(types.Contains)); + } } }