Skip to content

Commit

Permalink
Add SlackOptionsResponse model for Dialog Options handling (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
zmarushchak-hs authored Apr 5, 2022
1 parent 66878b4 commit 40fcfe8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
package com.hubspot.slack.client.models;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.hubspot.slack.client.models.interaction.BlocksLoadOptionsResponse;
import com.hubspot.slack.client.models.interaction.SlackOptionsResponse;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SlackOptionsResponse.class, name = SlackOptionsResponse.TYPE),
@JsonSubTypes.Type(value = BlocksLoadOptionsResponse.class, name = BlocksLoadOptionsResponse.TYPE),
})
public interface BaseSlackOptionsResponse {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
public interface BlocksLoadOptionsResponseIF extends BaseSlackOptionsResponse {
String TYPE = "blocks_load_options_response";

@JsonInclude(Include.NON_EMPTY)
List<Option> getOptions();

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

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.annotation.Nullable;

import org.immutables.value.Value.Derived;
import org.immutables.value.Value.Immutable;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.PropertyNamingStrategy.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.hubspot.immutables.style.HubSpotStyle;
import com.hubspot.slack.client.models.BaseSlackOptionsResponse;
import com.hubspot.slack.client.models.actions.Option;
import com.hubspot.slack.client.models.dialog.form.elements.SlackFormOption;
import com.hubspot.slack.client.models.dialog.form.elements.SlackFormOptionGroup;

@Immutable
@HubSpotStyle
@JsonNaming(SnakeCaseStrategy.class)
@JsonInclude(Include.NON_NULL)
public interface SlackOptionsResponseIF extends BaseSlackOptionsResponse {
String TYPE = "slack_options_response";

// NOTE: slack annoyingly has a different model for dialog options and attachment options
// https://api.slack.com/dialogs | https://api.slack.com/docs/message-menus

static SlackOptionsResponse emptyAttachmentOptions() {
return SlackOptionsResponse
.builder()
.setAttachmentOptions(Collections.emptyList())
.build();
}

static SlackOptionsResponse ofAttachmentOptions(List<Option> options) {
return SlackOptionsResponse.builder().setAttachmentOptions(options).build();
}

static SlackOptionsResponse emptyDialogOptions() {
return SlackOptionsResponse
.builder()
.setDialogOptions(Collections.emptyList())
.build();
}

static SlackOptionsResponse ofDialogOptions(
List<SlackFormOption> dialogOptions
) {
return SlackOptionsResponse.builder().setDialogOptions(dialogOptions).build();
}

static SlackOptionsResponse emptyDialogOptionGroups() {
return SlackOptionsResponse
.builder()
.setDialogOptionGroups(Collections.emptyList())
.build();
}

static SlackOptionsResponse ofDialogOptionGroups(
List<SlackFormOptionGroup> optionGroups
) {
return SlackOptionsResponse.builder().setDialogOptionGroups(optionGroups).build();
}

Optional<List<Option>> getAttachmentOptions();

// attachment option groups aren't yet supported in the slack-client

Optional<List<SlackFormOption>> getDialogOptions();

Optional<List<SlackFormOptionGroup>> getDialogOptionGroups();

@Derived
@Nullable
default Object getOptions() {
return Stream.of(getAttachmentOptions(), getDialogOptions())
.findFirst()
.orElse(null)
.orElse(null);
}

@Derived
@Nullable
default Object getOptionGroups() {
return getDialogOptionGroups().orElse(null);
}
}

0 comments on commit 40fcfe8

Please sign in to comment.