Debian Codegraph @

views.go

raw symbols SCIP
1 package slack
2
3 import (
4 "context"
5 "encoding/json"
6 )
7
8 const (
9 VTModal ViewType = "modal"
10 VTHomeTab ViewType = "home"
11 )
12
13 type ViewType string
14
15 type ViewState struct {
16 Values map[string]map[string]BlockAction `json:"values"`
17 }
18
19 type View struct {
20 SlackResponse
21 ID string `json:"id"`
22 TeamID string `json:"team_id"`
23 Type ViewType `json:"type"`
24 Title *TextBlockObject `json:"title"`
25 Close *TextBlockObject `json:"close"`
26 Submit *TextBlockObject `json:"submit"`
27 Blocks Blocks `json:"blocks"`
28 PrivateMetadata string `json:"private_metadata"`
29 CallbackID string `json:"callback_id"`
30 State *ViewState `json:"state"`
31 Hash string `json:"hash"`
32 ClearOnClose bool `json:"clear_on_close"`
33 NotifyOnClose bool `json:"notify_on_close"`
34 RootViewID string `json:"root_view_id"`
35 PreviousViewID string `json:"previous_view_id"`
36 AppID string `json:"app_id"`
37 ExternalID string `json:"external_id"`
38 BotID string `json:"bot_id"`
39 AppInstalledTeamID string `json:"app_installed_team_id"`
40 }
41
42 type ViewSubmissionCallbackResponseURL struct {
43 BlockID string `json:"block_id"`
44 ActionID string `json:"action_id"`
45 ChannelID string `json:"channel_id"`
46 ResponseURL string `json:"response_url"`
47 }
48
49 type ViewSubmissionCallback struct {
50 Hash string `json:"hash"`
51 ResponseURLs []ViewSubmissionCallbackResponseURL `json:"response_urls,omitempty"`
52 }
53
54 type ViewClosedCallback struct {
55 IsCleared bool `json:"is_cleared"`
56 }
57
58 const (
59 RAClear ViewResponseAction = "clear"
60 RAUpdate ViewResponseAction = "update"
61 RAPush ViewResponseAction = "push"
62 RAErrors ViewResponseAction = "errors"
63 )
64
65 type ViewResponseAction string
66
67 type ViewSubmissionResponse struct {
68 ResponseAction ViewResponseAction `json:"response_action"`
69 View *ModalViewRequest `json:"view,omitempty"`
70 Errors map[string]string `json:"errors,omitempty"`
71 }
72
73 func NewClearViewSubmissionResponse() *ViewSubmissionResponse {
74 return &ViewSubmissionResponse{
75 ResponseAction: RAClear,
76 }
77 }
78
79 func NewUpdateViewSubmissionResponse(view *ModalViewRequest) *ViewSubmissionResponse {
80 return &ViewSubmissionResponse{
81 ResponseAction: RAUpdate,
82 View: view,
83 }
84 }
85
86 func NewPushViewSubmissionResponse(view *ModalViewRequest) *ViewSubmissionResponse {
87 return &ViewSubmissionResponse{
88 ResponseAction: RAPush,
89 View: view,
90 }
91 }
92
93 func NewErrorsViewSubmissionResponse(errors map[string]string) *ViewSubmissionResponse {
94 return &ViewSubmissionResponse{
95 ResponseAction: RAErrors,
96 Errors: errors,
97 }
98 }
99
100 type ModalViewRequest struct {
101 Type ViewType `json:"type"`
102 Title *TextBlockObject `json:"title,omitempty"`
103 Blocks Blocks `json:"blocks"`
104 Close *TextBlockObject `json:"close,omitempty"`
105 Submit *TextBlockObject `json:"submit,omitempty"`
106 PrivateMetadata string `json:"private_metadata,omitempty"`
107 CallbackID string `json:"callback_id,omitempty"`
108 ClearOnClose bool `json:"clear_on_close,omitempty"`
109 NotifyOnClose bool `json:"notify_on_close,omitempty"`
110 ExternalID string `json:"external_id,omitempty"`
111 }
112
113 func (v *ModalViewRequest) ViewType() ViewType {
114 return v.Type
115 }
116
117 type HomeTabViewRequest struct {
118 Type ViewType `json:"type"`
119 Blocks Blocks `json:"blocks"`
120 PrivateMetadata string `json:"private_metadata,omitempty"`
121 CallbackID string `json:"callback_id,omitempty"`
122 ExternalID string `json:"external_id,omitempty"`
123 }
124
125 func (v *HomeTabViewRequest) ViewType() ViewType {
126 return v.Type
127 }
128
129 type openViewRequest struct {
130 TriggerID string `json:"trigger_id"`
131 View ModalViewRequest `json:"view"`
132 }
133
134 type publishViewRequest struct {
135 UserID string `json:"user_id"`
136 View HomeTabViewRequest `json:"view"`
137 Hash string `json:"hash,omitempty"`
138 }
139
140 type pushViewRequest struct {
141 TriggerID string `json:"trigger_id"`
142 View ModalViewRequest `json:"view"`
143 }
144
145 type updateViewRequest struct {
146 View ModalViewRequest `json:"view"`
147 ExternalID string `json:"external_id,omitempty"`
148 Hash string `json:"hash,omitempty"`
149 ViewID string `json:"view_id,omitempty"`
150 }
151
152 type ViewResponse struct {
153 SlackResponse
154 View `json:"view"`
155 }
156
157 // OpenView opens a view for a user.
158 func (api *Client) OpenView(triggerID string, view ModalViewRequest) (*ViewResponse, error) {
159 return api.OpenViewContext(context.Background(), triggerID, view)
160 }
161
162 // ValidateUniqueBlockID will verify if each input block has a unique block ID if set
163 func ValidateUniqueBlockID(view ModalViewRequest) bool {
164
165 uniqueBlockID := map[string]bool{}
166
167 for _, b := range view.Blocks.BlockSet {
168 if inputBlock, ok := b.(*InputBlock); ok {
169 if _, ok := uniqueBlockID[inputBlock.BlockID]; ok {
170 return false
171 }
172 uniqueBlockID[inputBlock.BlockID] = true
173 }
174 }
175
176 return true
177 }
178
179 // OpenViewContext opens a view for a user with a custom context.
180 func (api *Client) OpenViewContext(
181 ctx context.Context,
182 triggerID string,
183 view ModalViewRequest,
184 ) (*ViewResponse, error) {
185 if triggerID == "" {
186 return nil, ErrParametersMissing
187 }
188
189 if !ValidateUniqueBlockID(view) {
190 return nil, ErrBlockIDNotUnique
191 }
192
193 req := openViewRequest{
194 TriggerID: triggerID,
195 View: view,
196 }
197 encoded, err := json.Marshal(req)
198 if err != nil {
199 return nil, err
200 }
201 endpoint := api.endpoint + "views.open"
202 resp := &ViewResponse{}
203 err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
204 if err != nil {
205 return nil, err
206 }
207 return resp, resp.Err()
208 }
209
210 // PublishView publishes a static view for a user.
211 func (api *Client) PublishView(userID string, view HomeTabViewRequest, hash string) (*ViewResponse, error) {
212 return api.PublishViewContext(context.Background(), userID, view, hash)
213 }
214
215 // PublishViewContext publishes a static view for a user with a custom context.
216 func (api *Client) PublishViewContext(
217 ctx context.Context,
218 userID string,
219 view HomeTabViewRequest,
220 hash string,
221 ) (*ViewResponse, error) {
222 if userID == "" {
223 return nil, ErrParametersMissing
224 }
225 req := publishViewRequest{
226 UserID: userID,
227 View: view,
228 Hash: hash,
229 }
230 encoded, err := json.Marshal(req)
231 if err != nil {
232 return nil, err
233 }
234 endpoint := api.endpoint + "views.publish"
235 resp := &ViewResponse{}
236 err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
237 if err != nil {
238 return nil, err
239 }
240 return resp, resp.Err()
241 }
242
243 // PushView pushes a view onto the stack of a root view.
244 func (api *Client) PushView(triggerID string, view ModalViewRequest) (*ViewResponse, error) {
245 return api.PushViewContext(context.Background(), triggerID, view)
246 }
247
248 // PublishViewContext pushes a view onto the stack of a root view with a custom context.
249 func (api *Client) PushViewContext(
250 ctx context.Context,
251 triggerID string,
252 view ModalViewRequest,
253 ) (*ViewResponse, error) {
254 if triggerID == "" {
255 return nil, ErrParametersMissing
256 }
257 req := pushViewRequest{
258 TriggerID: triggerID,
259 View: view,
260 }
261 encoded, err := json.Marshal(req)
262 if err != nil {
263 return nil, err
264 }
265 endpoint := api.endpoint + "views.push"
266 resp := &ViewResponse{}
267 err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
268 if err != nil {
269 return nil, err
270 }
271 return resp, resp.Err()
272 }
273
274 // UpdateView updates an existing view.
275 func (api *Client) UpdateView(view ModalViewRequest, externalID, hash, viewID string) (*ViewResponse, error) {
276 return api.UpdateViewContext(context.Background(), view, externalID, hash, viewID)
277 }
278
279 // UpdateViewContext updates an existing view with a custom context.
280 func (api *Client) UpdateViewContext(
281 ctx context.Context,
282 view ModalViewRequest,
283 externalID, hash,
284 viewID string,
285 ) (*ViewResponse, error) {
286 if externalID == "" && viewID == "" {
287 return nil, ErrParametersMissing
288 }
289 req := updateViewRequest{
290 View: view,
291 ExternalID: externalID,
292 Hash: hash,
293 ViewID: viewID,
294 }
295 encoded, err := json.Marshal(req)
296 if err != nil {
297 return nil, err
298 }
299 endpoint := api.endpoint + "views.update"
300 resp := &ViewResponse{}
301 err = postJSON(ctx, api.httpclient, endpoint, api.token, encoded, resp, api)
302 if err != nil {
303 return nil, err
304 }
305 return resp, resp.Err()
306 }