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-09-09 18:01:25 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
|
|
|
|
|
2024-04-19 07:53:23 +00:00
|
|
|
import type { InitConverseJSParams, ChatIncludeMode, ExternalAuthResult } from 'shared/lib/types'
|
2023-05-04 17:14:23 +00:00
|
|
|
import { inIframe } from './lib/utils'
|
|
|
|
import { initDom } from './lib/dom'
|
|
|
|
import {
|
|
|
|
defaultConverseParams,
|
|
|
|
localRoomAnonymousParams,
|
|
|
|
localRoomAuthenticatedParams,
|
|
|
|
remoteRoomAnonymousParams,
|
|
|
|
remoteRoomAuthenticatedParams
|
|
|
|
} from './lib/converse-params'
|
2024-06-16 17:48:02 +00:00
|
|
|
import { getLocalAuthentInfos, getLivechatTokenAuthInfos } from './lib/auth'
|
2024-03-27 15:26:32 +00:00
|
|
|
import { randomNick } from './lib/nick'
|
|
|
|
import { slowModePlugin } from './lib/plugins/slow-mode'
|
|
|
|
import { windowTitlePlugin } from './lib/plugins/window-title'
|
|
|
|
import { livechatSpecificsPlugin } from './lib/plugins/livechat-specific'
|
|
|
|
import { livechatViewerModePlugin } from './lib/plugins/livechat-viewer-mode'
|
2024-04-11 09:25:04 +00:00
|
|
|
import { livechatMiniMucHeadPlugin } from './lib/plugins/livechat-mini-muc-head'
|
2024-05-28 15:56:24 +00:00
|
|
|
import { livechatEmojisPlugin } from './lib/plugins/livechat-emojis'
|
2024-07-10 14:54:54 +00:00
|
|
|
import { moderationDelayPlugin } from './lib/plugins/moderation-delay'
|
2024-09-11 17:21:57 +00:00
|
|
|
import { livechatAnnouncementsPlugin } from './lib/plugins/livechat-announcements'
|
2023-05-04 17:14:23 +00:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
converse: {
|
|
|
|
initialize: (args: any) => void
|
|
|
|
plugins: {
|
|
|
|
add: (name: string, plugin: any) => void
|
|
|
|
}
|
2024-05-28 15:56:24 +00:00
|
|
|
emojis: any
|
2024-03-27 15:26:32 +00:00
|
|
|
livechatDisconnect?: Function
|
2024-07-10 14:54:54 +00:00
|
|
|
env: {
|
|
|
|
html: Function
|
|
|
|
sizzle: Function
|
2024-07-26 08:51:55 +00:00
|
|
|
dayjs: Function
|
2024-09-11 17:21:57 +00:00
|
|
|
__: Function
|
2024-07-10 14:54:54 +00:00
|
|
|
}
|
2021-05-04 11:00:44 +00:00
|
|
|
}
|
2024-03-27 15:26:32 +00:00
|
|
|
initConversePlugins: typeof initConversePlugins
|
|
|
|
initConverse: typeof initConverse
|
2024-09-09 18:01:25 +00:00
|
|
|
reconnectConverse?: (params: any) => void
|
2024-04-19 07:53:23 +00:00
|
|
|
externalAuthGetResult?: (data: ExternalAuthResult) => void
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 15:26:32 +00:00
|
|
|
/**
|
|
|
|
* Initilialize ConverseJS plugins.
|
|
|
|
* @param peertubeEmbedded true if we are embedded in Peertube, false if it is the old full page mode.
|
|
|
|
*/
|
|
|
|
function initConversePlugins (peertubeEmbedded: boolean): void {
|
|
|
|
const converse = window.converse
|
|
|
|
|
|
|
|
if (!peertubeEmbedded) {
|
|
|
|
// When in full page mode, this plugin ensure the window title is equal to the room title.
|
|
|
|
converse.plugins.add('livechatWindowTitlePlugin', windowTitlePlugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow mode
|
|
|
|
converse.plugins.add('converse-slow-mode', slowModePlugin)
|
|
|
|
|
|
|
|
// livechatSpecifics plugins add some customization for the livechat plugin.
|
|
|
|
converse.plugins.add('livechatSpecifics', livechatSpecificsPlugin)
|
|
|
|
|
2024-05-28 15:56:24 +00:00
|
|
|
// Channels emojis.
|
|
|
|
converse.plugins.add('livechatEmojis', livechatEmojisPlugin)
|
|
|
|
|
2024-04-11 09:25:04 +00:00
|
|
|
if (peertubeEmbedded) {
|
|
|
|
// This plugins handles some buttons that are generated by Peertube, to include them in the MUC menu.
|
|
|
|
converse.plugins.add('livechatMiniMucHeadPlugin', livechatMiniMucHeadPlugin)
|
|
|
|
}
|
|
|
|
|
2024-03-27 15:26:32 +00:00
|
|
|
// Viewer mode (anonymous accounts, before they have chosen their nickname).
|
|
|
|
converse.plugins.add('livechatViewerModePlugin', livechatViewerModePlugin)
|
2024-07-10 14:54:54 +00:00
|
|
|
|
|
|
|
converse.plugins.add('converse-moderation-delay', moderationDelayPlugin)
|
2024-09-11 17:21:57 +00:00
|
|
|
|
|
|
|
converse.plugins.add('livechatAnnouncementsPlugin', livechatAnnouncementsPlugin)
|
2024-03-27 15:26:32 +00:00
|
|
|
}
|
|
|
|
window.initConversePlugins = initConversePlugins
|
|
|
|
|
2024-03-26 14:38:22 +00:00
|
|
|
/**
|
|
|
|
* Init ConverseJS
|
|
|
|
* @param initConverseParams ConverseJS init Params
|
|
|
|
* @param chatIncludeMode How the chat is included in the html page
|
|
|
|
* @param peertubeAuthHeader when embedded in Peertube, we can get the header from peertubeHelpers
|
|
|
|
*/
|
2024-03-27 15:26:32 +00:00
|
|
|
async function initConverse (
|
2023-12-27 14:51:43 +00:00
|
|
|
initConverseParams: InitConverseJSParams,
|
2024-03-26 14:38:22 +00:00
|
|
|
chatIncludeMode: ChatIncludeMode = 'chat-only',
|
2024-09-09 18:01:25 +00:00
|
|
|
peertubeAuthHeader?: Record<string, string> | null
|
2023-12-27 14:51:43 +00:00
|
|
|
): Promise<void> {
|
2023-05-04 17:14:23 +00:00
|
|
|
// First, fixing relative websocket urls.
|
|
|
|
if (initConverseParams.localWebsocketServiceUrl?.startsWith('/')) {
|
|
|
|
initConverseParams.localWebsocketServiceUrl = new URL(
|
|
|
|
initConverseParams.localWebsocketServiceUrl,
|
2022-08-24 15:55:24 +00:00
|
|
|
(window.location.protocol === 'http:' ? 'ws://' : 'wss://') + window.location.host
|
|
|
|
).toString()
|
|
|
|
}
|
|
|
|
|
2023-05-04 17:14:23 +00:00
|
|
|
const {
|
|
|
|
isRemoteChat,
|
|
|
|
remoteAnonymousXMPPServer,
|
|
|
|
remoteAuthenticatedXMPPServer,
|
|
|
|
authenticationUrl,
|
|
|
|
autoViewerMode,
|
|
|
|
forceReadonly
|
|
|
|
} = initConverseParams
|
2021-02-20 19:42:41 +00:00
|
|
|
|
2023-05-04 17:14:23 +00:00
|
|
|
const converse = window.converse
|
2021-05-03 18:37:23 +00:00
|
|
|
|
2023-05-04 17:14:23 +00:00
|
|
|
const isInIframe = inIframe()
|
|
|
|
initDom(initConverseParams, isInIframe)
|
2024-04-03 09:43:36 +00:00
|
|
|
|
|
|
|
// Autofocus: false if besides video, or if an external iframe
|
|
|
|
initConverseParams.autofocus = (chatIncludeMode === 'peertube-fullpage') ||
|
|
|
|
(chatIncludeMode === 'chat-only' && !isInIframe)
|
|
|
|
|
|
|
|
// hide participant if in an external iframe, or besides video.
|
|
|
|
initConverseParams.forceDefaultHideMucParticipants = (isInIframe || chatIncludeMode === 'peertube-video')
|
|
|
|
|
2024-04-02 15:40:43 +00:00
|
|
|
const params = defaultConverseParams(initConverseParams)
|
2024-03-26 14:38:22 +00:00
|
|
|
params.view_mode = chatIncludeMode === 'chat-only' ? 'fullscreen' : 'embedded'
|
|
|
|
params.allow_url_history_change = chatIncludeMode === 'chat-only'
|
2021-05-05 16:16:59 +00:00
|
|
|
|
2024-09-09 18:01:25 +00:00
|
|
|
let isAuthenticated = false
|
|
|
|
let isAuthenticatedWithExternalAccount = false
|
|
|
|
let isRemoteWithNicknameSet = false
|
2023-01-11 17:05:18 +00:00
|
|
|
|
2024-04-17 16:30:39 +00:00
|
|
|
// OIDC (OpenID Connect):
|
2024-04-22 12:28:55 +00:00
|
|
|
const tryOIDC = (initConverseParams.externalAuthOIDC?.length ?? 0) > 0
|
2024-04-17 16:30:39 +00:00
|
|
|
|
2024-06-16 17:48:02 +00:00
|
|
|
let auth
|
2024-06-19 10:49:35 +00:00
|
|
|
let usedLivechatToken = false
|
2024-06-16 17:48:02 +00:00
|
|
|
if (chatIncludeMode === 'chat-only') {
|
|
|
|
// In this mode, we can check if there is a token in the url.
|
|
|
|
auth = getLivechatTokenAuthInfos()
|
2024-06-19 10:49:35 +00:00
|
|
|
if (auth) { usedLivechatToken = true }
|
2024-06-16 17:48:02 +00:00
|
|
|
}
|
|
|
|
auth ??= await getLocalAuthentInfos(authenticationUrl, tryOIDC, peertubeAuthHeader)
|
2024-04-17 16:30:39 +00:00
|
|
|
|
2023-01-11 17:05:18 +00:00
|
|
|
if (auth) {
|
2023-05-04 17:14:23 +00:00
|
|
|
if (!isRemoteChat) {
|
|
|
|
localRoomAuthenticatedParams(initConverseParams, auth, params)
|
|
|
|
isAuthenticated = true
|
2024-04-18 14:24:09 +00:00
|
|
|
isAuthenticatedWithExternalAccount = auth.type !== 'peertube'
|
2023-05-04 17:14:23 +00:00
|
|
|
} else if (remoteAuthenticatedXMPPServer) {
|
|
|
|
remoteRoomAuthenticatedParams(initConverseParams, auth, params)
|
2023-04-21 14:56:48 +00:00
|
|
|
isAuthenticated = true
|
2024-04-18 14:24:09 +00:00
|
|
|
isAuthenticatedWithExternalAccount = auth.type !== 'peertube'
|
2023-05-04 17:14:23 +00:00
|
|
|
} else if (remoteAnonymousXMPPServer) {
|
|
|
|
// remote server does not allow remote authenticated users, falling back to anonymous mode
|
|
|
|
remoteRoomAnonymousParams(initConverseParams, auth, params)
|
|
|
|
isRemoteWithNicknameSet = true
|
|
|
|
} else {
|
2024-04-04 14:48:19 +00:00
|
|
|
console.error('Remote server does not allow remote connection')
|
|
|
|
params.jid = null
|
2023-05-04 17:14:23 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!isRemoteChat) {
|
|
|
|
localRoomAnonymousParams(initConverseParams, params)
|
|
|
|
} else if (remoteAnonymousXMPPServer) {
|
|
|
|
remoteRoomAnonymousParams(initConverseParams, null, params)
|
|
|
|
} else {
|
2024-04-04 14:48:19 +00:00
|
|
|
console.error('Remote server does not allow remote connection')
|
|
|
|
params.jid = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (params.jid === null) {
|
|
|
|
if (chatIncludeMode === 'chat-only') {
|
|
|
|
// Special use case: when mode=chat-only, and no params.jid: display an error page.
|
|
|
|
// Note: this can happen if anonymous users are not allowed on the server.
|
|
|
|
console.error('Seems that anonymous users are not allowed on the target server')
|
|
|
|
// FIXME: localize?
|
|
|
|
document.body.innerHTML = '<h1>This chatroom does not exist, or is not accessible to you.</h1>'
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
throw new Error('Can\'t connect, no JID')
|
2021-05-04 11:00:44 +00:00
|
|
|
}
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 16:35:28 +00:00
|
|
|
if (!isAuthenticated) {
|
|
|
|
console.log('User is not authenticated.')
|
2021-12-14 12:02:15 +00:00
|
|
|
if (forceReadonly) {
|
2022-01-10 02:06:16 +00:00
|
|
|
params.nickname = randomNick('Viewer')
|
2021-12-14 12:02:15 +00:00
|
|
|
}
|
2021-05-05 16:35:28 +00:00
|
|
|
// TODO: try to make these params work
|
2021-08-06 15:52:32 +00:00
|
|
|
// params.muc_nickname_from_jid = true => compute the muc nickname from the jid (will be random here)
|
|
|
|
// params.auto_register_muc_nickname = true => maybe not relevant here (dont do what i thought)
|
|
|
|
// params.muc_show_logs_before_join = true => displays muc history on top of nickname form. But it's not updated.
|
2021-05-05 16:35:28 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 09:18:00 +00:00
|
|
|
// no viewer mode if authenticated.
|
|
|
|
params.livechat_enable_viewer_mode = autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet
|
2024-04-17 16:30:39 +00:00
|
|
|
|
2024-04-18 14:24:09 +00:00
|
|
|
params.livechat_specific_external_authent = isAuthenticatedWithExternalAccount
|
2024-06-20 14:46:14 +00:00
|
|
|
params.livechat_specific_is_anonymous = !isAuthenticated
|
2024-04-18 14:24:09 +00:00
|
|
|
|
2024-04-17 16:30:39 +00:00
|
|
|
if (tryOIDC && !isAuthenticated) {
|
2024-04-22 12:28:55 +00:00
|
|
|
params.livechat_external_auth_oidc_buttons = initConverseParams.externalAuthOIDC
|
2024-04-18 14:24:09 +00:00
|
|
|
}
|
2024-04-18 08:58:08 +00:00
|
|
|
|
2024-04-18 14:24:09 +00:00
|
|
|
if (tryOIDC) { // also needed when authenticated (for the signout button)
|
2024-04-18 08:58:08 +00:00
|
|
|
switch (chatIncludeMode) {
|
|
|
|
case 'peertube-video':
|
|
|
|
params.livechat_external_auth_reconnect_mode = 'button-close-open'
|
|
|
|
break
|
|
|
|
case 'peertube-fullpage':
|
|
|
|
case 'chat-only':
|
|
|
|
default:
|
|
|
|
params.livechat_external_auth_reconnect_mode = 'reload'
|
|
|
|
}
|
2024-04-17 16:30:39 +00:00
|
|
|
}
|
2022-01-07 18:20:28 +00:00
|
|
|
|
2024-04-11 09:25:04 +00:00
|
|
|
if (chatIncludeMode === 'peertube-video') {
|
|
|
|
params.livechat_mini_muc_head = true // we must replace the muc-head by the custom buttons toolbar.
|
|
|
|
}
|
|
|
|
|
2024-06-19 10:49:35 +00:00
|
|
|
// We enable task list only if we are in the peertube interface, or if:
|
|
|
|
// * mode === chat-only + !transparent + !readonly + is using a livechat token
|
|
|
|
// Technically it would work in 'chat-only' mode, but i don't want to add too many things to test
|
|
|
|
// (and i now there is some CSS bugs in the task list).
|
2024-07-29 16:58:02 +00:00
|
|
|
// Same for the moderator notes app.
|
2024-08-01 16:58:25 +00:00
|
|
|
let enableApps = false
|
2024-05-11 15:37:20 +00:00
|
|
|
if (chatIncludeMode === 'peertube-video' || chatIncludeMode === 'peertube-fullpage') {
|
2024-08-01 16:58:25 +00:00
|
|
|
enableApps = true
|
2024-06-19 10:49:35 +00:00
|
|
|
} else if (
|
|
|
|
chatIncludeMode === 'chat-only' &&
|
|
|
|
usedLivechatToken &&
|
|
|
|
!initConverseParams.transparent &&
|
|
|
|
!initConverseParams.forceReadonly
|
|
|
|
) {
|
2024-08-01 16:58:25 +00:00
|
|
|
enableApps = true
|
2024-06-19 10:49:35 +00:00
|
|
|
}
|
2024-08-01 16:58:25 +00:00
|
|
|
if (enableApps) {
|
2024-05-12 14:02:13 +00:00
|
|
|
params.livechat_task_app_enabled = true
|
2024-06-19 10:49:35 +00:00
|
|
|
params.livechat_task_app_restore = chatIncludeMode === 'peertube-fullpage' || chatIncludeMode === 'chat-only'
|
2024-07-29 16:58:02 +00:00
|
|
|
params.livechat_note_app_enabled = true
|
|
|
|
params.livechat_note_app_restore = chatIncludeMode === 'peertube-fullpage' || chatIncludeMode === 'chat-only'
|
2024-08-01 16:58:25 +00:00
|
|
|
params.livechat_mam_search_app_enabled = true
|
2024-07-29 16:58:02 +00:00
|
|
|
}
|
2024-05-11 15:37:20 +00:00
|
|
|
|
2024-04-03 09:18:00 +00:00
|
|
|
try {
|
2024-03-28 11:22:30 +00:00
|
|
|
if (window.reconnectConverse) { // this is set in the livechatSpecificsPlugin
|
|
|
|
window.reconnectConverse(params)
|
|
|
|
} else {
|
|
|
|
converse.initialize(params)
|
|
|
|
}
|
2021-05-18 16:48:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed initializing converseJS', error)
|
|
|
|
}
|
2021-02-20 19:42:41 +00:00
|
|
|
}
|
2024-03-27 15:26:32 +00:00
|
|
|
window.initConverse = initConverse
|