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

Support Attachment upon link unfurling #287

Merged
merged 2 commits into from
May 12, 2022
Merged
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
Expand Up @@ -5,14 +5,15 @@
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.models.BlockOrAttachment;
import com.hubspot.slack.client.models.ChatUnfurlBlocksOrAttachment;
import com.hubspot.slack.client.models.json.BlockOrAttachmentDeserializer;
import java.util.List;
import org.immutables.value.Value;

@HubSpotStyle
@Value.Immutable
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public interface ChatUnfurlBlocksIF {
public interface ChatUnfurlBlocksIF extends ChatUnfurlBlocksOrAttachment {
@Value.Parameter(order = 1)
@JsonDeserialize(contentUsing = BlockOrAttachmentDeserializer.class)
List<? extends BlockOrAttachment> getBlocks();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package com.hubspot.slack.client.methods.params.chat;

import java.net.URI;
import java.util.Map;
import java.util.Optional;

import org.immutables.value.Value.Immutable;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.methods.interceptor.HasChannel;
import com.hubspot.slack.client.models.BlockOrAttachment;
import com.hubspot.slack.client.models.ChatUnfurlBlocksOrAttachment;
import com.hubspot.slack.client.models.json.BlockOrAttachmentDeserializer;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.immutables.value.Value.Immutable;

@Immutable
@HubSpotStyle
Expand All @@ -24,7 +25,8 @@ public interface ChatUnfurlParamsIF extends HasChannel {

String getTs();

Map<String, ChatUnfurlBlocks> getUnfurls();
@JsonDeserialize(contentUsing = BlockOrAttachmentDeserializer.class)
Map<String, ChatUnfurlBlocksOrAttachment> getUnfurls();

Optional<Boolean> isUserAuthRequired();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hubspot.slack.client.models;

public interface BlockOrAttachment {
public interface BlockOrAttachment extends ChatUnfurlBlocksOrAttachment {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.hubspot.slack.client.models;

// We need common interface to make unfurl call using any of - old or Block Kit framework.
// Having this we can construct ChatUnfurlParams for both cases.
public interface ChatUnfurlBlocksOrAttachment {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.hubspot.slack.client.methods.params.chat.ChatUnfurlBlocks;
import com.hubspot.slack.client.models.Attachment;
import com.hubspot.slack.client.models.BlockOrAttachment;
import com.hubspot.slack.client.models.ChatUnfurlBlocksOrAttachment;
import com.hubspot.slack.client.models.blocks.Block;

public class BlockOrAttachmentDeserializer extends StdDeserializer<BlockOrAttachment> {
public class BlockOrAttachmentDeserializer extends StdDeserializer<ChatUnfurlBlocksOrAttachment> {
private static final String BLOCKS_FIELD = "blocks";
private static final String TYPE_FIELD = "type";

protected BlockOrAttachmentDeserializer() {
super(BlockOrAttachment.class);
super(ChatUnfurlBlocksOrAttachment.class);
}

@Override
public BlockOrAttachment deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
public ChatUnfurlBlocksOrAttachment deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);

if (node.has(TYPE_FIELD)) {
if (node.has(BLOCKS_FIELD)) {
return codec.treeToValue(node, ChatUnfurlBlocks.class);
} else if (node.has(TYPE_FIELD)){
return codec.treeToValue(node, Block.class);
} else {
return codec.treeToValue(node, Attachment.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public void itCanBuildChatUnfurlParamsWithBlock() throws URISyntaxException {
public void itCanDeserChatUnfurlParamsWithBlock() throws IOException {
testSerialization("chat_unfurl_params_using_block.json", ChatUnfurlParams.class);
}

@Test
public void itCanDeserChatUnfurlParamsWithAttachment() throws IOException {
testSerialization("chat_unfurl_params_using_attachment.json", ChatUnfurlParams.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"channel": "C123456",
"ts": "123456789.9875",
"unfurls": {
"https://gentle-buttons.com/carafe": {
"fallback": null,
"color": null,
"pretext": null,
"author_name": null,
"author_link": null,
"author_icon": null,
"title": null,
"title_link": null,
"text": "Take a look at this carafe, just another cousin of glass",
"image_url": null,
"attachment_type": null,
"fields": [],
"footer": null,
"footer_icon": null,
"thumb_url": null,
"callback_id": null,
"actions": [],
"mrkdwn_in": [],
"ts": null
}
},
"user_auth_message": null,
"user_auth_url": null,
"is_user_auth_required": null
}