Skip to content

Commit

Permalink
Merge pull request #109 from redcraft/master
Browse files Browse the repository at this point in the history
Added integrations support
  • Loading branch information
bcorne authored Aug 3, 2016
2 parents b9a8a3d + 04d2b29 commit 987e2ae
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ullink.slack.simpleslackapi;

public interface SlackIntegration {
String getId();
String getName();
boolean isDeleted();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ enum SlackPresence { UNKNOWN, ACTIVE, AWAY, AUTO }
String getTimeZone();
String getTimeZoneLabel();
Integer getTimeZoneOffset();
SlackPresence getPresence();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public interface SlackSession {

Collection<SlackBot> getBots();

Collection<SlackIntegration> getIntegrations();

SlackChannel findChannelByName(String channelName);

SlackChannel findChannelById(String channelId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ abstract class AbstractSlackSessionImpl implements SlackSession

protected Map<String, SlackChannel> channels = new HashMap<>();
protected Map<String, SlackUser> users = new HashMap<>();
protected Map<String, SlackIntegration> integrations = new HashMap<>();
protected SlackPersona sessionPersona;
protected SlackTeam team;

Expand Down Expand Up @@ -55,6 +56,11 @@ public Collection<SlackUser> getUsers()
return new ArrayList<>(users.values());
}

@Override
public Collection<SlackIntegration> getIntegrations() {
return new ArrayList<>(integrations.values());
}

@Override
@Deprecated
public Collection<SlackBot> getBots()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
class SlackBotImpl extends SlackPersonaImpl implements SlackBot
{
SlackBotImpl(String id, String userName, String realName, String userMail, String userSkype, String userPhone, String userTitle,
boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted, boolean ultraRestricted)
boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted, boolean ultraRestricted, SlackPresence presence)
{
super(id, userName, realName, userMail, userSkype, userPhone, userTitle, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, true,null,null,0);
super(id, userName, realName, userMail, userSkype, userPhone, userTitle, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, true,null,null,0, presence);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ullink.slack.simpleslackapi.impl;

import com.ullink.slack.simpleslackapi.SlackIntegration;

class SlackIntegrationImpl implements SlackIntegration {

private final String id;
private final String name;
private final boolean deleted;

public SlackIntegrationImpl(String id, String name, boolean deleted) {
this.id = id;
this.name = name;
this.deleted = deleted;
}

@Override
public String getId() {
return id;
}

@Override
public String getName() {
return name;
}

@Override
public boolean isDeleted() {
return deleted;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.ullink.slack.simpleslackapi.impl;

import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.ullink.slack.simpleslackapi.SlackIntegration;
import com.ullink.slack.simpleslackapi.SlackPersona;
import com.ullink.slack.simpleslackapi.SlackTeam;
import com.ullink.slack.simpleslackapi.SlackUser;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.util.Map;

class SlackJSONParsingUtils {

Expand Down Expand Up @@ -39,7 +42,18 @@ static final SlackUser buildSlackUser(JSONObject jsonUser)
title = (String) profileJSON.get("title");
phone = (String) profileJSON.get("phone");
}
return new SlackUserImpl(id, name, realName, email, skype, title, phone, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, bot, tz, tzLabel, tzOffset == null ? null : new Integer(tzOffset.intValue()));

String presence = (String) jsonUser.get("presence");
SlackPersona.SlackPresence slackPresence = SlackPersona.SlackPresence.UNKNOWN;
if ("active".equals(presence))
{
slackPresence = SlackPersona.SlackPresence.ACTIVE;
}
if ("away".equals(presence))
{
slackPresence = SlackPersona.SlackPresence.AWAY;
}
return new SlackUserImpl(id, name, realName, email, skype, title, phone, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, bot, tz, tzLabel, tzOffset == null ? null : new Integer(tzOffset.intValue()), slackPresence);
}

private static Boolean ifNullFalse(JSONObject jsonUser, String field) {
Expand Down Expand Up @@ -98,4 +112,11 @@ static final SlackTeam buildSlackTeam(JSONObject jsonTeam) {
String domain = (String) jsonTeam.get("domain");
return new SlackTeamImpl(id, name, domain);
}

static final SlackIntegration buildSlackIntegration(JSONObject jsonIntegration) {
String id = (String) jsonIntegration.get("id");
String name = (String) jsonIntegration.get("name");
boolean deleted = ifNullFalse(jsonIntegration, "deleted");
return new SlackIntegrationImpl(id, name, deleted);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.ullink.slack.simpleslackapi.impl;

import java.util.HashMap;
import java.util.Map;
import com.ullink.slack.simpleslackapi.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ullink.slack.simpleslackapi.SlackChannel;
import com.ullink.slack.simpleslackapi.SlackPersona;
import com.ullink.slack.simpleslackapi.SlackTeam;
import com.ullink.slack.simpleslackapi.SlackUser;

import java.util.HashMap;
import java.util.Map;

class SlackJSONSessionStatusParser {
private static final Logger LOGGER = LoggerFactory.getLogger(SlackJSONSessionStatusParser.class);
private static final Logger LOGGER = LoggerFactory.getLogger(SlackJSONSessionStatusParser.class);

private Map<String, SlackChannel> channels = new HashMap<>();
private Map<String, SlackUser> users = new HashMap<>();
private Map<String, SlackChannel> channels = new HashMap<>();
private Map<String, SlackUser> users = new HashMap<>();
private Map<String, SlackIntegration> integrations = new HashMap<>();

private SlackPersona sessionPersona;

Expand All @@ -44,7 +43,12 @@ Map<String, SlackUser> getUsers()
return users;
}

public String getWebSocketURL() {
Map<String,SlackIntegration> getIntegrations() {
return integrations;
}

public String getWebSocketURL()
{
return webSocketURL;
}

Expand Down Expand Up @@ -72,14 +76,14 @@ void parse() throws ParseException {
users.put(slackUser.getId(), slackUser);
}

JSONArray botsJson = (JSONArray) jsonResponse.get("bots");
if (botsJson != null) {
for (Object jsonObject : botsJson)
JSONArray integrationsJson = (JSONArray) jsonResponse.get("bots");
if (integrationsJson != null) {
for (Object jsonObject : integrationsJson)
{
JSONObject jsonBot = (JSONObject) jsonObject;
SlackUser slackUser = SlackJSONParsingUtils.buildSlackUser(jsonBot);
LOGGER.debug("slack bot found : " + slackUser.getId());
users.put(slackUser.getId(), slackUser);
JSONObject jsonIntegration = (JSONObject) jsonObject;
SlackIntegration slackIntegration = SlackJSONParsingUtils.buildSlackIntegration(jsonIntegration);
LOGGER.debug("slack integration found : " + slackIntegration.getId());
integrations.put(slackIntegration.getId(), slackIntegration);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ class SlackPersonaImpl implements SlackPersona {
final String timeZone;
final String timeZoneLabel;
final Integer timeZoneOffset;
final SlackPresence presence;

SlackPersonaImpl(String id, String userName, String realName, String userMail, String userSkype, String userPhone, String userTitle,
boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted,
boolean ultraRestricted, boolean bot, String timeZone, String timeZoneLabel, Integer timeZoneOffset) {
boolean ultraRestricted, boolean bot, String timeZone, String timeZoneLabel, Integer timeZoneOffset,
SlackPresence presence) {
this.id = id;
this.userName = userName;
this.realName = realName;
Expand All @@ -41,6 +43,7 @@ class SlackPersonaImpl implements SlackPersona {
this.timeZone = timeZone;
this.timeZoneLabel = timeZoneLabel;
this.timeZoneOffset = timeZoneOffset;
this.presence = presence;
}

@Override
Expand Down Expand Up @@ -142,4 +145,9 @@ public Integer getTimeZoneOffset()
return timeZoneOffset;
}

@Override
public SlackPresence getPresence() {
return presence;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ public String toString()

SlackUserImpl(String id, String userName, String realName, String userMail, String userSkype, String userTitle, String userPhone,
boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted,
boolean ultraRestricted, boolean bot, String timeZone, String timeZoneLabel, Integer timeZoneOffset)
boolean ultraRestricted, boolean bot, String timeZone, String timeZoneLabel, Integer timeZoneOffset,
SlackPresence slackPresence)
{
super(id, userName, realName, userMail, userSkype, userPhone, userTitle, deleted, admin, owner, primaryOwner,
restricted, ultraRestricted, bot, timeZone, timeZoneLabel, timeZoneOffset);
restricted, ultraRestricted, bot, timeZone, timeZoneLabel, timeZoneOffset, slackPresence);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ private void connectImpl() throws IOException, ClientProtocolException, ConnectE
}

users = sessionParser.getUsers();
integrations = sessionParser.getIntegrations();
channels = sessionParser.getChannels();
sessionPersona = sessionParser.getSessionPersona();
team = sessionParser.getTeam();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void connect()
channels.put("channelid4",new SlackChannelImpl("channelid4", "testchannel4", "topicchannel4", "topicchannel4", false, false));
channels.put("channelid5",new SlackChannelImpl("channelid5", "testchannel5", "topicchannel5", "topicchannel5", false, false));

users.put("userid1",new SlackUserImpl("userid1", "username1", "realname1","[email protected]", "testSkype", "testPhone", "testTitle", false,false,false,false,false,false, false,"tz","tzLabel",new Integer(0)));
users.put("userid2",new SlackUserImpl("userid2", "username2", "realname2","[email protected]", "testSkype", "testPhone", "testTitle", false,false,false,false,false,false, false,"tz","tzLabel",new Integer(0)));
users.put("userid3",new SlackUserImpl("userid3", "username3", "realname3","[email protected]", "testSkype", "testPhone", "testTitle", true,false,false,false,false,false, false,"tz","tzLabel",new Integer(0)));
users.put("userid4",new SlackUserImpl("userid4", "username4", "realname4","[email protected]", "testSkype", "testPhone", "testTitle", false,false,false,false,false,false, false,"tz","tzLabel",new Integer(0)));
users.put("userid5",new SlackUserImpl("userid5", "username5", "realname4","[email protected]", "testSkype", "testPhone", "testTitle", true,false,false,false,false,false, false,"tz","tzLabel",new Integer(0)));

users.put("botid1",new SlackUserImpl("botid1", "botname1", "real bot name 1", null, "testSkype", "testPhone", "testTitle", false,false,false,false,false,false,true,"tz","tzLabel",new Integer(0)));
users.put("botid2",new SlackUserImpl("botid2", "botname2", "real bot name 2", null, "testSkype", "testPhone", "testTitle", false,false,false,false,false,false,true,"tz","tzLabel",new Integer(0)));
users.put("botid3",new SlackUserImpl("botid3", "botname3", "real bot name 3", null, "testSkype", "testPhone", "testTitle", true,false,false,false,false,false,true,"tz","tzLabel",new Integer(0)));
users.put("userid1",new SlackUserImpl("userid1", "username1", "realname1","[email protected]", "testSkype", "testPhone", "testTitle", false,false,false,false,false,false, false,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
users.put("userid2",new SlackUserImpl("userid2", "username2", "realname2","[email protected]", "testSkype", "testPhone", "testTitle", false,false,false,false,false,false, false,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
users.put("userid3",new SlackUserImpl("userid3", "username3", "realname3","[email protected]", "testSkype", "testPhone", "testTitle", true,false,false,false,false,false, false,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
users.put("userid4",new SlackUserImpl("userid4", "username4", "realname4","[email protected]", "testSkype", "testPhone", "testTitle", false,false,false,false,false,false, false,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
users.put("userid5",new SlackUserImpl("userid5", "username5", "realname4","[email protected]", "testSkype", "testPhone", "testTitle", true,false,false,false,false,false, false,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));

users.put("botid1",new SlackUserImpl("botid1", "botname1", "real bot name 1", null, "testSkype", "testPhone", "testTitle", false,false,false,false,false,false,true,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
users.put("botid2",new SlackUserImpl("botid2", "botname2", "real bot name 2", null, "testSkype", "testPhone", "testTitle", false,false,false,false,false,false,true,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
users.put("botid3",new SlackUserImpl("botid3", "botname3", "real bot name 3", null, "testSkype", "testPhone", "testTitle", true,false,false,false,false,false,true,"tz","tzLabel",new Integer(0), SlackPersona.SlackPresence.ACTIVE));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public void setHeartbeat(long heartbeat, TimeUnit unit) {

@Override
public void connect() {
SlackUser user1 = new SlackUserImpl("TESTUSER1", "test user 1", "", "", "testSkype", "testPhone", "testTitle", false, false, false, false, false, false, false, "tz", "tzLabel", new Integer(0));
SlackUser user2 = new SlackUserImpl("TESTUSER2", "test user 2", "", "", "testSkype", "testPhone", "testTitle", false, false, false, false, false, false, false, "tz", "tzLabel", new Integer(0));
SlackUser user3 = new SlackUserImpl("TESTUSER3", "test user 3", "", "", "testSkype", "testPhone", "testTitle", false, false, false, false, false, false, false, "tz", "tzLabel", new Integer(0));
SlackUser user1 = new SlackUserImpl("TESTUSER1", "test user 1", "", "", "testSkype", "testPhone", "testTitle", false, false, false, false, false, false, false, "tz", "tzLabel", new Integer(0), SlackPersona.SlackPresence.ACTIVE);
SlackUser user2 = new SlackUserImpl("TESTUSER2", "test user 2", "", "", "testSkype", "testPhone", "testTitle", false, false, false, false, false, false, false, "tz", "tzLabel", new Integer(0), SlackPersona.SlackPresence.ACTIVE);
SlackUser user3 = new SlackUserImpl("TESTUSER3", "test user 3", "", "", "testSkype", "testPhone", "testTitle", false, false, false, false, false, false, false, "tz", "tzLabel", new Integer(0), SlackPersona.SlackPresence.ACTIVE);
users.put(user1.getId(), user1);
users.put(user2.getId(), user2);
users.put(user3.getId(), user3);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.ullink.slack.simpleslackapi.impl;

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class TestSlackJSONSessionStatusParser
{
Expand Down Expand Up @@ -36,5 +38,10 @@ public void testParsingSessionDescription() throws Exception
assertThat(parser.getTeam().getId()).isEqualTo("TEAM");
assertThat(parser.getTeam().getName()).isEqualTo("Example Team");
assertThat(parser.getTeam().getDomain()).isEqualTo("example");

assertThat(parser.getIntegrations().get("INTEGRATION1").getName()).isEqualTo("bot1");
assertThat(parser.getIntegrations().get("INTEGRATION1").isDeleted()).isEqualTo(false);
assertThat(parser.getIntegrations().get("INTEGRATION2").getName()).isEqualTo("bot2");
assertThat(parser.getIntegrations().get("INTEGRATION2").isDeleted()).isEqualTo(true);
}
}
12 changes: 12 additions & 0 deletions sources/src/test/resources/test_json.json
Original file line number Diff line number Diff line change
Expand Up @@ -272,5 +272,17 @@
"type": "service"
}
},
"bots": [
{
"id": "INTEGRATION1",
"deleted": false,
"name": "bot1"
},
{
"id": "INTEGRATION2",
"deleted": true,
"name": "bot2"
}
],
"url": "wss:\/\/mywebsocketurl"
}

0 comments on commit 987e2ae

Please sign in to comment.