-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
canvas.go
264 lines (221 loc) · 7.31 KB
/
canvas.go
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
package slack
import (
"context"
"encoding/json"
"net/url"
)
type CanvasDetails struct {
CanvasID string `json:"canvas_id"`
}
type DocumentContent struct {
Type string `json:"type"`
Markdown string `json:"markdown,omitempty"`
}
type CanvasChange struct {
Operation string `json:"operation"`
SectionID string `json:"section_id,omitempty"`
DocumentContent DocumentContent `json:"document_content"`
}
type EditCanvasParams struct {
CanvasID string `json:"canvas_id"`
Changes []CanvasChange `json:"changes"`
}
type SetCanvasAccessParams struct {
CanvasID string `json:"canvas_id"`
AccessLevel string `json:"access_level"`
ChannelIDs []string `json:"channel_ids,omitempty"`
UserIDs []string `json:"user_ids,omitempty"`
}
type DeleteCanvasAccessParams struct {
CanvasID string `json:"canvas_id"`
ChannelIDs []string `json:"channel_ids,omitempty"`
UserIDs []string `json:"user_ids,omitempty"`
}
type LookupCanvasSectionsCriteria struct {
SectionTypes []string `json:"section_types,omitempty"`
ContainsText string `json:"contains_text,omitempty"`
}
type LookupCanvasSectionsParams struct {
CanvasID string `json:"canvas_id"`
Criteria LookupCanvasSectionsCriteria `json:"criteria"`
}
type CanvasSection struct {
ID string `json:"id"`
}
type LookupCanvasSectionsResponse struct {
SlackResponse
Sections []CanvasSection `json:"sections"`
}
// CreateCanvas creates a new canvas.
// For more details, see CreateCanvasContext documentation.
func (api *Client) CreateCanvas(title string, documentContent DocumentContent) (string, error) {
return api.CreateCanvasContext(context.Background(), title, documentContent)
}
// CreateCanvasContext creates a new canvas with a custom context.
// Slack API docs: https://api.slack.com/methods/canvases.create
func (api *Client) CreateCanvasContext(ctx context.Context, title string, documentContent DocumentContent) (string, error) {
values := url.Values{
"token": {api.token},
}
if title != "" {
values.Add("title", title)
}
if documentContent.Type != "" {
documentContentJSON, err := json.Marshal(documentContent)
if err != nil {
return "", err
}
values.Add("document_content", string(documentContentJSON))
}
response := struct {
SlackResponse
CanvasID string `json:"canvas_id"`
}{}
err := api.postMethod(ctx, "canvases.create", values, &response)
if err != nil {
return "", err
}
return response.CanvasID, response.Err()
}
// DeleteCanvas deletes an existing canvas.
// For more details, see DeleteCanvasContext documentation.
func (api *Client) DeleteCanvas(canvasID string) error {
return api.DeleteCanvasContext(context.Background(), canvasID)
}
// DeleteCanvasContext deletes an existing canvas with a custom context.
// Slack API docs: https://api.slack.com/methods/canvases.delete
func (api *Client) DeleteCanvasContext(ctx context.Context, canvasID string) error {
values := url.Values{
"token": {api.token},
"canvas_id": {canvasID},
}
response := struct {
SlackResponse
}{}
err := api.postMethod(ctx, "canvases.delete", values, &response)
if err != nil {
return err
}
return response.Err()
}
// EditCanvas edits an existing canvas.
// For more details, see EditCanvasContext documentation.
func (api *Client) EditCanvas(params EditCanvasParams) error {
return api.EditCanvasContext(context.Background(), params)
}
// EditCanvasContext edits an existing canvas with a custom context.
// Slack API docs: https://api.slack.com/methods/canvases.edit
func (api *Client) EditCanvasContext(ctx context.Context, params EditCanvasParams) error {
values := url.Values{
"token": {api.token},
"canvas_id": {params.CanvasID},
}
changesJSON, err := json.Marshal(params.Changes)
if err != nil {
return err
}
values.Add("changes", string(changesJSON))
response := struct {
SlackResponse
}{}
err = api.postMethod(ctx, "canvases.edit", values, &response)
if err != nil {
return err
}
return response.Err()
}
// SetCanvasAccess sets the access level to a canvas for specified entities.
// For more details, see SetCanvasAccessContext documentation.
func (api *Client) SetCanvasAccess(params SetCanvasAccessParams) error {
return api.SetCanvasAccessContext(context.Background(), params)
}
// SetCanvasAccessContext sets the access level to a canvas for specified entities with a custom context.
// Slack API docs: https://api.slack.com/methods/canvases.access.set
func (api *Client) SetCanvasAccessContext(ctx context.Context, params SetCanvasAccessParams) error {
values := url.Values{
"token": {api.token},
"canvas_id": {params.CanvasID},
"access_level": {params.AccessLevel},
}
if len(params.ChannelIDs) > 0 {
channelIDsJSON, err := json.Marshal(params.ChannelIDs)
if err != nil {
return err
}
values.Add("channel_ids", string(channelIDsJSON))
}
if len(params.UserIDs) > 0 {
userIDsJSON, err := json.Marshal(params.UserIDs)
if err != nil {
return err
}
values.Add("user_ids", string(userIDsJSON))
}
response := struct {
SlackResponse
}{}
err := api.postMethod(ctx, "canvases.access.set", values, &response)
if err != nil {
return err
}
return response.Err()
}
// DeleteCanvasAccess removes access to a canvas for specified entities.
// For more details, see DeleteCanvasAccessContext documentation.
func (api *Client) DeleteCanvasAccess(params DeleteCanvasAccessParams) error {
return api.DeleteCanvasAccessContext(context.Background(), params)
}
// DeleteCanvasAccessContext removes access to a canvas for specified entities with a custom context.
// Slack API docs: https://api.slack.com/methods/canvases.access.delete
func (api *Client) DeleteCanvasAccessContext(ctx context.Context, params DeleteCanvasAccessParams) error {
values := url.Values{
"token": {api.token},
"canvas_id": {params.CanvasID},
}
if len(params.ChannelIDs) > 0 {
channelIDsJSON, err := json.Marshal(params.ChannelIDs)
if err != nil {
return err
}
values.Add("channel_ids", string(channelIDsJSON))
}
if len(params.UserIDs) > 0 {
userIDsJSON, err := json.Marshal(params.UserIDs)
if err != nil {
return err
}
values.Add("user_ids", string(userIDsJSON))
}
response := struct {
SlackResponse
}{}
err := api.postMethod(ctx, "canvases.access.delete", values, &response)
if err != nil {
return err
}
return response.Err()
}
// LookupCanvasSections finds sections matching the provided criteria.
// For more details, see LookupCanvasSectionsContext documentation.
func (api *Client) LookupCanvasSections(params LookupCanvasSectionsParams) ([]CanvasSection, error) {
return api.LookupCanvasSectionsContext(context.Background(), params)
}
// LookupCanvasSectionsContext finds sections matching the provided criteria with a custom context.
// Slack API docs: https://api.slack.com/methods/canvases.sections.lookup
func (api *Client) LookupCanvasSectionsContext(ctx context.Context, params LookupCanvasSectionsParams) ([]CanvasSection, error) {
values := url.Values{
"token": {api.token},
"canvas_id": {params.CanvasID},
}
criteriaJSON, err := json.Marshal(params.Criteria)
if err != nil {
return nil, err
}
values.Add("criteria", string(criteriaJSON))
response := LookupCanvasSectionsResponse{}
err = api.postMethod(ctx, "canvases.sections.lookup", values, &response)
if err != nil {
return nil, err
}
return response.Sections, response.Err()
}