]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/video-channels.ts
Update hls.js
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / video-channels.ts
CommitLineData
a1587156
C
1/* eslint-disable @typescript-eslint/no-floating-promises */
2
5f04dd2f 3import * as request from 'supertest'
8d2be0ed
C
4import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
5import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
91b66319 6import { makeGetRequest, updateAvatarRequest } from '../requests/requests'
8d2be0ed
C
7import { ServerInfo } from '../server/servers'
8import { User } from '../../models/users/user.model'
9import { getMyUserInformation } from '../users/users'
5f04dd2f
C
10
11function getVideoChannelsList (url: string, start: number, count: number, sort?: string) {
48dce1c9 12 const path = '/api/v1/video-channels'
5f04dd2f
C
13
14 const req = request(url)
15 .get(path)
16 .query({ start: start })
17 .query({ count: count })
18
19 if (sort) req.query({ sort })
20
21 return req.set('Accept', 'application/json')
22 .expect(200)
23 .expect('Content-Type', /json/)
24}
25
91b66319 26function getAccountVideoChannelsList (parameters: {
a1587156
C
27 url: string
28 accountName: string
29 start?: number
30 count?: number
31 sort?: string
91b66319
C
32 specialStatus?: number
33}) {
34 const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200 } = parameters
35
ad9e39fb 36 const path = '/api/v1/accounts/' + accountName + '/video-channels'
5f04dd2f 37
91b66319
C
38 return makeGetRequest({
39 url,
40 path,
41 query: {
42 start,
43 count,
44 sort
45 },
46 statusCodeExpected: specialStatus
47 })
5f04dd2f
C
48}
49
48dce1c9
C
50function addVideoChannel (
51 url: string,
52 token: string,
08c1efbe 53 videoChannelAttributesArg: VideoChannelCreate,
48dce1c9
C
54 expectedStatus = 200
55) {
cc918ac3 56 const path = '/api/v1/video-channels/'
5f04dd2f
C
57
58 // Default attributes
59 let attributes = {
08c1efbe 60 displayName: 'my super video channel',
2422c46b
C
61 description: 'my super channel description',
62 support: 'my super channel support'
5f04dd2f
C
63 }
64 attributes = Object.assign(attributes, videoChannelAttributesArg)
65
66 return request(url)
67 .post(path)
68 .send(attributes)
69 .set('Accept', 'application/json')
70 .set('Authorization', 'Bearer ' + token)
71 .expect(expectedStatus)
72}
73
48dce1c9
C
74function updateVideoChannel (
75 url: string,
76 token: string,
f5b0af50 77 channelName: string,
08c1efbe 78 attributes: VideoChannelUpdate,
48dce1c9
C
79 expectedStatus = 204
80) {
7d14d4d2 81 const body: any = {}
f5b0af50 82 const path = '/api/v1/video-channels/' + channelName
5f04dd2f 83
7d14d4d2
C
84 if (attributes.displayName) body.displayName = attributes.displayName
85 if (attributes.description) body.description = attributes.description
86 if (attributes.support) body.support = attributes.support
87 if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate
5f04dd2f
C
88
89 return request(url)
90 .put(path)
91 .send(body)
92 .set('Accept', 'application/json')
93 .set('Authorization', 'Bearer ' + token)
94 .expect(expectedStatus)
95}
96
f5b0af50
C
97function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) {
98 const path = '/api/v1/video-channels/' + channelName
5f04dd2f
C
99
100 return request(url)
48dce1c9 101 .delete(path)
5f04dd2f
C
102 .set('Accept', 'application/json')
103 .set('Authorization', 'Bearer ' + token)
104 .expect(expectedStatus)
105}
106
f5b0af50
C
107function getVideoChannel (url: string, channelName: string) {
108 const path = '/api/v1/video-channels/' + channelName
5f04dd2f
C
109
110 return request(url)
111 .get(path)
112 .set('Accept', 'application/json')
113 .expect(200)
114 .expect('Content-Type', /json/)
115}
116
4bbfc6c6 117function updateVideoChannelAvatar (options: {
a1587156
C
118 url: string
119 accessToken: string
120 fixture: string
8a19bee1 121 videoChannelName: string | number
4bbfc6c6
C
122}) {
123
8a19bee1 124 const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
4bbfc6c6
C
125
126 return updateAvatarRequest(Object.assign(options, { path }))
127}
128
df0b219d
C
129function setDefaultVideoChannel (servers: ServerInfo[]) {
130 const tasks: Promise<any>[] = []
131
132 for (const server of servers) {
133 const p = getMyUserInformation(server.url, server.accessToken)
a1587156 134 .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
df0b219d
C
135
136 tasks.push(p)
137 }
138
139 return Promise.all(tasks)
140}
141
5f04dd2f
C
142// ---------------------------------------------------------------------------
143
144export {
4bbfc6c6 145 updateVideoChannelAvatar,
5f04dd2f 146 getVideoChannelsList,
975e6e0e 147 getAccountVideoChannelsList,
5f04dd2f
C
148 addVideoChannel,
149 updateVideoChannel,
150 deleteVideoChannel,
df0b219d
C
151 getVideoChannel,
152 setDefaultVideoChannel
5f04dd2f 153}