2021-05-03 16:30:02 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
|
interface Window {
|
|
|
|
converse: {
|
|
|
|
initialize: (args: any) => void
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface InitConverseParams {
|
|
|
|
jid: string
|
|
|
|
assetsPath: string
|
|
|
|
room: string
|
|
|
|
boshServiceUrl: string
|
|
|
|
websocketServiceUrl: string
|
2021-05-04 11:00:44 +00:00
|
|
|
authenticationUrl: string
|
2021-05-05 15:20:06 +00:00
|
|
|
advancedControls: 'true' | 'false'
|
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,
|
|
|
|
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,
|
|
|
|
advancedControls
|
2021-05-03 18:37:23 +00:00
|
|
|
}: InitConverseParams) {
|
2021-05-05 15:20:06 +00:00
|
|
|
const isInIframe = inIframe()
|
2021-05-05 15:32:50 +00:00
|
|
|
|
|
|
|
if (isInIframe) {
|
|
|
|
const body = document.querySelector('body')
|
|
|
|
if (body) {
|
|
|
|
body.classList.add('livechat-iframe')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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',
|
|
|
|
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
|
|
|
|
],
|
|
|
|
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,
|
|
|
|
muc_mention_autocomplete_min_chars: 3,
|
|
|
|
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,
|
|
|
|
spoiler: true,
|
|
|
|
emoji: true,
|
|
|
|
toggle_occupants: true
|
2021-05-18 17:09:29 +00:00
|
|
|
},
|
2021-11-09 16:45:54 +00:00
|
|
|
theme: 'peertube',
|
2021-05-18 17:09:29 +00:00
|
|
|
persistent_store: 'sessionStorage'
|
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
|
2021-05-04 11:00:44 +00:00
|
|
|
if (authenticationUrl !== '') {
|
|
|
|
const auth = await authenticatedMode(authenticationUrl)
|
|
|
|
if (auth) {
|
|
|
|
params.authentication = 'login'
|
|
|
|
params.auto_login = true
|
|
|
|
params.jid = auth.jid
|
|
|
|
params.password = auth.password
|
2021-05-04 14:33:32 +00:00
|
|
|
if (auth.nickname) {
|
|
|
|
params.nickname = auth.nickname
|
|
|
|
} else {
|
|
|
|
params.muc_nickname_from_jid = true
|
|
|
|
}
|
2021-05-18 17:00:26 +00:00
|
|
|
// We dont need the keepalive. And I suppose it is related to some bugs when opening a previous chat window.
|
|
|
|
params.keepalive = false
|
2021-05-05 16:35:28 +00:00
|
|
|
isAuthenticated = true
|
2021-05-04 11:00:44 +00:00
|
|
|
// FIXME: use params.oauth_providers?
|
|
|
|
}
|
2021-05-03 18:37:23 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 16:35:28 +00:00
|
|
|
if (!isAuthenticated) {
|
|
|
|
console.log('User is not authenticated.')
|
|
|
|
// 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-06 10:34:42 +00:00
|
|
|
if (advancedControls) {
|
|
|
|
// with the builtin prosody, no need to use this warning (except if we open to external clients?)
|
|
|
|
params.show_retraction_warning = false
|
|
|
|
} else {
|
2021-05-05 15:20:06 +00:00
|
|
|
// These params are for externals XMPP servers.
|
|
|
|
// NB: because we dont know if external servers have authentication mecanism,
|
|
|
|
// we disable all moderation functionnality.
|
|
|
|
// This is also done for backward compatibility with older installations.
|
|
|
|
params.muc_disable_slash_commands = [
|
|
|
|
'admin', 'ban', 'clear', 'deop', 'destroy', 'kick',
|
|
|
|
'member', 'modtools', 'mute', 'op', 'owner', 'register',
|
|
|
|
'revoke', 'subject', 'topic', 'voice'
|
|
|
|
]
|
|
|
|
params.modtools_disable_assign = true
|
|
|
|
}
|
|
|
|
|
2021-05-18 16:48:19 +00:00
|
|
|
try {
|
|
|
|
window.converse.initialize(params)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed initializing converseJS', error)
|
|
|
|
}
|
2021-02-20 19:42:41 +00:00
|
|
|
}
|