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