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

Add Action parser #238

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,12 +1,12 @@
package com.ullink.slack.simpleslackapi;

import com.ullink.slack.simpleslackapi.events.SlackMessagePosted;
import org.threeten.bp.LocalDate;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ullink.slack.simpleslackapi.events.SlackMessagePosted;
import org.threeten.bp.LocalDate;

public interface ChannelHistoryModule {

enum MessageSubTypeFilter {
Expand Down Expand Up @@ -66,5 +66,9 @@ private Set<String> getUsersMessagesSubTypesCode() {

List<SlackMessagePosted> fetchUpdatingHistoryOfChannel(String channelId, LocalDate day, int numberOfMessages);

void markHistoryOfChannelAsRead(String channelId, String messageTimestamp);

void markHistoryOfChannelAsRead(String channelId);


}
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
package com.ullink.slack.simpleslackapi.impl;

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

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ullink.slack.simpleslackapi.ChannelHistoryModule;
import com.ullink.slack.simpleslackapi.SlackChannel;
import com.ullink.slack.simpleslackapi.SlackMessageHandle;
import com.ullink.slack.simpleslackapi.SlackSession;
import com.ullink.slack.simpleslackapi.events.ReactionAdded;
import com.ullink.slack.simpleslackapi.events.ReactionRemoved;
import com.ullink.slack.simpleslackapi.events.SlackMessagePosted;
import com.ullink.slack.simpleslackapi.listeners.ReactionAddedListener;
import com.ullink.slack.simpleslackapi.listeners.ReactionRemovedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessagePostedListener;
import com.ullink.slack.simpleslackapi.replies.GenericSlackReply;
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.temporal.ChronoUnit;
import com.ullink.slack.simpleslackapi.ChannelHistoryModule;
import com.ullink.slack.simpleslackapi.SlackSession;
import com.ullink.slack.simpleslackapi.listeners.ReactionAddedListener;
import com.ullink.slack.simpleslackapi.listeners.ReactionRemovedListener;
import com.ullink.slack.simpleslackapi.listeners.SlackMessagePostedListener;

import java.util.*;

public class ChannelHistoryModuleImpl implements ChannelHistoryModule {

private final SlackSession session;
private static final String FETCH_CHANNEL_HISTORY_COMMAND = "channels.history";
private static final String FETCH_GROUP_HISTORY_COMMAND = "groups.history";
private static final String FETCH_IM_HISTORY_COMMAND = "im.history";
private static final String MARK_CHANNEL_HISTORY_COMMAND = "channels.mark";
private static final String MARK_GROUP_HISTORY_COMMAND = "groups.mark";
private static final String MARK_IM_HISTORY_COMMAND = "im.mark";
private static final int DEFAULT_HISTORY_FETCH_SIZE = 1000;

public ChannelHistoryModuleImpl(SlackSession session) {
Expand Down Expand Up @@ -159,6 +158,45 @@ public List<SlackMessagePosted> fetchUpdatingHistoryOfChannel(String channelId,
return messages;
}

@Override
public void markHistoryOfChannelAsRead(String channelId, String messageTimestamp) {
Map<String, String> params = new HashMap<>();
params.put("channel", channelId);
params.put("ts", messageTimestamp);
SlackChannel channel = session.findChannelById(channelId);
switch (channel.getType()) {
case INSTANT_MESSAGING:
markHistoryOfChannelAsRead(params, MARK_IM_HISTORY_COMMAND);
break;
case PRIVATE_GROUP:
markHistoryOfChannelAsRead(params, MARK_GROUP_HISTORY_COMMAND);
break;
default:
markHistoryOfChannelAsRead(params, MARK_CHANNEL_HISTORY_COMMAND);
break;
}
}

@Override
public void markHistoryOfChannelAsRead(String channelId) {
markHistoryOfChannelAsRead(channelId, convertDateToSlackTimestamp(ZonedDateTime.now()));

}

private void markHistoryOfChannelAsRead(Map<String, String> params, String command) {
SlackMessageHandle<GenericSlackReply> handle = session.postGenericSlackCommand(params, command);
GenericSlackReply replyEv = handle.getReply();
String answer = replyEv.getPlainAnswer();
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(answer).getAsJsonObject();
boolean result = GsonHelper.getBooleanOrDefaultValue(jsonObject.get("ok"),false);
if(!result){
throw new IllegalArgumentException(GsonHelper.getStringOrDefaultValue(jsonObject.get("error"),"channel_not_found"));
}

}


public static class ChannelHistoryReactionAddedListener implements ReactionAddedListener {

List<SlackMessagePosted> messages = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,18 @@ private static ArrayList<SlackAttachment> extractAttachmentsFromMessageJSON(Json
GsonHelper.getBooleanOrDefaultValue(field.get("short"),false));
}
}
if (obj.get("actions") != null) {
for (JsonElement fieldElement : obj.get("actions").getAsJsonArray()) {
JsonObject action = fieldElement.getAsJsonObject();
slackAttachment.addAction(
GsonHelper.getStringOrNull(action.get("name")),
GsonHelper.getStringOrNull(action.get("value")),
GsonHelper.getStringOrNull(action.get("text")),
GsonHelper.getStringOrNull(action.get("type"))
);

}
}
attachments.add(slackAttachment);
}

Expand Down