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