]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/core-utils/common/url.ts
Merge branch 'release/4.3.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 addQueryParams (url: string, params: { [ id: string ]: string }) {
5 const objUrl = new URL(url)
6
7 for (const key of Object.keys(params)) {
8 objUrl.searchParams.append(key, params[key])
9 }
10
11 return objUrl.toString()
12 }
13
14 function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) {
15 return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist)
16 }
17
18 function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) {
19 return '/w/p/' + playlist.shortUUID
20 }
21
22 function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) {
23 return '/w/' + video.shortUUID
24 }
25
26 function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) {
27 return (base ?? window.location.origin) + buildVideoWatchPath(video)
28 }
29
30 function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) {
31 return '/video-playlists/embed/' + playlist.uuid
32 }
33
34 function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) {
35 return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist)
36 }
37
38 function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) {
39 return '/videos/embed/' + video.uuid
40 }
41
42 function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) {
43 return (base ?? window.location.origin) + buildVideoEmbedPath(video)
44 }
45
46 function decorateVideoLink (options: {
47 url: string
48
49 startTime?: number
50 stopTime?: number
51
52 subtitle?: string
53
54 loop?: boolean
55 autoplay?: boolean
56 muted?: boolean
57
58 // Embed options
59 title?: boolean
60 warningTitle?: boolean
61
62 controls?: boolean
63 controlBar?: boolean
64
65 peertubeLink?: boolean
66 p2p?: boolean
67 }) {
68 const { url } = options
69
70 const params = new URLSearchParams()
71
72 if (options.startTime !== undefined && options.startTime !== null) {
73 const startTimeInt = Math.floor(options.startTime)
74 params.set('start', secondsToTime(startTimeInt))
75 }
76
77 if (options.stopTime) {
78 const stopTimeInt = Math.floor(options.stopTime)
79 params.set('stop', secondsToTime(stopTimeInt))
80 }
81
82 if (options.subtitle) params.set('subtitle', options.subtitle)
83
84 if (options.loop === true) params.set('loop', '1')
85 if (options.autoplay === true) params.set('autoplay', '1')
86 if (options.muted === true) params.set('muted', '1')
87 if (options.title === false) params.set('title', '0')
88 if (options.warningTitle === false) params.set('warningTitle', '0')
89
90 if (options.controls === false) params.set('controls', '0')
91 if (options.controlBar === false) params.set('controlBar', '0')
92
93 if (options.peertubeLink === false) params.set('peertubeLink', '0')
94 if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0')
95
96 return buildUrl(url, params)
97 }
98
99 function decoratePlaylistLink (options: {
100 url: string
101
102 playlistPosition?: number
103 }) {
104 const { url } = options
105
106 const params = new URLSearchParams()
107
108 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
109
110 return buildUrl(url, params)
111 }
112
113 // ---------------------------------------------------------------------------
114
115 export {
116 addQueryParams,
117
118 buildPlaylistLink,
119 buildVideoLink,
120
121 buildVideoWatchPath,
122 buildPlaylistWatchPath,
123
124 buildPlaylistEmbedPath,
125 buildVideoEmbedPath,
126
127 buildPlaylistEmbedLink,
128 buildVideoEmbedLink,
129
130 decorateVideoLink,
131 decoratePlaylistLink
132 }
133
134 function buildUrl (url: string, params: URLSearchParams) {
135 let hasParams = false
136 params.forEach(() => { hasParams = true })
137
138 if (hasParams) return url + '?' + params.toString()
139
140 return url
141 }