2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-03-27 15:26:32 +00:00
|
|
|
import { randomNick, getPreviousAnonymousNick, setPreviousAnonymousNick } from '../nick'
|
|
|
|
|
|
|
|
export const livechatViewerModePlugin = {
|
|
|
|
dependencies: ['converse-muc', 'converse-muc-views'],
|
|
|
|
initialize: function (this: any) {
|
|
|
|
const _converse = this._converse
|
|
|
|
|
2024-04-03 09:18:00 +00:00
|
|
|
_converse.api.settings.extend({
|
2024-04-08 17:02:56 +00:00
|
|
|
livechat_enable_viewer_mode: false,
|
|
|
|
livechat_peertube_video_original_url: undefined,
|
2024-04-16 09:43:38 +00:00
|
|
|
livechat_peertube_video_uuid: undefined,
|
2024-04-22 12:28:55 +00:00
|
|
|
livechat_external_auth_oidc_buttons: undefined,
|
2024-04-18 08:58:08 +00:00
|
|
|
livechat_external_auth_reconnect_mode: undefined
|
2024-04-03 09:18:00 +00:00
|
|
|
})
|
2024-03-27 15:26:32 +00:00
|
|
|
|
2024-04-03 09:18:00 +00:00
|
|
|
const originalGetDefaultMUCNickname = _converse.getDefaultMUCNickname
|
|
|
|
if (!originalGetDefaultMUCNickname) {
|
2024-03-27 15:26:32 +00:00
|
|
|
console.error('[livechatViewerModePlugin] getDefaultMUCNickname is not initialized.')
|
|
|
|
} else {
|
|
|
|
Object.assign(_converse, {
|
|
|
|
getDefaultMUCNickname: function (this: any): any {
|
2024-04-03 09:18:00 +00:00
|
|
|
if (!_converse.api.settings.get('livechat_enable_viewer_mode')) {
|
|
|
|
return originalGetDefaultMUCNickname.apply(this, arguments)
|
|
|
|
}
|
|
|
|
return originalGetDefaultMUCNickname.apply(this, arguments) ??
|
|
|
|
getPreviousAnonymousNick() ??
|
|
|
|
randomNick('Anonymous')
|
2024-03-27 15:26:32 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-18 09:23:28 +00:00
|
|
|
function refreshViewerMode (canChat: boolean | null): void {
|
2024-03-27 15:26:32 +00:00
|
|
|
console.log('[livechatViewerModePlugin] refreshViewerMode: ' + (canChat ? 'off' : 'on'))
|
2024-04-18 09:23:28 +00:00
|
|
|
if (canChat === null) {
|
|
|
|
document.body.removeAttribute('livechat-viewer-mode')
|
|
|
|
} else if (canChat) {
|
|
|
|
document.body.setAttribute('livechat-viewer-mode', 'off')
|
2024-03-27 15:26:32 +00:00
|
|
|
} else {
|
2024-04-18 09:23:28 +00:00
|
|
|
document.body.setAttribute('livechat-viewer-mode', 'on')
|
2024-03-27 15:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_converse.api.listen.on('livechatViewerModeSetNickname', () => refreshViewerMode(true))
|
|
|
|
|
|
|
|
_converse.ChatRoomOccupants.prototype.on('change:nick', (data: any, nick: string) => {
|
|
|
|
try {
|
2024-04-03 09:18:00 +00:00
|
|
|
if (!_converse.api.settings.get('livechat_enable_viewer_mode')) {
|
2024-04-18 09:23:28 +00:00
|
|
|
refreshViewerMode(null)
|
2024-04-03 09:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-27 15:26:32 +00:00
|
|
|
// 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)
|
2024-04-03 09:18:00 +00:00
|
|
|
refreshViewerMode(!!nick && !/^Anonymous /.test(nick))
|
2024-03-27 15:26:32 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error on nick change handling...', err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_converse.api.listen.on('chatRoomInitialized', function (this: any, model: any): void {
|
2024-04-03 09:18:00 +00:00
|
|
|
if (!_converse.api.settings.get('livechat_enable_viewer_mode')) {
|
2024-04-18 09:23:28 +00:00
|
|
|
refreshViewerMode(null)
|
2024-04-03 09:18:00 +00:00
|
|
|
return
|
|
|
|
}
|
2024-03-27 15:26:32 +00:00
|
|
|
// 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.
|
2024-04-03 09:18:00 +00:00
|
|
|
const nick = getPreviousAnonymousNick() ?? (model?.get ? model.get('nick') : '')
|
2024-03-27 15:26:32 +00:00
|
|
|
refreshViewerMode(nick && !/^Anonymous /.test(nick))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|