summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/slack-go/slack/remotefiles.go
blob: 8a908a8f3fdc14c1d58faed56d79dc913dcc667d (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
306
307
308
309
310
311
312
313
314
315
316
package slack

import (
	"context"
	"fmt"
	"io"
	"net/url"
	"strconv"
	"strings"
)

const (
	DEFAULT_REMOTE_FILES_CHANNEL = ""
	DEFAULT_REMOTE_FILES_TS_FROM = 0
	DEFAULT_REMOTE_FILES_TS_TO   = -1
	DEFAULT_REMOTE_FILES_COUNT   = 100
)

// RemoteFile contains all the information for a remote file
// For more details:
// https://api.slack.com/messaging/files/remote
type RemoteFile struct {
	ID              string   `json:"id"`
	Created         JSONTime `json:"created"`
	Timestamp       JSONTime `json:"timestamp"`
	Name            string   `json:"name"`
	Title           string   `json:"title"`
	Mimetype        string   `json:"mimetype"`
	Filetype        string   `json:"filetype"`
	PrettyType      string   `json:"pretty_type"`
	User            string   `json:"user"`
	Editable        bool     `json:"editable"`
	Size            int      `json:"size"`
	Mode            string   `json:"mode"`
	IsExternal      bool     `json:"is_external"`
	ExternalType    string   `json:"external_type"`
	IsPublic        bool     `json:"is_public"`
	PublicURLShared bool     `json:"public_url_shared"`
	DisplayAsBot    bool     `json:"display_as_bot"`
	Username        string   `json:"username"`
	URLPrivate      string   `json:"url_private"`
	Permalink       string   `json:"permalink"`
	CommentsCount   int      `json:"comments_count"`
	IsStarred       bool     `json:"is_starred"`
	Shares          Share    `json:"shares"`
	Channels        []string `json:"channels"`
	Groups          []string `json:"groups"`
	IMs             []string `json:"ims"`
	ExternalID      string   `json:"external_id"`
	ExternalURL     string   `json:"external_url"`
	HasRichPreview  bool     `json:"has_rich_preview"`
}

// RemoteFileParameters contains required and optional parameters for a remote file.
//
// ExternalID is a user defined GUID, ExternalURL is where the remote file can be accessed,
// and Title is the name of the file.
//
// For more details:
// https://api.slack.com/methods/files.remote.add
type RemoteFileParameters struct {
	ExternalID            string // required
	ExternalURL           string // required
	Title                 string // required
	Filetype              string
	IndexableFileContents string
	PreviewImage          string
	PreviewImageReader    io.Reader
}

// ListRemoteFilesParameters contains arguments for the ListRemoteFiles method.
// For more details:
// https://api.slack.com/methods/files.remote.list
type ListRemoteFilesParameters struct {
	Channel       string
	Cursor        string
	Limit         int
	TimestampFrom JSONTime
	TimestampTo   JSONTime
}

type remoteFileResponseFull struct {
	RemoteFile `json:"file"`
	Paging     `json:"paging"`
	Files      []RemoteFile `json:"files"`
	SlackResponse
}

func (api *Client) remoteFileRequest(ctx context.Context, path string, values url.Values) (*remoteFileResponseFull, error) {
	response := &remoteFileResponseFull{}
	err := api.postMethod(ctx, path, values, response)
	if err != nil {
		return nil, err
	}

	return response, response.Err()
}

// AddRemoteFile adds a remote file. Unlike regular files, remote files must be explicitly shared.
// For more details:
// https://api.slack.com/methods/files.remote.add
func (api *Client) AddRemoteFile(params RemoteFileParameters) (*RemoteFile, error) {
	return api.AddRemoteFileContext(context.Background(), params)
}

// AddRemoteFileContext adds a remote file and setting a custom context
// For more details see the AddRemoteFile documentation.
func (api *Client) AddRemoteFileContext(ctx context.Context, params RemoteFileParameters) (remotefile *RemoteFile, err error) {
	if params.ExternalID == "" || params.ExternalURL == "" || params.Title == "" {
		return nil, ErrParametersMissing
	}
	response := &remoteFileResponseFull{}
	values := url.Values{
		"token":        {api.token},
		"external_id":  {params.ExternalID},
		"external_url": {params.ExternalURL},
		"title":        {params.Title},
	}
	if params.Filetype != "" {
		values.Add("filetype", params.Filetype)
	}
	if params.IndexableFileContents != "" {
		values.Add("indexable_file_contents", params.IndexableFileContents)
	}
	if params.PreviewImage != "" {
		err = postLocalWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.add", params.PreviewImage, "preview_image", api.token, values, response, api)
	} else if params.PreviewImageReader != nil {
		err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.add", "preview.png", "preview_image", api.token, values, params.PreviewImageReader, response, api)
	} else {
		response, err = api.remoteFileRequest(ctx, "files.remote.add", values)
	}

	if err != nil {
		return nil, err
	}

	return &response.RemoteFile, response.Err()
}

// ListRemoteFiles retrieves all remote files according to the parameters given. Uses cursor based pagination.
// For more details:
// https://api.slack.com/methods/files.remote.list
func (api *Client) ListRemoteFiles(params ListRemoteFilesParameters) ([]RemoteFile, error) {
	return api.ListRemoteFilesContext(context.Background(), params)
}

// ListRemoteFilesContext retrieves all remote files according to the parameters given with a custom context. Uses cursor based pagination.
// For more details see the ListRemoteFiles documentation.
func (api *Client) ListRemoteFilesContext(ctx context.Context, params ListRemoteFilesParameters) ([]RemoteFile, error) {
	values := url.Values{
		"token": {api.token},
	}
	if params.Channel != DEFAULT_REMOTE_FILES_CHANNEL {
		values.Add("channel", params.Channel)
	}
	if params.TimestampFrom != DEFAULT_REMOTE_FILES_TS_FROM {
		values.Add("ts_from", strconv.FormatInt(int64(params.TimestampFrom), 10))
	}
	if params.TimestampTo != DEFAULT_REMOTE_FILES_TS_TO {
		values.Add("ts_to", strconv.FormatInt(int64(params.TimestampTo), 10))
	}
	if params.Limit != DEFAULT_REMOTE_FILES_COUNT {
		values.Add("limit", strconv.Itoa(params.Limit))
	}
	if params.Cursor != "" {
		values.Add("cursor", params.Cursor)
	}

	response, err := api.remoteFileRequest(ctx, "files.remote.list", values)
	if err != nil {
		return nil, err
	}

	params.Cursor = response.SlackResponse.ResponseMetadata.Cursor

	return response.Files, nil
}

// GetRemoteFileInfo retrieves the complete remote file information.
// For more details:
// https://api.slack.com/methods/files.remote.info
func (api *Client) GetRemoteFileInfo(externalID, fileID string) (remotefile *RemoteFile, err error) {
	return api.GetRemoteFileInfoContext(context.Background(), externalID, fileID)
}

// GetRemoteFileInfoContext retrieves the complete remote file information given with a custom context.
// For more details see the GetRemoteFileInfo documentation.
func (api *Client) GetRemoteFileInfoContext(ctx context.Context, externalID, fileID string) (remotefile *RemoteFile, err error) {
	if fileID == "" && externalID == "" {
		return nil, fmt.Errorf("either externalID or fileID is required")
	}
	if fileID != "" && externalID != "" {
		return nil, fmt.Errorf("don't provide both externalID and fileID")
	}
	values := url.Values{
		"token": {api.token},
	}
	if fileID != "" {
		values.Add("file", fileID)
	}
	if externalID != "" {
		values.Add("external_id", externalID)
	}
	response, err := api.remoteFileRequest(ctx, "files.remote.info", values)
	if err != nil {
		return nil, err
	}
	return &response.RemoteFile, err
}

// ShareRemoteFile shares a remote file to channels
// For more details:
// https://api.slack.com/methods/files.remote.share
func (api *Client) ShareRemoteFile(channels []string, externalID, fileID string) (file *RemoteFile, err error) {
	return api.ShareRemoteFileContext(context.Background(), channels, externalID, fileID)
}

// ShareRemoteFileContext shares a remote file to channels with a custom context.
// For more details see the ShareRemoteFile documentation.
func (api *Client) ShareRemoteFileContext(ctx context.Context, channels []string, externalID, fileID string) (file *RemoteFile, err error) {
	if channels == nil || len(channels) == 0 {
		return nil, ErrParametersMissing
	}
	if fileID == "" && externalID == "" {
		return nil, fmt.Errorf("either externalID or fileID is required")
	}
	values := url.Values{
		"token":    {api.token},
		"channels": {strings.Join(channels, ",")},
	}
	if fileID != "" {
		values.Add("file", fileID)
	}
	if externalID != "" {
		values.Add("external_id", externalID)
	}
	response, err := api.remoteFileRequest(ctx, "files.remote.share", values)
	if err != nil {
		return nil, err
	}
	return &response.RemoteFile, err
}

// UpdateRemoteFile updates a remote file
// For more details:
// https://api.slack.com/methods/files.remote.update
func (api *Client) UpdateRemoteFile(fileID string, params RemoteFileParameters) (remotefile *RemoteFile, err error) {
	return api.UpdateRemoteFileContext(context.Background(), fileID, params)
}

// UpdateRemoteFileContext updates a remote file with a custom context
// For more details see the UpdateRemoteFile documentation.
func (api *Client) UpdateRemoteFileContext(ctx context.Context, fileID string, params RemoteFileParameters) (remotefile *RemoteFile, err error) {
	response := &remoteFileResponseFull{}
	values := url.Values{
		"token": {api.token},
	}
	if fileID != "" {
		values.Add("file", fileID)
	}
	if params.ExternalID != "" {
		values.Add("external_id", params.ExternalID)
	}
	if params.ExternalURL != "" {
		values.Add("external_url", params.ExternalURL)
	}
	if params.Title != "" {
		values.Add("title", params.Title)
	}
	if params.Filetype != "" {
		values.Add("filetype", params.Filetype)
	}
	if params.IndexableFileContents != "" {
		values.Add("indexable_file_contents", params.IndexableFileContents)
	}
	if params.PreviewImageReader != nil {
		err = postWithMultipartResponse(ctx, api.httpclient, api.endpoint+"files.remote.update", "preview.png", "preview_image", api.token, values, params.PreviewImageReader, response, api)
	} else {
		response, err = api.remoteFileRequest(ctx, "files.remote.update", values)
	}

	if err != nil {
		return nil, err
	}

	return &response.RemoteFile, response.Err()
}

// RemoveRemoteFile removes a remote file.
// For more details:
// https://api.slack.com/methods/files.remote.remove
func (api *Client) RemoveRemoteFile(externalID, fileID string) (err error) {
	return api.RemoveRemoteFileContext(context.Background(), externalID, fileID)
}

// RemoveRemoteFileContext removes a remote file with a custom context
// For more information see the RemoveRemoteFiles documentation.
func (api *Client) RemoveRemoteFileContext(ctx context.Context, externalID, fileID string) (err error) {
	if fileID == "" && externalID == "" {
		return fmt.Errorf("either externalID or fileID is required")
	}
	if fileID != "" && externalID != "" {
		return fmt.Errorf("don't provide both externalID and fileID")
	}
	values := url.Values{
		"token": {api.token},
	}
	if fileID != "" {
		values.Add("file", fileID)
	}
	if externalID != "" {
		values.Add("external_id", externalID)
	}
	_, err = api.remoteFileRequest(ctx, "files.remote.remove", values)
	return err
}