|
| 1 | +package slack |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/url" |
| 7 | +) |
| 8 | + |
| 9 | +type CanvasDetails struct { |
| 10 | + CanvasID string `json:"canvas_id"` |
| 11 | +} |
| 12 | + |
| 13 | +type DocumentContent struct { |
| 14 | + Type string `json:"type"` |
| 15 | + Markdown string `json:"markdown,omitempty"` |
| 16 | +} |
| 17 | + |
| 18 | +type CanvasChange struct { |
| 19 | + Operation string `json:"operation"` |
| 20 | + SectionID string `json:"section_id,omitempty"` |
| 21 | + DocumentContent DocumentContent `json:"document_content"` |
| 22 | +} |
| 23 | + |
| 24 | +type EditCanvasParams struct { |
| 25 | + CanvasID string `json:"canvas_id"` |
| 26 | + Changes []CanvasChange `json:"changes"` |
| 27 | +} |
| 28 | + |
| 29 | +type SetCanvasAccessParams struct { |
| 30 | + CanvasID string `json:"canvas_id"` |
| 31 | + AccessLevel string `json:"access_level"` |
| 32 | + ChannelIDs []string `json:"channel_ids,omitempty"` |
| 33 | + UserIDs []string `json:"user_ids,omitempty"` |
| 34 | +} |
| 35 | + |
| 36 | +type DeleteCanvasAccessParams struct { |
| 37 | + CanvasID string `json:"canvas_id"` |
| 38 | + ChannelIDs []string `json:"channel_ids,omitempty"` |
| 39 | + UserIDs []string `json:"user_ids,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +type LookupCanvasSectionsCriteria struct { |
| 43 | + SectionTypes []string `json:"section_types,omitempty"` |
| 44 | + ContainsText string `json:"contains_text,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +type LookupCanvasSectionsParams struct { |
| 48 | + CanvasID string `json:"canvas_id"` |
| 49 | + Criteria LookupCanvasSectionsCriteria `json:"criteria"` |
| 50 | +} |
| 51 | + |
| 52 | +type CanvasSection struct { |
| 53 | + ID string `json:"id"` |
| 54 | +} |
| 55 | + |
| 56 | +type LookupCanvasSectionsResponse struct { |
| 57 | + SlackResponse |
| 58 | + Sections []CanvasSection `json:"sections"` |
| 59 | +} |
| 60 | + |
| 61 | +// CreateCanvas creates a new canvas. |
| 62 | +// For more details, see CreateCanvasContext documentation. |
| 63 | +func (api *Client) CreateCanvas(title string, documentContent DocumentContent) (string, error) { |
| 64 | + return api.CreateCanvasContext(context.Background(), title, documentContent) |
| 65 | +} |
| 66 | + |
| 67 | +// CreateCanvasContext creates a new canvas with a custom context. |
| 68 | +// Slack API docs: https://api.slack.com/methods/canvases.create |
| 69 | +func (api *Client) CreateCanvasContext(ctx context.Context, title string, documentContent DocumentContent) (string, error) { |
| 70 | + values := url.Values{ |
| 71 | + "token": {api.token}, |
| 72 | + } |
| 73 | + if title != "" { |
| 74 | + values.Add("title", title) |
| 75 | + } |
| 76 | + if documentContent.Type != "" { |
| 77 | + documentContentJSON, err := json.Marshal(documentContent) |
| 78 | + if err != nil { |
| 79 | + return "", err |
| 80 | + } |
| 81 | + values.Add("document_content", string(documentContentJSON)) |
| 82 | + } |
| 83 | + |
| 84 | + response := struct { |
| 85 | + SlackResponse |
| 86 | + CanvasID string `json:"canvas_id"` |
| 87 | + }{} |
| 88 | + |
| 89 | + err := api.postMethod(ctx, "canvases.create", values, &response) |
| 90 | + if err != nil { |
| 91 | + return "", err |
| 92 | + } |
| 93 | + |
| 94 | + return response.CanvasID, response.Err() |
| 95 | +} |
| 96 | + |
| 97 | +// DeleteCanvas deletes an existing canvas. |
| 98 | +// For more details, see DeleteCanvasContext documentation. |
| 99 | +func (api *Client) DeleteCanvas(canvasID string) error { |
| 100 | + return api.DeleteCanvasContext(context.Background(), canvasID) |
| 101 | +} |
| 102 | + |
| 103 | +// DeleteCanvasContext deletes an existing canvas with a custom context. |
| 104 | +// Slack API docs: https://api.slack.com/methods/canvases.delete |
| 105 | +func (api *Client) DeleteCanvasContext(ctx context.Context, canvasID string) error { |
| 106 | + values := url.Values{ |
| 107 | + "token": {api.token}, |
| 108 | + "canvas_id": {canvasID}, |
| 109 | + } |
| 110 | + |
| 111 | + response := struct { |
| 112 | + SlackResponse |
| 113 | + }{} |
| 114 | + |
| 115 | + err := api.postMethod(ctx, "canvases.delete", values, &response) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + return response.Err() |
| 121 | +} |
| 122 | + |
| 123 | +// EditCanvas edits an existing canvas. |
| 124 | +// For more details, see EditCanvasContext documentation. |
| 125 | +func (api *Client) EditCanvas(params EditCanvasParams) error { |
| 126 | + return api.EditCanvasContext(context.Background(), params) |
| 127 | +} |
| 128 | + |
| 129 | +// EditCanvasContext edits an existing canvas with a custom context. |
| 130 | +// Slack API docs: https://api.slack.com/methods/canvases.edit |
| 131 | +func (api *Client) EditCanvasContext(ctx context.Context, params EditCanvasParams) error { |
| 132 | + values := url.Values{ |
| 133 | + "token": {api.token}, |
| 134 | + "canvas_id": {params.CanvasID}, |
| 135 | + } |
| 136 | + |
| 137 | + changesJSON, err := json.Marshal(params.Changes) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + values.Add("changes", string(changesJSON)) |
| 142 | + |
| 143 | + response := struct { |
| 144 | + SlackResponse |
| 145 | + }{} |
| 146 | + |
| 147 | + err = api.postMethod(ctx, "canvases.edit", values, &response) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + return response.Err() |
| 153 | +} |
| 154 | + |
| 155 | +// SetCanvasAccess sets the access level to a canvas for specified entities. |
| 156 | +// For more details, see SetCanvasAccessContext documentation. |
| 157 | +func (api *Client) SetCanvasAccess(params SetCanvasAccessParams) error { |
| 158 | + return api.SetCanvasAccessContext(context.Background(), params) |
| 159 | +} |
| 160 | + |
| 161 | +// SetCanvasAccessContext sets the access level to a canvas for specified entities with a custom context. |
| 162 | +// Slack API docs: https://api.slack.com/methods/canvases.access.set |
| 163 | +func (api *Client) SetCanvasAccessContext(ctx context.Context, params SetCanvasAccessParams) error { |
| 164 | + values := url.Values{ |
| 165 | + "token": {api.token}, |
| 166 | + "canvas_id": {params.CanvasID}, |
| 167 | + "access_level": {params.AccessLevel}, |
| 168 | + } |
| 169 | + if len(params.ChannelIDs) > 0 { |
| 170 | + channelIDsJSON, err := json.Marshal(params.ChannelIDs) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + values.Add("channel_ids", string(channelIDsJSON)) |
| 175 | + } |
| 176 | + if len(params.UserIDs) > 0 { |
| 177 | + userIDsJSON, err := json.Marshal(params.UserIDs) |
| 178 | + if err != nil { |
| 179 | + return err |
| 180 | + } |
| 181 | + values.Add("user_ids", string(userIDsJSON)) |
| 182 | + } |
| 183 | + |
| 184 | + response := struct { |
| 185 | + SlackResponse |
| 186 | + }{} |
| 187 | + |
| 188 | + err := api.postMethod(ctx, "canvases.access.set", values, &response) |
| 189 | + if err != nil { |
| 190 | + return err |
| 191 | + } |
| 192 | + |
| 193 | + return response.Err() |
| 194 | +} |
| 195 | + |
| 196 | +// DeleteCanvasAccess removes access to a canvas for specified entities. |
| 197 | +// For more details, see DeleteCanvasAccessContext documentation. |
| 198 | +func (api *Client) DeleteCanvasAccess(params DeleteCanvasAccessParams) error { |
| 199 | + return api.DeleteCanvasAccessContext(context.Background(), params) |
| 200 | +} |
| 201 | + |
| 202 | +// DeleteCanvasAccessContext removes access to a canvas for specified entities with a custom context. |
| 203 | +// Slack API docs: https://api.slack.com/methods/canvases.access.delete |
| 204 | +func (api *Client) DeleteCanvasAccessContext(ctx context.Context, params DeleteCanvasAccessParams) error { |
| 205 | + values := url.Values{ |
| 206 | + "token": {api.token}, |
| 207 | + "canvas_id": {params.CanvasID}, |
| 208 | + } |
| 209 | + if len(params.ChannelIDs) > 0 { |
| 210 | + channelIDsJSON, err := json.Marshal(params.ChannelIDs) |
| 211 | + if err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + values.Add("channel_ids", string(channelIDsJSON)) |
| 215 | + } |
| 216 | + if len(params.UserIDs) > 0 { |
| 217 | + userIDsJSON, err := json.Marshal(params.UserIDs) |
| 218 | + if err != nil { |
| 219 | + return err |
| 220 | + } |
| 221 | + values.Add("user_ids", string(userIDsJSON)) |
| 222 | + } |
| 223 | + |
| 224 | + response := struct { |
| 225 | + SlackResponse |
| 226 | + }{} |
| 227 | + |
| 228 | + err := api.postMethod(ctx, "canvases.access.delete", values, &response) |
| 229 | + if err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + |
| 233 | + return response.Err() |
| 234 | +} |
| 235 | + |
| 236 | +// LookupCanvasSections finds sections matching the provided criteria. |
| 237 | +// For more details, see LookupCanvasSectionsContext documentation. |
| 238 | +func (api *Client) LookupCanvasSections(params LookupCanvasSectionsParams) ([]CanvasSection, error) { |
| 239 | + return api.LookupCanvasSectionsContext(context.Background(), params) |
| 240 | +} |
| 241 | + |
| 242 | +// LookupCanvasSectionsContext finds sections matching the provided criteria with a custom context. |
| 243 | +// Slack API docs: https://api.slack.com/methods/canvases.sections.lookup |
| 244 | +func (api *Client) LookupCanvasSectionsContext(ctx context.Context, params LookupCanvasSectionsParams) ([]CanvasSection, error) { |
| 245 | + values := url.Values{ |
| 246 | + "token": {api.token}, |
| 247 | + "canvas_id": {params.CanvasID}, |
| 248 | + } |
| 249 | + |
| 250 | + criteriaJSON, err := json.Marshal(params.Criteria) |
| 251 | + if err != nil { |
| 252 | + return nil, err |
| 253 | + } |
| 254 | + values.Add("criteria", string(criteriaJSON)) |
| 255 | + |
| 256 | + response := LookupCanvasSectionsResponse{} |
| 257 | + |
| 258 | + err = api.postMethod(ctx, "canvases.sections.lookup", values, &response) |
| 259 | + if err != nil { |
| 260 | + return nil, err |
| 261 | + } |
| 262 | + |
| 263 | + return response.Sections, response.Err() |
| 264 | +} |
0 commit comments