From 7d4577efec2e73270fc2e28a81d63112f2f727eb Mon Sep 17 00:00:00 2001 From: John Livingston Date: Wed, 3 Apr 2024 11:18:00 +0200 Subject: [PATCH] Fix ConverseJS plugins + viewer mode refactoring. --- conversejs/builtin.ts | 9 ++---- .../custom/templates/muc-bottom-panel.js | 6 ++-- conversejs/lib/plugins/livechat-specific.ts | 12 ++++---- .../lib/plugins/livechat-viewer-mode.ts | 30 ++++++++++++------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/conversejs/builtin.ts b/conversejs/builtin.ts index 0a9dd8c2..6797500b 100644 --- a/conversejs/builtin.ts +++ b/conversejs/builtin.ts @@ -49,7 +49,6 @@ function initConversePlugins (peertubeEmbedded: boolean): void { converse.plugins.add('livechatSpecifics', livechatSpecificsPlugin) // Viewer mode (anonymous accounts, before they have chosen their nickname). - // This plugin will be blacklisted in initConverse if not necessary. converse.plugins.add('livechatViewerModePlugin', livechatViewerModePlugin) } window.initConversePlugins = initConversePlugins @@ -133,12 +132,10 @@ async function initConverse ( // params.muc_show_logs_before_join = true => displays muc history on top of nickname form. But it's not updated. } - try { - if (!(autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet)) { - params.blacklisted_plugins ??= [] - params.blacklisted_plugins.push('livechatViewerModePlugin') - } + // no viewer mode if authenticated. + params.livechat_enable_viewer_mode = autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet + try { if (window.reconnectConverse) { // this is set in the livechatSpecificsPlugin window.reconnectConverse(params) } else { diff --git a/conversejs/custom/templates/muc-bottom-panel.js b/conversejs/custom/templates/muc-bottom-panel.js index 2723b663..37beeb84 100644 --- a/conversejs/custom/templates/muc-bottom-panel.js +++ b/conversejs/custom/templates/muc-bottom-panel.js @@ -77,16 +77,16 @@ const tplSlowMode = (o) => { } export default (o) => { - if (api.settings.get('livechat_viewer_mode')) { + if (api.settings.get('livechat_enable_viewer_mode')) { const model = o.model const i18nNickname = __('Nickname') const i18nJoin = __('Enter groupchat') - const i18n_heading = __('Choose a nickname to enter') + const i18nHeading = __('Choose a nickname to enter') return html`
setNickname(ev, model)}>
- +
refreshViewerMode(true)) _converse.ChatRoomOccupants.prototype.on('change:nick', (data: any, nick: string) => { try { + if (!_converse.api.settings.get('livechat_enable_viewer_mode')) { + return + } // 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) + refreshViewerMode(!!nick && !/^Anonymous /.test(nick)) } } catch (err) { console.error('Error on nick change handling...', err) @@ -48,11 +53,14 @@ export const livechatViewerModePlugin = { }) _converse.api.listen.on('chatRoomInitialized', function (this: any, model: any): void { + if (!_converse.api.settings.get('livechat_enable_viewer_mode')) { + return + } // 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') : '') + const nick = getPreviousAnonymousNick() ?? (model?.get ? model.get('nick') : '') refreshViewerMode(nick && !/^Anonymous /.test(nick)) }) }