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
|
|
|
export const livechatSpecificsPlugin = {
|
|
|
|
dependencies: ['converse-muc', 'converse-muc-views'],
|
|
|
|
initialize: function (this: any) {
|
|
|
|
const _converse = this._converse
|
2024-04-18 14:24:09 +00:00
|
|
|
|
|
|
|
_converse.api.settings.extend({
|
|
|
|
// if user is authenticated with an external account (to add a logout button)
|
2024-06-20 14:46:14 +00:00
|
|
|
livechat_specific_external_authent: false,
|
|
|
|
// if user is anonymous
|
|
|
|
livechat_specific_is_anonymous: false
|
2024-04-18 14:24:09 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
_converse.api.listen.on('getHeadingButtons', (view: any, buttons: any[]) => {
|
|
|
|
if (view.model.get('type') !== _converse.CHATROOMS_TYPE) {
|
|
|
|
// only on MUC.
|
2024-04-29 13:50:38 +00:00
|
|
|
return buttons
|
2024-04-18 14:24:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_converse.api.settings.get('livechat_specific_external_authent')) {
|
|
|
|
// Adding a logout button
|
|
|
|
buttons.push({
|
|
|
|
i18n_text: _converse.__('Log out'),
|
|
|
|
handler: async (ev: Event) => {
|
|
|
|
ev.preventDefault()
|
|
|
|
ev.stopPropagation()
|
|
|
|
|
|
|
|
const messages = [_converse.__('Are you sure you want to leave this groupchat?')]
|
|
|
|
const result = await _converse.api.confirm(_converse.__('Confirm'), messages)
|
|
|
|
if (!result) { return }
|
|
|
|
|
|
|
|
// Deleting access token in sessionStorage.
|
2024-04-19 07:53:23 +00:00
|
|
|
window.sessionStorage.removeItem('peertube-plugin-livechat-external-auth-oidc-token')
|
2024-04-18 14:24:09 +00:00
|
|
|
|
|
|
|
const reconnectMode = _converse.api.settings.get('livechat_external_auth_reconnect_mode')
|
|
|
|
if (reconnectMode === 'button-close-open') {
|
|
|
|
const button = document.getElementsByClassName('peertube-plugin-livechat-button-close')[0]
|
|
|
|
if ((button as HTMLAnchorElement).click) { (button as HTMLAnchorElement).click() }
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location.reload()
|
|
|
|
},
|
|
|
|
a_class: 'close-chatbox-button',
|
|
|
|
icon_class: 'fa-sign-out-alt',
|
|
|
|
name: 'signout'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return buttons
|
|
|
|
})
|
|
|
|
|
2024-03-27 15:26:32 +00:00
|
|
|
_converse.api.listen.on('chatRoomViewInitialized', function (this: any, _model: any): void {
|
|
|
|
// Remove the spinner if present...
|
|
|
|
document.getElementById('livechat-loading-spinner')?.remove()
|
|
|
|
})
|
|
|
|
|
2024-04-03 09:18:00 +00:00
|
|
|
// Adding a method on window.converse, so we can close the chat on navigation-end event
|
2024-03-27 15:26:32 +00:00
|
|
|
// (when chatIncludeMode is peertube-*)
|
|
|
|
window.converse.livechatDisconnect = function livechatDisconnect () {
|
2024-04-03 09:18:00 +00:00
|
|
|
if (_converse.api.connection.connected()) {
|
|
|
|
console.log('[livechatSpecificsPlugin] disconnecting converseJS...')
|
|
|
|
_converse.api.user.logout()
|
|
|
|
}
|
2024-03-27 15:26:32 +00:00
|
|
|
}
|
2024-03-28 10:16:33 +00:00
|
|
|
|
2024-03-28 11:22:30 +00:00
|
|
|
// To reconnect ConverseJS when joining another room (or the same one),
|
|
|
|
// we store the relevant closure function:
|
|
|
|
window.reconnectConverse = function reconnectConverse (params: any): void {
|
|
|
|
console.log('[livechatSpecificsPlugin] reconnecting converseJS...')
|
|
|
|
|
|
|
|
// The new room to join:
|
|
|
|
_converse.api.settings.set('auto_join_rooms', params.auto_join_rooms)
|
|
|
|
_converse.api.settings.set('notify_all_room_messages', params.notify_all_room_messages)
|
|
|
|
|
|
|
|
// update connection parameters (in case the user logged in after the first chat)
|
|
|
|
for (const k of [
|
|
|
|
'bosh_service_url', 'websocket_url',
|
|
|
|
'authentication', 'nickname', 'muc_nickname_from_jid', 'auto_login', 'jid', 'password', 'keepalive'
|
|
|
|
]) {
|
|
|
|
_converse.api.settings.set(k, params[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
// update other settings
|
2024-04-16 09:43:38 +00:00
|
|
|
for (const k of [
|
|
|
|
'hide_muc_participants',
|
|
|
|
'livechat_enable_viewer_mode',
|
2024-04-22 12:28:55 +00:00
|
|
|
'livechat_external_auth_oidc_buttons',
|
2024-04-18 08:58:08 +00:00
|
|
|
'livechat_external_auth_reconnect_mode',
|
2024-04-18 14:24:09 +00:00
|
|
|
'livechat_mini_muc_head',
|
2024-05-11 15:37:20 +00:00
|
|
|
'livechat_specific_external_authent',
|
2024-05-12 14:02:13 +00:00
|
|
|
'livechat_task_app_enabled',
|
2024-05-28 15:56:24 +00:00
|
|
|
'livechat_task_app_restore',
|
|
|
|
'livechat_custom_emojis_url',
|
|
|
|
'emoji_categories'
|
2024-04-16 09:43:38 +00:00
|
|
|
]) {
|
2024-03-28 11:22:30 +00:00
|
|
|
_converse.api.settings.set(k, params[k])
|
|
|
|
}
|
|
|
|
|
2024-05-28 15:56:24 +00:00
|
|
|
// We also unload emojis, in case there are custom emojis.
|
|
|
|
window.converse.emojis = {
|
|
|
|
initialized: false,
|
|
|
|
initialized_promise: getOpenPromise()
|
|
|
|
}
|
|
|
|
|
2024-03-28 11:22:30 +00:00
|
|
|
// Then login.
|
|
|
|
_converse.api.user.login()
|
|
|
|
}
|
|
|
|
|
2024-03-28 10:16:33 +00:00
|
|
|
if (window.location.protocol === 'http:') {
|
|
|
|
// We are probably on a dev instance, so we will add _converse in window:
|
|
|
|
(window as any)._livechatConverse = _converse
|
|
|
|
}
|
2024-03-27 15:26:32 +00:00
|
|
|
},
|
|
|
|
overrides: {
|
|
|
|
ChatRoom: {
|
|
|
|
getActionInfoMessage: function (this: any, code: string, nick: string, actor: any): any {
|
|
|
|
if (code === '303') {
|
|
|
|
// When there is numerous anonymous users joining at the same time,
|
|
|
|
// they can all change their nicknames at the same time, generating a log of action messages.
|
|
|
|
// To mitigate this, will don't display nickname changes if the previous nick is something like
|
|
|
|
// 'Anonymous 12345'.
|
|
|
|
if (/^Anonymous \d+$/.test(nick)) {
|
|
|
|
// To avoid displaying the message, we just have to return an empty one
|
|
|
|
// (createInfoMessage will ignore if !data.message).
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.__super__.getActionInfoMessage(code, nick, actor)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ChatRoomMessage: {
|
|
|
|
/* By default, ConverseJS groups messages from the same users for a 10 minutes period.
|
|
|
|
* This make no sense in a livechat room. So we override isFollowup to ignore. */
|
|
|
|
isFollowup: function isFollowup () { return false }
|
|
|
|
},
|
|
|
|
ChatRoomOccupants: {
|
|
|
|
comparator: function (this: any, occupant1: any, occupant2: any): Number {
|
|
|
|
// Overriding Occupants comparators, to display anonymous users at the end of the list.
|
|
|
|
const nick1: string = occupant1.getDisplayName()
|
|
|
|
const nick2: string = occupant2.getDisplayName()
|
|
|
|
const b1 = nick1.startsWith('Anonymous ')
|
|
|
|
const b2 = nick2.startsWith('Anonymous ')
|
|
|
|
if (b1 === b2) {
|
|
|
|
// Both startswith anonymous, or non of it: fallback to the standard comparator.
|
|
|
|
return this.__super__.comparator(occupant1, occupant2)
|
|
|
|
}
|
|
|
|
// Else: Anonymous always last.
|
|
|
|
return b1 ? 1 : -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-28 15:56:24 +00:00
|
|
|
|
|
|
|
// FIXME: this function is copied from @converse. Should not do so.
|
|
|
|
function getOpenPromise (): any {
|
|
|
|
const wrapper: any = {
|
|
|
|
isResolved: false,
|
|
|
|
isPending: true,
|
|
|
|
isRejected: false
|
|
|
|
}
|
|
|
|
const promise: any = new Promise((resolve, reject) => {
|
|
|
|
wrapper.resolve = resolve
|
|
|
|
wrapper.reject = reject
|
|
|
|
})
|
|
|
|
Object.assign(promise, wrapper)
|
|
|
|
promise.then(
|
|
|
|
function (v: any) {
|
|
|
|
promise.isResolved = true
|
|
|
|
promise.isPending = false
|
|
|
|
promise.isRejected = false
|
|
|
|
return v
|
|
|
|
},
|
|
|
|
function (e: any) {
|
|
|
|
promise.isResolved = false
|
|
|
|
promise.isPending = false
|
|
|
|
promise.isRejected = true
|
|
|
|
throw (e)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return promise
|
|
|
|
}
|