peertube-plugin-livechat/client/common/videowatch/uri.ts

101 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-05-23 09:42:14 +00:00
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
//
// SPDX-License-Identifier: AGPL-3.0-only
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
import type { Video } from '@peertube/peertube-types'
2021-12-14 14:54:36 +00:00
import { AutoColors, isAutoColorsAvailable } from 'shared/lib/autocolors'
import { getBaseRoute } from '../../utils/uri'
import { logger } from '../../utils/logger'
import { computeAutoColors } from '../../utils/colors'
2021-12-14 14:54:36 +00:00
2021-12-14 16:46:07 +00:00
interface UriOptions {
readonly?: boolean | 'noscroll'
transparent?: boolean
2021-12-14 16:46:07 +00:00
ignoreAutoColors?: boolean
permanent?: boolean
}
function getIframeUri (
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
}
let iframeUriStr = getBaseRoute(registerOptions, uriOptions.permanent)
iframeUriStr += '/webchat/room/' + encodeURIComponent(video.uuid)
2021-12-14 14:54:36 +00:00
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['converse-theme'])
2021-12-14 14:54:36 +00:00
) {
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) {
iFrameUri.searchParams.set('_readonly', (typeof uriOptions.readonly === 'string') ? uriOptions.readonly : 'true')
2021-12-14 16:46:07 +00:00
}
if (uriOptions.transparent) {
iFrameUri.searchParams.set('_transparent', 'true')
}
2021-12-14 16:46:07 +00:00
iframeUriStr = iFrameUri.href
return iframeUriStr
2021-12-14 14:54:36 +00:00
}
interface GetXMPPAddrSettings {
'prosody-room-allow-s2s': boolean
'prosody-room-type': string
}
2023-04-14 09:57:01 +00:00
interface XMPPAddr {
uri: string
jid: string
}
function getXMPPAddr (
registerOptions: RegisterClientOptions, settings: GetXMPPAddrSettings, video: Video
2023-04-14 09:57:01 +00:00
): XMPPAddr | null {
// returns something like xmpp:256896ac-199a-4dab-bb3a-4fd916140272@room.instance.tdl?join
if (!settings['prosody-room-allow-s2s']) {
return null
}
let uuid: string
if (settings['prosody-room-type'] === 'channel') {
uuid = 'channel.' + video.channel.id.toString()
} else {
uuid = video.uuid.toString()
}
const hostname = window.location.hostname
2023-04-14 09:57:01 +00:00
const jid = uuid + '@room.' + hostname
return {
jid,
uri: 'xmpp:' + jid + '?join'
}
}
export type {
UriOptions
}
2021-12-14 14:54:36 +00:00
export {
getIframeUri,
2023-04-14 09:57:01 +00:00
getXMPPAddr
2021-12-14 14:54:36 +00:00
}