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