]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/video-channels.ts
Move to eslint
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / video-channels.ts
1 /* eslint-disable @typescript-eslint/no-floating-promises */
2
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 { makeGetRequest, updateAvatarRequest } from '../requests/requests'
7 import { ServerInfo } from '../server/servers'
8 import { User } from '../../models/users/user.model'
9 import { getMyUserInformation } from '../users/users'
10
11 function getVideoChannelsList (url: string, start: number, count: number, sort?: string) {
12 const path = '/api/v1/video-channels'
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
26 function getAccountVideoChannelsList (parameters: {
27 url: string
28 accountName: string
29 start?: number
30 count?: number
31 sort?: string
32 specialStatus?: number
33 }) {
34 const { url, accountName, start, count, sort = 'createdAt', specialStatus = 200 } = parameters
35
36 const path = '/api/v1/accounts/' + accountName + '/video-channels'
37
38 return makeGetRequest({
39 url,
40 path,
41 query: {
42 start,
43 count,
44 sort
45 },
46 statusCodeExpected: specialStatus
47 })
48 }
49
50 function addVideoChannel (
51 url: string,
52 token: string,
53 videoChannelAttributesArg: VideoChannelCreate,
54 expectedStatus = 200
55 ) {
56 const path = '/api/v1/video-channels/'
57
58 // Default attributes
59 let attributes = {
60 displayName: 'my super video channel',
61 description: 'my super channel description',
62 support: 'my super channel support'
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
74 function updateVideoChannel (
75 url: string,
76 token: string,
77 channelName: string,
78 attributes: VideoChannelUpdate,
79 expectedStatus = 204
80 ) {
81 const body: any = {}
82 const path = '/api/v1/video-channels/' + channelName
83
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
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
97 function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) {
98 const path = '/api/v1/video-channels/' + channelName
99
100 return request(url)
101 .delete(path)
102 .set('Accept', 'application/json')
103 .set('Authorization', 'Bearer ' + token)
104 .expect(expectedStatus)
105 }
106
107 function getVideoChannel (url: string, channelName: string) {
108 const path = '/api/v1/video-channels/' + channelName
109
110 return request(url)
111 .get(path)
112 .set('Accept', 'application/json')
113 .expect(200)
114 .expect('Content-Type', /json/)
115 }
116
117 function updateVideoChannelAvatar (options: {
118 url: string
119 accessToken: string
120 fixture: string
121 videoChannelName: string | number
122 }) {
123
124 const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
125
126 return updateAvatarRequest(Object.assign(options, { path }))
127 }
128
129 function setDefaultVideoChannel (servers: ServerInfo[]) {
130 const tasks: Promise<any>[] = []
131
132 for (const server of servers) {
133 const p = getMyUserInformation(server.url, server.accessToken)
134 .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
135
136 tasks.push(p)
137 }
138
139 return Promise.all(tasks)
140 }
141
142 // ---------------------------------------------------------------------------
143
144 export {
145 updateVideoChannelAvatar,
146 getVideoChannelsList,
147 getAccountVideoChannelsList,
148 addVideoChannel,
149 updateVideoChannel,
150 deleteVideoChannel,
151 getVideoChannel,
152 setDefaultVideoChannel
153 }