Add avatar to prune script
[oweals/peertube.git] / shared / extra-utils / videos / video-channels.ts
1 import * as request from 'supertest'
2 import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
3 import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
4 import { makeGetRequest, updateAvatarRequest } from '../requests/requests'
5 import { ServerInfo } from '../server/servers'
6 import { User } from '../../models/users/user.model'
7 import { getMyUserInformation } from '../users/users'
8
9 function getVideoChannelsList (url: string, start: number, count: number, sort?: string) {
10   const path = '/api/v1/video-channels'
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
24 function 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
34   const path = '/api/v1/accounts/' + accountName + '/video-channels'
35
36   return makeGetRequest({
37     url,
38     path,
39     query: {
40       start,
41       count,
42       sort
43     },
44     statusCodeExpected: specialStatus
45   })
46 }
47
48 function addVideoChannel (
49   url: string,
50   token: string,
51   videoChannelAttributesArg: VideoChannelCreate,
52   expectedStatus = 200
53 ) {
54   const path = '/api/v1/video-channels/'
55
56   // Default attributes
57   let attributes = {
58     displayName: 'my super video channel',
59     description: 'my super channel description',
60     support: 'my super channel support'
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
72 function updateVideoChannel (
73   url: string,
74   token: string,
75   channelName: string,
76   attributes: VideoChannelUpdate,
77   expectedStatus = 204
78 ) {
79   const body: any = {}
80   const path = '/api/v1/video-channels/' + channelName
81
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
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
95 function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = 204) {
96   const path = '/api/v1/video-channels/' + channelName
97
98   return request(url)
99     .delete(path)
100     .set('Accept', 'application/json')
101     .set('Authorization', 'Bearer ' + token)
102     .expect(expectedStatus)
103 }
104
105 function getVideoChannel (url: string, channelName: string) {
106   const path = '/api/v1/video-channels/' + channelName
107
108   return request(url)
109     .get(path)
110     .set('Accept', 'application/json')
111     .expect(200)
112     .expect('Content-Type', /json/)
113 }
114
115 function updateVideoChannelAvatar (options: {
116   url: string,
117   accessToken: string,
118   fixture: string,
119   videoChannelName: string | number
120 }) {
121
122   const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
123
124   return updateAvatarRequest(Object.assign(options, { path }))
125 }
126
127 function 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
140 // ---------------------------------------------------------------------------
141
142 export {
143   updateVideoChannelAvatar,
144   getVideoChannelsList,
145   getAccountVideoChannelsList,
146   addVideoChannel,
147   updateVideoChannel,
148   deleteVideoChannel,
149   getVideoChannel,
150   setDefaultVideoChannel
151 }