]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/core-utils/common/url.ts
Merge branch 'release/4.0.0' into develop
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / url.ts
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 p2p?: boolean
54 }) {
55 const { url } = options
56
57 const params = new URLSearchParams()
58
59 if (options.startTime !== undefined && options.startTime !== null) {
60 const startTimeInt = Math.floor(options.startTime)
61 params.set('start', secondsToTime(startTimeInt))
62 }
63
64 if (options.stopTime) {
65 const stopTimeInt = Math.floor(options.stopTime)
66 params.set('stop', secondsToTime(stopTimeInt))
67 }
68
69 if (options.subtitle) params.set('subtitle', options.subtitle)
70
71 if (options.loop === true) params.set('loop', '1')
72 if (options.autoplay === true) params.set('autoplay', '1')
73 if (options.muted === true) params.set('muted', '1')
74 if (options.title === false) params.set('title', '0')
75 if (options.warningTitle === false) params.set('warningTitle', '0')
76 if (options.controls === false) params.set('controls', '0')
77 if (options.peertubeLink === false) params.set('peertubeLink', '0')
78 if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0')
79
80 return buildUrl(url, params)
81 }
82
83 function decoratePlaylistLink (options: {
84 url: string
85
86 playlistPosition?: number
87 }) {
88 const { url } = options
89
90 const params = new URLSearchParams()
91
92 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
93
94 return buildUrl(url, params)
95 }
96
97 // ---------------------------------------------------------------------------
98
99 export {
100 buildPlaylistLink,
101 buildVideoLink,
102
103 buildVideoWatchPath,
104 buildPlaylistWatchPath,
105
106 buildPlaylistEmbedPath,
107 buildVideoEmbedPath,
108
109 buildPlaylistEmbedLink,
110 buildVideoEmbedLink,
111
112 decorateVideoLink,
113 decoratePlaylistLink
114 }
115
116 function buildUrl (url: string, params: URLSearchParams) {
117 let hasParams = false
118 params.forEach(() => { hasParams = true })
119
120 if (hasParams) return url + '?' + params.toString()
121
122 return url
123 }