aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-09 11:21:30 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commita54618880c394ad7571f3f3222dc96ec2dd10d9a (patch)
treec9f7b05e578abc2383bccd707c11438c61857c72 /shared/extra-utils
parent57f879a540551c3b958b0991c8e1e3657a4481d8 (diff)
downloadPeerTube-a54618880c394ad7571f3f3222dc96ec2dd10d9a.tar.gz
PeerTube-a54618880c394ad7571f3f3222dc96ec2dd10d9a.tar.zst
PeerTube-a54618880c394ad7571f3f3222dc96ec2dd10d9a.zip
Introduce channels command
Diffstat (limited to 'shared/extra-utils')
-rw-r--r--shared/extra-utils/server/servers.ts3
-rw-r--r--shared/extra-utils/shared/abstract-command.ts21
-rw-r--r--shared/extra-utils/videos/channels-command.ts157
-rw-r--r--shared/extra-utils/videos/channels.ts20
-rw-r--r--shared/extra-utils/videos/index.ts5
-rw-r--r--shared/extra-utils/videos/video-channels.ts192
6 files changed, 204 insertions, 194 deletions
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 6a1dadbcc..68e10af5f 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -22,6 +22,7 @@ import {
22 BlacklistCommand, 22 BlacklistCommand,
23 CaptionsCommand, 23 CaptionsCommand,
24 ChangeOwnershipCommand, 24 ChangeOwnershipCommand,
25 ChannelsCommand,
25 HistoryCommand, 26 HistoryCommand,
26 ImportsCommand, 27 ImportsCommand,
27 LiveCommand, 28 LiveCommand,
@@ -119,6 +120,7 @@ interface ServerInfo {
119 historyCommand?: HistoryCommand 120 historyCommand?: HistoryCommand
120 importsCommand?: ImportsCommand 121 importsCommand?: ImportsCommand
121 streamingPlaylistsCommand?: StreamingPlaylistsCommand 122 streamingPlaylistsCommand?: StreamingPlaylistsCommand
123 channelsCommand?: ChannelsCommand
122} 124}
123 125
124function parallelTests () { 126function parallelTests () {
@@ -353,6 +355,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
353 server.historyCommand = new HistoryCommand(server) 355 server.historyCommand = new HistoryCommand(server)
354 server.importsCommand = new ImportsCommand(server) 356 server.importsCommand = new ImportsCommand(server)
355 server.streamingPlaylistsCommand = new StreamingPlaylistsCommand(server) 357 server.streamingPlaylistsCommand = new StreamingPlaylistsCommand(server)
358 server.channelsCommand = new ChannelsCommand(server)
356 359
357 res(server) 360 res(server)
358 }) 361 })
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts
index be368376f..6a9ab1348 100644
--- a/shared/extra-utils/shared/abstract-command.ts
+++ b/shared/extra-utils/shared/abstract-command.ts
@@ -1,4 +1,6 @@
1import { isAbsolute, join } from 'path'
1import { HttpStatusCode } from '@shared/core-utils' 2import { HttpStatusCode } from '@shared/core-utils'
3import { root } from '../miscs'
2import { 4import {
3 makeDeleteRequest, 5 makeDeleteRequest,
4 makeGetRequest, 6 makeGetRequest,
@@ -146,6 +148,25 @@ abstract class AbstractCommand {
146 }) 148 })
147 } 149 }
148 150
151 protected updateImageRequest (options: InternalCommonCommandOptions & {
152 fixture: string
153 fieldname: string
154 }) {
155 let filePath = ''
156 if (isAbsolute(options.fixture)) {
157 filePath = options.fixture
158 } else {
159 filePath = join(root(), 'server', 'tests', 'fixtures', options.fixture)
160 }
161
162 return this.postUploadRequest({
163 ...options,
164
165 fields: {},
166 attaches: { [options.fieldname]: filePath }
167 })
168 }
169
149 private buildCommonRequestOptions (options: InternalCommonCommandOptions) { 170 private buildCommonRequestOptions (options: InternalCommonCommandOptions) {
150 const { path } = options 171 const { path } = options
151 172
diff --git a/shared/extra-utils/videos/channels-command.ts b/shared/extra-utils/videos/channels-command.ts
new file mode 100644
index 000000000..a98c5cc93
--- /dev/null
+++ b/shared/extra-utils/videos/channels-command.ts
@@ -0,0 +1,157 @@
1import { pick } from 'lodash'
2import { ResultList, VideoChannel, VideoChannelCreateResult } from '@shared/models'
3import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
4import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
5import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
6import { unwrapBody } from '../requests'
7import { AbstractCommand, OverrideCommandOptions } from '../shared'
8
9export class ChannelsCommand extends AbstractCommand {
10
11 list (options: OverrideCommandOptions & {
12 start?: number
13 count?: number
14 sort?: string
15 withStats?: boolean
16 } = {}) {
17 const path = '/api/v1/video-channels'
18
19 return this.getRequestBody<ResultList<VideoChannel>>({
20 ...options,
21
22 path,
23 query: pick(options, [ 'start', 'count', 'sort', 'withStats' ]),
24 implicitToken: false,
25 defaultExpectedStatus: HttpStatusCode.OK_200
26 })
27 }
28
29 listByAccount (options: OverrideCommandOptions & {
30 accountName: string
31 start?: number
32 count?: number
33 sort?: string
34 withStats?: boolean
35 search?: string
36 }) {
37 const { accountName, sort = 'createdAt' } = options
38 const path = '/api/v1/accounts/' + accountName + '/video-channels'
39
40 return this.getRequestBody<ResultList<VideoChannel>>({
41 ...options,
42
43 path,
44 query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search' ]) },
45 implicitToken: false,
46 defaultExpectedStatus: HttpStatusCode.OK_200
47 })
48 }
49
50 async create (options: OverrideCommandOptions & {
51 attributes: VideoChannelCreate
52 }) {
53 const path = '/api/v1/video-channels/'
54
55 // Default attributes
56 const defaultAttributes = {
57 displayName: 'my super video channel',
58 description: 'my super channel description',
59 support: 'my super channel support'
60 }
61 const attributes = { ...defaultAttributes, ...options.attributes }
62
63 const body = await unwrapBody<{ videoChannel: VideoChannelCreateResult }>(this.postBodyRequest({
64 ...options,
65
66 path,
67 fields: attributes,
68 implicitToken: true,
69 defaultExpectedStatus: HttpStatusCode.OK_200
70 }))
71
72 return body.videoChannel
73 }
74
75 update (options: OverrideCommandOptions & {
76 channelName: string
77 attributes: VideoChannelUpdate
78 }) {
79 const { channelName, attributes } = options
80 const path = '/api/v1/video-channels/' + channelName
81
82 return this.putBodyRequest({
83 ...options,
84
85 path,
86 fields: attributes,
87 implicitToken: true,
88 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
89 })
90 }
91
92 delete (options: OverrideCommandOptions & {
93 channelName: string
94 }) {
95 const path = '/api/v1/video-channels/' + options.channelName
96
97 return this.deleteRequest({
98 ...options,
99
100 path,
101 implicitToken: true,
102 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
103 })
104 }
105
106 get (options: OverrideCommandOptions & {
107 channelName: string
108 }) {
109 const path = '/api/v1/video-channels/' + options.channelName
110
111 return this.getRequestBody<VideoChannel>({
112 ...options,
113
114 path,
115 implicitToken: false,
116 defaultExpectedStatus: HttpStatusCode.OK_200
117 })
118 }
119
120 updateImage (options: OverrideCommandOptions & {
121 fixture: string
122 channelName: string | number
123 type: 'avatar' | 'banner'
124 }) {
125 const { channelName, fixture, type } = options
126
127 const path = `/api/v1/video-channels/${channelName}/${type}/pick`
128
129 return this.updateImageRequest({
130 ...options,
131
132 path,
133 fixture,
134 fieldname: type + 'file',
135
136 implicitToken: true,
137 defaultExpectedStatus: HttpStatusCode.OK_200
138 })
139 }
140
141 deleteImage (options: OverrideCommandOptions & {
142 channelName: string | number
143 type: 'avatar' | 'banner'
144 }) {
145 const { channelName, type } = options
146
147 const path = `/api/v1/video-channels/${channelName}/${type}`
148
149 return this.deleteRequest({
150 ...options,
151
152 path,
153 implicitToken: true,
154 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
155 })
156 }
157}
diff --git a/shared/extra-utils/videos/channels.ts b/shared/extra-utils/videos/channels.ts
new file mode 100644
index 000000000..a77543c92
--- /dev/null
+++ b/shared/extra-utils/videos/channels.ts
@@ -0,0 +1,20 @@
1import { User } from '../../models/users/user.model'
2import { ServerInfo } from '../server/servers'
3import { getMyUserInformation } from '../users/users'
4
5function setDefaultVideoChannel (servers: ServerInfo[]) {
6 const tasks: Promise<any>[] = []
7
8 for (const server of servers) {
9 const p = getMyUserInformation(server.url, server.accessToken)
10 .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
11
12 tasks.push(p)
13 }
14
15 return Promise.all(tasks)
16}
17
18export {
19 setDefaultVideoChannel
20}
diff --git a/shared/extra-utils/videos/index.ts b/shared/extra-utils/videos/index.ts
index f87ae8eea..3bc219281 100644
--- a/shared/extra-utils/videos/index.ts
+++ b/shared/extra-utils/videos/index.ts
@@ -1,7 +1,9 @@
1export * from './blacklist-command' 1export * from './blacklist-command'
2export * from './captions'
3export * from './captions-command' 2export * from './captions-command'
3export * from './captions'
4export * from './change-ownership-command' 4export * from './change-ownership-command'
5export * from './channels'
6export * from './channels-command'
5export * from './history-command' 7export * from './history-command'
6export * from './imports-command' 8export * from './imports-command'
7export * from './live-command' 9export * from './live-command'
@@ -11,6 +13,5 @@ export * from './playlists'
11export * from './services-command' 13export * from './services-command'
12export * from './streaming-playlists-command' 14export * from './streaming-playlists-command'
13export * from './streaming-playlists' 15export * from './streaming-playlists'
14export * from './video-channels'
15export * from './video-comments' 16export * from './video-comments'
16export * from './videos' 17export * from './videos'
diff --git a/shared/extra-utils/videos/video-channels.ts b/shared/extra-utils/videos/video-channels.ts
deleted file mode 100644
index 0aab93e52..000000000
--- a/shared/extra-utils/videos/video-channels.ts
+++ /dev/null
@@ -1,192 +0,0 @@
1/* eslint-disable @typescript-eslint/no-floating-promises */
2
3import * as request from 'supertest'
4import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
5import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
6import { makeDeleteRequest, makeGetRequest, updateImageRequest } from '../requests/requests'
7import { ServerInfo } from '../server/servers'
8import { MyUser, User } from '../../models/users/user.model'
9import { getMyUserInformation } from '../users/users'
10import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
11
12function getVideoChannelsList (url: string, start: number, count: number, sort?: string, withStats?: boolean) {
13 const path = '/api/v1/video-channels'
14
15 const req = request(url)
16 .get(path)
17 .query({ start: start })
18 .query({ count: count })
19
20 if (sort) req.query({ sort })
21 if (withStats) req.query({ withStats })
22
23 return req.set('Accept', 'application/json')
24 .expect(HttpStatusCode.OK_200)
25 .expect('Content-Type', /json/)
26}
27
28function getAccountVideoChannelsList (parameters: {
29 url: string
30 accountName: string
31 start?: number
32 count?: number
33 sort?: string
34 specialStatus?: HttpStatusCode
35 withStats?: boolean
36 search?: string
37}) {
38 const {
39 url,
40 accountName,
41 start,
42 count,
43 sort = 'createdAt',
44 specialStatus = HttpStatusCode.OK_200,
45 withStats = false,
46 search
47 } = parameters
48
49 const path = '/api/v1/accounts/' + accountName + '/video-channels'
50
51 return makeGetRequest({
52 url,
53 path,
54 query: {
55 start,
56 count,
57 sort,
58 withStats,
59 search
60 },
61 statusCodeExpected: specialStatus
62 })
63}
64
65function addVideoChannel (
66 url: string,
67 token: string,
68 videoChannelAttributesArg: VideoChannelCreate,
69 expectedStatus = HttpStatusCode.OK_200
70) {
71 const path = '/api/v1/video-channels/'
72
73 // Default attributes
74 let attributes = {
75 displayName: 'my super video channel',
76 description: 'my super channel description',
77 support: 'my super channel support'
78 }
79 attributes = Object.assign(attributes, videoChannelAttributesArg)
80
81 return request(url)
82 .post(path)
83 .send(attributes)
84 .set('Accept', 'application/json')
85 .set('Authorization', 'Bearer ' + token)
86 .expect(expectedStatus)
87}
88
89function updateVideoChannel (
90 url: string,
91 token: string,
92 channelName: string,
93 attributes: VideoChannelUpdate,
94 expectedStatus = HttpStatusCode.NO_CONTENT_204
95) {
96 const body: any = {}
97 const path = '/api/v1/video-channels/' + channelName
98
99 if (attributes.displayName) body.displayName = attributes.displayName
100 if (attributes.description) body.description = attributes.description
101 if (attributes.support) body.support = attributes.support
102 if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate
103
104 return request(url)
105 .put(path)
106 .send(body)
107 .set('Accept', 'application/json')
108 .set('Authorization', 'Bearer ' + token)
109 .expect(expectedStatus)
110}
111
112function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
113 const path = '/api/v1/video-channels/' + channelName
114
115 return request(url)
116 .delete(path)
117 .set('Accept', 'application/json')
118 .set('Authorization', 'Bearer ' + token)
119 .expect(expectedStatus)
120}
121
122function getVideoChannel (url: string, channelName: string) {
123 const path = '/api/v1/video-channels/' + channelName
124
125 return request(url)
126 .get(path)
127 .set('Accept', 'application/json')
128 .expect(HttpStatusCode.OK_200)
129 .expect('Content-Type', /json/)
130}
131
132function updateVideoChannelImage (options: {
133 url: string
134 accessToken: string
135 fixture: string
136 videoChannelName: string | number
137 type: 'avatar' | 'banner'
138}) {
139 const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}/pick`
140
141 return updateImageRequest({ ...options, path, fieldname: options.type + 'file' })
142}
143
144function deleteVideoChannelImage (options: {
145 url: string
146 accessToken: string
147 videoChannelName: string | number
148 type: 'avatar' | 'banner'
149}) {
150 const path = `/api/v1/video-channels/${options.videoChannelName}/${options.type}`
151
152 return makeDeleteRequest({
153 url: options.url,
154 token: options.accessToken,
155 path,
156 statusCodeExpected: 204
157 })
158}
159
160function setDefaultVideoChannel (servers: ServerInfo[]) {
161 const tasks: Promise<any>[] = []
162
163 for (const server of servers) {
164 const p = getMyUserInformation(server.url, server.accessToken)
165 .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
166
167 tasks.push(p)
168 }
169
170 return Promise.all(tasks)
171}
172
173async function getDefaultVideoChannel (url: string, token: string) {
174 const res = await getMyUserInformation(url, token)
175
176 return (res.body as MyUser).videoChannels[0].id
177}
178
179// ---------------------------------------------------------------------------
180
181export {
182 updateVideoChannelImage,
183 getVideoChannelsList,
184 getAccountVideoChannelsList,
185 addVideoChannel,
186 updateVideoChannel,
187 deleteVideoChannel,
188 getVideoChannel,
189 setDefaultVideoChannel,
190 deleteVideoChannelImage,
191 getDefaultVideoChannel
192}