Skip to content
Draft
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,6 +11,7 @@
import com.slack.api.model.block.ContextBlockElement;
import com.slack.api.model.block.ContextActionsBlockElement;
import com.slack.api.model.block.LayoutBlock;
import com.slack.api.model.block.TableCell;
import com.slack.api.model.block.composition.TextObject;
import com.slack.api.model.block.element.BlockElement;
import com.slack.api.model.block.element.RichTextElement;
Expand Down Expand Up @@ -80,6 +81,7 @@ public static void registerTypeAdapters(GsonBuilder builder, boolean failOnUnkno
.registerTypeAdapter(ContextActionsBlockElement.class, new GsonContextActionsBlockElementFactory(failOnUnknownProps))
.registerTypeAdapter(BlockElement.class, new GsonBlockElementFactory(failOnUnknownProps))
.registerTypeAdapter(RichTextElement.class, new GsonRichTextElementFactory(failOnUnknownProps))
.registerTypeAdapter(TableCell.class, new GsonTableCellFactory(failOnUnknownProps))
.registerTypeAdapter(FunctionExecutedEvent.InputValue.class, new GsonFunctionExecutedEventInputValueFactory())
.registerTypeAdapter(Attachment.VideoHtml.class, new GsonMessageAttachmentVideoHtmlFactory(failOnUnknownProps))
.registerTypeAdapter(MessageChangedEvent.PreviousMessage.class, new GsonMessageChangedEventPreviousMessageFactory(failOnUnknownProps))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@ public static ActionsBlock actions(List<BlockElement> elements) {
public static ActionsBlock actions(String blockId, List<BlockElement> elements) {
return ActionsBlock.builder().blockId(blockId).elements(elements).build();
}

// AlertBlock

public static AlertBlock alert(ModelConfigurator<AlertBlock.AlertBlockBuilder> configurator) {
return configurator.configure(AlertBlock.builder()).build();
}

// CallBlock

public static CallBlock call(ModelConfigurator<CallBlock.CallBlockBuilder> configurator) {
return configurator.configure(CallBlock.builder()).build();
}

// CardBlock

public static CardBlock card(ModelConfigurator<CardBlock.CardBlockBuilder> configurator) {
return configurator.configure(CardBlock.builder()).build();
}

// CarouselBlock

public static CarouselBlock carousel(ModelConfigurator<CarouselBlock.CarouselBlockBuilder> configurator) {
return configurator.configure(CarouselBlock.builder()).build();
}

// ContextBlock

Expand Down Expand Up @@ -70,25 +94,19 @@ public static DividerBlock divider(String blockId) {
public static DividerBlock divider() {
return DividerBlock.builder().build();
}

// FileBlock

public static FileBlock file(ModelConfigurator<FileBlock.FileBlockBuilder> configurator) {
return configurator.configure(FileBlock.builder()).build();
}

// HeaderBlock

public static HeaderBlock header(ModelConfigurator<HeaderBlock.HeaderBlockBuilder> configurator) {
return configurator.configure(HeaderBlock.builder()).build();
}

// CallBlock

public static CallBlock call(ModelConfigurator<CallBlock.CallBlockBuilder> configurator) {
return configurator.configure(CallBlock.builder()).build();
}

// FileBlock

public static FileBlock file(ModelConfigurator<FileBlock.FileBlockBuilder> configurator) {
return configurator.configure(FileBlock.builder()).build();
}

// ImageBlock

public static ImageBlock image(ModelConfigurator<ImageBlock.ImageBlockBuilder> configurator) {
Expand Down Expand Up @@ -119,32 +137,22 @@ public static SectionBlock section(ModelConfigurator<SectionBlock.SectionBlockBu
return configurator.configure(SectionBlock.builder()).build();
}

// VideoBlock
public static VideoBlock video(ModelConfigurator<VideoBlock.VideoBlockBuilder> configurator) {
return configurator.configure(VideoBlock.builder()).build();
}

// ShareShortcutBlock

public static ShareShortcutBlock shareShortcut() {
return ShareShortcutBlock.builder().build();
}

// AlertBlock
// TableBlock

public static AlertBlock alert(ModelConfigurator<AlertBlock.AlertBlockBuilder> configurator) {
return configurator.configure(AlertBlock.builder()).build();
}

// CardBlock

public static CardBlock card(ModelConfigurator<CardBlock.CardBlockBuilder> configurator) {
return configurator.configure(CardBlock.builder()).build();
public static TableBlock table(ModelConfigurator<TableBlock.TableBlockBuilder> configurator) {
return configurator.configure(TableBlock.builder()).build();
}

// CarouselBlock
// VideoBlock

public static CarouselBlock carousel(ModelConfigurator<CarouselBlock.CarouselBlockBuilder> configurator) {
return configurator.configure(CarouselBlock.builder()).build();
public static VideoBlock video(ModelConfigurator<VideoBlock.VideoBlockBuilder> configurator) {
return configurator.configure(VideoBlock.builder()).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.slack.api.model.block;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* A {@code raw_number} table cell, holding a numeric value. The optional {@code text}
* field carries the display representation of the value (for example a formatted string).
*
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RawNumberTableCell implements TableCell {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

👁️‍🗨️ question: Is this pattern more standard than perhaps reversing the order for more "direct" imports?

- RawNumberTableCell
+ TableCellRawNumber

🪬 note: I might prefer the latter but not a blocker!

public static final String TYPE = "raw_number";
private final String type = TYPE;
private Double value;
private String text;
}
Comment on lines +18 to +23

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

📝 note: I find raw_number is also supported with the Data Table block which makes me curious if this should be exported as a composition object instead?

🔗 https://github.com/slackapi/java-slack-sdk/tree/main/slack-api-model/src/main/java/com/slack/api/model/block/composition

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.slack.api.model.block;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* A {@code raw_text} table cell, holding unformatted plain text.
*
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RawTextTableCell implements TableCell {
public static final String TYPE = "raw_text";
private final String type = TYPE;
private String text;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class RichTextBlock implements LayoutBlock {
public class RichTextBlock implements LayoutBlock, TableCell {
public static final String TYPE = "rich_text";
private final String type = TYPE;
@Builder.Default
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.slack.api.model.block;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
* Displays tabular data as a grid of rows and cells.
*
* <p>Each row is a list of {@link TableCell cells}; a cell may be a
* {@link RawTextTableCell} ({@code raw_text}), {@link RawNumberTableCell}
* ({@code raw_number}), or a {@link RichTextBlock} ({@code rich_text}). A table
* supports up to 100 rows and up to 20 cells per row, with optional per-column
* behavior configured via {@link TableColumnSetting column settings}.</p>
*
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TableBlock implements LayoutBlock {
public static final String TYPE = "table";
private final String type = TYPE;

/**
* The rows of the table. Each row is a list of cells. Maximum 100 rows, with up to
* 20 cells per row.
*/
@Builder.Default
private List<List<TableCell>> rows = new ArrayList<>();

/**
* Per-column behavior configuration (alignment, wrapping). Maximum 20 items.
*/
private List<TableColumnSetting> columnSettings;

private String blockId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.slack.api.model.block;

/**
* A single cell within a {@link TableBlock} row. A cell can be one of:
*
* <ul>
* <li>{@link RawTextTableCell} ({@code raw_text})</li>
* <li>{@link RawNumberTableCell} ({@code raw_number})</li>
* <li>{@link com.slack.api.model.block.RichTextBlock RichTextBlock} ({@code rich_text})</li>
* </ul>
*
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a>
*/
public interface TableCell {

String getType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.slack.api.model.block;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Describes the behavior of a single column in a {@link TableBlock}. Up to 20 column
* settings can be supplied, one per column.
*
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a>
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class TableColumnSetting {
/**
* Horizontal text alignment for the column. One of {@code left}, {@code center},
* or {@code right}. Defaults to {@code left}.
*/
private String align;
/**
* Whether text in the column wraps onto multiple lines. Defaults to {@code false}.
*/
private Boolean isWrapped;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,42 @@ public LayoutBlock deserialize(JsonElement json, Type typeOfT, JsonDeserializati

private Class<? extends LayoutBlock> getLayoutClassInstance(String typeName) {
switch (typeName) {
case SectionBlock.TYPE:
return SectionBlock.class;
case DividerBlock.TYPE:
return DividerBlock.class;
case ImageBlock.TYPE:
return ImageBlock.class;
case ActionsBlock.TYPE:
return ActionsBlock.class;
case AlertBlock.TYPE:
return AlertBlock.class;
case CallBlock.TYPE:
return CallBlock.class;
case CardBlock.TYPE:
return CardBlock.class;
case CarouselBlock.TYPE:
return CarouselBlock.class;
case ContextBlock.TYPE:
return ContextBlock.class;
case ContextActionsBlock.TYPE:
return ContextActionsBlock.class;
case CallBlock.TYPE:
return CallBlock.class;
case ActionsBlock.TYPE:
return ActionsBlock.class;
case DividerBlock.TYPE:
return DividerBlock.class;
case FileBlock.TYPE:
return FileBlock.class;
case InputBlock.TYPE:
return InputBlock.class;
case HeaderBlock.TYPE:
return HeaderBlock.class;
case ImageBlock.TYPE:
return ImageBlock.class;
case InputBlock.TYPE:
return InputBlock.class;
case MarkdownBlock.TYPE:
return MarkdownBlock.class;
case VideoBlock.TYPE:
return VideoBlock.class;
case RichTextBlock.TYPE:
return RichTextBlock.class;
case SectionBlock.TYPE:
return SectionBlock.class;
case ShareShortcutBlock.TYPE:
return ShareShortcutBlock.class;
case AlertBlock.TYPE:
return AlertBlock.class;
case CardBlock.TYPE:
return CardBlock.class;
case CarouselBlock.TYPE:
return CarouselBlock.class;
case TableBlock.TYPE:
return TableBlock.class;
case VideoBlock.TYPE:
return VideoBlock.class;
default:
if (failOnUnknownProperties) {
throw new JsonParseException("Unsupported layout block type: " + typeName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.slack.api.util.json;

import com.google.gson.*;
import com.slack.api.model.block.RawNumberTableCell;
import com.slack.api.model.block.RawTextTableCell;
import com.slack.api.model.block.RichTextBlock;
import com.slack.api.model.block.TableCell;

import java.lang.reflect.Type;

/**
* Factory for deserializing the cells of a
* {@link com.slack.api.model.block.TableBlock table block}.
*
* @see <a href="https://docs.slack.dev/reference/block-kit/blocks/table-block">Table block</a>
*/
public class GsonTableCellFactory implements JsonDeserializer<TableCell>, JsonSerializer<TableCell> {

private final boolean failOnUnknownProperties;

public GsonTableCellFactory() {
this(false);
}

public GsonTableCellFactory(boolean failOnUnknownProperties) {
this.failOnUnknownProperties = failOnUnknownProperties;
}

@Override
public TableCell deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final JsonPrimitive prim = (JsonPrimitive) jsonObject.get("type");
final String typeName = prim.getAsString();
final Class<? extends TableCell> clazz = getTableCellClassInstance(typeName);
return context.deserialize(jsonObject, clazz);
}

@Override
public JsonElement serialize(TableCell src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src);
}

private Class<? extends TableCell> getTableCellClassInstance(String typeName) {
switch (typeName) {
case RawTextTableCell.TYPE:
return RawTextTableCell.class;
case RawNumberTableCell.TYPE:
return RawNumberTableCell.class;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

🧮 question: Without testing on a personal sandbox I forget if this is stable? It's documented so no blocker here either!

case RichTextBlock.TYPE:
return RichTextBlock.class;
default:
throw new JsonParseException("Unknown table cell type: " + typeName);
}
}
}
Loading
Loading