2021-05-03 16:30:02 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
interface Window {
|
|
|
|
converse: {
|
|
|
|
initialize: (args: any) => void
|
2021-11-26 16:33:55 +00:00
|
|
|
plugins: {
|
|
|
|
add: (name: string, plugin: any) => void
|
|
|
|
}
|
2021-05-03 16:30:02 +00:00
|
|
|
}
|
|
|
|
initConverse: (args: any) => void
|
|
|
|
}
|
|
|
|
|
|
|
|
function inIframe (): boolean {
|
2021-02-20 22:29:07 +00:00
|
|
|
try {
|
|
|
|
return window.self !== window.top
|
|
|
|
} catch (e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 11:00:44 +00:00
|
|
|
interface AuthentInfos {
|
|
|
|
jid: string
|
|
|
|
password: string
|
2021-05-04 14:33:32 +00:00
|
|
|
nickname?: string
|
2021-05-04 11:00:44 +00:00
|
|
|
}
|
|
|
|
async function authenticatedMode (authenticationUrl: string): Promise<false | AuthentInfos> {
|
|
|
|
try {
|
|
|
|
if (!window.fetch) {
|
|
|
|
console.error('Your browser has not the fetch api, we cant log you in')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (!window.localStorage) {
|
|
|
|
// FIXME: is the Peertube token always in localStorage?
|
|
|
|
console.error('Your browser has no localStorage, we cant log you in')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const tokenType = window.localStorage.getItem('token_type') ?? ''
|
|
|
|
const accessToken = window.localStorage.getItem('access_token') ?? ''
|
|
|
|
const refreshToken = window.localStorage.getItem('refresh_token') ?? ''
|
|
|
|
if (tokenType === '' && accessToken === '' && refreshToken === '') {
|
|
|
|
console.info('User seems not to be logged in.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
const response = await window.fetch(authenticationUrl, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: new Headers({
|
|
|
|
Authorization: tokenType + ' ' + accessToken,
|
|
|
|
'content-type': 'application/json;charset=UTF-8'
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
console.error('Failed fetching user informations')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const data = await response.json()
|
|
|
|
if ((typeof data) !== 'object') {
|
|
|
|
console.error('Failed reading user informations')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!data.jid || !data.password) {
|
|
|
|
console.error('User informations does not contain required fields')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
jid: data.jid,
|
2021-05-04 14:33:32 +00:00
|
|
|
password: data.password,
|
|
|
|
nickname: data.nickname
|
2021-05-04 11:00:44 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
2021-05-03 18:37:23 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 02:06:16 +00:00
|
|
|
function randomNick (base: string): string {
|
|
|
|
// using a 6 digit random number to generate a nickname with low colision risk
|
|
|
|
const n = 100000 + Math.floor(Math.random() * 900000)
|
|
|
|
return base + ' ' + n.toString()
|
|
|
|
}
|
|
|
|
|
2021-05-03 18:37:23 +00:00
|
|
|
interface InitConverseParams {
|
|
|
|
jid: string
|
2023-04-21 14:56:48 +00:00
|
|
|
remoteAnonymousXMPPServer: boolean
|
2021-05-03 18:37:23 +00:00
|
|
|
assetsPath: string
|
|
|
|
room: string
|
|
|
|
boshServiceUrl: string
|
|
|
|
websocketServiceUrl: string
|
2021-05-04 11:00:44 +00:00
|
|
|
authenticationUrl: string
|
2022-01-07 18:20:28 +00:00
|
|
|
autoViewerMode: boolean
|
2022-01-04 16:42:03 +00:00
|
|
|
forceReadonly: boolean | 'noscroll'
|
|
|
|
noScroll: boolean
|
2021-11-18 10:08:12 +00:00
|
|
|
theme: string
|
2022-01-16 16:50:11 +00:00
|
|
|
transparent: boolean
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
2021-05-04 11:00:44 +00:00
|
|
|
window.initConverse = async function initConverse ({
|
2021-02-20 22:12:12 +00:00
|
|
|
jid,
|
2023-04-21 14:56:48 +00:00
|
|
|
remoteAnonymousXMPPServer,
|
2021-02-20 22:12:12 +00:00
|
|
|
assetsPath,
|
2021-02-20 19:42:41 +00:00
|
|
|
room,
|
2021-02-20 22:12:12 +00:00
|
|
|
boshServiceUrl,
|
2021-05-03 18:37:23 +00:00
|
|
|
websocketServiceUrl,
|
2021-05-05 15:20:06 +00:00
|
|
|
authenticationUrl,
|
2022-01-07 18:20:28 +00:00
|
|
|
autoViewerMode,
|
2021-12-14 12:02:15 +00:00
|
|
|
forceReadonly,
|
2022-01-16 16:50:11 +00:00
|
|
|
theme,
|
|
|
|
transparent
|
2021-05-03 18:37:23 +00:00
|
|
|
}: InitConverseParams) {
|
2021-05-05 15:20:06 +00:00
|
|
|
const isInIframe = inIframe()
|
2023-01-12 10:46:02 +00:00
|
|
|
const converse = window.converse
|
2021-05-05 15:32:50 +00:00
|
|
|
|
2021-12-14 12:02:15 +00:00
|
|
|
const body = document.querySelector('body')
|
2021-05-05 15:32:50 +00:00
|
|
|
if (isInIframe) {
|
|
|
|
if (body) {
|
|
|
|
body.classList.add('livechat-iframe')
|
2021-11-23 17:10:01 +00:00
|
|
|
// prevent horizontal scrollbar when in iframe. (don't know why, but does not work if done by CSS)
|
|
|
|
body.style.overflowX = 'hidden'
|
2021-05-05 15:32:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-14 12:02:15 +00:00
|
|
|
if (forceReadonly) {
|
|
|
|
body?.classList.add('livechat-readonly')
|
2022-01-04 16:42:03 +00:00
|
|
|
if (forceReadonly === 'noscroll') {
|
|
|
|
body?.classList.add('livechat-noscroll')
|
|
|
|
}
|
2021-12-14 12:02:15 +00:00
|
|
|
}
|
2022-01-16 16:50:11 +00:00
|
|
|
if (transparent) {
|
|
|
|
body?.classList.add('livechat-transparent')
|
|
|
|
}
|
2021-05-05 15:32:50 +00:00
|
|
|
|
2022-08-24 15:55:24 +00:00
|
|
|
if (websocketServiceUrl?.startsWith('/')) {
|
|
|
|
websocketServiceUrl = new URL(
|
|
|
|
websocketServiceUrl,
|
|
|
|
(window.location.protocol === 'http:' ? 'ws://' : 'wss://') + window.location.host
|
|
|
|
).toString()
|
|
|
|
}
|
|
|
|
|
2023-01-16 17:04:20 +00:00
|
|
|
const mucShowInfoMessages = forceReadonly
|
|
|
|
? [
|
|
|
|
// in readonly mode, show only following info messages:
|
|
|
|
'301', '307', '321', '322', '332', '333' // disconnected
|
|
|
|
]
|
|
|
|
: [
|
|
|
|
// FIXME: wait for a response here, and rewrite: https://github.com/conversejs/converse.js/issues/3125
|
|
|
|
'100', '102', '103', '172', '173', '174', // visibility_changes
|
|
|
|
'110', // self
|
|
|
|
'104', '201', // non_privacy_changes
|
|
|
|
'170', '171', // muc_logging_changes
|
|
|
|
'210', '303', // nickname_changes
|
|
|
|
'301', '307', '321', '322', '332', '333', // disconnected
|
|
|
|
'owner', 'admin', 'member', 'exadmin', 'exowner', 'exoutcast', 'exmember', // affiliation_changes
|
|
|
|
// 'entered', 'exited', // join_leave_events
|
|
|
|
'op', 'deop', 'voice', 'mute' // role_changes
|
|
|
|
]
|
|
|
|
|
2021-05-03 18:37:23 +00:00
|
|
|
const params: any = {
|
2021-02-20 22:12:12 +00:00
|
|
|
assets_path: assetsPath,
|
2021-02-20 19:42:41 +00:00
|
|
|
|
|
|
|
authentication: 'anonymous',
|
2022-11-01 10:58:02 +00:00
|
|
|
ping_interval: 25, // must be set accordingly to c2s_close_timeout backend websocket settings and nginx timeout
|
2021-02-20 19:42:41 +00:00
|
|
|
auto_login: true,
|
|
|
|
auto_join_rooms: [
|
|
|
|
room
|
|
|
|
],
|
2021-05-18 17:13:47 +00:00
|
|
|
keepalive: true,
|
2021-02-21 15:10:52 +00:00
|
|
|
discover_connection_methods: false, // this parameter seems buggy with converseJS 7.0.4
|
2021-02-20 22:12:12 +00:00
|
|
|
bosh_service_url: boshServiceUrl === '' ? undefined : boshServiceUrl,
|
|
|
|
websocket_url: websocketServiceUrl === '' ? undefined : websocketServiceUrl,
|
|
|
|
jid: jid,
|
2021-02-20 19:42:41 +00:00
|
|
|
notify_all_room_messages: [
|
|
|
|
room
|
|
|
|
],
|
2023-01-12 10:53:12 +00:00
|
|
|
show_desktop_notifications: false,
|
|
|
|
show_tab_notifications: false,
|
2021-02-20 19:42:41 +00:00
|
|
|
singleton: true,
|
2021-05-05 16:09:33 +00:00
|
|
|
auto_focus: !isInIframe,
|
2021-05-05 15:20:06 +00:00
|
|
|
hide_muc_participants: isInIframe,
|
2021-02-20 19:42:41 +00:00
|
|
|
play_sounds: false,
|
2021-12-14 12:02:15 +00:00
|
|
|
muc_mention_autocomplete_min_chars: 2,
|
2021-02-20 19:42:41 +00:00
|
|
|
muc_mention_autocomplete_filter: 'contains',
|
|
|
|
muc_instant_rooms: true,
|
|
|
|
show_client_info: false,
|
|
|
|
allow_adhoc_commands: false,
|
|
|
|
allow_contact_requests: false,
|
2021-05-03 18:37:23 +00:00
|
|
|
allow_logout: false,
|
2021-02-20 19:42:41 +00:00
|
|
|
show_controlbox_by_default: false,
|
2021-05-03 18:37:23 +00:00
|
|
|
view_mode: 'fullscreen',
|
2021-05-05 16:03:38 +00:00
|
|
|
allow_message_corrections: 'all',
|
2021-05-06 10:34:42 +00:00
|
|
|
allow_message_retraction: 'all',
|
|
|
|
visible_toolbar_buttons: {
|
|
|
|
call: false,
|
2022-01-03 16:20:39 +00:00
|
|
|
spoiler: false,
|
2021-05-06 10:34:42 +00:00
|
|
|
emoji: true,
|
|
|
|
toggle_occupants: true
|
2021-05-18 17:09:29 +00:00
|
|
|
},
|
2021-11-18 10:08:12 +00:00
|
|
|
theme: theme || 'peertube',
|
2022-10-31 17:03:40 +00:00
|
|
|
dark_theme: theme || 'peertube', // dark theme should be the same as theme
|
2021-11-24 14:59:28 +00:00
|
|
|
persistent_store: 'sessionStorage',
|
2021-11-26 16:33:55 +00:00
|
|
|
show_images_inline: false, // for security reason, and to avoid bugs when image is larger that iframe
|
2021-12-06 13:47:34 +00:00
|
|
|
render_media: false, // for security reason, and to avoid bugs when image is larger that iframe
|
2023-01-11 17:05:18 +00:00
|
|
|
whitelisted_plugins: ['livechatWindowTitlePlugin', 'livechatViewerModePlugin', 'livechatDisconnectOnUnloadPlugin'],
|
2023-01-12 10:46:02 +00:00
|
|
|
show_retraction_warning: false, // No need to use this warning (except if we open to external clients?)
|
2023-01-16 17:04:20 +00:00
|
|
|
muc_show_info_messages: mucShowInfoMessages,
|
2023-01-12 10:46:02 +00:00
|
|
|
send_chat_state_notifications: false // don't send this for performance reason
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 16:16:59 +00:00
|
|
|
// TODO: params.clear_messages_on_reconnection = true when muc_mam will be available.
|
|
|
|
|
2021-05-05 16:35:28 +00:00
|
|
|
let isAuthenticated: boolean = false
|
2023-04-21 14:56:48 +00:00
|
|
|
let isRemoteWithNicknameSet: boolean = false
|
2023-01-11 17:05:18 +00:00
|
|
|
if (authenticationUrl === '') {
|
|
|
|
throw new Error('Missing authenticationUrl')
|
|
|
|
}
|
|
|
|
|
|
|
|
// The user will never se the «trusted browser» checkbox (that allows to save credentials).
|
|
|
|
// So we have to disable it
|
|
|
|
// (and ensure clear_cache_on_logout is true,
|
|
|
|
// see https://conversejs.org/docs/html/configuration.html#allow-user-trust-override).
|
|
|
|
params.clear_cache_on_logout = true
|
|
|
|
params.allow_user_trust_override = false
|
|
|
|
|
|
|
|
const auth = await authenticatedMode(authenticationUrl)
|
|
|
|
if (auth) {
|
2023-04-21 14:56:48 +00:00
|
|
|
if (remoteAnonymousXMPPServer) {
|
|
|
|
// Spécial case: anonymous connection to remote XMPP server.
|
|
|
|
if (auth.nickname) {
|
|
|
|
params.nickname = auth.nickname
|
|
|
|
isRemoteWithNicknameSet = true
|
|
|
|
}
|
2023-01-11 17:05:18 +00:00
|
|
|
} else {
|
2023-04-21 14:56:48 +00:00
|
|
|
params.authentication = 'login'
|
|
|
|
params.auto_login = true
|
|
|
|
params.jid = auth.jid
|
|
|
|
params.password = auth.password
|
|
|
|
if (auth.nickname) {
|
|
|
|
params.nickname = auth.nickname
|
|
|
|
} else {
|
|
|
|
params.muc_nickname_from_jid = true
|
|
|
|
}
|
|
|
|
// We dont need the keepalive. And I suppose it is related to some bugs when opening a previous chat window.
|
|
|
|
params.keepalive = false
|
|
|
|
isAuthenticated = true
|
|
|
|
// FIXME: use params.oauth_providers?
|
2021-05-04 11:00:44 +00:00
|
|
|
}
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 16:35:28 +00:00
|
|
|
if (!isAuthenticated) {
|
|
|
|
console.log('User is not authenticated.')
|
2021-12-14 12:02:15 +00:00
|
|
|
if (forceReadonly) {
|
2022-01-10 02:06:16 +00:00
|
|
|
params.nickname = randomNick('Viewer')
|
2021-12-14 12:02:15 +00:00
|
|
|
}
|
2021-05-05 16:35:28 +00:00
|
|
|
// TODO: try to make these params work
|
2021-08-06 15:52:32 +00:00
|
|
|
// params.muc_nickname_from_jid = true => compute the muc nickname from the jid (will be random here)
|
|
|
|
// params.auto_register_muc_nickname = true => maybe not relevant here (dont do what i thought)
|
|
|
|
// params.muc_show_logs_before_join = true => displays muc history on top of nickname form. But it's not updated.
|
2021-05-05 16:35:28 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 16:48:19 +00:00
|
|
|
try {
|
2023-01-12 10:46:02 +00:00
|
|
|
converse.plugins.add('livechatWindowTitlePlugin', {
|
2021-11-26 16:33:55 +00:00
|
|
|
dependencies: ['converse-muc-views'],
|
|
|
|
overrides: {
|
|
|
|
ChatRoomView: {
|
2022-01-04 03:23:13 +00:00
|
|
|
requestUpdate: function (this: any): any {
|
2021-11-26 16:33:55 +00:00
|
|
|
console.log('[livechatWindowTitlePlugin] updating the document title.')
|
|
|
|
try {
|
2022-01-04 16:49:41 +00:00
|
|
|
if (this.model?.getDisplayName) {
|
|
|
|
const title = this.model.getDisplayName()
|
|
|
|
if (document.title !== title) {
|
|
|
|
document.title = title
|
|
|
|
}
|
2021-11-26 16:33:55 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
2022-01-04 03:23:13 +00:00
|
|
|
console.error('[livechatWindowTitlePlugin] Failed updating the window title', err)
|
2021-11-26 16:33:55 +00:00
|
|
|
}
|
2022-01-04 03:23:13 +00:00
|
|
|
return this.__super__.requestUpdate.apply(this)
|
2021-11-26 16:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-01-07 18:20:28 +00:00
|
|
|
|
2023-04-21 14:56:48 +00:00
|
|
|
if (autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet) {
|
2023-01-12 10:46:02 +00:00
|
|
|
converse.plugins.add('livechatViewerModePlugin', {
|
2022-01-07 18:20:28 +00:00
|
|
|
dependencies: ['converse-muc', 'converse-muc-views'],
|
|
|
|
initialize: function () {
|
|
|
|
const _converse = this._converse
|
|
|
|
const getDefaultMUCNickname = _converse.getDefaultMUCNickname
|
|
|
|
if (!getDefaultMUCNickname) {
|
|
|
|
console.error('[livechatViewerModePlugin] getDefaultMUCNickname is not initialized.')
|
|
|
|
} else {
|
|
|
|
Object.assign(_converse, {
|
|
|
|
getDefaultMUCNickname: function (this: any): any {
|
2022-01-10 02:06:16 +00:00
|
|
|
return getDefaultMUCNickname.apply(this, arguments) ?? randomNick('Anonymous')
|
2022-01-07 18:20:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_converse.api.settings.update({
|
|
|
|
livechat_viewer_mode: true
|
|
|
|
})
|
|
|
|
|
|
|
|
function refreshViewerMode (canChat: boolean): void {
|
|
|
|
console.log('[livechatViewerModePlugin] refreshViewerMode: ' + (canChat ? 'off' : 'on'))
|
|
|
|
if (canChat) {
|
|
|
|
body?.setAttribute('livechat-viewer-mode', 'off')
|
|
|
|
} else {
|
|
|
|
body?.setAttribute('livechat-viewer-mode', 'on')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_converse.api.listen.on('livechatViewerModeSetNickname', () => refreshViewerMode(true))
|
|
|
|
_converse.api.listen.on('chatRoomInitialized', function (this: any, model: any): void {
|
|
|
|
const nick = model?.get ? model.get('nick') : ''
|
|
|
|
refreshViewerMode(nick && !/^Anonymous /.test(nick))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-16 19:05:09 +00:00
|
|
|
// The following code does not work. Just keeping it in case we want to investigate.
|
|
|
|
// if (authenticationUrl !== '') {
|
|
|
|
// // We are in builtin-prosody mode. I'll try to disconnect from the room
|
|
|
|
// // on page unload. This is to avoid some bugs:
|
|
|
|
// // - users are not show as disconnected until a long timeout
|
|
|
|
// // - anonymous users' nicknames are not available before this timeout
|
|
|
|
// // - logged in users sometimes can't switch between iframe and fullscreen more than 1 time
|
2023-01-12 10:46:02 +00:00
|
|
|
// converse.plugins.add('livechatDisconnectOnUnloadPlugin', {
|
2022-01-16 19:05:09 +00:00
|
|
|
// initialize: function () {
|
|
|
|
// const _converse = this._converse
|
|
|
|
// const { unloadevent } = _converse
|
|
|
|
// // eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
// window.addEventListener(unloadevent, async () => {
|
|
|
|
// console.log('[livechatDisconnectOnUnloadPlugin] Disconnecting...')
|
|
|
|
// await _converse.api.user.logout()
|
|
|
|
// }, { once: true, passive: true })
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
2023-01-12 10:46:02 +00:00
|
|
|
converse.initialize(params)
|
2021-05-18 16:48:19 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed initializing converseJS', error)
|
|
|
|
}
|
2021-02-20 19:42:41 +00:00
|
|
|
}
|