aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-06 09:55:05 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:16 +0200
commite8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6 (patch)
treec888e13aca3fc239f46d44045df6bf8e2a2ef0d3 /shared
parent329619b3453479f76c049816b7403b86e9d45cb5 (diff)
downloadPeerTube-e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6.tar.gz
PeerTube-e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6.tar.zst
PeerTube-e8bd7ce7ccafe3e064b03978e9b512c1a4cc99e6.zip
Introduce CustomPage command
Diffstat (limited to 'shared')
-rw-r--r--shared/extra-utils/bulk/bulk.ts10
-rw-r--r--shared/extra-utils/cli/cli.ts6
-rw-r--r--shared/extra-utils/custom-pages/custom-pages.ts47
-rw-r--r--shared/extra-utils/custom-pages/index.ts1
-rw-r--r--shared/extra-utils/index.ts2
-rw-r--r--shared/extra-utils/requests/requests.ts5
-rw-r--r--shared/extra-utils/server/servers.ts3
-rw-r--r--shared/extra-utils/shared/abstract-command.ts44
8 files changed, 73 insertions, 45 deletions
diff --git a/shared/extra-utils/bulk/bulk.ts b/shared/extra-utils/bulk/bulk.ts
index c102383e3..a58fb3fbb 100644
--- a/shared/extra-utils/bulk/bulk.ts
+++ b/shared/extra-utils/bulk/bulk.ts
@@ -1,11 +1,11 @@
1 1
2import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model' 2import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4import { AbstractCommand, CommonCommandOptions } from '../shared' 4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5 5
6class BulkCommand extends AbstractCommand { 6export class BulkCommand extends AbstractCommand {
7 7
8 removeCommentsOf (options: CommonCommandOptions & { 8 removeCommentsOf (options: OverrideCommandOptions & {
9 attributes: BulkRemoveCommentsOfBody 9 attributes: BulkRemoveCommentsOfBody
10 }) { 10 }) {
11 const { attributes } = options 11 const { attributes } = options
@@ -18,7 +18,3 @@ class BulkCommand extends AbstractCommand {
18 }) 18 })
19 } 19 }
20} 20}
21
22export {
23 BulkCommand
24}
diff --git a/shared/extra-utils/cli/cli.ts b/shared/extra-utils/cli/cli.ts
index 1bf100869..bc1dddc68 100644
--- a/shared/extra-utils/cli/cli.ts
+++ b/shared/extra-utils/cli/cli.ts
@@ -1,7 +1,7 @@
1import { exec } from 'child_process' 1import { exec } from 'child_process'
2import { AbstractCommand } from '../shared' 2import { AbstractCommand } from '../shared'
3 3
4class CLICommand extends AbstractCommand { 4export class CLICommand extends AbstractCommand {
5 5
6 static exec (command: string) { 6 static exec (command: string) {
7 return new Promise<string>((res, rej) => { 7 return new Promise<string>((res, rej) => {
@@ -21,7 +21,3 @@ class CLICommand extends AbstractCommand {
21 return CLICommand.exec(`${this.getEnv()} ${command}`) 21 return CLICommand.exec(`${this.getEnv()} ${command}`)
22 } 22 }
23} 23}
24
25export {
26 CLICommand
27}
diff --git a/shared/extra-utils/custom-pages/custom-pages.ts b/shared/extra-utils/custom-pages/custom-pages.ts
index bf2d16c70..56dabdc0f 100644
--- a/shared/extra-utils/custom-pages/custom-pages.ts
+++ b/shared/extra-utils/custom-pages/custom-pages.ts
@@ -1,31 +1,30 @@
1import { CustomPage } from '@shared/models'
1import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 2import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
2import { makeGetRequest, makePutBodyRequest } from '../requests/requests' 3import { AbstractCommand, OverrideCommandOptions } from '../shared'
3 4
4function getInstanceHomepage (url: string, statusCodeExpected = HttpStatusCode.OK_200) { 5export class CustomPagesCommand extends AbstractCommand {
5 const path = '/api/v1/custom-pages/homepage/instance'
6 6
7 return makeGetRequest({ 7 getInstanceHomepage (options: OverrideCommandOptions = {}) {
8 url, 8 const path = '/api/v1/custom-pages/homepage/instance'
9 path,
10 statusCodeExpected
11 })
12}
13
14function updateInstanceHomepage (url: string, token: string, content: string) {
15 const path = '/api/v1/custom-pages/homepage/instance'
16 9
17 return makePutBodyRequest({ 10 return this.getRequestBody<CustomPage>({
18 url, 11 ...options,
19 path, 12 path,
20 token, 13 defaultExpectedStatus: HttpStatusCode.OK_200
21 fields: { content }, 14 })
22 statusCodeExpected: HttpStatusCode.NO_CONTENT_204 15 }
23 })
24}
25 16
26// --------------------------------------------------------------------------- 17 updateInstanceHomepage (options: OverrideCommandOptions & {
18 content: string
19 }) {
20 const { content } = options
21 const path = '/api/v1/custom-pages/homepage/instance'
27 22
28export { 23 return this.putBodyRequest({
29 getInstanceHomepage, 24 ...options,
30 updateInstanceHomepage 25 path,
26 fields: { content },
27 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
28 })
29 }
31} 30}
diff --git a/shared/extra-utils/custom-pages/index.ts b/shared/extra-utils/custom-pages/index.ts
new file mode 100644
index 000000000..5e70778f8
--- /dev/null
+++ b/shared/extra-utils/custom-pages/index.ts
@@ -0,0 +1 @@
export * from './custom-pages'
diff --git a/shared/extra-utils/index.ts b/shared/extra-utils/index.ts
index 4bf057492..10781d049 100644
--- a/shared/extra-utils/index.ts
+++ b/shared/extra-utils/index.ts
@@ -2,7 +2,7 @@ export * from './bulk'
2 2
3export * from './cli' 3export * from './cli'
4 4
5export * from './custom-pages/custom-pages' 5export * from './custom-pages'
6 6
7export * from './feeds/feeds' 7export * from './feeds/feeds'
8 8
diff --git a/shared/extra-utils/requests/requests.ts b/shared/extra-utils/requests/requests.ts
index 38e24d897..eb59ca600 100644
--- a/shared/extra-utils/requests/requests.ts
+++ b/shared/extra-utils/requests/requests.ts
@@ -182,6 +182,10 @@ function decodeQueryString (path: string) {
182 return decode(path.split('?')[1]) 182 return decode(path.split('?')[1])
183} 183}
184 184
185function unwrap <T> (test: request.Test): Promise<T> {
186 return test.then(res => res.body)
187}
188
185// --------------------------------------------------------------------------- 189// ---------------------------------------------------------------------------
186 190
187export { 191export {
@@ -194,5 +198,6 @@ export {
194 makePutBodyRequest, 198 makePutBodyRequest,
195 makeDeleteRequest, 199 makeDeleteRequest,
196 makeRawRequest, 200 makeRawRequest,
201 unwrap,
197 updateImageRequest 202 updateImageRequest
198} 203}
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 1b0775421..e07926065 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -8,6 +8,7 @@ import { randomInt } from '../../core-utils/miscs/miscs'
8import { VideoChannel } from '../../models/videos' 8import { VideoChannel } from '../../models/videos'
9import { BulkCommand } from '../bulk' 9import { BulkCommand } from '../bulk'
10import { CLICommand } from '../cli' 10import { CLICommand } from '../cli'
11import { CustomPagesCommand } from '../custom-pages'
11import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs' 12import { buildServerDirectory, getFileSize, isGithubCI, root, wait } from '../miscs/miscs'
12import { makeGetRequest } from '../requests/requests' 13import { makeGetRequest } from '../requests/requests'
13 14
@@ -65,6 +66,7 @@ interface ServerInfo {
65 66
66 bulkCommand?: BulkCommand 67 bulkCommand?: BulkCommand
67 cliCommand?: CLICommand 68 cliCommand?: CLICommand
69 customPageCommand?: CustomPagesCommand
68} 70}
69 71
70function parallelTests () { 72function parallelTests () {
@@ -272,6 +274,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
272 274
273 server.bulkCommand = new BulkCommand(server) 275 server.bulkCommand = new BulkCommand(server)
274 server.cliCommand = new CLICommand(server) 276 server.cliCommand = new CLICommand(server)
277 server.customPageCommand = new CustomPagesCommand(server)
275 278
276 res(server) 279 res(server)
277 }) 280 })
diff --git a/shared/extra-utils/shared/abstract-command.ts b/shared/extra-utils/shared/abstract-command.ts
index bb06b6bdb..d8eba373d 100644
--- a/shared/extra-utils/shared/abstract-command.ts
+++ b/shared/extra-utils/shared/abstract-command.ts
@@ -1,15 +1,20 @@
1import { HttpStatusCode } from '@shared/core-utils' 1import { HttpStatusCode } from '@shared/core-utils'
2import { makePostBodyRequest } from '../requests/requests' 2import { makeGetRequest, makePostBodyRequest, makePutBodyRequest, unwrap } from '../requests/requests'
3import { ServerInfo } from '../server/servers' 3import { ServerInfo } from '../server/servers'
4 4
5export interface CommonCommandOptions { 5export interface OverrideCommandOptions {
6 token?: string 6 token?: string
7 expectedStatus?: number 7 expectedStatus?: number
8} 8}
9 9
10interface CommonCommandOptions extends OverrideCommandOptions {
11 path: string
12 defaultExpectedStatus: number
13}
14
10abstract class AbstractCommand { 15abstract class AbstractCommand {
11 16
12 private expectedStatus = HttpStatusCode.OK_200 17 private expectedStatus: HttpStatusCode
13 18
14 constructor ( 19 constructor (
15 protected server: ServerInfo 20 protected server: ServerInfo
@@ -25,20 +30,43 @@ abstract class AbstractCommand {
25 this.expectedStatus = status 30 this.expectedStatus = status
26 } 31 }
27 32
33 protected getRequestBody <T> (options: CommonCommandOptions) {
34 return unwrap<T>(makeGetRequest(this.buildCommonRequestOptions(options)))
35 }
36
37 protected putBodyRequest (options: CommonCommandOptions & {
38 fields?: { [ fieldName: string ]: any }
39 }) {
40 const { fields } = options
41
42 return makePutBodyRequest({
43 ...this.buildCommonRequestOptions(options),
44
45 fields
46 })
47 }
48
28 protected postBodyRequest (options: CommonCommandOptions & { 49 protected postBodyRequest (options: CommonCommandOptions & {
29 path: string
30 defaultExpectedStatus: number
31 fields?: { [ fieldName: string ]: any } 50 fields?: { [ fieldName: string ]: any }
32 }) { 51 }) {
33 const { token, fields, expectedStatus, defaultExpectedStatus, path } = options 52 const { fields } = options
34 53
35 return makePostBodyRequest({ 54 return makePostBodyRequest({
55 ...this.buildCommonRequestOptions(options),
56
57 fields
58 })
59 }
60
61 private buildCommonRequestOptions (options: CommonCommandOptions) {
62 const { token, expectedStatus, defaultExpectedStatus, path } = options
63
64 return {
36 url: this.server.url, 65 url: this.server.url,
37 path, 66 path,
38 token: token ?? this.server.accessToken, 67 token: token ?? this.server.accessToken,
39 fields,
40 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus 68 statusCodeExpected: expectedStatus ?? this.expectedStatus ?? defaultExpectedStatus
41 }) 69 }
42 } 70 }
43} 71}
44 72