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
|
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
import type { RegisterClientOptions } from '@peertube/peertube-types/client'
|
|
|
|
import type { InitConverseJSParams, ChatPeertubeIncludeMode } from 'shared/lib/types'
|
2024-01-08 10:56:44 +00:00
|
|
|
import { computeAutoColors } from './colors'
|
2024-03-28 14:06:15 +00:00
|
|
|
import { getBaseRoute } from './uri'
|
2024-06-14 13:17:14 +00:00
|
|
|
import { logger } from './logger'
|
2023-12-27 11:48:45 +00:00
|
|
|
|
2024-04-03 14:00:28 +00:00
|
|
|
// FIXME: better declaration (see builtin.ts)
|
2023-12-27 11:48:45 +00:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
converse?: any
|
|
|
|
initConverse: Function
|
2024-03-27 15:26:32 +00:00
|
|
|
initConversePlugins: Function
|
2023-12-27 11:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-05 14:34:51 +00:00
|
|
|
let pollListenerInitiliazed: boolean = false
|
|
|
|
|
2024-01-08 10:56:44 +00:00
|
|
|
/**
|
|
|
|
* load the ConverseJS CSS.
|
|
|
|
* @param url CSS url
|
|
|
|
*/
|
2023-12-27 11:48:45 +00:00
|
|
|
async function loadCSS (url: string): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const css = document.createElement('link')
|
|
|
|
css.onerror = () => reject(new URIError(`CSS ${url} didn't load correctly.`))
|
|
|
|
css.onload = () => resolve()
|
|
|
|
css.setAttribute('type', 'text/css')
|
|
|
|
css.setAttribute('rel', 'stylesheet')
|
|
|
|
css.setAttribute('href', url)
|
|
|
|
document.head.appendChild(css)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-08 10:56:44 +00:00
|
|
|
/**
|
|
|
|
* Loads a JS script.
|
|
|
|
* @param url script url
|
|
|
|
*/
|
2023-12-27 11:48:45 +00:00
|
|
|
async function loadScript (url: string): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const script = document.createElement('script')
|
|
|
|
script.onerror = () => reject(new URIError(`Script ${url} didn't load correctly.`))
|
|
|
|
script.onload = () => resolve()
|
|
|
|
script.async = true
|
|
|
|
script.src = url
|
|
|
|
document.head.appendChild(script)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-08 10:56:44 +00:00
|
|
|
/**
|
|
|
|
* Initialize needed CSS vars to apply the current Peertube theme to the livechat.
|
|
|
|
*/
|
|
|
|
function loadColors (): void {
|
|
|
|
const colors = computeAutoColors()
|
|
|
|
if (!colors) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const body = document.querySelector('body')
|
|
|
|
if (!body) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body.style.setProperty('--peertube-main-foreground', colors.mainForeground)
|
|
|
|
body.style.setProperty('--peertube-main-background', colors.mainBackground)
|
|
|
|
body.style.setProperty('--peertube-grey-foreground', colors.greyForeground)
|
|
|
|
body.style.setProperty('--peertube-grey-background', colors.greyBackground)
|
|
|
|
body.style.setProperty('--peertube-menu-foreground', colors.menuForeground)
|
|
|
|
body.style.setProperty('--peertube-menu-background', colors.menuBackground)
|
|
|
|
body.style.setProperty('--peertube-input-foreground', colors.inputForeground)
|
|
|
|
body.style.setProperty('--peertube-input-background', colors.inputBackground)
|
|
|
|
body.style.setProperty('--peertube-button-foreground', colors.buttonForeground)
|
|
|
|
body.style.setProperty('--peertube-button-background', colors.buttonBackground)
|
|
|
|
body.style.setProperty('--peertube-link', colors.link)
|
|
|
|
body.style.setProperty('--peertube-link-hover', colors.linkHover)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads ConverseJS.
|
|
|
|
* ConverseJS is loaded asyncrhonously for several reasons:
|
|
|
|
* * to avoid loading big JS files each time you open Peertube
|
|
|
|
* * we need ConverseJS in serveral different scopes
|
|
|
|
* ('common' for the full page and 'videowatch' when you view a video).
|
|
|
|
* So we don't want to bundle ConverseJS with scoped JS files.
|
|
|
|
* * for now, we can't build ConverseJS without webpack
|
|
|
|
* (esbuild does not provide same alias as webpack, to customize ConverseJS).
|
|
|
|
*
|
|
|
|
* Once loadConverseJS has resolved, you can call window.initConverse.
|
|
|
|
* @param converseJSParams Params to apply to ConverseJS
|
|
|
|
*/
|
2023-12-27 11:48:45 +00:00
|
|
|
async function loadConverseJS (converseJSParams: InitConverseJSParams): Promise<void> {
|
2024-01-08 10:56:44 +00:00
|
|
|
// always loading colors, even if already done: so it will update if the current theme is changed.
|
|
|
|
loadColors()
|
|
|
|
|
2023-12-27 11:48:45 +00:00
|
|
|
if (!window.converse) {
|
|
|
|
await Promise.all([
|
|
|
|
loadCSS(converseJSParams.staticBaseUrl + 'conversejs/converse.min.css'),
|
|
|
|
loadScript(converseJSParams.staticBaseUrl + 'conversejs/converse.min.js')
|
|
|
|
])
|
|
|
|
}
|
|
|
|
if (!window.initConverse) {
|
|
|
|
await loadScript(converseJSParams.staticBaseUrl + 'static/builtin.js')
|
2024-03-27 15:26:32 +00:00
|
|
|
window.initConversePlugins(true)
|
2023-12-27 11:48:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
/**
|
|
|
|
* Loads the chat in the given container.
|
|
|
|
* @param clientOptions Peertube client options
|
|
|
|
* @param container the dom element where to insert the chat
|
|
|
|
* @param roomKey the room to join
|
|
|
|
* @param chatIncludeMode the include mode
|
2024-04-03 10:13:44 +00:00
|
|
|
* @param forceType only usable for admins/moderators, to enter rooms that have not the current type (channel/video)
|
2024-03-28 14:06:15 +00:00
|
|
|
*/
|
|
|
|
async function displayConverseJS (
|
|
|
|
clientOptions: RegisterClientOptions,
|
|
|
|
container: HTMLElement,
|
|
|
|
roomKey: string,
|
2024-04-03 10:13:44 +00:00
|
|
|
chatIncludeMode: ChatPeertubeIncludeMode,
|
|
|
|
forceType: boolean
|
2024-03-28 14:06:15 +00:00
|
|
|
): Promise<void> {
|
|
|
|
const peertubeHelpers = clientOptions.peertubeHelpers
|
|
|
|
|
|
|
|
const spinner = document.createElement('div')
|
|
|
|
spinner.classList.add('livechat-spinner')
|
|
|
|
spinner.setAttribute('id', 'livechat-loading-spinner')
|
|
|
|
spinner.innerHTML = '<div></div>'
|
2024-04-02 15:40:43 +00:00
|
|
|
container.append(spinner)
|
2024-03-28 14:06:15 +00:00
|
|
|
// spinner will be removed by a converse plugin
|
|
|
|
|
2024-04-02 15:40:43 +00:00
|
|
|
const converseRoot = document.createElement('converse-root')
|
|
|
|
converseRoot.classList.add('theme-peertube')
|
|
|
|
container.append(converseRoot)
|
|
|
|
|
2024-04-03 13:06:18 +00:00
|
|
|
converseRoot.addEventListener('click', ev => {
|
|
|
|
// For some reason, there are some buttons in ConverseJS that are not working properly.
|
|
|
|
// When clicked, it does not prevent the default, so it try to open href="#".
|
|
|
|
// We will catch such clicks in converse-root, and prevent default!
|
|
|
|
if (!ev.target) { return }
|
|
|
|
|
2024-04-03 14:48:43 +00:00
|
|
|
const a: HTMLAnchorElement | null = ('tagName' in ev.target) && (ev.target as HTMLAnchorElement).tagName === 'A'
|
2024-04-03 13:06:18 +00:00
|
|
|
? ev.target as HTMLAnchorElement
|
|
|
|
: (ev.target as HTMLElement).closest('a')
|
|
|
|
|
|
|
|
if (!a) { return }
|
|
|
|
if (a.getAttribute('href') !== '#') { return }
|
|
|
|
|
2024-06-14 13:17:14 +00:00
|
|
|
logger.log('Intercepting a click on href=# in converse root, canceling the event.')
|
2024-04-03 13:06:18 +00:00
|
|
|
ev.preventDefault()
|
|
|
|
})
|
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
const authHeader = peertubeHelpers.getAuthHeader()
|
|
|
|
|
|
|
|
const response = await fetch(
|
2024-04-03 10:13:44 +00:00
|
|
|
getBaseRoute(clientOptions) + '/api/configuration/room/' +
|
|
|
|
encodeURIComponent(roomKey) +
|
|
|
|
(forceType ? '?forcetype=1' : ''),
|
2024-03-28 14:06:15 +00:00
|
|
|
{
|
|
|
|
method: 'GET',
|
2024-04-17 16:30:39 +00:00
|
|
|
headers: authHeader
|
2024-03-28 14:06:15 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
if (!response.ok) {
|
2024-04-04 08:58:16 +00:00
|
|
|
throw new Error('Can\'t get room configuration.')
|
2024-03-28 14:06:15 +00:00
|
|
|
}
|
|
|
|
const converseJSParams: InitConverseJSParams = await (response).json()
|
|
|
|
|
2024-07-05 14:34:51 +00:00
|
|
|
if (!pollListenerInitiliazed) {
|
|
|
|
// First time we got here, initiliaze this event:
|
|
|
|
const i18nVoteOk = await clientOptions.peertubeHelpers.translate(LOC_POLL_VOTE_OK)
|
|
|
|
pollListenerInitiliazed = true
|
|
|
|
document.addEventListener('livechat-poll-vote', () => {
|
|
|
|
clientOptions.peertubeHelpers.notifier.success(i18nVoteOk)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-28 14:06:15 +00:00
|
|
|
await loadConverseJS(converseJSParams)
|
2024-04-04 15:02:21 +00:00
|
|
|
await window.initConverse(converseJSParams, chatIncludeMode, authHeader ?? null)
|
2024-03-28 14:06:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 11:48:45 +00:00
|
|
|
export {
|
2024-03-28 14:06:15 +00:00
|
|
|
displayConverseJS
|
2023-12-27 11:48:45 +00:00
|
|
|
}
|