1 import { Video, VideoPlaylist } from '../../models'
2 import { secondsToTime } from './date'
4 function addQueryParams (url: string, params: { [ id: string ]: string }) {
5 const objUrl = new URL(url)
7 for (const key of Object.keys(params)) {
8 objUrl.searchParams.append(key, params[key])
11 return objUrl.toString()
14 function buildPlaylistLink (playlist: Pick<VideoPlaylist, 'shortUUID'>, base?: string) {
15 return (base ?? window.location.origin) + buildPlaylistWatchPath(playlist)
18 function buildPlaylistWatchPath (playlist: Pick<VideoPlaylist, 'shortUUID'>) {
19 return '/w/p/' + playlist.shortUUID
22 function buildVideoWatchPath (video: Pick<Video, 'shortUUID'>) {
23 return '/w/' + video.shortUUID
26 function buildVideoLink (video: Pick<Video, 'shortUUID'>, base?: string) {
27 return (base ?? window.location.origin) + buildVideoWatchPath(video)
30 function buildPlaylistEmbedPath (playlist: Pick<VideoPlaylist, 'uuid'>) {
31 return '/video-playlists/embed/' + playlist.uuid
34 function buildPlaylistEmbedLink (playlist: Pick<VideoPlaylist, 'uuid'>, base?: string) {
35 return (base ?? window.location.origin) + buildPlaylistEmbedPath(playlist)
38 function buildVideoEmbedPath (video: Pick<Video, 'uuid'>) {
39 return '/videos/embed/' + video.uuid
42 function buildVideoEmbedLink (video: Pick<Video, 'uuid'>, base?: string) {
43 return (base ?? window.location.origin) + buildVideoEmbedPath(video)
46 function decorateVideoLink (options: {
60 warningTitle?: boolean
65 peertubeLink?: boolean
68 const { url } = options
70 const params = new URLSearchParams()
72 if (options.startTime !== undefined && options.startTime !== null) {
73 const startTimeInt = Math.floor(options.startTime)
74 params.set('start', secondsToTime(startTimeInt))
77 if (options.stopTime) {
78 const stopTimeInt = Math.floor(options.stopTime)
79 params.set('stop', secondsToTime(stopTimeInt))
82 if (options.subtitle) params.set('subtitle', options.subtitle)
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')
90 if (options.controls === false) params.set('controls', '0')
91 if (options.controlBar === false) params.set('controlBar', '0')
93 if (options.peertubeLink === false) params.set('peertubeLink', '0')
94 if (options.p2p !== undefined) params.set('p2p', options.p2p ? '1' : '0')
96 return buildUrl(url, params)
99 function decoratePlaylistLink (options: {
102 playlistPosition?: number
104 const { url } = options
106 const params = new URLSearchParams()
108 if (options.playlistPosition) params.set('playlistPosition', '' + options.playlistPosition)
110 return buildUrl(url, params)
113 // ---------------------------------------------------------------------------
122 buildPlaylistWatchPath,
124 buildPlaylistEmbedPath,
127 buildPlaylistEmbedLink,
134 function buildUrl (url: string, params: URLSearchParams) {
135 let hasParams = false
136 params.forEach(() => { hasParams = true })
138 if (hasParams) return url + '?' + params.toString()