Skip to content

Commit

Permalink
FIX listening for channel creation
Browse files Browse the repository at this point in the history
This change is making the slack session to listen to channel
creation in order to have an up to date channel repository.

Change-Id: I1e6cf7d101844bf43903e85558b39c719dc0ea15
  • Loading branch information
Benoit Corne committed Apr 3, 2015
1 parent 9f02bd1 commit bcc8d47
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ullink.slack.simpleslackapi;

public interface SlackChannelCreated
{
SlackChannel getSlackChannel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
abstract class AbstractSlackSessionImpl implements SlackSession
{

protected Map<String, SlackChannel> channels = new HashMap<>();
protected Map<String, SlackUser> users = new HashMap<>();
protected Map<String, SlackBot> bots = new HashMap<>();
protected Map<String, SlackChannel> channels = new HashMap<>();
protected Map<String, SlackUser> users = new HashMap<>();
protected Map<String, SlackBot> bots = new HashMap<>();

protected Set<SlackMessageListener> messageListeners = new HashSet<>();
protected Set<SlackMessageListener> messageListeners = new HashSet<>();

static final SlackChatConfiguration DEFAULT_CONFIGURATION = SlackChatConfiguration.getConfiguration().asUser();

Expand Down Expand Up @@ -59,7 +59,16 @@ public SlackChannel findChannelByName(String channelName)
@Override
public SlackChannel findChannelById(String channelId)
{
return channels.get(channelId);
SlackChannel toReturn = channels.get(channelId);
if (toReturn == null)
{
// direct channel case
if (channelId != null && channelId.startsWith("D"))
{
toReturn = new SlackChannelImpl(channelId, "", "", "");
}
}
return toReturn;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.ullink.slack.simpleslackapi.impl;

import com.ullink.slack.simpleslackapi.SlackChannel;
import com.ullink.slack.simpleslackapi.SlackChannelCreated;

public class SlackChannelCreatedImpl implements SlackChannelCreated
{
private SlackChannel slackChannel;

SlackChannelCreatedImpl(SlackChannel slackChannel)
{
this.slackChannel = slackChannel;
}

@Override
public SlackChannel getSlackChannel()
{
return slackChannel;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.common.io.CharStreams;
import com.ullink.slack.simpleslackapi.SlackAttachment;
import com.ullink.slack.simpleslackapi.SlackChannel;
import com.ullink.slack.simpleslackapi.SlackChannelCreated;
import com.ullink.slack.simpleslackapi.SlackGroupJoined;
import com.ullink.slack.simpleslackapi.SlackMessage;
import com.ullink.slack.simpleslackapi.SlackMessageHandle;
Expand Down Expand Up @@ -402,7 +403,7 @@ else if ("message".equals(type))
}
else if ("group_joined".equals(type))
{
SlackGroupJoined groupJoined = parseGroupJoined(object);
SlackGroupJoined groupJoined = new SlackGroupJoinedImpl(parseChannelDescription(object));
if (groupJoined != null)
{
SlackChannel channel = groupJoined.getSlackChannel();
Expand All @@ -412,14 +413,26 @@ else if ("group_joined".equals(type))
}
}
}
else if ("channel_created".equals(type))
{
SlackChannelCreated channelCreated = new SlackChannelCreatedImpl(parseChannelDescription(object));
if (channelCreated != null)
{
SlackChannel channel = channelCreated.getSlackChannel();
if (channel != null)
{
channels.put(channel.getId(), channel);
}
}
}
}
}

private SlackGroupJoined parseGroupJoined(JSONObject object)
private SlackChannel parseChannelDescription(JSONObject object)
{
JSONObject channel = (JSONObject) object.get("channel");
SlackChannel slackChannel = SlackJSONParsingUtils.buildSlackChannel(channel, users);
return new SlackGroupJoinedImpl(slackChannel);
return slackChannel;
}

private JSONObject parseObject(String json)
Expand Down

0 comments on commit bcc8d47

Please sign in to comment.