diff options
Diffstat (limited to 'shared/core-utils')
-rw-r--r-- | shared/core-utils/common/array.ts (renamed from shared/core-utils/utils/array.ts) | 0 | ||||
-rw-r--r-- | shared/core-utils/common/env.ts | 30 | ||||
-rw-r--r-- | shared/core-utils/common/index.ts | 9 | ||||
-rw-r--r-- | shared/core-utils/common/object.ts | 35 | ||||
-rw-r--r-- | shared/core-utils/common/path.ts | 48 | ||||
-rw-r--r-- | shared/core-utils/common/random.ts | 8 | ||||
-rw-r--r-- | shared/core-utils/common/time.ts | 7 | ||||
-rw-r--r-- | shared/core-utils/common/types.ts | 45 | ||||
-rw-r--r-- | shared/core-utils/common/url.ts | 2 | ||||
-rw-r--r-- | shared/core-utils/common/version.ts (renamed from shared/core-utils/common/miscs.ts) | 23 | ||||
-rw-r--r-- | shared/core-utils/i18n/i18n.ts | 3 | ||||
-rw-r--r-- | shared/core-utils/index.ts | 1 | ||||
-rw-r--r-- | shared/core-utils/users/user-role.ts | 2 | ||||
-rw-r--r-- | shared/core-utils/utils/index.ts | 2 | ||||
-rw-r--r-- | shared/core-utils/utils/object.ts | 15 | ||||
-rw-r--r-- | shared/core-utils/videos/bitrate.ts | 2 |
16 files changed, 143 insertions, 89 deletions
diff --git a/shared/core-utils/utils/array.ts b/shared/core-utils/common/array.ts index 9e326a5aa..9e326a5aa 100644 --- a/shared/core-utils/utils/array.ts +++ b/shared/core-utils/common/array.ts | |||
diff --git a/shared/core-utils/common/env.ts b/shared/core-utils/common/env.ts new file mode 100644 index 000000000..38c96b152 --- /dev/null +++ b/shared/core-utils/common/env.ts | |||
@@ -0,0 +1,30 @@ | |||
1 | function parallelTests () { | ||
2 | return process.env.MOCHA_PARALLEL === 'true' | ||
3 | } | ||
4 | |||
5 | function isGithubCI () { | ||
6 | return !!process.env.GITHUB_WORKSPACE | ||
7 | } | ||
8 | |||
9 | function areHttpImportTestsDisabled () { | ||
10 | const disabled = process.env.DISABLE_HTTP_IMPORT_TESTS === 'true' | ||
11 | |||
12 | if (disabled) console.log('DISABLE_HTTP_IMPORT_TESTS env set to "true" so import tests are disabled') | ||
13 | |||
14 | return disabled | ||
15 | } | ||
16 | |||
17 | function areObjectStorageTestsDisabled () { | ||
18 | const disabled = process.env.ENABLE_OBJECT_STORAGE_TESTS !== 'true' | ||
19 | |||
20 | if (disabled) console.log('ENABLE_OBJECT_STORAGE_TESTS env is not set to "true" so object storage tests are disabled') | ||
21 | |||
22 | return disabled | ||
23 | } | ||
24 | |||
25 | export { | ||
26 | parallelTests, | ||
27 | isGithubCI, | ||
28 | areHttpImportTestsDisabled, | ||
29 | areObjectStorageTestsDisabled | ||
30 | } | ||
diff --git a/shared/core-utils/common/index.ts b/shared/core-utils/common/index.ts index 0908ff981..720977ead 100644 --- a/shared/core-utils/common/index.ts +++ b/shared/core-utils/common/index.ts | |||
@@ -1,6 +1,11 @@ | |||
1 | export * from './array' | ||
2 | export * from './random' | ||
1 | export * from './date' | 3 | export * from './date' |
2 | export * from './miscs' | 4 | export * from './env' |
5 | export * from './object' | ||
6 | export * from './path' | ||
3 | export * from './regexp' | 7 | export * from './regexp' |
8 | export * from './time' | ||
4 | export * from './promises' | 9 | export * from './promises' |
5 | export * from './types' | ||
6 | export * from './url' | 10 | export * from './url' |
11 | export * from './version' | ||
diff --git a/shared/core-utils/common/object.ts b/shared/core-utils/common/object.ts new file mode 100644 index 000000000..49d209819 --- /dev/null +++ b/shared/core-utils/common/object.ts | |||
@@ -0,0 +1,35 @@ | |||
1 | function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> { | ||
2 | const result: any = {} | ||
3 | |||
4 | for (const key of keys) { | ||
5 | if (Object.prototype.hasOwnProperty.call(object, key)) { | ||
6 | result[key] = object[key] | ||
7 | } | ||
8 | } | ||
9 | |||
10 | return result | ||
11 | } | ||
12 | |||
13 | function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] { | ||
14 | return (Object.keys(object) as K[]).filter(k => keys.includes(k)) | ||
15 | } | ||
16 | |||
17 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | ||
18 | return (a: any, b: any) => { | ||
19 | if (a[key] < b[key]) { | ||
20 | return order === 'asc' ? -1 : 1 | ||
21 | } | ||
22 | |||
23 | if (a[key] > b[key]) { | ||
24 | return order === 'asc' ? 1 : -1 | ||
25 | } | ||
26 | |||
27 | return 0 | ||
28 | } | ||
29 | } | ||
30 | |||
31 | export { | ||
32 | pick, | ||
33 | getKeys, | ||
34 | sortObjectComparator | ||
35 | } | ||
diff --git a/shared/core-utils/common/path.ts b/shared/core-utils/common/path.ts new file mode 100644 index 000000000..006505316 --- /dev/null +++ b/shared/core-utils/common/path.ts | |||
@@ -0,0 +1,48 @@ | |||
1 | import { basename, extname, isAbsolute, join, resolve } from 'path' | ||
2 | |||
3 | let rootPath: string | ||
4 | |||
5 | function root () { | ||
6 | if (rootPath) return rootPath | ||
7 | |||
8 | rootPath = __dirname | ||
9 | |||
10 | if (basename(rootPath) === 'tools') rootPath = resolve(rootPath, '..') | ||
11 | if (basename(rootPath) === 'scripts') rootPath = resolve(rootPath, '..') | ||
12 | if (basename(rootPath) === 'common') rootPath = resolve(rootPath, '..') | ||
13 | if (basename(rootPath) === 'core-utils') rootPath = resolve(rootPath, '..') | ||
14 | if (basename(rootPath) === 'shared') rootPath = resolve(rootPath, '..') | ||
15 | if (basename(rootPath) === 'server') rootPath = resolve(rootPath, '..') | ||
16 | if (basename(rootPath) === 'dist') rootPath = resolve(rootPath, '..') | ||
17 | |||
18 | return rootPath | ||
19 | } | ||
20 | |||
21 | function buildPath (path: string) { | ||
22 | if (isAbsolute(path)) return path | ||
23 | |||
24 | return join(root(), path) | ||
25 | } | ||
26 | |||
27 | function getLowercaseExtension (filename: string) { | ||
28 | const ext = extname(filename) || '' | ||
29 | |||
30 | return ext.toLowerCase() | ||
31 | } | ||
32 | |||
33 | function buildAbsoluteFixturePath (path: string, customCIPath = false) { | ||
34 | if (isAbsolute(path)) return path | ||
35 | |||
36 | if (customCIPath && process.env.GITHUB_WORKSPACE) { | ||
37 | return join(process.env.GITHUB_WORKSPACE, 'fixtures', path) | ||
38 | } | ||
39 | |||
40 | return join(root(), 'server', 'tests', 'fixtures', path) | ||
41 | } | ||
42 | |||
43 | export { | ||
44 | root, | ||
45 | buildPath, | ||
46 | buildAbsoluteFixturePath, | ||
47 | getLowercaseExtension | ||
48 | } | ||
diff --git a/shared/core-utils/common/random.ts b/shared/core-utils/common/random.ts new file mode 100644 index 000000000..705735d09 --- /dev/null +++ b/shared/core-utils/common/random.ts | |||
@@ -0,0 +1,8 @@ | |||
1 | // high excluded | ||
2 | function randomInt (low: number, high: number) { | ||
3 | return Math.floor(Math.random() * (high - low) + low) | ||
4 | } | ||
5 | |||
6 | export { | ||
7 | randomInt | ||
8 | } | ||
diff --git a/shared/core-utils/common/time.ts b/shared/core-utils/common/time.ts new file mode 100644 index 000000000..2992609ca --- /dev/null +++ b/shared/core-utils/common/time.ts | |||
@@ -0,0 +1,7 @@ | |||
1 | function wait (milliseconds: number) { | ||
2 | return new Promise(resolve => setTimeout(resolve, milliseconds)) | ||
3 | } | ||
4 | |||
5 | export { | ||
6 | wait | ||
7 | } | ||
diff --git a/shared/core-utils/common/types.ts b/shared/core-utils/common/types.ts deleted file mode 100644 index bd2a97b98..000000000 --- a/shared/core-utils/common/types.ts +++ /dev/null | |||
@@ -1,45 +0,0 @@ | |||
1 | /* eslint-disable @typescript-eslint/array-type */ | ||
2 | |||
3 | export type FunctionPropertyNames<T> = { | ||
4 | [K in keyof T]: T[K] extends Function ? K : never | ||
5 | }[keyof T] | ||
6 | |||
7 | export type FunctionProperties<T> = Pick<T, FunctionPropertyNames<T>> | ||
8 | |||
9 | export type AttributesOnly<T> = { | ||
10 | [K in keyof T]: T[K] extends Function ? never : T[K] | ||
11 | } | ||
12 | |||
13 | export type PickWith<T, KT extends keyof T, V> = { | ||
14 | [P in KT]: T[P] extends V ? V : never | ||
15 | } | ||
16 | |||
17 | export type PickWithOpt<T, KT extends keyof T, V> = { | ||
18 | [P in KT]?: T[P] extends V ? V : never | ||
19 | } | ||
20 | |||
21 | // https://github.com/krzkaczor/ts-essentials Rocks! | ||
22 | export type DeepPartial<T> = { | ||
23 | [P in keyof T]?: T[P] extends Array<infer U> | ||
24 | ? Array<DeepPartial<U>> | ||
25 | : T[P] extends ReadonlyArray<infer U> | ||
26 | ? ReadonlyArray<DeepPartial<U>> | ||
27 | : DeepPartial<T[P]> | ||
28 | } | ||
29 | |||
30 | type Primitive = string | Function | number | boolean | Symbol | undefined | null | ||
31 | export type DeepOmitHelper<T, K extends keyof T> = { | ||
32 | [P in K]: // extra level of indirection needed to trigger homomorhic behavior | ||
33 | T[P] extends infer TP // distribute over unions | ||
34 | ? TP extends Primitive | ||
35 | ? TP // leave primitives and functions alone | ||
36 | : TP extends any[] | ||
37 | ? DeepOmitArray<TP, K> // Array special handling | ||
38 | : DeepOmit<TP, K> | ||
39 | : never | ||
40 | } | ||
41 | export type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>> | ||
42 | |||
43 | export type DeepOmitArray<T extends any[], K> = { | ||
44 | [P in keyof T]: DeepOmit<T[P], K> | ||
45 | } | ||
diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts index 9c111cbcc..8020d9b28 100644 --- a/shared/core-utils/common/url.ts +++ b/shared/core-utils/common/url.ts | |||
@@ -50,6 +50,7 @@ function decorateVideoLink (options: { | |||
50 | warningTitle?: boolean | 50 | warningTitle?: boolean |
51 | controls?: boolean | 51 | controls?: boolean |
52 | peertubeLink?: boolean | 52 | peertubeLink?: boolean |
53 | p2p?: boolean | ||
53 | }) { | 54 | }) { |
54 | const { url } = options | 55 | const { url } = options |
55 | 56 | ||
@@ -74,6 +75,7 @@ function decorateVideoLink (options: { | |||
74 | if (options.warningTitle === false) params.set('warningTitle', '0') | 75 | if (options.warningTitle === false) params.set('warningTitle', '0') |
75 | if (options.controls === false) params.set('controls', '0') | 76 | if (options.controls === false) params.set('controls', '0') |
76 | if (options.peertubeLink === false) params.set('peertubeLink', '0') | 77 | if (options.peertubeLink === false) params.set('peertubeLink', '0') |
78 | if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0') | ||
77 | 79 | ||
78 | return buildUrl(url, params) | 80 | return buildUrl(url, params) |
79 | } | 81 | } |
diff --git a/shared/core-utils/common/miscs.ts b/shared/core-utils/common/version.ts index bc65dc338..8a64f8c4d 100644 --- a/shared/core-utils/common/miscs.ts +++ b/shared/core-utils/common/version.ts | |||
@@ -1,8 +1,3 @@ | |||
1 | // high excluded | ||
2 | function randomInt (low: number, high: number) { | ||
3 | return Math.floor(Math.random() * (high - low) + low) | ||
4 | } | ||
5 | |||
6 | // Thanks https://stackoverflow.com/a/16187766 | 1 | // Thanks https://stackoverflow.com/a/16187766 |
7 | function compareSemVer (a: string, b: string) { | 2 | function compareSemVer (a: string, b: string) { |
8 | const regExStrip0 = /(\.0+)+$/ | 3 | const regExStrip0 = /(\.0+)+$/ |
@@ -20,22 +15,6 @@ function compareSemVer (a: string, b: string) { | |||
20 | return segmentsA.length - segmentsB.length | 15 | return segmentsA.length - segmentsB.length |
21 | } | 16 | } |
22 | 17 | ||
23 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | ||
24 | return (a: any, b: any) => { | ||
25 | if (a[key] < b[key]) { | ||
26 | return order === 'asc' ? -1 : 1 | ||
27 | } | ||
28 | |||
29 | if (a[key] > b[key]) { | ||
30 | return order === 'asc' ? 1 : -1 | ||
31 | } | ||
32 | |||
33 | return 0 | ||
34 | } | ||
35 | } | ||
36 | |||
37 | export { | 18 | export { |
38 | randomInt, | 19 | compareSemVer |
39 | compareSemVer, | ||
40 | sortObjectComparator | ||
41 | } | 20 | } |
diff --git a/shared/core-utils/i18n/i18n.ts b/shared/core-utils/i18n/i18n.ts index f27de20f1..ae6af8192 100644 --- a/shared/core-utils/i18n/i18n.ts +++ b/shared/core-utils/i18n/i18n.ts | |||
@@ -28,6 +28,8 @@ export const I18N_LOCALES = { | |||
28 | 'ru-RU': 'русский', | 28 | 'ru-RU': 'русский', |
29 | 'sq': 'Shqip', | 29 | 'sq': 'Shqip', |
30 | 'sv-SE': 'Svenska', | 30 | 'sv-SE': 'Svenska', |
31 | 'nn': 'norsk nynorsk', | ||
32 | 'nb-NO': 'norsk bokmål', | ||
31 | 'th-TH': 'ไทย', | 33 | 'th-TH': 'ไทย', |
32 | 'vi-VN': 'Tiếng Việt', | 34 | 'vi-VN': 'Tiếng Việt', |
33 | 'zh-Hans-CN': '简体中文(中国)', | 35 | 'zh-Hans-CN': '简体中文(中国)', |
@@ -52,6 +54,7 @@ const I18N_LOCALE_ALIAS = { | |||
52 | 'nl': 'nl-NL', | 54 | 'nl': 'nl-NL', |
53 | 'pl': 'pl-PL', | 55 | 'pl': 'pl-PL', |
54 | 'pt': 'pt-BR', | 56 | 'pt': 'pt-BR', |
57 | 'nb': 'nb-NO', | ||
55 | 'ru': 'ru-RU', | 58 | 'ru': 'ru-RU', |
56 | 'sv': 'sv-SE', | 59 | 'sv': 'sv-SE', |
57 | 'th': 'th-TH', | 60 | 'th': 'th-TH', |
diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts index e0a6a8087..8daaa2d04 100644 --- a/shared/core-utils/index.ts +++ b/shared/core-utils/index.ts | |||
@@ -4,5 +4,4 @@ export * from './i18n' | |||
4 | export * from './plugins' | 4 | export * from './plugins' |
5 | export * from './renderer' | 5 | export * from './renderer' |
6 | export * from './users' | 6 | export * from './users' |
7 | export * from './utils' | ||
8 | export * from './videos' | 7 | export * from './videos' |
diff --git a/shared/core-utils/users/user-role.ts b/shared/core-utils/users/user-role.ts index 81cba1dad..cc757d779 100644 --- a/shared/core-utils/users/user-role.ts +++ b/shared/core-utils/users/user-role.ts | |||
@@ -14,8 +14,8 @@ const userRoleRights: { [ id in UserRole ]: UserRight[] } = { | |||
14 | [UserRole.MODERATOR]: [ | 14 | [UserRole.MODERATOR]: [ |
15 | UserRight.MANAGE_VIDEO_BLACKLIST, | 15 | UserRight.MANAGE_VIDEO_BLACKLIST, |
16 | UserRight.MANAGE_ABUSES, | 16 | UserRight.MANAGE_ABUSES, |
17 | UserRight.MANAGE_ANY_VIDEO_CHANNEL, | ||
17 | UserRight.REMOVE_ANY_VIDEO, | 18 | UserRight.REMOVE_ANY_VIDEO, |
18 | UserRight.REMOVE_ANY_VIDEO_CHANNEL, | ||
19 | UserRight.REMOVE_ANY_VIDEO_PLAYLIST, | 19 | UserRight.REMOVE_ANY_VIDEO_PLAYLIST, |
20 | UserRight.REMOVE_ANY_VIDEO_COMMENT, | 20 | UserRight.REMOVE_ANY_VIDEO_COMMENT, |
21 | UserRight.UPDATE_ANY_VIDEO, | 21 | UserRight.UPDATE_ANY_VIDEO, |
diff --git a/shared/core-utils/utils/index.ts b/shared/core-utils/utils/index.ts deleted file mode 100644 index 8d16365a8..000000000 --- a/shared/core-utils/utils/index.ts +++ /dev/null | |||
@@ -1,2 +0,0 @@ | |||
1 | export * from './array' | ||
2 | export * from './object' | ||
diff --git a/shared/core-utils/utils/object.ts b/shared/core-utils/utils/object.ts deleted file mode 100644 index 9a8a98f9b..000000000 --- a/shared/core-utils/utils/object.ts +++ /dev/null | |||
@@ -1,15 +0,0 @@ | |||
1 | function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> { | ||
2 | const result: any = {} | ||
3 | |||
4 | for (const key of keys) { | ||
5 | if (Object.prototype.hasOwnProperty.call(object, key)) { | ||
6 | result[key] = object[key] | ||
7 | } | ||
8 | } | ||
9 | |||
10 | return result | ||
11 | } | ||
12 | |||
13 | export { | ||
14 | pick | ||
15 | } | ||
diff --git a/shared/core-utils/videos/bitrate.ts b/shared/core-utils/videos/bitrate.ts index c1891188f..30d22df09 100644 --- a/shared/core-utils/videos/bitrate.ts +++ b/shared/core-utils/videos/bitrate.ts | |||
@@ -1,4 +1,4 @@ | |||
1 | import { VideoResolution } from "@shared/models" | 1 | import { VideoResolution } from '@shared/models' |
2 | 2 | ||
3 | type BitPerPixel = { [ id in VideoResolution ]: number } | 3 | type BitPerPixel = { [ id in VideoResolution ]: number } |
4 | 4 | ||