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