diff options
author | Chocobozzz <me@florianbigard.com> | 2021-07-26 15:04:37 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-07-26 15:04:37 +0200 |
commit | 15a7eafb892441957ba7dd6fcbf556086fe5b2b3 (patch) | |
tree | 0786bd1a96c7d168a097ffcf5893737db2ab578e /shared/core-utils/common | |
parent | 9162fdd36300d2478f13d6ad346ec2c323f40faa (diff) | |
download | PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.tar.gz PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.tar.zst PeerTube-15a7eafb892441957ba7dd6fcbf556086fe5b2b3.zip |
Refactor video links builders
Diffstat (limited to 'shared/core-utils/common')
-rw-r--r-- | shared/core-utils/common/date.ts | 47 | ||||
-rw-r--r-- | shared/core-utils/common/index.ts | 3 | ||||
-rw-r--r-- | shared/core-utils/common/miscs.ts | 10 | ||||
-rw-r--r-- | shared/core-utils/common/promises.ts | 12 | ||||
-rw-r--r-- | shared/core-utils/common/url.ts | 130 |
5 files changed, 190 insertions, 12 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 | ||
46 | function 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 | |||
62 | function 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 | ||
48 | export { | 91 | export { |
@@ -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 @@ | |||
1 | export * from './date' | 1 | export * from './date' |
2 | export * from './miscs' | 2 | export * from './miscs' |
3 | export * from './regexp' | 3 | export * from './regexp' |
4 | export * from './promises' | ||
4 | export * from './types' | 5 | export * from './types' |
5 | export * from './url | 6 | export * 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 | ||
23 | function isPromise (value: any) { | ||
24 | return value && typeof value.then === 'function' | ||
25 | } | ||
26 | |||
27 | function isCatchable (value: any) { | ||
28 | return value && typeof value.catch === 'function' | ||
29 | } | ||
30 | |||
31 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | 23 | function 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') { | |||
45 | export { | 37 | export { |
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 @@ | |||
1 | function isPromise (value: any) { | ||
2 | return value && typeof value.then === 'function' | ||
3 | } | ||
4 | |||
5 | function isCatchable (value: any) { | ||
6 | return value && typeof value.catch === 'function' | ||
7 | } | ||
8 | |||
9 | export { | ||
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 @@ | |||
1 | import { Video, VideoPlaylist } from '../../models' | ||
2 | import { secondsToTime } from './date' | ||
3 | |||
4 | function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) { | ||
5 | return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist) | ||
6 | } | ||
7 | |||
8 | function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) { | ||
9 | return '/w/p/' + playlist.shortUUID | ||
10 | } | ||
11 | |||
12 | function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) { | ||
13 | return '/w/' + video.shortUUID | ||
14 | } | ||
15 | |||
16 | function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) { | ||
17 | return (base ?? window.location.origin) + buildVideoWatchPath(video) | ||
18 | } | ||
19 | |||
20 | function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) { | ||
21 | return '/video-playlists/embed/' + playlist.uuid | ||
22 | } | ||
23 | |||
24 | function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) { | ||
25 | return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist) | ||
26 | } | ||
27 | |||
28 | function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) { | ||
29 | return '/videos/embed/' + video.uuid | ||
30 | } | ||
31 | |||
32 | function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) { | ||
33 | return (base ?? window.location.origin) + buildVideoEmbedPath(video) | ||
34 | } | ||
35 | |||
36 | function 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 | |||
81 | function 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 | |||
97 | export { | ||
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 | |||
114 | function 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 | |||
123 | function 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 | } | ||