Fix ConverseJS plugins + viewer mode refactoring.

This commit is contained in:
John Livingston 2024-04-03 11:18:00 +02:00
parent 610040b253
commit 7d4577efec
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
4 changed files with 31 additions and 26 deletions

View File

@ -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 {

View File

@ -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`
<div class="livechat-viewer-mode-nick chatroom-form-container"
@submit=${ev => setNickname(ev, model)}>
<form class="converse-form chatroom-form">
<label>${i18n_heading}</label>
<label>${i18nHeading}</label>
<fieldset class="form-group">
<input type="text"
required="required"

View File

@ -7,12 +7,13 @@ export const livechatSpecificsPlugin = {
document.getElementById('livechat-loading-spinner')?.remove()
})
// Adding a methode on window.converse, so we can close the chat on navigation-end event
// Adding a method on window.converse, so we can close the chat on navigation-end event
// (when chatIncludeMode is peertube-*)
window.converse.livechatDisconnect = function livechatDisconnect () {
console.log('[livechatSpecificsPlugin] disconnecting converseJS...')
_converse.api.user.logout()
window.converse.livechatDisconnect = undefined // will be set again on next initialize.
if (_converse.api.connection.connected()) {
console.log('[livechatSpecificsPlugin] disconnecting converseJS...')
_converse.api.user.logout()
}
}
// To reconnect ConverseJS when joining another room (or the same one),
@ -33,8 +34,7 @@ export const livechatSpecificsPlugin = {
}
// update other settings
params.blacklisted_plugins ??= []
for (const k of ['hide_muc_participants', 'blacklisted_plugins']) {
for (const k of ['hide_muc_participants', 'livechat_enable_viewer_mode']) {
_converse.api.settings.set(k, params[k])
}

View File

@ -5,15 +5,22 @@ export const livechatViewerModePlugin = {
initialize: function (this: any) {
const _converse = this._converse
const previousNickname = getPreviousAnonymousNick()
_converse.api.settings.extend({
livechat_enable_viewer_mode: false
})
const getDefaultMUCNickname = _converse.getDefaultMUCNickname
if (!getDefaultMUCNickname) {
const originalGetDefaultMUCNickname = _converse.getDefaultMUCNickname
if (!originalGetDefaultMUCNickname) {
console.error('[livechatViewerModePlugin] getDefaultMUCNickname is not initialized.')
} else {
Object.assign(_converse, {
getDefaultMUCNickname: function (this: any): any {
return getDefaultMUCNickname.apply(this, arguments) ?? previousNickname ?? randomNick('Anonymous')
if (!_converse.api.settings.get('livechat_enable_viewer_mode')) {
return originalGetDefaultMUCNickname.apply(this, arguments)
}
return originalGetDefaultMUCNickname.apply(this, arguments) ??
getPreviousAnonymousNick() ??
randomNick('Anonymous')
}
})
}
@ -27,20 +34,18 @@ export const livechatViewerModePlugin = {
}
}
if (previousNickname === null) {
_converse.api.settings.update({
livechat_viewer_mode: true
})
}
_converse.api.listen.on('livechatViewerModeSetNickname', () => 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))
})
}