Skip to content

Commit

Permalink
Updated FetchConversations to fetch all conversations, not just the d…
Browse files Browse the repository at this point in the history
…efaults.
  • Loading branch information
Silvenga authored and soxtoby committed Sep 2, 2020
1 parent 696658c commit 68de054
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
10 changes: 9 additions & 1 deletion SlackNet.Bot/SlackBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
63 changes: 40 additions & 23 deletions SlackNet.Tests/SlackBotTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
Expand Down Expand Up @@ -207,15 +208,15 @@ 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()
.And.ShouldBe(expectedConversation);
_sut.GetConversationByName("foo")
.ShouldComplete()
.And.ShouldBe(expectedConversation);
_api.Conversations.Received(1).List();
_api.Conversations.Received(1).List(types: IsOfAllConversationTypes());
}

[Test]
Expand All @@ -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")
Expand All @@ -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)
Expand All @@ -259,15 +260,15 @@ 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()
.And.ShouldOnlyContain(new[] { conversation1, conversation2 });
_sut.GetConversations()
.ShouldComplete()
.And.ShouldOnlyContain(new[] { conversation1, conversation2 });
_api.Conversations.Received(1).List();
_api.Conversations.Received(1).List(types: IsOfAllConversationTypes());
}

[Test]
Expand All @@ -276,16 +277,16 @@ 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()
.And.ShouldOnlyContain(new[] { conversation1, conversation2 });
_sut.GetConversations()
.ShouldComplete()
.And.ShouldOnlyContain(new[] { conversation1, conversation2 });
_api.Conversations.Received(1).List();
_api.Conversations.Received(1).List(types: IsOfAllConversationTypes());
}

[Test]
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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");

Expand All @@ -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]
Expand Down Expand Up @@ -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");

Expand All @@ -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]
Expand All @@ -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();

Expand All @@ -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]
Expand All @@ -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();

Expand All @@ -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]
Expand All @@ -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();

Expand All @@ -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]
Expand All @@ -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();

Expand All @@ -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
Expand All @@ -751,6 +752,22 @@ private static ConversationListResponse ConversationList(params Conversation[] c

private static string[] UserIds(params string[] userIds) =>
Arg.Is<string[]>(us => us.SequenceEqual(userIds));

private static IEnumerable<ConversationType> IsOfAllConversationTypes(params ConversationType[] conversationTypes)
{
if (conversationTypes == null)
{
conversationTypes = new[]
{
ConversationType.PrivateChannel,
ConversationType.Mpim,
ConversationType.PublicChannel,
ConversationType.Im
};
}

return Arg.Is<IEnumerable<ConversationType>>(types => conversationTypes.All(types.Contains));
}
}
}

0 comments on commit 68de054

Please sign in to comment.