Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conversations.members update #233

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
package com.ullink.slack.simpleslackapi;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.threeten.bp.LocalDateTime;

import com.ullink.slack.simpleslackapi.SlackSession.GetMembersForChannelCallable;

import com.ullink.slack.simpleslackapi.SlackUser;

//TODO: a domain object
public class SlackChannel {
private static final Logger LOGGER = LoggerFactory.getLogger(SlackChannel.class);
private static final long REFRESH_MEMBERS_EVERY_SECONDS = TimeUnit.HOURS.toSeconds(1);

private final boolean direct;
private String id;
private String name;
private Set<SlackUser> members = new HashSet<>();
private GetMembersForChannelCallable getMembersForChannelCallable;
private String topic;
private String purpose;
private boolean isMember;
private boolean isArchived;

public SlackChannel(String id, String name, String topic, String purpose, boolean direct, boolean isMember, boolean isArchived)
private LocalDateTime membersLastUpdated;

public SlackChannel(String id,
String name,
GetMembersForChannelCallable getMembersForChannelCallable,
String topic,
String purpose,
boolean direct,
boolean isMember,
boolean isArchived)
{
this.id = id;
this.name = name;
this.getMembersForChannelCallable = getMembersForChannelCallable;
this.topic = topic;
this.purpose = purpose;
this.direct = direct;
Expand Down Expand Up @@ -49,9 +67,17 @@ public String getName()
return name;
}

public Collection<SlackUser> getMembers()
{
return new ArrayList<>(members);
public Collection<SlackUser> getMembers() {
if (shouldRefreshMembers()) {
try {
members = getMembersForChannelCallable.setChannelId(id).call();
membersLastUpdated = LocalDateTime.now();
} catch (Exception e) {
LOGGER.error("Failed to refresh members for {}", name, e);
}
}

return members;
}

public String getTopic()
Expand Down Expand Up @@ -101,4 +127,9 @@ public SlackChannelType getType()
public enum SlackChannelType {
PUBLIC_CHANNEL, PRIVATE_GROUP, INSTANT_MESSAGING
}

private boolean shouldRefreshMembers() {
return getType().equals(SlackChannelType.PUBLIC_CHANNEL) &&
(membersLastUpdated == null || LocalDateTime.now().isAfter(membersLastUpdated.plusSeconds(REFRESH_MEMBERS_EVERY_SECONDS)));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
package com.ullink.slack.simpleslackapi;

import com.ullink.slack.simpleslackapi.listeners.*;
import com.ullink.slack.simpleslackapi.replies.*;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

import com.ullink.slack.simpleslackapi.listeners.PinAddedListener;
import com.ullink.slack.simpleslackapi.listeners.PinRemovedListener;
import com.ullink.slack.simpleslackapi.listeners.PresenceChangeListener;
import com.ullink.slack.simpleslackapi.listeners.ReactionAddedListener;
import com.ullink.slack.simpleslackapi.listeners.ReactionRemovedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelArchivedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelCreatedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelDeletedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelJoinedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelLeftListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelRenamedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelUnarchivedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackConnectedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackDisconnectedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackGroupJoinedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessageDeletedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessagePostedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessageUpdatedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackTeamJoinListener;
import com.ullink.slack.simpleslackapi.listeners.SlackUserChangeListener;
import com.ullink.slack.simpleslackapi.listeners.UserTypingListener;
import com.ullink.slack.simpleslackapi.replies.EmojiSlackReply;
import com.ullink.slack.simpleslackapi.replies.GenericSlackReply;
import com.ullink.slack.simpleslackapi.replies.ParsedSlackReply;
import com.ullink.slack.simpleslackapi.replies.SlackChannelReply;
import com.ullink.slack.simpleslackapi.replies.SlackMessageReply;

public interface SlackSession {

Expand All @@ -15,6 +41,12 @@ public interface SlackSession {

Collection<SlackUser> getUsers();

interface GetMembersForChannelCallable extends Callable<Set<SlackUser>> {
GetMembersForChannelCallable setChannelId(String channelId);
}

GetMembersForChannelCallable getMembersForChannelCallable(String channelId);

Collection<SlackBot> getBots();

Collection<SlackIntegration> getIntegrations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
import com.ullink.slack.simpleslackapi.listeners.SlackTeamJoinListener;
import com.ullink.slack.simpleslackapi.listeners.SlackUserChangeListener;
import com.ullink.slack.simpleslackapi.listeners.UserTypingListener;
import com.ullink.slack.simpleslackapi.replies.*;
import com.ullink.slack.simpleslackapi.replies.EmojiSlackReply;
import com.ullink.slack.simpleslackapi.replies.GenericSlackReply;
import com.ullink.slack.simpleslackapi.replies.ParsedSlackReply;
import com.ullink.slack.simpleslackapi.replies.SlackChannelReply;
import com.ullink.slack.simpleslackapi.replies.SlackMessageReply;

public class SlackSessionWrapper implements SlackSession
{
Expand All @@ -51,6 +55,11 @@ public SlackSessionWrapper(SlackSession delegate)
return delegate.getUsers();
}

@Override
public GetMembersForChannelCallable getMembersForChannelCallable(String channelId) {
return delegate.getMembersForChannelCallable(channelId);
}

@Override public Collection<SlackBot> getBots()
{
return delegate.getBots();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,46 @@
package com.ullink.slack.simpleslackapi.impl;

import com.ullink.slack.simpleslackapi.*;
import com.ullink.slack.simpleslackapi.listeners.*;
import com.ullink.slack.simpleslackapi.replies.SlackMessageReply;

import java.util.*;
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.SlackAttachment;
import com.ullink.slack.simpleslackapi.SlackBot;
import com.ullink.slack.simpleslackapi.SlackChannel;
import com.ullink.slack.simpleslackapi.SlackChatConfiguration;
import com.ullink.slack.simpleslackapi.SlackIntegration;
import com.ullink.slack.simpleslackapi.SlackMessageHandle;
import com.ullink.slack.simpleslackapi.SlackPersona;
import com.ullink.slack.simpleslackapi.SlackPreparedMessage;
import com.ullink.slack.simpleslackapi.SlackSession;
import com.ullink.slack.simpleslackapi.SlackTeam;
import com.ullink.slack.simpleslackapi.SlackUser;
import com.ullink.slack.simpleslackapi.listeners.PinAddedListener;
import com.ullink.slack.simpleslackapi.listeners.PinRemovedListener;
import com.ullink.slack.simpleslackapi.listeners.PresenceChangeListener;
import com.ullink.slack.simpleslackapi.listeners.ReactionAddedListener;
import com.ullink.slack.simpleslackapi.listeners.ReactionRemovedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelArchivedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelCreatedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelDeletedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelJoinedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelLeftListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelRenamedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackChannelUnarchivedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackConnectedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackDisconnectedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackGroupJoinedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessageDeletedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessagePostedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessageUpdatedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackTeamJoinListener;
import com.ullink.slack.simpleslackapi.listeners.SlackUserChangeListener;
import com.ullink.slack.simpleslackapi.listeners.UserTypingListener;
import com.ullink.slack.simpleslackapi.replies.SlackMessageReply;

abstract class AbstractSlackSessionImpl implements SlackSession
{

Expand Down Expand Up @@ -102,7 +135,7 @@ public SlackChannel findChannelById(String channelId)
// direct channel case
if (channelId != null && channelId.startsWith("D"))
{
toReturn = new SlackChannel(channelId, "", "", "", true, false, false);
toReturn = new SlackChannel(channelId, "", getMembersForChannelCallable(channelId),"", "", true, false, false);
}
}
return toReturn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static SlackEvent decode(SlackSession slackSession, JsonObject obj) {
private static SlackChannelJoined extractChannelJoinedEvent(SlackSession slackSession, JsonObject obj)
{
JsonObject channelJSONObject = obj.get("channel").getAsJsonObject();
SlackChannel slackChannel = parseChannelDescription(channelJSONObject);
SlackChannel slackChannel = parseChannelDescription(slackSession, channelJSONObject);
return new SlackChannelJoined(slackChannel);
}

Expand All @@ -114,14 +114,14 @@ private static SlackChannelLeft extractChannelLeftEvent(SlackSession slackSessio
private static SlackGroupJoined extractGroupJoinedEvent(SlackSession slackSession, JsonObject obj)
{
JsonObject channelJSONObject = obj.get("channel").getAsJsonObject();
SlackChannel slackChannel = parseChannelDescription(channelJSONObject);
SlackChannel slackChannel = parseChannelDescription(slackSession, channelJSONObject);
return new SlackGroupJoined(slackChannel);
}

private static SlackChannelRenamed extractChannelRenamedEvent(SlackSession slackSession, JsonObject obj)
{
JsonObject channelJSONObject = obj.get("channel").getAsJsonObject();
SlackChannel channel = parseChannelDescription(channelJSONObject);
SlackChannel channel = parseChannelDescription(slackSession, channelJSONObject);
return new SlackChannelRenamed(channel, channel.getName());
}

Expand All @@ -148,7 +148,7 @@ private static SlackChannelArchived extractChannelArchiveEvent(SlackSession slac
private static SlackChannelCreated extractChannelCreatedEvent(SlackSession slackSession, JsonObject obj)
{
JsonObject channelJSONObject = obj.get("channel").getAsJsonObject();
SlackChannel channel = parseChannelDescription(channelJSONObject);
SlackChannel channel = parseChannelDescription(slackSession, channelJSONObject);
String creatorId = GsonHelper.getStringOrNull(channelJSONObject.get("creator"));
SlackUser user = slackSession.findUserById(creatorId);
return new SlackChannelCreated(channel, user);
Expand Down Expand Up @@ -183,7 +183,7 @@ private static SlackChannel getChannel(SlackSession slackSession, String channel
if (channelId.startsWith("D"))
{
// direct messaging, on the fly channel creation
return new SlackChannel(channelId, channelId, "", "", true, false, false);
return new SlackChannel(channelId, channelId, slackSession.getMembersForChannelCallable(channelId), "", "", true, false, false);
}
else
{
Expand Down Expand Up @@ -304,7 +304,7 @@ private static SlackMessagePosted parseMessagePublishedWithFile(JsonObject obj,
return new SlackMessagePosted(text, user, user, channel, ts,file,obj.toString(), SlackMessagePosted.MessageSubType.fromCode(subtype), threadTimestamp);
}

private static SlackChannel parseChannelDescription(JsonObject channelJSONObject) {
private static SlackChannel parseChannelDescription(SlackSession slackSession, JsonObject channelJSONObject) {
String id = GsonHelper.getStringOrNull(channelJSONObject.get("id"));
String name = GsonHelper.getStringOrNull(channelJSONObject.get("name"));
String topic = null;
Expand All @@ -316,7 +316,7 @@ private static SlackChannel parseChannelDescription(JsonObject channelJSONObject
purpose = GsonHelper.getStringOrNull((channelJSONObject.get("purpose").getAsJsonObject().get("value")));
}
boolean isArchived = GsonHelper.getBooleanOrDefaultValue(channelJSONObject.get("is_archived"), false);
return new SlackChannel(id, name, topic, purpose, id.startsWith("D"),false, isArchived);
return new SlackChannel(id, name, slackSession.getMembersForChannelCallable(id), topic, purpose, id.startsWith("D"),false, isArchived);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ullink.slack.simpleslackapi.*;
import com.ullink.slack.simpleslackapi.SlackSession.GetMembersForChannelCallable;

class SlackJSONParsingUtils {

Expand Down Expand Up @@ -59,7 +60,7 @@ static final SlackUser buildSlackUser(JsonObject jsonUser)
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);
}

static final SlackChannel buildSlackChannel(JsonObject jsonChannel, Map<String, SlackUser> knownUsersById) {
static final SlackChannel buildSlackChannel(JsonObject jsonChannel, Map<String, SlackUser> knownUsersById, GetMembersForChannelCallable getMembersForChannelCallable) {
String id = GsonHelper.getStringOrNull(jsonChannel.get("id"));
String name = GsonHelper.getStringOrNull(jsonChannel.get("name"));

Expand All @@ -82,8 +83,7 @@ static final SlackChannel buildSlackChannel(JsonObject jsonChannel, Map<String,
if (jsonChannel.has("is_archived")) {
isArchived = jsonChannel.get("is_archived").getAsBoolean();
}

SlackChannel toReturn = new SlackChannel(id, name, topic, purpose, false, isMember, isArchived);
SlackChannel toReturn = new SlackChannel(id, name, getMembersForChannelCallable, topic, purpose, false, isMember, isArchived);
JsonArray membersJson = GsonHelper.getJsonArrayOrNull(jsonChannel.get("members"));
if (membersJson != null) {
for (JsonElement jsonMembersObject : membersJson) {
Expand All @@ -95,10 +95,9 @@ static final SlackChannel buildSlackChannel(JsonObject jsonChannel, Map<String,
return toReturn;
}

static final SlackChannel buildSlackImChannel(JsonObject jsonChannel, Map<String, SlackUser> knownUsersById)
{
static final SlackChannel buildSlackImChannel(JsonObject jsonChannel, Map<String, SlackUser> knownUsersById, GetMembersForChannelCallable getMembersForChannelCallable) {
String id = GsonHelper.getStringOrNull(jsonChannel.get("id"));
SlackChannel toReturn = new SlackChannel(id, null, null, null, true, false, false);
SlackChannel toReturn = new SlackChannel(id, null, getMembersForChannelCallable, null, null, true, false, false);
String memberId = GsonHelper.getStringOrNull(jsonChannel.get("user"));
SlackUser user = knownUsersById.get(memberId);
toReturn.addUser(user);
Expand Down
Loading