]>
Commit | Line | Data |
---|---|---|
a1587156 C |
1 | /* eslint-disable @typescript-eslint/no-floating-promises */ |
2 | ||
5f04dd2f | 3 | import * as request from 'supertest' |
8d2be0ed C |
4 | import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model' |
5 | import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model' | |
213e30ef | 6 | import { makeDeleteRequest, makeGetRequest, updateImageRequest } from '../requests/requests' |
8d2be0ed | 7 | import { ServerInfo } from '../server/servers' |
f6d6e7f8 | 8 | import { MyUser, User } from '../../models/users/user.model' |
8d2be0ed | 9 | import { getMyUserInformation } from '../users/users' |
2d53be02 | 10 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
5f04dd2f | 11 | |
714bfcc5 | 12 | function getVideoChannelsList (url: string, start: number, count: number, sort?: string, withStats?: boolean) { |
48dce1c9 | 13 | const path = '/api/v1/video-channels' |
5f04dd2f C |
14 | |
15 | const req = request(url) | |
16 | .get(path) | |
17 | .query({ start: start }) | |
18 | .query({ count: count }) | |
19 | ||
20 | if (sort) req.query({ sort }) | |
714bfcc5 | 21 | if (withStats) req.query({ withStats }) |
5f04dd2f C |
22 | |
23 | return req.set('Accept', 'application/json') | |
2d53be02 | 24 | .expect(HttpStatusCode.OK_200) |
5f04dd2f C |
25 | .expect('Content-Type', /json/) |
26 | } | |
27 | ||
91b66319 | 28 | function getAccountVideoChannelsList (parameters: { |
a1587156 C |
29 | url: string |
30 | accountName: string | |
31 | start?: number | |
32 | count?: number | |
33 | sort?: string | |
2d53be02 | 34 | specialStatus?: HttpStatusCode |
714bfcc5 | 35 | withStats?: boolean |
7b390964 | 36 | search?: string |
91b66319 | 37 | }) { |
2d53be02 RK |
38 | const { |
39 | url, | |
40 | accountName, | |
41 | start, | |
42 | count, | |
43 | sort = 'createdAt', | |
44 | specialStatus = HttpStatusCode.OK_200, | |
45 | withStats = false, | |
46 | search | |
47 | } = parameters | |
91b66319 | 48 | |
ad9e39fb | 49 | const path = '/api/v1/accounts/' + accountName + '/video-channels' |
5f04dd2f | 50 | |
91b66319 C |
51 | return makeGetRequest({ |
52 | url, | |
53 | path, | |
54 | query: { | |
55 | start, | |
56 | count, | |
714bfcc5 | 57 | sort, |
7b390964 RK |
58 | withStats, |
59 | search | |
91b66319 C |
60 | }, |
61 | statusCodeExpected: specialStatus | |
62 | }) | |
5f04dd2f C |
63 | } |
64 | ||
48dce1c9 C |
65 | function addVideoChannel ( |
66 | url: string, | |
67 | token: string, | |
08c1efbe | 68 | videoChannelAttributesArg: VideoChannelCreate, |
2d53be02 | 69 | expectedStatus = HttpStatusCode.OK_200 |
48dce1c9 | 70 | ) { |
cc918ac3 | 71 | const path = '/api/v1/video-channels/' |
5f04dd2f C |
72 | |
73 | // Default attributes | |
74 | let attributes = { | |
08c1efbe | 75 | displayName: 'my super video channel', |
2422c46b C |
76 | description: 'my super channel description', |
77 | support: 'my super channel support' | |
5f04dd2f C |
78 | } |
79 | attributes = Object.assign(attributes, videoChannelAttributesArg) | |
80 | ||
81 | return request(url) | |
82 | .post(path) | |
83 | .send(attributes) | |
84 | .set('Accept', 'application/json') | |
85 | .set('Authorization', 'Bearer ' + token) | |
86 | .expect(expectedStatus) | |
87 | } | |
88 | ||
48dce1c9 C |
89 | function updateVideoChannel ( |
90 | url: string, | |
91 | token: string, | |
f5b0af50 | 92 | channelName: string, |
08c1efbe | 93 | attributes: VideoChannelUpdate, |
2d53be02 | 94 | expectedStatus = HttpStatusCode.NO_CONTENT_204 |
48dce1c9 | 95 | ) { |
7d14d4d2 | 96 | const body: any = {} |
f5b0af50 | 97 | const path = '/api/v1/video-channels/' + channelName |
5f04dd2f | 98 | |
7d14d4d2 C |
99 | if (attributes.displayName) body.displayName = attributes.displayName |
100 | if (attributes.description) body.description = attributes.description | |
101 | if (attributes.support) body.support = attributes.support | |
102 | if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate | |
5f04dd2f C |
103 | |
104 | return request(url) | |
105 | .put(path) | |
106 | .send(body) | |
107 | .set('Accept', 'application/json') | |
108 | .set('Authorization', 'Bearer ' + token) | |
109 | .expect(expectedStatus) | |
110 | } | |
111 | ||
2d53be02 | 112 | function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) { |
f5b0af50 | 113 | const path = '/api/v1/video-channels/' + channelName |
5f04dd2f C |
114 | |
115 | return request(url) | |
48dce1c9 | 116 | .delete(path) |
5f04dd2f C |
117 | .set('Accept', 'application/json') |
118 | .set('Authorization', 'Bearer ' + token) | |
119 | .expect(expectedStatus) | |
120 | } | |
121 | ||
f5b0af50 C |
122 | function getVideoChannel (url: string, channelName: string) { |
123 | const path = '/api/v1/video-channels/' + channelName | |
5f04dd2f C |
124 | |
125 | return request(url) | |
126 | .get(path) | |
127 | .set('Accept', 'application/json') | |
2d53be02 | 128 | .expect(HttpStatusCode.OK_200) |
5f04dd2f C |
129 | .expect('Content-Type', /json/) |
130 | } | |
131 | ||
213e30ef | 132 | function updateVideoChannelImage (options: { |
a1587156 C |
133 | url: string |
134 | accessToken: string | |
135 | fixture: string | |
8a19bee1 | 136 | videoChannelName: string | number |
213e30ef | 137 | type: 'avatar' | 'banner' |
4bbfc6c6 | 138 | }) { |
213e30ef | 139 | const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}/pick` |
4bbfc6c6 | 140 | |
213e30ef C |
141 | return updateImageRequest({ ...options, path, fieldname: options.type + 'file' }) |
142 | } | |
143 | ||
144 | function deleteVideoChannelImage (options: { | |
145 | url: string | |
146 | accessToken: string | |
147 | videoChannelName: string | number | |
148 | type: 'avatar' | 'banner' | |
149 | }) { | |
150 | const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}` | |
4bbfc6c6 | 151 | |
213e30ef C |
152 | return makeDeleteRequest({ |
153 | url: options.url, | |
154 | token: options.accessToken, | |
155 | path, | |
156 | statusCodeExpected: 204 | |
157 | }) | |
4bbfc6c6 C |
158 | } |
159 | ||
df0b219d C |
160 | function setDefaultVideoChannel (servers: ServerInfo[]) { |
161 | const tasks: Promise<any>[] = [] | |
162 | ||
163 | for (const server of servers) { | |
164 | const p = getMyUserInformation(server.url, server.accessToken) | |
a1587156 | 165 | .then(res => { server.videoChannel = (res.body as User).videoChannels[0] }) |
df0b219d C |
166 | |
167 | tasks.push(p) | |
168 | } | |
169 | ||
170 | return Promise.all(tasks) | |
171 | } | |
172 | ||
f6d6e7f8 | 173 | async function getDefaultVideoChannel (url: string, token: string) { |
174 | const res = await getMyUserInformation(url, token) | |
175 | ||
176 | return (res.body as MyUser).videoChannels[0].id | |
177 | } | |
178 | ||
5f04dd2f C |
179 | // --------------------------------------------------------------------------- |
180 | ||
181 | export { | |
213e30ef | 182 | updateVideoChannelImage, |
5f04dd2f | 183 | getVideoChannelsList, |
975e6e0e | 184 | getAccountVideoChannelsList, |
5f04dd2f C |
185 | addVideoChannel, |
186 | updateVideoChannel, | |
187 | deleteVideoChannel, | |
df0b219d | 188 | getVideoChannel, |
213e30ef | 189 | setDefaultVideoChannel, |
f6d6e7f8 | 190 | deleteVideoChannelImage, |
191 | getDefaultVideoChannel | |
5f04dd2f | 192 | } |