Fix ConverseJS plugins + viewer mode refactoring.
This commit is contained in:
parent
610040b253
commit
7d4577efec
@ -49,7 +49,6 @@ function initConversePlugins (peertubeEmbedded: boolean): void {
|
|||||||
converse.plugins.add('livechatSpecifics', livechatSpecificsPlugin)
|
converse.plugins.add('livechatSpecifics', livechatSpecificsPlugin)
|
||||||
|
|
||||||
// Viewer mode (anonymous accounts, before they have chosen their nickname).
|
// 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)
|
converse.plugins.add('livechatViewerModePlugin', livechatViewerModePlugin)
|
||||||
}
|
}
|
||||||
window.initConversePlugins = initConversePlugins
|
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.
|
// params.muc_show_logs_before_join = true => displays muc history on top of nickname form. But it's not updated.
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// no viewer mode if authenticated.
|
||||||
if (!(autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet)) {
|
params.livechat_enable_viewer_mode = autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet
|
||||||
params.blacklisted_plugins ??= []
|
|
||||||
params.blacklisted_plugins.push('livechatViewerModePlugin')
|
|
||||||
}
|
|
||||||
|
|
||||||
|
try {
|
||||||
if (window.reconnectConverse) { // this is set in the livechatSpecificsPlugin
|
if (window.reconnectConverse) { // this is set in the livechatSpecificsPlugin
|
||||||
window.reconnectConverse(params)
|
window.reconnectConverse(params)
|
||||||
} else {
|
} else {
|
||||||
|
@ -77,16 +77,16 @@ const tplSlowMode = (o) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default (o) => {
|
export default (o) => {
|
||||||
if (api.settings.get('livechat_viewer_mode')) {
|
if (api.settings.get('livechat_enable_viewer_mode')) {
|
||||||
const model = o.model
|
const model = o.model
|
||||||
const i18nNickname = __('Nickname')
|
const i18nNickname = __('Nickname')
|
||||||
const i18nJoin = __('Enter groupchat')
|
const i18nJoin = __('Enter groupchat')
|
||||||
const i18n_heading = __('Choose a nickname to enter')
|
const i18nHeading = __('Choose a nickname to enter')
|
||||||
return html`
|
return html`
|
||||||
<div class="livechat-viewer-mode-nick chatroom-form-container"
|
<div class="livechat-viewer-mode-nick chatroom-form-container"
|
||||||
@submit=${ev => setNickname(ev, model)}>
|
@submit=${ev => setNickname(ev, model)}>
|
||||||
<form class="converse-form chatroom-form">
|
<form class="converse-form chatroom-form">
|
||||||
<label>${i18n_heading}</label>
|
<label>${i18nHeading}</label>
|
||||||
<fieldset class="form-group">
|
<fieldset class="form-group">
|
||||||
<input type="text"
|
<input type="text"
|
||||||
required="required"
|
required="required"
|
||||||
|
@ -7,12 +7,13 @@ export const livechatSpecificsPlugin = {
|
|||||||
document.getElementById('livechat-loading-spinner')?.remove()
|
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-*)
|
// (when chatIncludeMode is peertube-*)
|
||||||
window.converse.livechatDisconnect = function livechatDisconnect () {
|
window.converse.livechatDisconnect = function livechatDisconnect () {
|
||||||
|
if (_converse.api.connection.connected()) {
|
||||||
console.log('[livechatSpecificsPlugin] disconnecting converseJS...')
|
console.log('[livechatSpecificsPlugin] disconnecting converseJS...')
|
||||||
_converse.api.user.logout()
|
_converse.api.user.logout()
|
||||||
window.converse.livechatDisconnect = undefined // will be set again on next initialize.
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// To reconnect ConverseJS when joining another room (or the same one),
|
// To reconnect ConverseJS when joining another room (or the same one),
|
||||||
@ -33,8 +34,7 @@ export const livechatSpecificsPlugin = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// update other settings
|
// update other settings
|
||||||
params.blacklisted_plugins ??= []
|
for (const k of ['hide_muc_participants', 'livechat_enable_viewer_mode']) {
|
||||||
for (const k of ['hide_muc_participants', 'blacklisted_plugins']) {
|
|
||||||
_converse.api.settings.set(k, params[k])
|
_converse.api.settings.set(k, params[k])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,15 +5,22 @@ export const livechatViewerModePlugin = {
|
|||||||
initialize: function (this: any) {
|
initialize: function (this: any) {
|
||||||
const _converse = this._converse
|
const _converse = this._converse
|
||||||
|
|
||||||
const previousNickname = getPreviousAnonymousNick()
|
_converse.api.settings.extend({
|
||||||
|
livechat_enable_viewer_mode: false
|
||||||
|
})
|
||||||
|
|
||||||
const getDefaultMUCNickname = _converse.getDefaultMUCNickname
|
const originalGetDefaultMUCNickname = _converse.getDefaultMUCNickname
|
||||||
if (!getDefaultMUCNickname) {
|
if (!originalGetDefaultMUCNickname) {
|
||||||
console.error('[livechatViewerModePlugin] getDefaultMUCNickname is not initialized.')
|
console.error('[livechatViewerModePlugin] getDefaultMUCNickname is not initialized.')
|
||||||
} else {
|
} else {
|
||||||
Object.assign(_converse, {
|
Object.assign(_converse, {
|
||||||
getDefaultMUCNickname: function (this: any): any {
|
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.api.listen.on('livechatViewerModeSetNickname', () => refreshViewerMode(true))
|
||||||
|
|
||||||
_converse.ChatRoomOccupants.prototype.on('change:nick', (data: any, nick: string) => {
|
_converse.ChatRoomOccupants.prototype.on('change:nick', (data: any, nick: string) => {
|
||||||
try {
|
try {
|
||||||
|
if (!_converse.api.settings.get('livechat_enable_viewer_mode')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
// On nick change, if the user is_me, storing the new nickname
|
// On nick change, if the user is_me, storing the new nickname
|
||||||
if (nick && data?.attributes?.is_me === true) {
|
if (nick && data?.attributes?.is_me === true) {
|
||||||
console.log('Nickname change, storing to previousAnonymousNick')
|
console.log('Nickname change, storing to previousAnonymousNick')
|
||||||
setPreviousAnonymousNick(nick)
|
setPreviousAnonymousNick(nick)
|
||||||
|
refreshViewerMode(!!nick && !/^Anonymous /.test(nick))
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error on nick change handling...', 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 {
|
_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.
|
// 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...
|
// Note: when previousNickname is set, model.get('nick') has not the nick yet...
|
||||||
// It will only come after receiving a presence stanza.
|
// It will only come after receiving a presence stanza.
|
||||||
// So we use previousNickname before trying to read the model.
|
// 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))
|
refreshViewerMode(nick && !/^Anonymous /.test(nick))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user