-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfolders.go
361 lines (318 loc) · 10.4 KB
/
folders.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package deviantart
import (
"fmt"
"github.com/dghubble/sling"
"github.com/google/uuid"
)
// CurrentUser allows to use endpoints for current user.
const CurrentUser = ""
// TODO: Embed to Gallery and Collection?
type Folder struct {
// FolderID int64 `json:"folderid"` // TODO: Remove it?
FolderID uuid.UUID `json:"folderid"`
Name string `json:"name"`
Owner *User `json:"owner,omitempty"` // TODO: Do we need this field?
}
type FoldersService[T Collection | Gallery] struct {
sling *sling.Sling
}
func newFoldersService[T Collection | Gallery](sling *sling.Sling) *FoldersService[T] {
return &FoldersService[T]{
sling: sling,
}
}
type FolderParams struct {
// The user who owns the folder, defaults to current user.
Username string `url:"username,omitempty"`
// Sort results by either newest or popular (when querying all folders
// only).
// This field is supported only by galleries.
SortMode string `url:"mode,omitempty"` // values(newest, popular) default: popular
}
type FoldersParams struct {
// The user to list folders for, if omitted the authenticated user is used.
Username string `url:"username,omitempty"`
// The option to include the content count per each collection folder.
CalculateSize bool `url:"calculate_size,omitempty"`
// Include first 5 deviations from the folder.
IncludePreload bool `url:"ext_preload,omitempty"`
// Filters collections with no deviations if true.
FilterEmptyFolder bool `url:"filter_empty_folder,omitempty"`
}
type FolderContent struct {
OffsetResponse[Deviation]
Name string `json:"name"`
}
// Folder fetches folder contents.
//
// To connect to this endpoint OAuth2 Access Token from the Client Credentials
// Grant, or Authorization Code Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
func (s *FoldersService[T]) Folder(folderID uuid.UUID, params *FolderParams, page *OffsetParams) (FolderContent, error) {
var (
success FolderContent
failure Error
)
_, err := s.sling.New().Get(folderID.String()).QueryStruct(params).QueryStruct(page).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return FolderContent{}, fmt.Errorf("unable to fetch folder: %w", err)
}
return success, nil
}
type usernameParams struct {
Username string `url:"username,omitempty"`
}
// All gets the "all" view of a users collection/gallery.
//
// To connect to this endpoint OAuth2 Access Token from the Client Credentials
// Grant, or Authorization Code Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
func (s *FoldersService[T]) All(username string, page *OffsetParams) (OffsetResponse[Deviation], error) {
var (
success OffsetResponse[Deviation]
failure Error
)
params := &usernameParams{Username: username}
_, err := s.sling.New().Get("all").QueryStruct(params).QueryStruct(page).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return OffsetResponse[Deviation]{}, fmt.Errorf("unable to fetch all content: %w", err)
}
return success, nil
}
// Folders fetches collection folders.
//
// To connect to this endpoint OAuth2 Access Token from the Client Credentials
// Grant, or Authorization Code Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
//
// TODO: Support `ext_preload` for collections.
func (s *FoldersService[T]) Folders(params *FoldersParams, page *OffsetParams) (OffsetResponse[T], error) {
var (
success OffsetResponse[T]
failure Error
)
_, err := s.sling.New().Get("folders").QueryStruct(params).QueryStruct(page).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return OffsetResponse[T]{}, fmt.Errorf("unable to fetch folders: %w", err)
}
return success, nil
}
type CopyDeviationsParams struct {
TargetFolderID uuid.UUID `url:"target_folderid,omitempty"`
DeviationIDs []uuid.UUID `url:"deviationids,omitempty"`
}
// CopyDeviations copies a list of deviations to a folder destination.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) CopyDeviations(params *CopyDeviationsParams) error {
var (
failure Error
)
_, err := s.sling.New().Get("folders/copy_deviations").QueryStruct(params).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to copy deviations: %w", err)
}
return nil
}
type CreateFolderParams struct {
// The name of the folder to create.
Folder string `url:"folder"`
// Folder description.
// This field is supported only by galleries.
Description string `url:"description,omitempty"`
// The UUID of the parent gallery if this is a subgallery.
// This field is supported only by galleries.
ParentFolderID uuid.UUID `url:"parent_folderid,omitempty"`
}
// Creates new collection folder.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) Create(params *CreateFolderParams) (Folder, error) {
var (
success Folder
failure Error
)
_, err := s.sling.New().Post("folders/create").BodyForm(params).Receive(&success, &failure)
if err := relevantError(err, failure); err != nil {
return Folder{}, fmt.Errorf("unable to create folder: %w", err)
}
return success, nil
}
type MoveDeviationsParams struct {
// The UUID of the folder to copy to.
SourceFolderID uuid.UUID `url:"source_folderid"`
// The UUID of the folder to copy to.
TargetFolderID uuid.UUID `url:"target_folderid"`
// The UUIDs of the deviations.
DeviationIDs []uuid.UUID `url:"deviationids"`
}
// MoveDeviations moves a list of deviations to a folder destination.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) MoveDeviations(params *MoveDeviationsParams) error {
var (
failure Error
)
_, err := s.sling.New().Post("folders/move_destination").BodyForm(params).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to move deviations: %w", err)
}
return nil
}
// Remove deletes collection folder.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) Remove(folderID uuid.UUID) error {
var (
failure Error
)
_, err := s.sling.New().Get("folders/remove/").Path(folderID.String()).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to remove folder: %w", err)
}
return nil
}
type RemoveDeviationsParams struct {
// The UUID of the folder to remove.
FolderID uuid.UUID `url:"folderid"`
// The UUIDs of the deviations.
DeviationIDs []uuid.UUID `url:"deviationids"`
}
// RemoveDeviations removes a list of deviations from a gallery folder.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) RemoveDeviations(params *RemoveDeviationsParams) error {
var (
failure Error
)
_, err := s.sling.New().Post("folders/remove_deviations").BodyForm(params).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to remove deviations: %w", err)
}
return nil
}
type UpdateFoldersParams struct {
// The UUID of the folder to rename.
FolderID uuid.UUID `url:"folderid"`
// Folder new name.
Name string `url:"name,omitempty"`
// Folder description.
Description string `url:"description,omitempty"`
// Folder thumb.
CoverDeviationID uuid.UUID `url:"cover_deviationid,omitempty"`
}
// Update updates folder.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) Update(params *UpdateFoldersParams) error {
var (
failure Error
)
_, err := s.sling.New().Get("folders/update").QueryStruct(params).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to update folders: %w", err)
}
return nil
}
type UpdateDeviationOrderParams struct {
// The UUID of the gallery folder.
FolderID uuid.UUID `url:"folderid"`
// The UUID of the deviation.
DeviationID uuid.UUID `url:"deviationid"`
// The new position.
Position int `url:"position"`
}
// UpdateDeviationOrder updates order of deviation in folder.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) UpdateDeviationOrder(params *UpdateDeviationOrderParams) error {
var (
failure Error
)
_, err := s.sling.New().Post("folders/update_deviation_order").BodyForm(params).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to update deviations order: %w", err)
}
return nil
}
type UpdateOrderParams struct {
// The UUID of the folder to reposition.
FolderID uuid.UUID `url:"folderid"`
// The new position.
Position int `url:"position"`
}
// UpdateOrder rearranges the position of folders.
//
// To connect to this endpoint OAuth2 Access Token from the Authorization Code
// Grant is required.
//
// The following scopes are required to access this resource:
//
// - browse
// - collection or gallery
func (s *FoldersService[T]) UpdateOrder(folderID uuid.UUID, position int) error {
type updateOrderParams struct {
FolderID string `url:"folderid"`
Position int `url:"position"`
}
var (
failure Error
)
params := &updateOrderParams{FolderID: folderID.String(), Position: position}
_, err := s.sling.New().Post("folders/update_order").BodyForm(params).Receive(nil, &failure)
if err := relevantError(err, failure); err != nil {
return fmt.Errorf("unable to update folders order: %w", err)
}
return nil
}