aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-26 15:04:37 +0200
committerChocobozzz <me@florianbigard.com>2021-07-26 15:04:37 +0200
commit15a7eafb892441957ba7dd6fcbf556086fe5b2b3 (patch)
tree0786bd1a96c7d168a097ffcf5893737db2ab578e /shared
parent9162fdd36300d2478f13d6ad346ec2c323f40faa (diff)
downloadPeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.tar.gz
PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.tar.zst
PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.zip
Refactor video links builders
Diffstat (limited to 'shared')
-rw-r--r--shared/core-utils/common/date.ts47
-rw-r--r--shared/core-utils/common/index.ts3
-rw-r--r--shared/core-utils/common/miscs.ts10
-rw-r--r--shared/core-utils/common/promises.ts12
-rw-r--r--shared/core-utils/common/url.ts130
-rw-r--r--shared/core-utils/index.ts1
-rw-r--r--shared/core-utils/logs/index.ts1
-rw-r--r--shared/core-utils/logs/logs.ts25
-rw-r--r--shared/core-utils/plugins/hooks.ts2
-rw-r--r--shared/extra-utils/server/server.ts2
10 files changed, 192 insertions, 41 deletions
diff --git a/shared/core-utils/common/date.ts b/shared/core-utils/common/date.ts
index 4f92f758f..3e4a3c08c 100644
--- a/shared/core-utils/common/date.ts
+++ b/shared/core-utils/common/date.ts
@@ -43,6 +43,49 @@ function isLastWeek (d: Date) {
43 return getDaysDifferences(now, d) <= 7 43 return getDaysDifferences(now, d) <= 7
44} 44}
45 45
46function timeToInt (time: number | string) {
47 if (!time) return 0
48 if (typeof time === 'number') return time
49
50 const reg = /^((\d+)[h:])?((\d+)[m:])?((\d+)s?)?$/
51 const matches = time.match(reg)
52
53 if (!matches) return 0
54
55 const hours = parseInt(matches[2] || '0', 10)
56 const minutes = parseInt(matches[4] || '0', 10)
57 const seconds = parseInt(matches[6] || '0', 10)
58
59 return hours * 3600 + minutes * 60 + seconds
60}
61
62function secondsToTime (seconds: number, full = false, symbol?: string) {
63 let time = ''
64
65 if (seconds === 0 && !full) return '0s'
66
67 const hourSymbol = (symbol || 'h')
68 const minuteSymbol = (symbol || 'm')
69 const secondsSymbol = full ? '' : 's'
70
71 const hours = Math.floor(seconds / 3600)
72 if (hours >= 1) time = hours + hourSymbol
73 else if (full) time = '0' + hourSymbol
74
75 seconds %= 3600
76 const minutes = Math.floor(seconds / 60)
77 if (minutes >= 1 && minutes < 10 && full) time += '0' + minutes + minuteSymbol
78 else if (minutes >= 1) time += minutes + minuteSymbol
79 else if (full) time += '00' + minuteSymbol
80
81 seconds %= 60
82 if (seconds >= 1 && seconds < 10 && full) time += '0' + seconds + secondsSymbol
83 else if (seconds >= 1) time += seconds + secondsSymbol
84 else if (full) time += '00'
85
86 return time
87}
88
46// --------------------------------------------------------------------------- 89// ---------------------------------------------------------------------------
47 90
48export { 91export {
@@ -51,7 +94,9 @@ export {
51 isThisMonth, 94 isThisMonth,
52 isToday, 95 isToday,
53 isLastMonth, 96 isLastMonth,
54 isLastWeek 97 isLastWeek,
98 timeToInt,
99 secondsToTime
55} 100}
56 101
57// --------------------------------------------------------------------------- 102// ---------------------------------------------------------------------------
diff --git a/shared/core-utils/common/index.ts b/shared/core-utils/common/index.ts
index 83f2ccbb6..0908ff981 100644
--- a/shared/core-utils/common/index.ts
+++ b/shared/core-utils/common/index.ts
@@ -1,5 +1,6 @@
1export * from './date' 1export * from './date'
2export * from './miscs' 2export * from './miscs'
3export * from './regexp' 3export * from './regexp'
4export * from './promises'
4export * from './types' 5export * from './types'
5export * from './url 6export * from './url'
diff --git a/shared/core-utils/common/miscs.ts b/shared/core-utils/common/miscs.ts
index 4780ca922..bc65dc338 100644
--- a/shared/core-utils/common/miscs.ts
+++ b/shared/core-utils/common/miscs.ts
@@ -20,14 +20,6 @@ function compareSemVer (a: string, b: string) {
20 return segmentsA.length - segmentsB.length 20 return segmentsA.length - segmentsB.length
21} 21}
22 22
23function isPromise (value: any) {
24 return value && typeof value.then === 'function'
25}
26
27function isCatchable (value: any) {
28 return value && typeof value.catch === 'function'
29}
30
31function sortObjectComparator (key: string, order: 'asc' | 'desc') { 23function sortObjectComparator (key: string, order: 'asc' | 'desc') {
32 return (a: any, b: any) => { 24 return (a: any, b: any) => {
33 if (a[key] < b[key]) { 25 if (a[key] < b[key]) {
@@ -45,7 +37,5 @@ function sortObjectComparator (key: string, order: 'asc' | 'desc') {
45export { 37export {
46 randomInt, 38 randomInt,
47 compareSemVer, 39 compareSemVer,
48 isPromise,
49 isCatchable,
50 sortObjectComparator 40 sortObjectComparator
51} 41}
diff --git a/shared/core-utils/common/promises.ts b/shared/core-utils/common/promises.ts
new file mode 100644
index 000000000..7ef9d60b6
--- /dev/null
+++ b/shared/core-utils/common/promises.ts
@@ -0,0 +1,12 @@
1function isPromise (value: any) {
2 return value && typeof value.then === 'function'
3}
4
5function isCatchable (value: any) {
6 return value && typeof value.catch === 'function'
7}
8
9export {
10 isPromise,
11 isCatchable
12}
diff --git a/shared/core-utils/common/url.ts b/shared/core-utils/common/url.ts
index e69de29bb..52ed247c4 100644
--- a/shared/core-utils/common/url.ts
+++ b/shared/core-utils/common/url.ts
@@ -0,0 +1,130 @@
1import { Video, VideoPlaylist } from '../../models'
2import { secondsToTime } from './date'
3
4function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) {
5 return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist)
6}
7
8function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) {
9 return '/w/p/' + playlist.shortUUID
10}
11
12function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) {
13 return '/w/' + video.shortUUID
14}
15
16function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) {
17 return (base ?? window.location.origin) + buildVideoWatchPath(video)
18}
19
20function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) {
21 return '/video-playlists/embed/' + playlist.uuid
22}
23
24function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) {
25 return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist)
26}
27
28function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) {
29 return '/videos/embed/' + video.uuid
30}
31
32function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) {
33 return (base ?? window.location.origin) + buildVideoEmbedPath(video)
34}
35
36function decorateVideoLink (options: {
37 url: string
38
39 startTime?: number
40 stopTime?: number
41
42 subtitle?: string
43
44 loop?: boolean
45 autoplay?: boolean
46 muted?: boolean
47
48 // Embed options
49 title?: boolean
50 warningTitle?: boolean
51 controls?: boolean
52 peertubeLink?: boolean
53}) {
54 const { url } = options
55
56 const params = generateParams(window.location.search)
57
58 if (options.startTime !== undefined && options.startTime !== null) {
59 const startTimeInt = Math.floor(options.startTime)
60 params.set('start', secondsToTime(startTimeInt))
61 }
62
63 if (options.stopTime) {
64 const stopTimeInt = Math.floor(options.stopTime)
65 params.set('stop', secondsToTime(stopTimeInt))
66 }
67
68 if (options.subtitle) params.set('subtitle', options.subtitle)
69
70 if (options.loop === true) params.set('loop', '1')
71 if (options.autoplay === true) params.set('autoplay', '1')
72 if (options.muted === true) params.set('muted', '1')
73 if (options.title === false) params.set('title', '0')
74 if (options.warningTitle === false) params.set('warningTitle', '0')
75 if (options.controls === false) params.set('controls', '0')
76 if (options.peertubeLink === false) params.set('peertubeLink', '0')
77
78 return buildUrl(url, params)
79}
80
81function decoratePlaylistLink (options: {
82 url: string
83
84 playlistPosition?: number
85}) {
86 const { url } = options
87
88 const params = generateParams(window.location.search)
89
90 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
91
92 return buildUrl(url, params)
93}
94
95// ---------------------------------------------------------------------------
96
97export {
98 buildPlaylistLink,
99 buildVideoLink,
100
101 buildVideoWatchPath,
102 buildPlaylistWatchPath,
103
104 buildPlaylistEmbedPath,
105 buildVideoEmbedPath,
106
107 buildPlaylistEmbedLink,
108 buildVideoEmbedLink,
109
110 decorateVideoLink,
111 decoratePlaylistLink
112}
113
114function buildUrl (url: string, params: URLSearchParams) {
115 let hasParams = false
116 params.forEach(() => { hasParams = true })
117
118 if (hasParams) return url + '?' + params.toString()
119
120 return url
121}
122
123function generateParams (url: string) {
124 const params = new URLSearchParams(window.location.search)
125 // Unused parameters in embed
126 params.delete('videoId')
127 params.delete('resume')
128
129 return params
130}
diff --git a/shared/core-utils/index.ts b/shared/core-utils/index.ts
index 0b05dc9eb..66d50ef93 100644
--- a/shared/core-utils/index.ts
+++ b/shared/core-utils/index.ts
@@ -1,7 +1,6 @@
1export * from './abuse' 1export * from './abuse'
2export * from './common' 2export * from './common'
3export * from './i18n' 3export * from './i18n'
4export * from './logs'
5export * from './plugins' 4export * from './plugins'
6export * from './renderer' 5export * from './renderer'
7export * from './users' 6export * from './users'
diff --git a/shared/core-utils/logs/index.ts b/shared/core-utils/logs/index.ts
deleted file mode 100644
index ceb5d7a7f..000000000
--- a/shared/core-utils/logs/index.ts
+++ /dev/null
@@ -1 +0,0 @@
1export * from './logs'
diff --git a/shared/core-utils/logs/logs.ts b/shared/core-utils/logs/logs.ts
deleted file mode 100644
index d0996cf55..000000000
--- a/shared/core-utils/logs/logs.ts
+++ /dev/null
@@ -1,25 +0,0 @@
1import { stat } from 'fs-extra'
2
3async function mtimeSortFilesDesc (files: string[], basePath: string) {
4 const promises = []
5 const out: { file: string, mtime: number }[] = []
6
7 for (const file of files) {
8 const p = stat(basePath + '/' + file)
9 .then(stats => {
10 if (stats.isFile()) out.push({ file, mtime: stats.mtime.getTime() })
11 })
12
13 promises.push(p)
14 }
15
16 await Promise.all(promises)
17
18 out.sort((a, b) => b.mtime - a.mtime)
19
20 return out
21}
22
23export {
24 mtimeSortFilesDesc
25}
diff --git a/shared/core-utils/plugins/hooks.ts b/shared/core-utils/plugins/hooks.ts
index 5405e0529..92cb5ad68 100644
--- a/shared/core-utils/plugins/hooks.ts
+++ b/shared/core-utils/plugins/hooks.ts
@@ -1,5 +1,5 @@
1import { HookType } from '../../models/plugins/hook-type.enum' 1import { HookType } from '../../models/plugins/hook-type.enum'
2import { isCatchable, isPromise } from '../miscs/miscs' 2import { isCatchable, isPromise } from '../common/promises'
3 3
4function getHookType (hookName: string) { 4function getHookType (hookName: string) {
5 if (hookName.startsWith('filter:')) return HookType.FILTER 5 if (hookName.startsWith('filter:')) return HookType.FILTER
diff --git a/shared/extra-utils/server/server.ts b/shared/extra-utils/server/server.ts
index d37a7f39c..3c335b8e4 100644
--- a/shared/extra-utils/server/server.ts
+++ b/shared/extra-utils/server/server.ts
@@ -2,7 +2,7 @@ import { ChildProcess, fork } from 'child_process'
2import { copy } from 'fs-extra' 2import { copy } from 'fs-extra'
3import { join } from 'path' 3import { join } from 'path'
4import { root } from '@server/helpers/core-utils' 4import { root } from '@server/helpers/core-utils'
5import { randomInt } from '../../core-utils/miscs/miscs' 5import { randomInt } from '@shared/core-utils'
6import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos' 6import { Video, VideoChannel, VideoCreateResult, VideoDetails } from '../../models/videos'
7import { BulkCommand } from '../bulk' 7import { BulkCommand } from '../bulk'
8import { CLICommand } from '../cli' 8import { CLICommand } from '../cli'