Share chat modal WIP.

This commit is contained in:
John Livingston
2021-12-14 17:46:07 +01:00
parent e6dd31afd0
commit 9f9643ac89
3 changed files with 142 additions and 19 deletions

View File

@ -3,38 +3,49 @@ import { AutoColors, isAutoColorsAvailable } from 'shared/lib/autocolors'
import { logger } from './logger'
import { computeAutoColors } from './colors'
function getBaseRoute ({ peertubeHelpers }: RegisterOptions): string {
interface UriOptions {
readonly?: boolean
ignoreAutoColors?: boolean
permanent?: boolean
}
function getBaseRoute ({ peertubeHelpers }: RegisterOptions, permanent: boolean = false): string {
if (permanent) {
return '/plugins/livechat/router'
}
// 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()
// we can't use '/plugins/livechat/router', because the loaded html page needs correct relative paths.
return staticBase.replace(/\/static.*$/, '/router')
}
function getIframeUri (registerOptions: RegisterOptions, settings: any, video: Video): string | null {
function getIframeUri (
registerOptions: RegisterOptions, settings: any, video: Video, uriOptions: UriOptions = {}
): string | null {
if (!settings) {
logger.error('Settings are not initialized, too soon to compute the iframeUri')
return null
}
let iframeUri = ''
let iframeUriStr = ''
const chatType: ChatType = (settings['chat-type'] ?? 'disabled') as ChatType
if (chatType === 'builtin-prosody' || chatType === 'builtin-converse') {
// Using the builtin converseJS
iframeUri = getBaseRoute(registerOptions) + '/webchat/room/' + encodeURIComponent(video.uuid)
iframeUriStr = getBaseRoute(registerOptions, uriOptions.permanent)
iframeUriStr += '/webchat/room/' + encodeURIComponent(video.uuid)
} else if (chatType === 'external-uri') {
iframeUri = settings['chat-uri'] || ''
iframeUri = iframeUri.replace(/{{VIDEO_UUID}}/g, encodeURIComponent(video.uuid))
if (iframeUri.includes('{{CHANNEL_ID}}')) {
iframeUriStr = settings['chat-uri'] || ''
iframeUriStr = iframeUriStr.replace(/{{VIDEO_UUID}}/g, encodeURIComponent(video.uuid))
if (iframeUriStr.includes('{{CHANNEL_ID}}')) {
if (!video.channel || !video.channel.id) {
logger.error('Missing channel info in video object.')
return null
}
iframeUri = iframeUri.replace(/{{CHANNEL_ID}}/g, encodeURIComponent(video.channel.id))
iframeUriStr = iframeUriStr.replace(/{{CHANNEL_ID}}/g, encodeURIComponent(video.channel.id))
}
if (!/^https?:\/\//.test(iframeUri)) {
if (!/^https?:\/\//.test(iframeUriStr)) {
logger.error('The webchaturi must begin with https://')
return null
}
@ -42,12 +53,15 @@ function getIframeUri (registerOptions: RegisterOptions, settings: any, video: V
logger.error('Chat disabled.')
return null
}
if (iframeUri === '') {
if (iframeUriStr === '') {
logger.error('No iframe uri')
return null
}
const iFrameUri = new URL(iframeUriStr, window.location.origin)
if (
!uriOptions.ignoreAutoColors &&
settings['converse-autocolors'] &&
isAutoColorsAvailable(settings['chat-type'] as ChatType, settings['converse-theme'])
) {
@ -55,21 +69,25 @@ function getIframeUri (registerOptions: RegisterOptions, settings: any, video: V
try {
const autocolors = computeAutoColors()
if (autocolors) {
const url = new URL(iframeUri, window.location.origin)
for (const p in autocolors) {
url.searchParams.set('_ac_' + p, autocolors[p as keyof AutoColors])
iFrameUri.searchParams.set('_ac_' + p, autocolors[p as keyof AutoColors])
}
iframeUri = url.href
}
} catch (err) {
logger.error(`Failed computing autocolors: '${err as string}'`)
}
}
return iframeUri
if (uriOptions.readonly) {
iFrameUri.searchParams.set('_readonly', 'true')
}
iframeUriStr = iFrameUri.href
return iframeUriStr
}
export {
UriOptions,
getBaseRoute,
getIframeUri
}