diff options
Diffstat (limited to 'shared/extra-utils/videos/channels-command.ts')
-rw-r--r-- | shared/extra-utils/videos/channels-command.ts | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/shared/extra-utils/videos/channels-command.ts b/shared/extra-utils/videos/channels-command.ts new file mode 100644 index 000000000..f8eb3f885 --- /dev/null +++ b/shared/extra-utils/videos/channels-command.ts | |||
@@ -0,0 +1,156 @@ | |||
1 | import { pick } from 'lodash' | ||
2 | import { HttpStatusCode, ResultList, VideoChannel, VideoChannelCreateResult } from '@shared/models' | ||
3 | import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' | ||
4 | import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' | ||
5 | import { unwrapBody } from '../requests' | ||
6 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
7 | |||
8 | export class ChannelsCommand extends AbstractCommand { | ||
9 | |||
10 | list (options: OverrideCommandOptions & { | ||
11 | start?: number | ||
12 | count?: number | ||
13 | sort?: string | ||
14 | withStats?: boolean | ||
15 | } = {}) { | ||
16 | const path = '/api/v1/video-channels' | ||
17 | |||
18 | return this.getRequestBody<ResultList<VideoChannel>>({ | ||
19 | ...options, | ||
20 | |||
21 | path, | ||
22 | query: pick(options, [ 'start', 'count', 'sort', 'withStats' ]), | ||
23 | implicitToken: false, | ||
24 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
25 | }) | ||
26 | } | ||
27 | |||
28 | listByAccount (options: OverrideCommandOptions & { | ||
29 | accountName: string | ||
30 | start?: number | ||
31 | count?: number | ||
32 | sort?: string | ||
33 | withStats?: boolean | ||
34 | search?: string | ||
35 | }) { | ||
36 | const { accountName, sort = 'createdAt' } = options | ||
37 | const path = '/api/v1/accounts/' + accountName + '/video-channels' | ||
38 | |||
39 | return this.getRequestBody<ResultList<VideoChannel>>({ | ||
40 | ...options, | ||
41 | |||
42 | path, | ||
43 | query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search' ]) }, | ||
44 | implicitToken: false, | ||
45 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
46 | }) | ||
47 | } | ||
48 | |||
49 | async create (options: OverrideCommandOptions & { | ||
50 | attributes: VideoChannelCreate | ||
51 | }) { | ||
52 | const path = '/api/v1/video-channels/' | ||
53 | |||
54 | // Default attributes | ||
55 | const defaultAttributes = { | ||
56 | displayName: 'my super video channel', | ||
57 | description: 'my super channel description', | ||
58 | support: 'my super channel support' | ||
59 | } | ||
60 | const attributes = { ...defaultAttributes, ...options.attributes } | ||
61 | |||
62 | const body = await unwrapBody<{ videoChannel: VideoChannelCreateResult }>(this.postBodyRequest({ | ||
63 | ...options, | ||
64 | |||
65 | path, | ||
66 | fields: attributes, | ||
67 | implicitToken: true, | ||
68 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
69 | })) | ||
70 | |||
71 | return body.videoChannel | ||
72 | } | ||
73 | |||
74 | update (options: OverrideCommandOptions & { | ||
75 | channelName: string | ||
76 | attributes: VideoChannelUpdate | ||
77 | }) { | ||
78 | const { channelName, attributes } = options | ||
79 | const path = '/api/v1/video-channels/' + channelName | ||
80 | |||
81 | return this.putBodyRequest({ | ||
82 | ...options, | ||
83 | |||
84 | path, | ||
85 | fields: attributes, | ||
86 | implicitToken: true, | ||
87 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
88 | }) | ||
89 | } | ||
90 | |||
91 | delete (options: OverrideCommandOptions & { | ||
92 | channelName: string | ||
93 | }) { | ||
94 | const path = '/api/v1/video-channels/' + options.channelName | ||
95 | |||
96 | return this.deleteRequest({ | ||
97 | ...options, | ||
98 | |||
99 | path, | ||
100 | implicitToken: true, | ||
101 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
102 | }) | ||
103 | } | ||
104 | |||
105 | get (options: OverrideCommandOptions & { | ||
106 | channelName: string | ||
107 | }) { | ||
108 | const path = '/api/v1/video-channels/' + options.channelName | ||
109 | |||
110 | return this.getRequestBody<VideoChannel>({ | ||
111 | ...options, | ||
112 | |||
113 | path, | ||
114 | implicitToken: false, | ||
115 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
116 | }) | ||
117 | } | ||
118 | |||
119 | updateImage (options: OverrideCommandOptions & { | ||
120 | fixture: string | ||
121 | channelName: string | number | ||
122 | type: 'avatar' | 'banner' | ||
123 | }) { | ||
124 | const { channelName, fixture, type } = options | ||
125 | |||
126 | const path = `/api/v1/video-channels/${channelName}/${type}/pick` | ||
127 | |||
128 | return this.updateImageRequest({ | ||
129 | ...options, | ||
130 | |||
131 | path, | ||
132 | fixture, | ||
133 | fieldname: type + 'file', | ||
134 | |||
135 | implicitToken: true, | ||
136 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
137 | }) | ||
138 | } | ||
139 | |||
140 | deleteImage (options: OverrideCommandOptions & { | ||
141 | channelName: string | number | ||
142 | type: 'avatar' | 'banner' | ||
143 | }) { | ||
144 | const { channelName, type } = options | ||
145 | |||
146 | const path = `/api/v1/video-channels/${channelName}/${type}` | ||
147 | |||
148 | return this.deleteRequest({ | ||
149 | ...options, | ||
150 | |||
151 | path, | ||
152 | implicitToken: true, | ||
153 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
154 | }) | ||
155 | } | ||
156 | } | ||