]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/core-utils/common/url.ts
Fix server lint
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / url.ts
CommitLineData
15a7eafb
C
1import { Video, VideoPlaylist } from '../../models'
2import { secondsToTime } from './date'
3
3545e72c
C
4function 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
15a7eafb
C
14function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) {
15 return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist)
16}
17
18function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) {
19 return '/w/p/' + playlist.shortUUID
20}
21
22function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) {
23 return '/w/' + video.shortUUID
24}
25
26function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) {
27 return (base ?? window.location.origin) + buildVideoWatchPath(video)
28}
29
30function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) {
31 return '/video-playlists/embed/' + playlist.uuid
32}
33
34function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) {
35 return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist)
36}
37
38function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) {
39 return '/videos/embed/' + video.uuid
40}
41
42function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) {
43 return (base ?? window.location.origin) + buildVideoEmbedPath(video)
44}
45
46function 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
60f013e1 61
15a7eafb 62 controls?: boolean
60f013e1
C
63 controlBar?: boolean
64
15a7eafb 65 peertubeLink?: boolean
85302118 66 p2p?: boolean
15a7eafb
C
67}) {
68 const { url } = options
69
bdb1dfc1 70 const params = new URLSearchParams()
15a7eafb
C
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')
60f013e1 89
15a7eafb 90 if (options.controls === false) params.set('controls', '0')
60f013e1
C
91 if (options.controlBar === false) params.set('controlBar', '0')
92
15a7eafb 93 if (options.peertubeLink === false) params.set('peertubeLink', '0')
85302118 94 if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0')
15a7eafb
C
95
96 return buildUrl(url, params)
97}
98
99function decoratePlaylistLink (options: {
100 url: string
101
102 playlistPosition?: number
103}) {
104 const { url } = options
105
bdb1dfc1 106 const params = new URLSearchParams()
15a7eafb
C
107
108 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
109
110 return buildUrl(url, params)
111}
112
113// ---------------------------------------------------------------------------
114
115export {
3545e72c
C
116 addQueryParams,
117
15a7eafb
C
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
134function 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}