diff --git a/client/common/configuration/register.ts b/client/common/configuration/register.ts index 9903da0d..01adfeda 100644 --- a/client/common/configuration/register.ts +++ b/client/common/configuration/register.ts @@ -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) } } diff --git a/conversejs/builtin.ts b/conversejs/builtin.ts index dfdd1474..8d29f54e 100644 --- a/conversejs/builtin.ts +++ b/conversejs/builtin.ts @@ -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) } diff --git a/conversejs/lib/plugins/livechat-specific.ts b/conversejs/lib/plugins/livechat-specific.ts index d6adea6d..9028e2fc 100644 --- a/conversejs/lib/plugins/livechat-specific.ts +++ b/conversejs/lib/plugins/livechat-specific.ts @@ -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