Skip to content

Commit

Permalink
Merge pull request #300 from nhaquet2/master
Browse files Browse the repository at this point in the history
Add legacyMode to use rtm.connect
  • Loading branch information
bcorne authored Oct 3, 2022
2 parents a3dba2a + 55c79bb commit 75e6e16
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 319 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ All these events can be listen provided your bot has the rights to (IE : the bot
* reaction removed from message event (since v0.5.0)
* team joined event

# Attention

From version 1.4.0 upward, channel members are not loaded in memory anymore in order for the api to start up faster. This should have no impact
on the current behavior.

# Thanks

Expand All @@ -97,6 +101,9 @@ Many thanks to everyone who has contributed to this library :
* Rhys Kenwell
* Aman Gupta
* Miklos Sagi
* Nathalie Mahe
* Sebastien Lelouvier
* Nicolas Haquet

(Let me know if I forgot someone, I'll fix that ASAP ;) )

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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import com.ullink.slack.simpleslackapi.*;
import com.ullink.slack.simpleslackapi.listeners.*;
import com.ullink.slack.simpleslackapi.replies.SlackMessageReply;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

abstract class AbstractSlackSessionImpl implements SlackSession
{

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

protected final List<SlackChannelArchivedListener> channelArchiveListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelCreatedListener> channelCreateListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelDeletedListener> channelDeleteListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelRenamedListener> channelRenamedListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelUnarchivedListener> channelUnarchiveListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelJoinedListener> channelJoinedListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelLeftListener> channelLeftListener = new CopyOnWriteArrayList<>();
protected final List<SlackGroupJoinedListener> groupJoinedListener = new CopyOnWriteArrayList<>();
protected final List<SlackMessageDeletedListener> messageDeletedListener = new CopyOnWriteArrayList<>();
protected final List<SlackMessagePostedListener> messagePostedListener = new CopyOnWriteArrayList<>();
protected final List<SlackMessageUpdatedListener> messageUpdatedListener = new CopyOnWriteArrayList<>();
protected final List<SlackConnectedListener> slackConnectedListener = new CopyOnWriteArrayList<>();
protected final List<ReactionAddedListener> reactionAddedListener = new CopyOnWriteArrayList<>();
protected final List<ReactionRemovedListener> reactionRemovedListener = new CopyOnWriteArrayList<>();
protected final List<SlackUserChangeListener> slackUserChangeListener = new CopyOnWriteArrayList<>();
protected final List<SlackTeamJoinListener> slackTeamJoinListener = new CopyOnWriteArrayList<>();
protected final List<PinAddedListener> pinAddedListener = new CopyOnWriteArrayList<>();
protected final List<PinRemovedListener> pinRemovedListener = new CopyOnWriteArrayList<>();
protected final List<PresenceChangeListener> presenceChangeListener = new CopyOnWriteArrayList<>();
protected final List<SlackDisconnectedListener> slackDisconnectedListener = new CopyOnWriteArrayList<>();
protected final List<UserTypingListener> userTypingListener = new CopyOnWriteArrayList<>();

static final SlackChatConfiguration DEFAULT_CONFIGURATION = SlackChatConfiguration.getConfiguration().asUser();
static final boolean DEFAULT_UNFURL = true;
protected final List<SlackChannelArchivedListener> channelArchiveListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelCreatedListener> channelCreateListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelDeletedListener> channelDeleteListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelRenamedListener> channelRenamedListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelUnarchivedListener> channelUnarchiveListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelJoinedListener> channelJoinedListener = new CopyOnWriteArrayList<>();
protected final List<SlackChannelLeftListener> channelLeftListener = new CopyOnWriteArrayList<>();
protected final List<SlackGroupJoinedListener> groupJoinedListener = new CopyOnWriteArrayList<>();
protected final List<SlackMessageDeletedListener> messageDeletedListener = new CopyOnWriteArrayList<>();
protected final List<SlackMessagePostedListener> messagePostedListener = new CopyOnWriteArrayList<>();
protected final List<SlackMessageUpdatedListener> messageUpdatedListener = new CopyOnWriteArrayList<>();
protected final List<SlackConnectedListener> slackConnectedListener = new CopyOnWriteArrayList<>();
protected final List<ReactionAddedListener> reactionAddedListener = new CopyOnWriteArrayList<>();
protected final List<ReactionRemovedListener> reactionRemovedListener = new CopyOnWriteArrayList<>();
protected final List<SlackUserChangeListener> slackUserChangeListener = new CopyOnWriteArrayList<>();
protected final List<SlackTeamJoinListener> slackTeamJoinListener = new CopyOnWriteArrayList<>();
protected final List<PinAddedListener> pinAddedListener = new CopyOnWriteArrayList<>();
protected final List<PinRemovedListener> pinRemovedListener = new CopyOnWriteArrayList<>();
protected final List<PresenceChangeListener> presenceChangeListener = new CopyOnWriteArrayList<>();
protected final List<SlackDisconnectedListener> slackDisconnectedListener = new CopyOnWriteArrayList<>();
protected final List<UserTypingListener> userTypingListener = new CopyOnWriteArrayList<>();

static final SlackChatConfiguration DEFAULT_CONFIGURATION = SlackChatConfiguration.getConfiguration().asUser();
static final boolean DEFAULT_UNFURL = true;

@Override
public SlackTeam getTeam()
Expand All @@ -61,7 +63,8 @@ public Collection<SlackUser> getUsers()
}

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

Expand Down Expand Up @@ -99,11 +102,7 @@ public SlackChannel findChannelById(String channelId)
SlackChannel toReturn = channels.get(channelId);
if (toReturn == null)
{
// direct channel case
if (channelId != null && channelId.startsWith("D"))
{
toReturn = SlackChannel.builder().id(channelId).name("").topic("").name("").build();
}
toReturn = SlackChannel.builder().id(channelId).name("").topic("").name("").build();
}
return toReturn;
}
Expand Down Expand Up @@ -194,19 +193,30 @@ public SlackMessageHandle<SlackMessageReply> sendMessage(SlackChannel channel, S
}

@Override
public SlackMessageHandle<SlackMessageReply> sendMessage(SlackChannel channel, SlackPreparedMessage preparedMessage) {
public SlackMessageHandle<SlackMessageReply> sendMessage(SlackChannel channel, SlackPreparedMessage preparedMessage)
{
return sendMessage(channel, preparedMessage, DEFAULT_CONFIGURATION);
}

@Override
public SlackMessageHandle<SlackMessageReply> sendMessage(SlackChannel channel, String message, SlackAttachment attachment, SlackChatConfiguration chatConfiguration, boolean unfurl)
{
SlackPreparedMessage preparedMessage = SlackPreparedMessage.builder()
SlackPreparedMessage preparedMessage;
if (attachment != null)
{
preparedMessage = SlackPreparedMessage.builder()
.message(message)
.unfurl(unfurl)
.attachment(attachment)
.build();

}
else
{
preparedMessage = SlackPreparedMessage.builder()
.message(message)
.unfurl(unfurl)
.build();
}
return sendMessage(channel, preparedMessage, chatConfiguration);
}

Expand All @@ -219,11 +229,22 @@ public SlackMessageHandle<SlackMessageReply> sendEphemeralMessage(SlackChannel c
@Override
public SlackMessageHandle<SlackMessageReply> sendEphemeralMessage(SlackChannel channel, SlackUser user, String message, SlackAttachment attachment, SlackChatConfiguration chatConfiguration, boolean unfurl)
{
SlackPreparedMessage preparedMessage = SlackPreparedMessage.builder()
SlackPreparedMessage preparedMessage;
if (attachment != null)
{
preparedMessage = SlackPreparedMessage.builder()
.message(message)
.unfurl(unfurl)
.attachment(attachment)
.build();
}
else
{
preparedMessage = SlackPreparedMessage.builder()
.message(message)
.unfurl(unfurl)
.build();
}

return sendEphemeralMessage(channel, user, preparedMessage, chatConfiguration);
}
Expand All @@ -250,9 +271,9 @@ public SlackMessageHandle<SlackMessageReply> sendEphemeralMessage(SlackChannel c
public SlackMessageHandle<SlackMessageReply> sendEphemeralMessage(SlackChannel channel, SlackUser user, String message, boolean unfurl)
{
SlackPreparedMessage preparedMessage = SlackPreparedMessage.builder()
.message(message)
.unfurl(unfurl)
.build();
.message(message)
.unfurl(unfurl)
.build();
return sendEphemeralMessage(channel, user, preparedMessage, DEFAULT_CONFIGURATION);
}

Expand Down Expand Up @@ -407,12 +428,14 @@ public void removeSlackConnectedListener(SlackConnectedListener listener)
}

@Override
public void addSlackDisconnectedListener(SlackDisconnectedListener listener) {
public void addSlackDisconnectedListener(SlackDisconnectedListener listener)
{
slackDisconnectedListener.add(listener);
}

@Override
public void removeSlackDisconnectedListener(SlackDisconnectedListener listener) {
public void removeSlackDisconnectedListener(SlackDisconnectedListener listener)
{
slackDisconnectedListener.remove(listener);
}

Expand Down Expand Up @@ -489,22 +512,26 @@ public void removePinRemovedListener(PinRemovedListener listener)
}

@Override
public void addPresenceChangeListener(PresenceChangeListener listener) {
public void addPresenceChangeListener(PresenceChangeListener listener)
{
presenceChangeListener.add(listener);
}

@Override
public void removePresenceChangeListener(PresenceChangeListener listener) {
public void removePresenceChangeListener(PresenceChangeListener listener)
{
presenceChangeListener.remove(listener);
}

@Override
public void addUserTypingListener(UserTypingListener listener) {
public void addUserTypingListener(UserTypingListener listener)
{
userTypingListener.add(listener);
}

@Override
public void removeUserTypingListener(UserTypingListener listener) {
public void removeUserTypingListener(UserTypingListener listener)
{
userTypingListener.remove(listener);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.ullink.slack.simpleslackapi.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
Expand All @@ -8,13 +13,6 @@
import com.ullink.slack.simpleslackapi.events.userchange.SlackTeamJoin;
import com.ullink.slack.simpleslackapi.events.userchange.SlackUserChange;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

Expand Down Expand Up @@ -214,29 +212,39 @@ private static SlackMessageDeleted parseMessageDeleted(JsonObject obj, SlackChan
return new SlackMessageDeleted(channel, deletedTs, ts);
}

private static SlackMessagePosted parseMessagePublished(JsonObject obj, SlackChannel channel, String ts, SlackSession slackSession) {
private static SlackMessagePosted parseMessagePublished(JsonObject obj, SlackChannel channel, String ts, SlackSession slackSession)
{
String text = GsonHelper.getStringOrNull(obj.get("text"));
String subtype = GsonHelper.getStringOrNull(obj.get("subtype"));
String userId;
//sloppy fix for finding userId inside File_comment subtype.
if (subtype !=null && subtype.equals("file_comment")) {
if (subtype != null && subtype.equals("file_comment"))
{
userId = GsonHelper.getStringOrNull(obj.get("comment").getAsJsonObject().get("user"));
} else {
}
else
{
userId = GsonHelper.getStringOrNull(obj.get("user"));
}
if (userId == null) {
}
if (userId == null)
{
userId = GsonHelper.getStringOrNull(obj.get("bot_id"));
}
SlackUser user = slackSession.findUserById(userId);
String threadTimestamp = GsonHelper.getStringOrNull(obj.get("thread_ts"));
if (user == null) {
if (user == null)
{

SlackIntegration integration = slackSession.findIntegrationById(userId);
if (integration == null) {
throw new IllegalStateException("unknown user id: " + userId);
if (integration == null)
{
LOGGER.info("unknown user id: " + userId + ". Fallbacking...");
user = SlackPersonaImpl.builder().id(userId).build();
}
else
{
user = new SlackIntegrationUser(integration);
}
user = new SlackIntegrationUser(integration);

}
Map<String, Integer> reacs = extractReactionsFromMessageJSON(obj);
ArrayList<SlackAttachment> attachments = extractAttachmentsFromMessageJSON(obj);
Expand Down
Loading

0 comments on commit 75e6e16

Please sign in to comment.