1 /* eslint-disable @typescript-eslint/no-floating-promises */
3 import * as request from 'supertest'
4 import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
5 import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
6 import { makeDeleteRequest, makeGetRequest, updateImageRequest } from '../requests/requests'
7 import { ServerInfo } from '../server/servers'
8 import { MyUser, User } from '../../models/users/user.model'
9 import { getMyUserInformation } from '../users/users'
10 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
12 function getVideoChannelsList (url: string, start: number, count: number, sort?: string, withStats?: boolean) {
13 const path = '/api/v1/video-channels'
15 const req = request(url)
17 .query({ start: start })
18 .query({ count: count })
20 if (sort) req.query({ sort })
21 if (withStats) req.query({ withStats })
23 return req.set('Accept', 'application/json')
24 .expect(HttpStatusCode.OK_200)
25 .expect('Content-Type', /json/)
28 function getAccountVideoChannelsList (parameters: {
34 specialStatus?: HttpStatusCode
44 specialStatus = HttpStatusCode.OK_200,
49 const path = '/api/v1/accounts/' + accountName + '/video-channels'
51 return makeGetRequest({
61 statusCodeExpected: specialStatus
65 function addVideoChannel (
68 videoChannelAttributesArg: VideoChannelCreate,
69 expectedStatus = HttpStatusCode.OK_200
71 const path = '/api/v1/video-channels/'
75 displayName: 'my super video channel',
76 description: 'my super channel description',
77 support: 'my super channel support'
79 attributes = Object.assign(attributes, videoChannelAttributesArg)
84 .set('Accept', 'application/json')
85 .set('Authorization', 'Bearer ' + token)
86 .expect(expectedStatus)
89 function updateVideoChannel (
93 attributes: VideoChannelUpdate,
94 expectedStatus = HttpStatusCode.NO_CONTENT_204
97 const path = '/api/v1/video-channels/' + channelName
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
107 .set('Accept', 'application/json')
108 .set('Authorization', 'Bearer ' + token)
109 .expect(expectedStatus)
112 function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
113 const path = '/api/v1/video-channels/' + channelName
117 .set('Accept', 'application/json')
118 .set('Authorization', 'Bearer ' + token)
119 .expect(expectedStatus)
122 function getVideoChannel (url: string, channelName: string) {
123 const path = '/api/v1/video-channels/' + channelName
127 .set('Accept', 'application/json')
128 .expect(HttpStatusCode.OK_200)
129 .expect('Content-Type', /json/)
132 function updateVideoChannelImage (options: {
136 videoChannelName: string | number
137 type: 'avatar' | 'banner'
139 const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}/pick`
141 return updateImageRequest({ ...options, path, fieldname: options.type + 'file' })
144 function deleteVideoChannelImage (options: {
147 videoChannelName: string | number
148 type: 'avatar' | 'banner'
150 const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}`
152 return makeDeleteRequest({
154 token: options.accessToken,
156 statusCodeExpected: 204
160 function setDefaultVideoChannel (servers: ServerInfo[]) {
161 const tasks: Promise<any>[] = []
163 for (const server of servers) {
164 const p = getMyUserInformation(server.url, server.accessToken)
165 .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
170 return Promise.all(tasks)
173 async function getDefaultVideoChannel (url: string, token: string) {
174 const res = await getMyUserInformation(url, token)
176 return (res.body as MyUser).videoChannels[0].id
179 // ---------------------------------------------------------------------------
182 updateVideoChannelImage,
183 getVideoChannelsList,
184 getAccountVideoChannelsList,
189 setDefaultVideoChannel,
190 deleteVideoChannelImage,
191 getDefaultVideoChannel