summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/slack-go/slack/views.go
blob: a3a1bd0569f1cc759f9ad7f6599e50cb498f2e2d (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package slack

import (
	"context"
	"encoding/json"
)

const (
	VTModal   ViewType = "modal"
	VTHomeTab ViewType = "home"
)

type ViewType string

type ViewState struct {
	Values map[string]map[string]BlockAction `json:"values"`
}

type View struct {
	SlackResponse
	ID              string           `json:"id"`
	TeamID          string           `json:"team_id"`
	Type            ViewType         `json:"type"`
	Title           *TextBlockObject `json:"title"`
	Close           *TextBlockObject `json:"close"`
	Submit          *TextBlockObject `json:"submit"`
	Blocks          Blocks           `json:"blocks"`
	PrivateMetadata string           `json:"private_metadata"`
	CallbackID      string           `json:"callback_id"`
	State           *ViewState       `json:"state"`
	Hash            string           `json:"hash"`
	ClearOnClose    bool             `json:"clear_on_close"`
	NotifyOnClose   bool             `json:"notify_on_close"`
	RootViewID      string           `json:"root_view_id"`
	PreviousViewID  string           `json:"previous_view_id"`
	AppID           string           `json:"app_id"`
	ExternalID      string           `json:"external_id"`
	BotID           string           `json:"bot_id"`
}

type ViewSubmissionCallbackResponseURL struct {
	BlockID     string `json:"block_id"`
	ActionID    string `json:"action_id"`
	ChannelID   string `json:"channel_id"`
	ResponseURL string `json:"response_url"`
}

type ViewSubmissionCallback struct {
	Hash         string                              `json:"hash"`
	ResponseURLs []ViewSubmissionCallbackResponseURL `json:"response_urls,omitempty"`
}

type ViewClosedCallback struct {
	IsCleared bool `json:"is_cleared"`
}

const (
	RAClear  ViewResponseAction = "clear"
	RAUpdate ViewResponseAction = "update"
	RAPush   ViewResponseAction = "push"
	RAErrors ViewResponseAction = "errors"
)

type ViewResponseAction string

type ViewSubmissionResponse struct {
	ResponseAction ViewResponseAction `json:"response_action"`
	View           *ModalViewRequest  `json:"view,omitempty"`
	Errors         map[string]string  `json:"errors,omitempty"`
}

func NewClearViewSubmissionResponse() *ViewSubmissionResponse {
	return &ViewSubmissionResponse{
		ResponseAction: RAClear,
	}
}

func NewUpdateViewSubmissionResponse(view *ModalViewRequest) *ViewSubmissionResponse {
	return &ViewSubmissionResponse{
		ResponseAction: RAUpdate,
		View:           view,
	}
}

func NewPushViewSubmissionResponse(view *ModalViewRequest) *ViewSubmissionResponse {
	return &ViewSubmissionResponse{
		ResponseAction: RAPush,
		View:           view,
	}
}

func NewErrorsViewSubmissionResponse(errors map[string]string) *ViewSubmissionResponse {
	return &ViewSubmissionResponse{
		ResponseAction: RAErrors,
		Errors:         errors,
	}
}

type ModalViewRequest struct {
	Type            ViewType         `json:"type"`
	Title           *TextBlockObject `json:"title,omitempty"`
	Blocks          Blocks           `json:"blocks"`
	Close           *TextBlockObject `json:"close,omitempty"`
	Submit          *TextBlockObject `json:"submit,omitempty"`
	PrivateMetadata string           `json:"private_metadata,omitempty"`
	CallbackID      string           `json:"callback_id,omitempty"`
	ClearOnClose    bool             `json:"clear_on_close,omitempty"`
	NotifyOnClose   bool             `json:"notify_on_close,omitempty"`
	ExternalID      string           `json:"external_id,omitempty"`
}

func (v *ModalViewRequest) ViewType() ViewType {
	return v.Type
}

type HomeTabViewRequest struct {
	Type            ViewType `json:"type"`
	Blocks          Blocks   `json:"blocks"`
	PrivateMetadata string   `json:"private_metadata,omitempty"`
	CallbackID      string   `json:"callback_id,omitempty"`
	ExternalID      string   `json:"external_id,omitempty"`
}

func (v *HomeTabViewRequest) ViewType() ViewType {
	return v.Type
}

type openViewRequest struct {
	TriggerID string           `json:"trigger_id"`
	View      ModalViewRequest `json:"view"`
}

type publishViewRequest struct {
	UserID string             `json:"user_id"`
	View   HomeTabViewRequest `json:"view"`
	Hash   string             `json:"hash,omitempty"`
}

type pushViewRequest struct {
	TriggerID string           `json:"trigger_id"`
	View      ModalViewRequest `json:"view"`
}

type updateViewRequest struct {
	View       ModalViewRequest `json:"view"`
	ExternalID string           `json:"external_id,omitempty"`
	Hash       string           `json:"hash,omitempty"`
	ViewID     string           `json:"view_id,omitempty"`
}

type ViewResponse struct {
	SlackResponse
	View `json:"view"`
}

// OpenView opens a view for a user.
func (api *Client) OpenView(triggerID string, view ModalViewRequest) (*ViewResponse, error) {
	return api.OpenViewContext(context.Background(), triggerID, view)
}

// ValidateUniqueBlockID will verify if each input block has a unique block ID if set
func ValidateUniqueBlockID(view ModalViewRequest) bool {

	uniqueBlockID := map[string]bool{}

	for _, b := range view.Blocks.BlockSet {
		if inputBlock, ok := b.(*InputBlock); ok {
			if _, ok := uniqueBlockID[inputBlock.BlockID]; ok {
				return false
			}
			uniqueBlockID[inputBlock.BlockID] = true
		}
	}

	return true
}

// OpenViewContext opens a view for a user with a custom context.
func (api *Client) OpenViewContext(
	ctx context.Context,
	triggerID string,
	view ModalViewRequest,
) (*ViewResponse, error) {
	if triggerID == "" {
		return nil, ErrParametersMissing
	}

	if !ValidateUniqueBlockID(view) {
		return nil, ErrBlockIDNotUnique
	}

	req := openViewRequest{
		TriggerID: triggerID,
		View:      view,
	}
	encoded, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	endpoint := api.endpoint + "views.open"
	resp := &ViewResponse{}
	err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
	if err != nil {
		return nil, err
	}
	return resp, resp.Err()
}

// PublishView publishes a static view for a user.
func (api *Client) PublishView(userID string, view HomeTabViewRequest, hash string) (*ViewResponse, error) {
	return api.PublishViewContext(context.Background(), userID, view, hash)
}

// PublishViewContext publishes a static view for a user with a custom context.
func (api *Client) PublishViewContext(
	ctx context.Context,
	userID string,
	view HomeTabViewRequest,
	hash string,
) (*ViewResponse, error) {
	if userID == "" {
		return nil, ErrParametersMissing
	}
	req := publishViewRequest{
		UserID: userID,
		View:   view,
		Hash:   hash,
	}
	encoded, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	endpoint := api.endpoint + "views.publish"
	resp := &ViewResponse{}
	err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
	if err != nil {
		return nil, err
	}
	return resp, resp.Err()
}

// PushView pushes a view onto the stack of a root view.
func (api *Client) PushView(triggerID string, view ModalViewRequest) (*ViewResponse, error) {
	return api.PushViewContext(context.Background(), triggerID, view)
}

// PublishViewContext pushes a view onto the stack of a root view with a custom context.
func (api *Client) PushViewContext(
	ctx context.Context,
	triggerID string,
	view ModalViewRequest,
) (*ViewResponse, error) {
	if triggerID == "" {
		return nil, ErrParametersMissing
	}
	req := pushViewRequest{
		TriggerID: triggerID,
		View:      view,
	}
	encoded, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	endpoint := api.endpoint + "views.push"
	resp := &ViewResponse{}
	err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
	if err != nil {
		return nil, err
	}
	return resp, resp.Err()
}

// UpdateView updates an existing view.
func (api *Client) UpdateView(view ModalViewRequest, externalID, hash, viewID string) (*ViewResponse, error) {
	return api.UpdateViewContext(context.Background(), view, externalID, hash, viewID)
}

// UpdateViewContext updates an existing view with a custom context.
func (api *Client) UpdateViewContext(
	ctx context.Context,
	view ModalViewRequest,
	externalID, hash,
	viewID string,
) (*ViewResponse, error) {
	if externalID == "" && viewID == "" {
		return nil, ErrParametersMissing
	}
	req := updateViewRequest{
		View:       view,
		ExternalID: externalID,
		Hash:       hash,
		ViewID:     viewID,
	}
	encoded, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	endpoint := api.endpoint + "views.update"
	resp := &ViewResponse{}
	err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
	if err != nil {
		return nil, err
	}
	return resp, resp.Err()
}