From c2d88009b3ae187dcd159856588237e59450fd65 Mon Sep 17 00:00:00 2001 From: Yauheni Patotski Date: Wed, 21 Feb 2018 23:48:08 +0300 Subject: [PATCH 1/2] Add Actian parser --- .../simpleslackapi/impl/SlackJSONMessageParser.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java index 3bbf84c8..0c133ac1 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/SlackJSONMessageParser.java @@ -485,7 +485,18 @@ private static ArrayList 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); } From c8c17c606c0d693247478459c0cfedb252d88c1c Mon Sep 17 00:00:00 2001 From: Yauheni Patotski Date: Thu, 22 Feb 2018 22:17:12 +0300 Subject: [PATCH 2/2] Add possibility to mark chat history as read --- .../simpleslackapi/ChannelHistoryModule.java | 10 +++- .../impl/ChannelHistoryModuleImpl.java | 60 +++++++++++++++---- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/ChannelHistoryModule.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/ChannelHistoryModule.java index ba909789..d777c838 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/ChannelHistoryModule.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/ChannelHistoryModule.java @@ -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 { @@ -66,5 +66,9 @@ private Set getUsersMessagesSubTypesCode() { List fetchUpdatingHistoryOfChannel(String channelId, LocalDate day, int numberOfMessages); + void markHistoryOfChannelAsRead(String channelId, String messageTimestamp); + + void markHistoryOfChannelAsRead(String channelId); + } diff --git a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/ChannelHistoryModuleImpl.java b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/ChannelHistoryModuleImpl.java index 042f556d..1bdbc444 100644 --- a/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/ChannelHistoryModuleImpl.java +++ b/sources/src/main/java/com/ullink/slack/simpleslackapi/impl/ChannelHistoryModuleImpl.java @@ -1,30 +1,26 @@ 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 { @@ -32,6 +28,9 @@ public class ChannelHistoryModuleImpl implements ChannelHistoryModule { 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) { @@ -159,6 +158,45 @@ public List fetchUpdatingHistoryOfChannel(String channelId, return messages; } + @Override + public void markHistoryOfChannelAsRead(String channelId, String messageTimestamp) { + Map 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 params, String command) { + SlackMessageHandle 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 messages = new ArrayList<>();