Reconnect livechat in embedded mode.

This commit is contained in:
John Livingston 2024-03-28 12:22:30 +01:00
parent 1ba1e08d69
commit ba52d4e3d8
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
3 changed files with 45 additions and 14 deletions

View File

@ -25,19 +25,6 @@ async function registerConfiguration (clientOptions: RegisterClientOptions): Pro
throw new Error('missing room parameter')
}
const response = await fetch(
getBaseRoute(clientOptions) + '/api/configuration/room/' + encodeURIComponent(roomKey),
{
method: 'GET',
headers: peertubeHelpers.getAuthHeader()
}
)
if (!response.ok) {
throw new Error('Can\'t get channel configuration options.')
}
const converseJSParams: InitConverseJSParams = await (response).json()
const container = document.createElement('div')
container.classList.add('livechat-embed-fullpage')
rootEl.append(container)
@ -55,10 +42,23 @@ async function registerConfiguration (clientOptions: RegisterClientOptions): Pro
const authHeader = peertubeHelpers.getAuthHeader()
const response = await fetch(
getBaseRoute(clientOptions) + '/api/configuration/room/' + encodeURIComponent(roomKey),
{
method: 'GET',
headers: peertubeHelpers.getAuthHeader()
}
)
if (!response.ok) {
throw new Error('Can\'t get channel configuration options.')
}
const converseJSParams: InitConverseJSParams = await (response).json()
await loadConverseJS(converseJSParams)
window.initConverse(converseJSParams, 'peertube-fullpage', authHeader ?? null)
} catch (err) {
console.error('[peertube-plugin-livechat] ' + (err as string))
// FIXME: do a better error page.
rootEl.innerText = await peertubeHelpers.translate(LOC_NOT_FOUND)
}
}

View File

@ -26,6 +26,7 @@ declare global {
}
initConversePlugins: typeof initConversePlugins
initConverse: typeof initConverse
reconnectConverse?: (room: string) => void
}
}
@ -142,7 +143,11 @@ async function initConverse (
params.blacklisted_plugins.push('livechatViewerModePlugin')
}
converse.initialize(params)
if (window.reconnectConverse) { // this is set in the livechatSpecificsPlugin
window.reconnectConverse(params)
} else {
converse.initialize(params)
}
} catch (error) {
console.error('Failed initializing converseJS', error)
}

View File

@ -15,6 +15,32 @@ export const livechatSpecificsPlugin = {
window.converse.livechatDisconnect = undefined // will be set again on next initialize.
}
// To reconnect ConverseJS when joining another room (or the same one),
// we store the relevant closure function:
window.reconnectConverse = function reconnectConverse (params: any): void {
console.log('[livechatSpecificsPlugin] reconnecting converseJS...')
// The new room to join:
_converse.api.settings.set('auto_join_rooms', params.auto_join_rooms)
_converse.api.settings.set('notify_all_room_messages', params.notify_all_room_messages)
// update connection parameters (in case the user logged in after the first chat)
for (const k of [
'bosh_service_url', 'websocket_url',
'authentication', 'nickname', 'muc_nickname_from_jid', 'auto_login', 'jid', 'password', 'keepalive'
]) {
_converse.api.settings.set(k, params[k])
}
// update other settings
for (const k of ['hide_muc_participants', 'blacklisted_plugins']) {
_converse.api.settings.set(k, params[k])
}
// Then login.
_converse.api.user.login()
}
if (window.location.protocol === 'http:') {
// We are probably on a dev instance, so we will add _converse in window:
(window as any)._livechatConverse = _converse