Skip to content

Commit

Permalink
Separating integrations from users
Browse files Browse the repository at this point in the history
  • Loading branch information
bcorne committed Aug 22, 2016
1 parent ed61f2b commit a541410
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface SlackSession {

SlackChannel findChannelById(String channelId);

SlackIntegration findIntegrationById(String integrationId);

SlackUser findUserById(String userId);

SlackUser findUserByUserName(String userName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ public SlackUser findUserByEmail(String userMail)
return null;
}

@Override
public SlackIntegration findIntegrationById(String integrationId)
{
return integrations.get(integrationId);
}

@Override
public SlackPersona sessionPersona()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.ullink.slack.simpleslackapi.impl;

import com.ullink.slack.simpleslackapi.SlackIntegration;
import com.ullink.slack.simpleslackapi.SlackUser;

class SlackIntegrationUser implements SlackIntegration, SlackUser
{
private SlackIntegration integration;

SlackIntegrationUser(SlackIntegration integration) {
this.integration = integration;
}

@Override
public String getId()
{
return integration.getId();
}

@Override
public String getUserName()
{
return integration.getName();
}

@Override
public String getRealName()
{
return integration.getName();
}

@Override
public String getUserMail()
{
return null;
}

@Override
public String getUserSkype()
{
return null;
}

@Override
public String getUserPhone()
{
return null;
}

@Override
public String getUserTitle()
{
return null;
}

@Override
public String getName()
{
return integration.getName();
}

@Override
public boolean isDeleted()
{
return integration.isDeleted();
}

@Override
public boolean isAdmin()
{
return false;
}

@Override
public boolean isOwner()
{
return false;
}

@Override
public boolean isPrimaryOwner()
{
return false;
}

@Override
public boolean isRestricted()
{
return false;
}

@Override
public boolean isUltraRestricted()
{
return false;
}

@Override
public boolean isBot()
{
return true;
}

@Override
public String getTimeZone()
{
return null;
}

@Override
public String getTimeZoneLabel()
{
return null;
}

@Override
public Integer getTimeZoneOffset()
{
return null;
}

@Override
public SlackPresence getPresence()
{
return SlackPresence.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.IllegalFormatException;
import java.util.Map;

import com.ullink.slack.simpleslackapi.*;
Expand Down Expand Up @@ -209,6 +210,15 @@ private static SlackMessagePostedImpl parseMessagePublished(JSONObject obj, Slac
}
String subtype = (String) obj.get("subtype");
SlackUser user = slackSession.findUserById(userId);
if (user == null) {

SlackIntegration integration = slackSession.findIntegrationById(userId);
if (integration == null) {
throw new IllegalStateException("unknown user id: " + userId);
}
user = new SlackIntegrationUser(integration);

}
Map<String, Integer> reacs = extractReactionsFromMessageJSON(obj);
ArrayList<SlackAttachment> attachments = extractAttachmentsFromMessageJSON(obj);
SlackMessagePostedImpl message = new SlackMessagePostedImpl(text, null, user, channel, ts, null, obj, SlackMessagePosted.MessageSubType.fromCode(subtype));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ void parse() throws ParseException {
for (Object jsonObject : integrationsJson)
{
JSONObject jsonIntegration = (JSONObject) jsonObject;
SlackUser slackUser = SlackJSONParsingUtils.buildSlackUser(jsonIntegration);
SlackIntegration slackIntegration = SlackJSONParsingUtils.buildSlackIntegration(jsonIntegration);
LOGGER.debug("slack integration found : " + slackIntegration.getId());
integrations.put(slackIntegration.getId(), slackIntegration);
users.put(slackUser.getId(), slackUser);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,6 @@ public SlackMessageHandle<SlackChannelReply> openMultipartyDirectMessageChannel(
return handle;
}


public SlackMessageHandle<EmojiSlackReply> listEmoji() {
SlackMessageHandleImpl<EmojiSlackReply> handle = new SlackMessageHandleImpl<>(getNextMessageId());
Map<String, String> arguments = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class TestSlackJSONMessageParser {
SlackSession session;

private static final String TEST_NEW_MESSAGE = "{\"type\":\"message\",\"channel\":\"TESTCHANNEL1\",\"user\":\"TESTUSER1\",\"text\":\"Test text 1\",\"ts\":\"1413187521.000004\"}";
private static final String TEST_NEW_MESSAGE_FROM_INTEGRATION = "{\"type\":\"message\",\"channel\":\"TESTCHANNEL1\",\"bot_id\":\"TESTINTEGRATION1\",\"text\":\"Test text 1\",\"ts\":\"1413187521.000004\"}";
private static final String TEST_DELETED_MESSAGE = "{\"type\":\"message\",\"channel\":\"TESTCHANNEL1\",\"user\":\"TESTUSER1\",\"text\":\"Test text 1\",\"ts\":\"1413187521.000005\", \"subtype\": \"message_deleted\", \"deleted_ts\": \"1358878749.000002\"}";
private static final String TEST_UPDATED_MESSAGE = "{\"type\":\"message\",\"channel\":\"TESTCHANNEL1\",\"text\":\"Test text 1\",\"ts\":\"1358878755.001234\", \"subtype\": \"message_changed\", \"message\": {\"type:\" \"message\", \"user\": \"TESTUSER1\", \"text\": \"newtext\", \"ts\": \"1413187521.000005\", \"edited\": { \"user\": \"TESTUSER1\", \"ts\":\"1358878755.001234\"}}}";

Expand Down Expand Up @@ -62,10 +63,15 @@ 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), 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);

SlackIntegration integration = new SlackIntegrationImpl("TESTINTEGRATION1","integration 1",false);

integrations.put(integration.getId(),integration);

SlackChannel channel1 = new SlackChannelImpl("TESTCHANNEL1", "testchannel1", null, null, false, false);
SlackChannel channel2 = new SlackChannelImpl("TESTCHANNEL2", "testchannel2", null, null, false, false);
SlackChannel channel3 = new SlackChannelImpl("TESTCHANNEL3", "testchannel3", null, null, false, false);
Expand Down Expand Up @@ -215,6 +221,19 @@ public void testParsingNewMessage() throws Exception {
Assertions.assertThat(slackMessage.getTimeStamp()).isEqualTo("1413187521.000004");
}

@Test
public void testParsingNewMessageFromIntegration() throws Exception {
JSONParser parser = new JSONParser();
JSONObject object = (JSONObject) parser.parse(TEST_NEW_MESSAGE_FROM_INTEGRATION);
SlackEvent event = SlackJSONMessageParser.decode(session, object);
Assertions.assertThat(event).isInstanceOf(SlackMessagePosted.class);
SlackMessagePosted slackMessage = (SlackMessagePosted) event;
Assertions.assertThat(slackMessage.getSender().getId()).isEqualTo("TESTINTEGRATION1");
Assertions.assertThat(slackMessage.getChannel().getId()).isEqualTo("TESTCHANNEL1");
Assertions.assertThat(slackMessage.getMessageContent()).isEqualTo("Test text 1");
Assertions.assertThat(slackMessage.getTimeStamp()).isEqualTo("1413187521.000004");
}

@Test
public void testParsingMessageDeleted() throws Exception {
JSONParser parser = new JSONParser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void testParsingSessionDescription() throws Exception
parser.parse();

assertThat(parser.getChannels()).containsOnlyKeys("CHANNELID1", "CHANNELID2", "CHANNELID3", "GROUPID1", "DIM01");
assertThat(parser.getUsers()).containsOnlyKeys("USERID1","USERID2","USERID3","USERID4","BOTID1","BOTID2","INTEGRATION1","INTEGRATION2");
assertThat(parser.getUsers()).containsOnlyKeys("USERID1","USERID2","USERID3","USERID4","BOTID1","BOTID2");
assertThat(parser.getWebSocketURL()).isEqualTo("wss://mywebsocketurl");
assertThat(parser.getUsers().get("USERID1").getTimeZone()).isEqualTo("Europe/Amsterdam");
assertThat(parser.getUsers().get("USERID1").getTimeZoneLabel()).isEqualTo("Central European Summer Time");
Expand Down

0 comments on commit a541410

Please sign in to comment.