2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
|
|
|
import type { Video } from '@peertube/peertube-types'
|
2021-12-14 14:54:36 +00:00
|
|
|
import type { ChatType } from 'shared/lib/types'
|
|
|
|
import { AutoColors, isAutoColorsAvailable } from 'shared/lib/autocolors'
|
|
|
|
import { logger } from './logger'
|
|
|
|
import { computeAutoColors } from './colors'
|
|
|
|
|
2021-12-14 16:46:07 +00:00
|
|
|
interface UriOptions {
|
2022-01-04 16:42:03 +00:00
|
|
|
readonly?: boolean | 'noscroll'
|
2021-12-14 16:46:07 +00:00
|
|
|
ignoreAutoColors?: boolean
|
|
|
|
permanent?: boolean
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
function getBaseRoute ({ peertubeHelpers }: RegisterClientOptions, permanent: boolean = false): string {
|
2021-12-14 16:46:07 +00:00
|
|
|
if (permanent) {
|
|
|
|
return '/plugins/livechat/router'
|
|
|
|
}
|
2021-12-14 14:54:36 +00:00
|
|
|
// NB: this will come with Peertube > 3.2.1 (3.3.0?)
|
|
|
|
if (peertubeHelpers.getBaseRouterRoute) {
|
|
|
|
return peertubeHelpers.getBaseRouterRoute()
|
|
|
|
}
|
|
|
|
// We are guessing the route with the correct plugin version with this trick:
|
|
|
|
const staticBase = peertubeHelpers.getBaseStaticRoute()
|
|
|
|
return staticBase.replace(/\/static.*$/, '/router')
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:46:07 +00:00
|
|
|
function getIframeUri (
|
2022-01-11 00:29:33 +00:00
|
|
|
registerOptions: RegisterClientOptions, settings: any, video: Video, uriOptions: UriOptions = {}
|
2021-12-14 16:46:07 +00:00
|
|
|
): string | null {
|
2021-12-14 14:54:36 +00:00
|
|
|
if (!settings) {
|
|
|
|
logger.error('Settings are not initialized, too soon to compute the iframeUri')
|
|
|
|
return null
|
|
|
|
}
|
2021-12-14 16:46:07 +00:00
|
|
|
let iframeUriStr = ''
|
2021-12-14 14:54:36 +00:00
|
|
|
const chatType: ChatType = (settings['chat-type'] ?? 'disabled') as ChatType
|
|
|
|
if (chatType === 'builtin-prosody' || chatType === 'builtin-converse') {
|
|
|
|
// Using the builtin converseJS
|
2021-12-14 16:46:07 +00:00
|
|
|
iframeUriStr = getBaseRoute(registerOptions, uriOptions.permanent)
|
|
|
|
iframeUriStr += '/webchat/room/' + encodeURIComponent(video.uuid)
|
2021-12-14 14:54:36 +00:00
|
|
|
} else if (chatType === 'external-uri') {
|
2021-12-14 16:46:07 +00:00
|
|
|
iframeUriStr = settings['chat-uri'] || ''
|
|
|
|
iframeUriStr = iframeUriStr.replace(/{{VIDEO_UUID}}/g, encodeURIComponent(video.uuid))
|
|
|
|
if (iframeUriStr.includes('{{CHANNEL_ID}}')) {
|
2021-12-14 14:54:36 +00:00
|
|
|
if (!video.channel || !video.channel.id) {
|
|
|
|
logger.error('Missing channel info in video object.')
|
|
|
|
return null
|
|
|
|
}
|
2021-12-14 16:46:07 +00:00
|
|
|
iframeUriStr = iframeUriStr.replace(/{{CHANNEL_ID}}/g, encodeURIComponent(video.channel.id))
|
2021-12-14 14:54:36 +00:00
|
|
|
}
|
2021-12-14 16:46:07 +00:00
|
|
|
if (!/^https?:\/\//.test(iframeUriStr)) {
|
2021-12-14 14:54:36 +00:00
|
|
|
logger.error('The webchaturi must begin with https://')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.error('Chat disabled.')
|
|
|
|
return null
|
|
|
|
}
|
2021-12-14 16:46:07 +00:00
|
|
|
if (iframeUriStr === '') {
|
2021-12-14 14:54:36 +00:00
|
|
|
logger.error('No iframe uri')
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:46:07 +00:00
|
|
|
const iFrameUri = new URL(iframeUriStr, window.location.origin)
|
|
|
|
|
2021-12-14 14:54:36 +00:00
|
|
|
if (
|
2021-12-14 16:46:07 +00:00
|
|
|
!uriOptions.ignoreAutoColors &&
|
2021-12-14 14:54:36 +00:00
|
|
|
settings['converse-autocolors'] &&
|
|
|
|
isAutoColorsAvailable(settings['chat-type'] as ChatType, settings['converse-theme'])
|
|
|
|
) {
|
|
|
|
logger.info('We have to try to compute autocolors.')
|
|
|
|
try {
|
|
|
|
const autocolors = computeAutoColors()
|
|
|
|
if (autocolors) {
|
|
|
|
for (const p in autocolors) {
|
2021-12-14 16:46:07 +00:00
|
|
|
iFrameUri.searchParams.set('_ac_' + p, autocolors[p as keyof AutoColors])
|
2021-12-14 14:54:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logger.error(`Failed computing autocolors: '${err as string}'`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 16:46:07 +00:00
|
|
|
if (uriOptions.readonly) {
|
2022-01-04 16:42:03 +00:00
|
|
|
iFrameUri.searchParams.set('_readonly', (typeof uriOptions.readonly === 'string') ? uriOptions.readonly : 'true')
|
2021-12-14 16:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iframeUriStr = iFrameUri.href
|
|
|
|
return iframeUriStr
|
2021-12-14 14:54:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2021-12-14 16:46:07 +00:00
|
|
|
UriOptions,
|
2021-12-14 14:54:36 +00:00
|
|
|
getBaseRoute,
|
|
|
|
getIframeUri
|
|
|
|
}
|