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