From 74bc037657f9fe170324e294aee1f2529988dea9 Mon Sep 17 00:00:00 2001 From: apresence Date: Sun, 25 Oct 2020 10:37:18 -0400 Subject: [PATCH] Alternative to pr/251. That method uses an enum with a function to translate the names. My implementation names the enum values the actual API string values which eliminates the need for the extra ConversationTypesToQueryParam() function. For example, instead of: Tuple.Create("types", Conversation.ConversationTypesToQueryParam(types)); We can leave this unchanged: parameters.Add(Tuple.Create("types", string.Join(",", types))); This works because when string.Join translate types[] into strings, it calls ToString() which by default returns the enum element name. This is also nice because we don't have to update the ConversationTypesToQueryParam() function whenever elements are added or removed from the enum. If there is some kind of naming standard violation here, (e.g. The first enum element should be PublicChannel instead of public_channel), yet another alternative would be to use a class instead of an enum. For example: public static class Type { public static readonly string PublicChannel = "public_channel"; public static readonly string PrivateChannel = "private_channel"; ... } There is a detailed discussion on using friendly strings with enums on Stack Overflow here: https://stackoverflow.com/questions/479410/enum-tostring-with-user-friendly-strings --- SlackAPI/Conversation.cs | 10 +++++++++- SlackAPI/SlackClient.cs | 4 ++-- SlackAPI/SlackTaskClient.cs | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/SlackAPI/Conversation.cs b/SlackAPI/Conversation.cs index def8d1a2..4f7d46e8 100644 --- a/SlackAPI/Conversation.cs +++ b/SlackAPI/Conversation.cs @@ -8,6 +8,14 @@ namespace SlackAPI { public class Conversation { + public enum Type + { + public_channel, + private_channel, + mpim, + im + } + public string id; public DateTime created; public DateTime last_read; @@ -16,4 +24,4 @@ public class Conversation public int unread_count; public Message latest; } -} +} \ No newline at end of file diff --git a/SlackAPI/SlackClient.cs b/SlackAPI/SlackClient.cs index eae70331..ae97c31e 100644 --- a/SlackAPI/SlackClient.cs +++ b/SlackAPI/SlackClient.cs @@ -147,7 +147,7 @@ public void ChannelsInvite(Action callback, string userId APIRequestWithToken(callback, parameters.ToArray()); } - public void GetConversationsList(Action callback, string cursor = "", bool ExcludeArchived = true, int limit = 100, string[] types = null) + public void GetConversationsList(Action callback, string cursor = "", bool ExcludeArchived = true, int limit = 100, Conversation.Type[] types = null) { List> parameters = new List>() { @@ -155,7 +155,7 @@ public void GetConversationsList(Action callback, str }; if (limit > 0) parameters.Add(Tuple.Create("limit", limit.ToString())); - if (types.Any()) + if (types != null && types.Any()) parameters.Add(Tuple.Create("types", string.Join(",", types))); if (!string.IsNullOrEmpty(cursor)) parameters.Add(Tuple.Create("cursor", cursor)); diff --git a/SlackAPI/SlackTaskClient.cs b/SlackAPI/SlackTaskClient.cs index c6815d26..f66abcfe 100644 --- a/SlackAPI/SlackTaskClient.cs +++ b/SlackAPI/SlackTaskClient.cs @@ -127,7 +127,7 @@ public Task ChannelsInviteAsync(string userId, string cha return APIRequestWithTokenAsync(parameters.ToArray()); } - public Task GetConversationsListAsync(string cursor = "", bool ExcludeArchived = true, int limit = 100, string[] types = null) + public Task GetConversationsListAsync(string cursor = "", bool ExcludeArchived = true, int limit = 100, Conversation.Type[] types = null) { List> parameters = new List>() {