Skip to content

Commit

Permalink
did some things related to #6 that I thought might work but didn't ac…
Browse files Browse the repository at this point in the history
…tually change very much
  • Loading branch information
fennifith committed Mar 18, 2017
1 parent 9287d54 commit de51103
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.bumptech.glide.Glide;
import com.ullink.slack.simpleslackapi.SlackAttachment;
import com.ullink.slack.simpleslackapi.SlackFile;

import org.json.simple.JSONObject;

Expand Down Expand Up @@ -225,4 +226,31 @@ public static AttachmentData fromFile(Context context, JSONObject object) {

return data;
}

public static AttachmentData fromFile(Context context, SlackFile file) {
AttachmentData data = new AttachmentData(context, new Identifier(file.getTitle(), file.getComment()));
data.title = file.getTitle();
data.titleLink = file.getPermalink();
data.pretext = file.getName();

String type = file.getFiletype();
if (type != null) {
switch (type) {
case "png":
data.pretext = null;
data.imageUrl = file.getUrlPrivate();
data.footer = file.getName();
break;
case "text":
data.text = file.getName();
break;
default:
data.text = file.getName();
data.textType = type;
break;
}
}

return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static MessageItemData from(Context context, JSONObject object) {
case "bot_message":
itemData = new UserMessageItemData(
context,
butterySlack.session.findUserById((String) object.get("bot_id")),
butterySlack.session.findUserById(senderId != null ? senderId : (String) object.get("bot_id")),
content,
timestamp,
attachments
Expand Down Expand Up @@ -177,6 +177,10 @@ public static MessageItemData from(Context context, SlackMessagePosted event) {
SlackMessagePosted.MessageSubType subType = event.getMessageSubType();
String type = subType != null ? subType.name() : null;

SlackUser sender = event.getSender();
String content = event.getMessageContent();
String timestamp = event.getTimestamp();

List<ItemData> attachments = new ArrayList<>();
if (event.getAttachments() != null) {
for (SlackAttachment attachment : event.getAttachments()) {
Expand All @@ -186,6 +190,15 @@ public static MessageItemData from(Context context, SlackMessagePosted event) {

if (type != null) {
switch (type) {
case "bot_message":
itemData = new UserMessageItemData(
context,
sender,
content,
timestamp,
attachments
);
break;
case "channel_archive":
case "channel_join":
case "channel_leave":
Expand All @@ -208,7 +221,10 @@ public static MessageItemData from(Context context, SlackMessagePosted event) {
);
break;
case "file_share":
//TODO: do some magic
content = null;

if (event.getSlackFile() != null)
attachments.add(AttachmentData.fromFile(context, event.getSlackFile()));
break;
case "me_message":
case "file_comment":
Expand All @@ -217,12 +233,19 @@ public static MessageItemData from(Context context, SlackMessagePosted event) {
case "message_deleted":
case "message_replied":
case "reply_broadcast":
case "bot_message":
}
}

if (itemData != null)
return itemData;
else return new UserMessageItemData(context, event, attachments);
else {
return new UserMessageItemData(
context,
sender,
content,
timestamp,
attachments
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public abstract class ChatFragment extends ButteryFragment implements SlackMessa
private Handler handler;
private Pool pagePool, sendingPool;
private Map<String, Boolean> pages;
private boolean isRegistered;

@Nullable
@Override
Expand Down Expand Up @@ -157,12 +158,19 @@ public void result(@NonNull Result result) {
}

final void registerListener() {
getButterySlack().session.addMessagePostedListener(this);
if (!isRegistered) {
getButterySlack().session.addMessagePostedListener(this);
isRegistered = true;
}
}

@Override
public void onDestroy() {
getButterySlack().session.removeMessagePostedListener(this);
if (isRegistered) {
getButterySlack().session.removeMessagePostedListener(this);
isRegistered = false;
}

super.onDestroy();
}

Expand Down

0 comments on commit de51103

Please sign in to comment.