Skip to content
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 @@ -11,7 +11,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface ActionsIF extends Block {
public interface ActionsIF extends Block, ContainerChildBlock {
String TYPE = "actions";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
@JsonSubTypes.Type(value = Card.class, name = Card.TYPE),
@JsonSubTypes.Type(value = Table.class, name = Table.TYPE),
@JsonSubTypes.Type(value = DataTable.class, name = DataTable.TYPE),
@JsonSubTypes.Type(value = ContainerBlock.class, name = ContainerBlock.TYPE),
}
)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public enum BlockElementLengthLimits {
MAX_TABLE_COLUMNS(20),
MAX_TABLE_BLOCK_ID_LENGTH(255),
MIN_DATA_TABLE_ROWS(2),
MAX_DATA_TABLE_PAGE_SIZE(100);
MAX_DATA_TABLE_PAGE_SIZE(100),
MAX_CONTAINER_CHILD_BLOCKS(10);

private final int limit;

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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.models.blocks.elements.Image;
import com.hubspot.slack.client.models.blocks.elements.richtextelements.RichTextBlock;
import com.hubspot.slack.client.models.blocks.objects.Text;
import com.hubspot.slack.client.models.blocks.objects.TextType;
import java.util.Optional;
import org.immutables.value.Value;
import org.immutables.value.Value.Check;
import org.immutables.value.Value.Immutable;

@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_ABSENT)
public interface ContainerBlockIF extends Block {
String TYPE = "container";

@Override
@Value.Derived
default String getType() {
return TYPE;
}

Optional<Text> getTitle();

Optional<RichTextBlock> getRichTextTitle();

ImmutableList<ContainerChildBlock> getChildBlocks();

Optional<Text> getSubtitle();

Optional<Image> getIcon();

Optional<ContainerBlockWidth> getWidth();

Optional<Boolean> getHasHeaderDivider();

@JsonProperty("is_collapsible")
Optional<Boolean> isCollapsible();

@JsonProperty("default_collapsed")
Optional<Boolean> isDefaultCollapsed();

@Check
default void check() {
Preconditions.checkState(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Text type validation is missing:

Title of the container, using plain_text formatting

We have two types of Text: plain_text and Markdown

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. We now validate in @Check that when title is present, its TextType is PLAIN_TEXT — building with a mrkdwn title throws IllegalStateException: title must use plain_text formatting.

getTitle().isPresent() || getRichTextTitle().isPresent(),
"container block must have either title or rich_text_title"
);
getTitle()
.ifPresent(title -> {
Preconditions.checkState(
title.getType() == TextType.PLAIN_TEXT,
"title must use plain_text formatting"
);
Preconditions.checkState(
title.getText().length() <=
BlockElementLengthLimits.MAX_CARD_TITLE_LENGTH.getLimit(),
"title cannot exceed %s characters",
BlockElementLengthLimits.MAX_CARD_TITLE_LENGTH.getLimit()
);
});
getSubtitle()
.ifPresent(subtitle ->
Preconditions.checkState(
subtitle.getText().length() <=
BlockElementLengthLimits.MAX_CARD_TITLE_LENGTH.getLimit(),
"subtitle cannot exceed %s characters",
BlockElementLengthLimits.MAX_CARD_TITLE_LENGTH.getLimit()
)
);
Preconditions.checkState(
getChildBlocks().size() <=
BlockElementLengthLimits.MAX_CONTAINER_CHILD_BLOCKS.getLimit(),
"child_blocks cannot exceed %s blocks",
BlockElementLengthLimits.MAX_CONTAINER_CHILD_BLOCKS.getLimit()
);
Preconditions.checkState(
!isDefaultCollapsed().orElse(false) || isCollapsible().orElse(false),
"default_collapsed requires is_collapsible to be true"
);
Preconditions.checkState(
!isCollapsible().orElse(false) || !getHasHeaderDivider().orElse(false),
"has_header_divider does not apply to collapsible blocks"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hubspot.slack.client.models.blocks;

import com.fasterxml.jackson.annotation.JsonValue;

public enum ContainerBlockWidth {
NARROW("narrow"),
STANDARD("standard"),
WIDE("wide"),
FULL("full");

private final String value;

ContainerBlockWidth(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.hubspot.slack.client.models.blocks;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.hubspot.slack.client.models.blocks.elements.richtextelements.RichTextBlock;

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
property = "type",
defaultImpl = UnknownBlock.class
)
@JsonSubTypes(
{
@JsonSubTypes.Type(value = Actions.class, name = Actions.TYPE),
@JsonSubTypes.Type(value = Context.class, name = Context.TYPE),
@JsonSubTypes.Type(value = Divider.class, name = Divider.TYPE),
@JsonSubTypes.Type(value = File.class, name = File.TYPE),
@JsonSubTypes.Type(value = Header.class, name = Header.TYPE),
@JsonSubTypes.Type(value = Image.class, name = Image.TYPE),
@JsonSubTypes.Type(value = Input.class, name = Input.TYPE),
@JsonSubTypes.Type(value = RichTextBlock.class, name = RichTextBlock.TYPE),
@JsonSubTypes.Type(value = Section.class, name = Section.TYPE),
@JsonSubTypes.Type(value = Table.class, name = Table.TYPE),
}
)
public interface ContainerChildBlock {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface ContextIF extends Block {
public interface ContextIF extends Block, ContainerChildBlock {
String TYPE = "context";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface DividerIF extends Block {
public interface DividerIF extends Block, ContainerChildBlock {
String TYPE = "divider";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface FileIF extends Block {
public interface FileIF extends Block, ContainerChildBlock {
String TYPE = "file";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface HeaderIF extends Block {
public interface HeaderIF extends Block, ContainerChildBlock {
String TYPE = "header";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface ImageIF extends Block, ImageBlockOrText {
public interface ImageIF extends Block, ImageBlockOrText, ContainerChildBlock {
String TYPE = "image";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface InputIF extends Block {
public interface InputIF extends Block, ContainerChildBlock {
String TYPE = "input";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface SectionIF extends Block {
public interface SectionIF extends Block, ContainerChildBlock {
String TYPE = "section";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface TableIF extends Block {
public interface TableIF extends Block, ContainerChildBlock {
String TYPE = "table";

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

import java.util.Optional;

public class UnknownBlock implements Block {
public class UnknownBlock implements Block, ContainerChildBlock {

public static final String TYPE = "unknown";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.google.common.collect.ImmutableList;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.models.blocks.Block;
import com.hubspot.slack.client.models.blocks.ContainerChildBlock;
import org.immutables.value.Value;
import org.immutables.value.Value.Immutable;

Expand All @@ -16,7 +17,7 @@
@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface RichTextBlockIF extends Block {
public interface RichTextBlockIF extends Block, ContainerChildBlock {
String TYPE = "rich_text";

@Override
Expand Down
Loading