Skip to content

Commit

Permalink
ENH adding some more details about users rights
Browse files Browse the repository at this point in the history
adding feature #18 as requested
deprecating SlackBot class (it is now a user returning true to isBot)
increasing version to release a java 7 bytecode compatible version.

Change-Id: I99e8233e38eba2dc9b343ec3a2b6750e565163da
  • Loading branch information
Benoit Corne committed Jul 27, 2015
1 parent 87d0414 commit 6a2977c
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 132 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ targetCompatibility = 1.7

group = 'com.ullink.slack'
archivesBaseName = 'simpleslackapi'
version = '0.4.3'
version = '0.4.4'


task javadocJar(type: Jar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ullink.slack.simpleslackapi;

@Deprecated
public interface SlackBot extends SlackPersona
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ public enum SlackPresence { UNKNOWN, ACTIVE, AWAY, AUTO }
String getId();

String getUserName();

String getUserMail();
boolean isDeleted();
boolean isAdmin();
boolean isOwner();
boolean isPrimaryOwner();
boolean isRestricted();
boolean isUltraRestricted();
boolean isBot();
}
4 changes: 1 addition & 3 deletions src/main/java/com/ullink/slack/simpleslackapi/SlackUser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.ullink.slack.simpleslackapi;

public interface SlackUser extends SlackPersona
public interface SlackUser extends SlackPersona, SlackBot
{
String getRealName();
String getUserMail();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ abstract class AbstractSlackSessionImpl implements SlackSession

protected Map<String, SlackChannel> channels = new HashMap<>();
protected Map<String, SlackUser> users = new HashMap<>();
protected Map<String, SlackBot> bots = new HashMap<>();
protected SlackPersona sessionPersona;

protected List<SlackChannelArchivedListener> channelArchiveListener = new ArrayList<SlackChannelArchivedListener>();
Expand Down Expand Up @@ -52,9 +51,16 @@ public Collection<SlackUser> getUsers()
}

@Override
@Deprecated
public Collection<SlackBot> getBots()
{
return new ArrayList<>(bots.values());
ArrayList<SlackBot> toReturn = new ArrayList<>();
for(SlackUser user : users.values()) {
if (user.isBot()) {
toReturn.add(user);
}
}
return toReturn;
}

@Override
Expand Down Expand Up @@ -124,9 +130,10 @@ public SlackPersona sessionPersona()
}

