]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/video-channels.ts
Server: Bulk update videos support field
[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: any = {}
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 if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate
84
85 return request(url)
86 .put(path)
87 .send(body)
88 .set('Accept', 'application/json')
89 .set('Authorization', 'Bearer ' + token)
90 .expect(expectedStatus)
91 }
92
93 function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) {
94 const path = '/api/v1/video-channels/' + channelName
95
96 return request(url)
97 .delete(path)
98 .set('Accept', 'application/json')
99 .set('Authorization', 'Bearer ' + token)
100 .expect(expectedStatus)
101 }
102
103 function getVideoChannel (url: string, channelName: string) {
104 const path = '/api/v1/video-channels/' + channelName
105
106 return request(url)
107 .get(path)
108 .set('Accept', 'application/json')
109 .expect(200)
110 .expect('Content-Type', /json/)
111 }
112
113 function updateVideoChannelAvatar (options: {
114 url: string,
115 accessToken: string,
116 fixture: string,
117 videoChannelName: string | number
118 }) {
119
120 const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
121
122 return updateAvatarRequest(Object.assign(options, { path }))
123 }
124
125 function setDefaultVideoChannel (servers: ServerInfo[]) {
126 const tasks: Promise<any>[] = []
127
128 for (const server of servers) {
129 const p = getMyUserInformation(server.url, server.accessToken)
130 .then(res => server.videoChannel = (res.body as User).videoChannels[0])
131
132 tasks.push(p)
133 }
134
135 return Promise.all(tasks)
136 }
137
138 // ---------------------------------------------------------------------------
139
140 export {
141 updateVideoChannelAvatar,
142 getVideoChannelsList,
143 getAccountVideoChannelsList,
144 addVideoChannel,
145 updateVideoChannel,
146 deleteVideoChannel,
147 getVideoChannel,
148 setDefaultVideoChannel
149 }