Skip to content

Commit

Permalink
FIX channel history browsing
Browse files Browse the repository at this point in the history
(see issue #49)
  • Loading branch information
bcorne committed Mar 7, 2016
1 parent ceee9f6 commit 61547ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

public interface SlackChannel
{

public enum SlackChannelType {
PUBLIC_CHANNEL, PRIVATE_GROUP, INSTANT_MESSAGING
}

String getId();

String getName();
Expand All @@ -16,4 +21,6 @@ public interface SlackChannel

boolean isDirect();

SlackChannelType getType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ullink.slack.simpleslackapi.SlackChannel;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.threeten.bp.LocalDate;
Expand All @@ -25,6 +27,8 @@ public class ChannelHistoryModuleImpl implements ChannelHistoryModule {

private final SlackSession session;
private static final String FETCH_CHANNEL_HISTORY_COMMAND = "channels.history";
private static final String FETCH_GROUP_HISTORY_COMMAND = "groups.history";
private static final String FETCH_IM_HISTORY_COMMAND = "im.history";

public ChannelHistoryModuleImpl(SlackSession session) {
this.session = session;
Expand Down Expand Up @@ -60,11 +64,19 @@ public List<SlackMessagePosted> fetchHistoryOfChannel(String channelId, LocalDat
} else {
params.put("count", String.valueOf(1000));
}
return fetchHistoryOfChannel(params);
SlackChannel channel =session.findChannelById(channelId);
switch (channel.getType()) {
case INSTANT_MESSAGING:
return fetchHistoryOfChannel(params,FETCH_IM_HISTORY_COMMAND);
case PRIVATE_GROUP:
return fetchHistoryOfChannel(params,FETCH_GROUP_HISTORY_COMMAND);
default:
return fetchHistoryOfChannel(params,FETCH_CHANNEL_HISTORY_COMMAND);
}
}

private List<SlackMessagePosted> fetchHistoryOfChannel(Map<String, String> params) {
SlackMessageHandle<GenericSlackReply> handle = session.postGenericSlackCommand(params, FETCH_CHANNEL_HISTORY_COMMAND);
private List<SlackMessagePosted> fetchHistoryOfChannel(Map<String, String> params, String command) {
SlackMessageHandle<GenericSlackReply> handle = session.postGenericSlackCommand(params, command);
GenericSlackReply replyEv = handle.getReply();
JSONObject answer = replyEv.getPlainAnswer();
JSONArray events = (JSONArray) answer.get("messages");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ public String getPurpose()
public boolean isDirect() {
return direct;
}

@Override
public SlackChannelType getType()
{
//that's a bit hacky
if (isDirect()) {
return SlackChannelType.INSTANT_MESSAGING;
}
if (id.startsWith("G")) {
return SlackChannelType.PRIVATE_GROUP;
}
return SlackChannelType.PUBLIC_CHANNEL;
}
}

0 comments on commit 61547ec

Please # to comment.