@Override
@Deprecated
public SlackBot findBotById(String botId)
{
return bots.get(botId);
return users.get(botId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,12 @@

import com.ullink.slack.simpleslackapi.SlackBot;

class SlackBotImpl implements SlackBot
@Deprecated
class SlackBotImpl extends SlackPersonaImpl implements SlackBot
{

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

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

@Override
public String getId()
SlackBotImpl(String id, String userName, String realName, String userMail, boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted, boolean ultraRestricted)
{
return id;
super(id, userName, realName, userMail, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, true);
}

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

@Override
public boolean isDeleted()
{
return deleted;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,35 @@ private SlackJSONParsingUtils()
// Helper class
}

static final SlackUserImpl buildSlackUser(JSONObject jsonUser)
static final SlackUser buildSlackUser(JSONObject jsonUser)
{
String id = (String) jsonUser.get("id");
String name = (String) jsonUser.get("name");
String realName = (String) jsonUser.get("real_name");
Boolean deleted = (Boolean) jsonUser.get("deleted");
if(deleted == null) deleted = false;
Boolean deleted = ifNullFalse(jsonUser,"deleted");
Boolean admin = ifNullFalse(jsonUser,"is_admin");
Boolean owner = ifNullFalse(jsonUser,"is_owner");
Boolean primaryOwner = ifNullFalse(jsonUser,"is_primary_owner");
Boolean restricted = ifNullFalse(jsonUser,"is_restricted");
Boolean ultraRestricted = ifNullFalse(jsonUser,"is_ultra_restricted");
Boolean bot = ifNullFalse(jsonUser,"is_bot");
JSONObject profileJSON = (JSONObject) jsonUser.get("profile");
String email = "";
if(profileJSON != null) {
if (profileJSON != null)
{
email = (String) profileJSON.get("email");
}
return new SlackUserImpl(id, name, realName, email, deleted);
return new SlackUserImpl(id, name, realName, email, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, bot);
}

static final SlackBotImpl buildSlackBot(JSONObject jsonBot)
private static Boolean ifNullFalse(JSONObject jsonUser, String field)
{
String id = (String) jsonBot.get("id");
String name = (String) jsonBot.get("name");
Boolean deleted = (Boolean) jsonBot.get("deleted");
return new SlackBotImpl(id, name, deleted);
Boolean deleted = (Boolean) jsonUser.get(field);
if (deleted == null)
{
deleted = false;
}
return deleted;
}

static final SlackChannelImpl buildSlackChannel(JSONObject jsonChannel, Map<String, SlackUser> knownUsersById)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,6 @@ void parse() throws ParseException
users.put(slackUser.getId(), slackUser);
}

JSONArray botsJson = (JSONArray) jsonResponse.get("bots");

for (Object jsonObject : botsJson)
{
JSONObject jsonBot = (JSONObject) jsonObject;
SlackBot slackBot = SlackJSONParsingUtils.buildSlackBot(jsonBot);
LOGGER.debug("slack bot found : " + slackBot.getId());
bots.put(slackBot.getId(), slackBot);
}

JSONArray channelsJson = (JSONArray) jsonResponse.get("channels");

for (Object jsonObject : channelsJson)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.ullink.slack.simpleslackapi.impl;

import com.ullink.slack.simpleslackapi.SlackPersona;

class SlackPersonaImpl implements SlackPersona
{
final String id;
final String userName;
final String realName;
final String userMail;
final boolean deleted;
final boolean admin;
final boolean owner;
final boolean primaryOwner;
final boolean restricted;
final boolean ultraRestricted;
final boolean bot;

SlackPersonaImpl(String id, String userName, String realName, String userMail, boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted, boolean ultraRestricted, boolean bot)
{
this.id = id;
this.userName = userName;
this.realName = realName;
this.userMail = userMail;
this.deleted = deleted;
this.admin = admin;
this.owner = owner;
this.primaryOwner = primaryOwner;
this.restricted = restricted;
this.ultraRestricted = ultraRestricted;
this.bot = bot;
}

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

@Override
public String getUserName()
{
return userName;
}

@Override
public boolean isDeleted()
{
return deleted;
}

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

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

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

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

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

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,20 @@

import com.ullink.slack.simpleslackapi.SlackUser;

class SlackUserImpl implements SlackUser
class SlackUserImpl extends SlackPersonaImpl implements SlackUser
{

String id;
String id;

@Override
public String toString() {
return "SlackUserImpl{" +
"id='" + id + '\'' +
", userName='" + userName + '\'' +
", realName='" + realName + '\'' +
", userMail='" + userMail + '\'' +
", isDeleted=" + isDeleted +
'}';
}

String userName;
String realName;
String userMail;
boolean isDeleted;

SlackUserImpl(String id, String userName, String realName, String userMail, boolean isDeleted)
{
this.id = id;
this.userName = userName;
this.realName = realName;
this.isDeleted = isDeleted;
this.userMail = userMail;
}

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

@Override
public String getUserName()
{
return userName;
}

@Override
public String getRealName()
public String toString()
{
return realName;
return "SlackUserImpl{" + "id='" + id + '\'' + ", userName='" + userName + '\'' + ", realName='" + realName + '\'' + ", userMail='" + userMail + '\'' + ", isDeleted=" + deleted + '\'' + ", isAdmin=" + admin + '\'' + ", isOwner="
+ owner + '\'' + ", isPrimaryOwner=" + primaryOwner + '\'' + ", isRestricted=" + restricted + '\'' + ", isUltraRestricted=" + ultraRestricted + '}';
}

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

@Override
public String getUserMail()
SlackUserImpl(String id, String userName, String realName, String userMail, boolean deleted, boolean admin, boolean owner, boolean primaryOwner, boolean restricted, boolean ultraRestricted, boolean bot)
{
return userMail;
super(id, userName, realName, userMail, deleted, admin, owner, primaryOwner, restricted, ultraRestricted, bot);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ private void connectImpl() throws IOException, ClientProtocolException, ConnectE
throw new ConnectException(sessionParser.getError());
}
users = sessionParser.getUsers();
bots = sessionParser.getBots();
channels = sessionParser.getChannels();
sessionPersona = sessionParser.getSessionPersona();
LOGGER.info(users.size() + " users found on this session");
LOGGER.info(bots.size() + " bots found on this session");
LOGGER.info(channels.size() + " channels found on this session");
String wssurl = sessionParser.getWebSocketURL();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public void connect()
channels.put("channelid4",new SlackChannelImpl("channelid4", "testchannel4", "topicchannel4", "topicchannel4", false));
channels.put("channelid5",new SlackChannelImpl("channelid5", "testchannel5", "topicchannel5", "topicchannel5", false));

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

bots.put("botid1",new SlackBotImpl("botid1", "botname1", false));
bots.put("botid2",new SlackBotImpl("botid2", "botname2", false));
bots.put("botid3",new SlackBotImpl("botid3", "botname2", true));
users.put("userid1",new SlackUserImpl("userid1", "username1", "realname1","[email protected]", false,false,false,false,false,false, false));
users.put("userid2",new SlackUserImpl("userid2", "username2", "realname2","[email protected]", false,false,false,false,false,false, false));
users.put("userid3",new SlackUserImpl("userid3", "username3", "realname3","[email protected]", true,false,false,false,false,false, false));
users.put("userid4",new SlackUserImpl("userid4", "username4", "realname4","[email protected]", false,false,false,false,false,false, false));
users.put("userid5",new SlackUserImpl("userid5", "username5", "realname4","[email protected]", true,false,false,false,false,false, false));

users.put("botid1",new SlackUserImpl("botid1", "botname1", "real bot name 1", null,false,false,false,false,false,false,true));
users.put("botid2",new SlackUserImpl("botid2", "botname2", "real bot name 2", null,false,false,false,false,false,false,true));
users.put("botid3",new SlackUserImpl("botid3", "botname3", "real bot name 3", null, true,false,false,false,false,false,true));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public void setup()
@Override
public void connect()
{
SlackUser user1 = new SlackUserImpl("TESTUSER1", "test user 1", "", "", false);
SlackUser user2 = new SlackUserImpl("TESTUSER2", "test user 2", "", "", false);
SlackUser user3 = new SlackUserImpl("TESTUSER3", "test user 3", "", "", false);
SlackUser user1 = new SlackUserImpl("TESTUSER1", "test user 1", "", "", false,false,false,false,false,false, false);
SlackUser user2 = new SlackUserImpl("TESTUSER2", "test user 2", "", "", false,false,false,false,false,false, false);
SlackUser user3 = new SlackUserImpl("TESTUSER3", "test user 3", "", "", false,false,false,false,false,false, false);
users.put(user1.getId(), user1);
users.put(user2.getId(), user2);
users.put(user3.getId(), user3);
Expand Down
Loading

0 comments on commit 6a2977c

Please sign in to comment.