Skip to content

Commit

Permalink
Expand use of Conversations to user-friendly enum and
Browse files Browse the repository at this point in the history
Expands the use of conversations to use an enum when specifying request instead of pure string. Expands on Inumedia#250
  • Loading branch information
alexherreid committed Sep 11, 2020
1 parent 06f2689 commit b5546bf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
52 changes: 42 additions & 10 deletions SlackAPI/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,46 @@

namespace SlackAPI
{
public class Conversation
{
public string id;
public DateTime created;
public DateTime last_read;
public bool is_open;
public bool is_starred;
public int unread_count;
public Message latest;
}
public class Conversation
{
public string id;
public DateTime created;
public DateTime last_read;
public bool is_open;
public bool is_starred;
public int unread_count;
public Message latest;

public static string ConversationTypesToQueryParam(ConversationTypes[] types)
{
//Translate the enum user-friendly names to API used names
List<string> typesAsString = new List<string>();
if (types.Contains(ConversationTypes.PublicChannel))
{
typesAsString.Add("public_channel");
}
if (types.Contains(ConversationTypes.PrivateChannel))
{
typesAsString.Add("private_channel");
}
if (types.Contains(ConversationTypes.GroupMessage))
{
typesAsString.Add("mpim");
}
if (types.Contains(ConversationTypes.DirectMessage))
{
typesAsString.Add("im");
}

return string.Join(",", types);
}
}

public enum ConversationTypes
{
PublicChannel, //public_channel
PrivateChannel, //private_channel
GroupMessage, //mpim
DirectMessage //im
}
}
6 changes: 3 additions & 3 deletions SlackAPI/SlackClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ public void ChannelsInvite(Action<ChannelInviteResponse> callback, string userId
APIRequestWithToken(callback, parameters.ToArray());
}

public void GetConversationsList(Action<ConversationsListResponse> callback, string cursor = "", bool ExcludeArchived = true, int limit = 100, string[] types = null)
public void GetConversationsList(Action<ConversationsListResponse> callback, string cursor = "", bool ExcludeArchived = true, int limit = 100, ConversationTypes[] types = null)
{
List<Tuple<string, string>> parameters = new List<Tuple<string, string>>()
{
Tuple.Create("exclude_archived", ExcludeArchived ? "1" : "0")
};
if (limit > 0)
Tuple.Create("limit", limit.ToString());
if (types.Any())
Tuple.Create("types", string.Join(",", types));
if (types != null && types.Any())
Tuple.Create("types", Conversation.ConversationTypesToQueryParam(types));
if (!string.IsNullOrEmpty(cursor))
parameters.Add(new Tuple<string, string>("cursor", cursor));

Expand Down
4 changes: 2 additions & 2 deletions SlackAPI/SlackTaskClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public Task<ChannelInviteResponse> ChannelsInviteAsync(string userId, string cha
return APIRequestWithTokenAsync<ChannelInviteResponse>(parameters.ToArray());
}

public Task<ConversationsListResponse> GetConversationsListAsync(string cursor = "", bool ExcludeArchived = true, int limit = 100, string[] types = null)
public Task<ConversationsListResponse> GetConversationsListAsync(string cursor = "", bool ExcludeArchived = true, int limit = 100, ConversationTypes[] types = null)
{
List<Tuple<string, string>> parameters = new List<Tuple<string, string>>()
{
Expand All @@ -136,7 +136,7 @@ public Task<ConversationsListResponse> GetConversationsListAsync(string cursor =
if (limit > 0)
parameters.Add(Tuple.Create("limit", limit.ToString()));
if (types != null && types.Any())
parameters.Add(Tuple.Create("types", string.Join(",", types)));
parameters.Add(Tuple.Create("types", Conversation.ConversationTypesToQueryParam(types)));
if (!string.IsNullOrEmpty(cursor))
parameters.Add(new Tuple<string, string>("cursor", cursor));

Expand Down

0 comments on commit b5546bf

Please sign in to comment.