From a54141071624effb65f6f711a1c5cbed71fdf680 Mon Sep 17 00:00:00 2001 From: Benoit Corne Date: Mon, 22 Aug 2016 19:12:12 +0200 Subject: [PATCH] Separating integrations from users --- .../slack/simpleslackapi/SlackSession.java | 2 + .../impl/AbstractSlackSessionImpl.java | 6 + .../impl/SlackIntegrationUser.java | 127 ++++++++++++++++++ .../impl/SlackJSONMessageParser.java | 10 ++ .../impl/SlackJSONSessionStatusParser.java | 2 - .../impl/SlackWebSocketSessionImpl.java | 1 - .../impl/TestSlackJSONMessageParser.java | 19 +++ .../TestSlackJSONSessionStatusParser.java | 2 +- 8 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackIntegrationUser.java diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/SlackSession.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/SlackSession.java index 00392626..80d8f0b4 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/SlackSession.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/SlackSession.java @@ -25,6 +25,8 @@ public interface SlackSession { SlackChannel findChannelById(String channelId); + SlackIntegration findIntegrationById(String integrationId); + SlackUser findUserById(String userId); SlackUser findUserByUserName(String userName); diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java index 3c36cd1f..6d19d9ec 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/AbstractSlackSessionImpl.java @@ -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() { diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackIntegrationUser.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackIntegrationUser.java new file mode 100644 index 00000000..60a0b6ed --- /dev/null +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackIntegrationUser.java @@ -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; + } +} diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java index 1e67125f..1900560c 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java @@ -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.*; @@ -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 reacs = extractReactionsFromMessageJSON(obj); ArrayList attachments = extractAttachmentsFromMessageJSON(obj); SlackMessagePostedImpl message = new SlackMessagePostedImpl(text, null, user, channel, ts, null, obj, SlackMessagePosted.MessageSubType.fromCode(subtype)); diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONSessionStatusParser.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONSessionStatusParser.java index af245766..31ea8132 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONSessionStatusParser.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONSessionStatusParser.java @@ -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); } } diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java index d37d8787..5ed4d548 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackWebSocketSessionImpl.java @@ -586,7 +586,6 @@ public SlackMessageHandle openMultipartyDirectMessageChannel( return handle; } - public SlackMessageHandle listEmoji() { SlackMessageHandleImpl handle = new SlackMessageHandleImpl<>(getNextMessageId()); Map arguments = new HashMap<>(); diff --git a/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONMessageParser.java b/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONMessageParser.java index eb80f6ea..f49b4793 100644 --- a/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONMessageParser.java +++ b/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONMessageParser.java @@ -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\"}}}"; @@ -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); @@ -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(); diff --git a/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONSessionStatusParser.java b/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONSessionStatusParser.java index 44219f07..8bfa4bda 100644 --- a/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONSessionStatusParser.java +++ b/sources/src/test/java/com/ullink/slack/simpleslackapi/impl/TestSlackJSONSessionStatusParser.java @@ -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");