summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/slack-go/slack/workflow_step.go
blob: bcc892c5ac1d670819f5291556e8816a2863eb0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package slack

import (
	"context"
	"encoding/json"
)

const VTWorkflowStep ViewType = "workflow_step"

type (
	ConfigurationModalRequest struct {
		ModalViewRequest
	}

	WorkflowStepCompleteResponse struct {
		WorkflowStepEditID string                `json:"workflow_step_edit_id"`
		Inputs             *WorkflowStepInputs   `json:"inputs,omitempty"`
		Outputs            *[]WorkflowStepOutput `json:"outputs,omitempty"`
	}

	WorkflowStepInputElement struct {
		Value                   string `json:"value"`
		SkipVariableReplacement bool   `json:"skip_variable_replacement"`
	}

	WorkflowStepInputs map[string]WorkflowStepInputElement

	WorkflowStepOutput struct {
		Name  string `json:"name"`
		Type  string `json:"type"`
		Label string `json:"label"`
	}
)

func NewConfigurationModalRequest(blocks Blocks, privateMetaData string, externalID string) *ConfigurationModalRequest {
	return &ConfigurationModalRequest{
		ModalViewRequest{
			Type:            VTWorkflowStep,
			Title:           nil, // slack configuration modal must not have a title!
			Blocks:          blocks,
			PrivateMetadata: privateMetaData,
			ExternalID:      externalID,
		},
	}
}

func (api *Client) SaveWorkflowStepConfiguration(workflowStepEditID string, inputs *WorkflowStepInputs, outputs *[]WorkflowStepOutput) error {
	return api.SaveWorkflowStepConfigurationContext(context.Background(), workflowStepEditID, inputs, outputs)
}

func (api *Client) SaveWorkflowStepConfigurationContext(ctx context.Context, workflowStepEditID string, inputs *WorkflowStepInputs, outputs *[]WorkflowStepOutput) error {
	// More information: https://api.slack.com/methods/workflows.updateStep
	wscr := WorkflowStepCompleteResponse{
		WorkflowStepEditID: workflowStepEditID,
		Inputs:             inputs,
		Outputs:            outputs,
	}

	endpoint := api.endpoint + "workflows.updateStep"
	jsonData, err := json.Marshal(wscr)
	if err != nil {
		return err
	}

	response := &SlackResponse{}
	if err := postJSON(ctx, api.httpclient, endpoint, api.token, jsonData, response, api); err != nil {
		return err
	}

	if !response.Ok {
		return response.Err()
	}

	return nil
}

func GetInitialOptionFromWorkflowStepInput(selection *SelectBlockElement, inputs *WorkflowStepInputs, options []*OptionBlockObject) (*OptionBlockObject, bool) {
	if len(*inputs) == 0 {
		return &OptionBlockObject{}, false
	}
	if len(options) == 0 {
		return &OptionBlockObject{}, false
	}

	if val, ok := (*inputs)[selection.ActionID]; ok {
		if val.SkipVariableReplacement {
			return &OptionBlockObject{}, false
		}

		for _, option := range options {
			if option.Value == val.Value {
				return option, true
			}
		}
	}

	return &OptionBlockObject{}, false
}