2023-08-01 15:01:09 +00:00
|
|
|
import type { InitConverseJSParams } 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'
|
|
|
|
import { getLocalAuthentInfos } from './lib/auth'
|
2024-01-09 11:54:30 +00:00
|
|
|
import { randomNick, getPreviousAnonymousNick, setPreviousAnonymousNick } from './lib/nick'
|
2023-05-04 17:14:23 +00:00
|
|
|
|
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
converse: {
|
|
|
|
initialize: (args: any) => void
|
|
|
|
plugins: {
|
|
|
|
add: (name: string, plugin: any) => void
|
|
|
|
}
|
2021-05-04 11:00:44 +00:00
|
|
|
}
|
2023-08-01 15:01:09 +00:00
|
|
|
initConverse: (args: InitConverseJSParams) => Promise<void>
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 15:01:09 +00:00
|
|
|
window.initConverse = async function initConverse (initConverseParams: InitConverseJSParams): 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)
|
|
|
|
const params = defaultConverseParams(initConverseParams, isInIframe)
|
2021-05-05 16:16:59 +00:00
|
|
|
|
2021-05-05 16:35:28 +00:00
|
|
|
let isAuthenticated: boolean = false
|
2023-04-21 14:56:48 +00:00
|
|
|
let isRemoteWithNicknameSet: boolean = false
|
2023-01-11 17:05:18 +00:00
|
|
|
|
2023-05-04 17:14:23 +00:00
|
|
|
const auth = await getLocalAuthentInfos(authenticationUrl)
|
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
|
|
|
|
} else if (remoteAuthenticatedXMPPServer) {
|
|
|
|
remoteRoomAuthenticatedParams(initConverseParams, auth, params)
|
2023-04-21 14:56:48 +00:00
|
|
|
isAuthenticated = true
|
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 {
|
|
|
|
throw new Error('Remote server does not allow remote connection')
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!isRemoteChat) {
|
|
|
|
localRoomAnonymousParams(initConverseParams, params)
|
|
|
|
} else if (remoteAnonymousXMPPServer) {
|
|
|
|
remoteRoomAnonymousParams(initConverseParams, null, params)
|
|
|
|
} else {
|
|
|
|
throw new Error('Remote server does not allow remote connection')
|
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
|
|
|
}
|
|
|
|
|
2021-05-18 16:48:19 +00:00
|
|
|
try {
|
2023-01-12 10:46:02 +00:00
|
|
|
converse.plugins.add('livechatWindowTitlePlugin', {
|
2021-11-26 16:33:55 +00:00
|
|
|
dependencies: ['converse-muc-views'],
|
|
|
|
overrides: {
|
|
|
|
ChatRoomView: {
|
2022-01-04 03:23:13 +00:00
|
|
|
requestUpdate: function (this: any): any {
|
2021-11-26 16:33:55 +00:00
|
|
|
console.log('[livechatWindowTitlePlugin] updating the document title.')
|
|
|
|
try {
|
2022-01-04 16:49:41 +00:00
|
|
|
if (this.model?.getDisplayName) {
|
|
|
|
const title = this.model.getDisplayName()
|
|
|
|
if (document.title !== title) {
|
|
|
|
document.title = title
|
|
|
|
}
|
2021-11-26 16:33:55 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-01-04 03:23:13 +00:00
|
|
|
console.error('[livechatWindowTitlePlugin] Failed updating the window title', err)
|
2021-11-26 16:33:55 +00:00
|
|
|
}
|
2022-01-04 03:23:13 +00:00
|
|
|
return this.__super__.requestUpdate.apply(this)
|
2021-11-26 16:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-01-07 18:20:28 +00:00
|
|
|
|
2023-04-21 14:56:48 +00:00
|
|
|
if (autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet) {
|
2024-01-09 11:54:30 +00:00
|
|
|
const previousNickname = getPreviousAnonymousNick()
|
2023-01-12 10:46:02 +00:00
|
|
|
converse.plugins.add('livechatViewerModePlugin', {
|
2022-01-07 18:20:28 +00:00
|
|
|
dependencies: ['converse-muc', 'converse-muc-views'],
|
|
|
|
initialize: function () {
|
|
|
|
const _converse = this._converse
|
|
|
|
const getDefaultMUCNickname = _converse.getDefaultMUCNickname
|
|
|
|
if (!getDefaultMUCNickname) {
|
|
|
|
console.error('[livechatViewerModePlugin] getDefaultMUCNickname is not initialized.')
|
|
|
|
} else {
|
|
|
|
Object.assign(_converse, {
|
|
|
|
getDefaultMUCNickname: function (this: any): any {
|
2024-01-09 11:54:30 +00:00
|
|
|
return getDefaultMUCNickname.apply(this, arguments) ?? previousNickname ?? randomNick('Anonymous')
|
2022-01-07 18:20:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function refreshViewerMode (canChat: boolean): void {
|
|
|
|
console.log('[livechatViewerModePlugin] refreshViewerMode: ' + (canChat ? 'off' : 'on'))
|
|
|
|
if (canChat) {
|
2023-05-04 17:14:23 +00:00
|
|
|
document.querySelector('body')?.setAttribute('livechat-viewer-mode', 'off')
|
2022-01-07 18:20:28 +00:00
|
|
|
} else {
|
2023-05-04 17:14:23 +00:00
|
|
|
document.querySelector('body')?.setAttribute('livechat-viewer-mode', 'on')
|
2022-01-07 18:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 11:54:30 +00:00
|
|
|
if (previousNickname === null) {
|
|
|
|
_converse.api.settings.update({
|
|
|
|
livechat_viewer_mode: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-07 18:20:28 +00:00
|
|
|
_converse.api.listen.on('livechatViewerModeSetNickname', () => refreshViewerMode(true))
|
2024-01-09 11:54:30 +00:00
|
|
|
|
|
|
|
_converse.ChatRoomOccupants.prototype.on('change:nick', (data: any, nick: string) => {
|
|
|
|
try {
|
|
|
|
// On nick change, if the user is_me, storing the new nickname
|
|
|
|
if (nick && data?.attributes?.is_me === true) {
|
|
|
|
console.log('Nickname change, storing to previousAnonymousNick')
|
|
|
|
setPreviousAnonymousNick(nick)
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error on nick change handling...', err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-01-07 18:20:28 +00:00
|
|
|
_converse.api.listen.on('chatRoomInitialized', function (this: any, model: any): void {
|
2024-01-09 11:54:30 +00:00
|
|
|
// When room is initialized, if user has chosen a nickname, set viewermode to off.
|
|
|
|
// Note: when previousNickname is set, model.get('nick') has not the nick yet...
|
|
|
|
// It will only come after receiving a presence stanza.
|
|
|
|
// So we use previousNickname before trying to read the model.
|
|
|
|
const nick = previousNickname ?? (model?.get ? model.get('nick') : '')
|
2022-01-07 18:20:28 +00:00
|
|
|
refreshViewerMode(nick && !/^Anonymous /.test(nick))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:05:09 +00:00
|
|
|
// The following code does not work. Just keeping it in case we want to investigate.
|
|
|
|
// if (authenticationUrl !== '') {
|
|
|
|
// // We are in builtin-prosody mode. I'll try to disconnect from the room
|
|
|
|
// // on page unload. This is to avoid some bugs:
|
|
|
|
// // - users are not show as disconnected until a long timeout
|
|
|
|
// // - anonymous users' nicknames are not available before this timeout
|
|
|
|
// // - logged in users sometimes can't switch between iframe and fullscreen more than 1 time
|
2023-01-12 10:46:02 +00:00
|
|
|
// converse.plugins.add('livechatDisconnectOnUnloadPlugin', {
|
2022-01-16 19:05:09 +00:00
|
|
|
// initialize: function () {
|
|
|
|
// const _converse = this._converse
|
|
|
|
// const { unloadevent } = _converse
|
|
|
|
// // eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
// window.addEventListener(unloadevent, async () => {
|
|
|
|
// console.log('[livechatDisconnectOnUnloadPlugin] Disconnecting...')
|
|
|
|
// await _converse.api.user.logout()
|
|
|
|
// }, { once: true, passive: true })
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
2023-01-12 10:46:02 +00:00
|
|
|
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
|
|
|
}
|