diff --git a/src/solid-xmpp-chat.js b/src/solid-xmpp-chat.js index 7843da90a57cf28d214c3895d11ced7dc11479b1..b96edd9423e20d461eccf840d4d14423083cd5a1 100644 --- a/src/solid-xmpp-chat.js +++ b/src/solid-xmpp-chat.js @@ -87,7 +87,7 @@ export const SolidXMPPChat = { } // Else, not logged in, on chat change }, - async initializeConverse() { + initializeConverse() { window.converse_sib = {}; // Deferred resolved at the end of this function @@ -138,176 +138,176 @@ export const SolidXMPPChat = { } } }); - // Initialize change change plugin - converse.plugins.add('conversejs-changechat', converse_sib.service.plugins.sibChat); + setTimeout(async () => { + // Initialize change change plugin + converse.plugins.add('conversejs-changechat', converse_sib.service.plugins.sibChat); - // Initialize deferred resolution plugin - converse.plugins.add('conversejs-sib-connected', { - initialize() { - this._converse.api.listen.on('connected', converse_sib.connected_deferred.resolve); - } - }); + // Initialize deferred resolution plugin + converse.plugins.add('conversejs-sib-connected', { + initialize() { + this._converse.api.listen.on('connected', converse_sib.connected_deferred.resolve); + } + }); - // Initialize AUTHFAIL plugin - converse.plugins.add('conversejs-sib-disconnected', { - initialize() { - this._converse.api.listen.on('disconnected', () => { - if(this._converse.connfeedback.attributes.connection_status === converse.env.Strophe.Status.AUTHFAIL) { - Array.from(document.querySelectorAll('solid-xmpp-chat')).map(el => el.shadowRoot.innerHTML = - `<div style='margin:3em;line-height:32px;'><b style='color:red;'>Erreur d'authentification.</b><br /><i style='color:grey;'>Êtes-vous sur le Hubl affilié à votre compte ?</i></div>` - ); - } else { - Array.from(document.querySelectorAll('solid-xmpp-chat')).map(el => el.shadowRoot.innerHTML = - `<div style='margin:3em;line-height:32px;'><b style='color:red;'>Erreur.</b><br /><i style='color:grey;'>${this._converse.connfeedback.attributes.message}</i></div>` - ); - } - }); - } - }); + // Initialize AUTHFAIL plugin + converse.plugins.add('conversejs-sib-disconnected', { + initialize() { + this._converse.api.listen.on('disconnected', () => { + if(this._converse.connfeedback.attributes.connection_status === converse.env.Strophe.Status.AUTHFAIL) { + Array.from(document.querySelectorAll('solid-xmpp-chat')).map(el => el.shadowRoot.innerHTML = + `<div style='margin:3em;line-height:32px;'><b style='color:red;'>Erreur d'authentification.</b><br /><i style='color:grey;'>Êtes-vous sur le Hubl affilié à votre compte ?</i></div>` + ); + } else { + Array.from(document.querySelectorAll('solid-xmpp-chat')).map(el => el.shadowRoot.innerHTML = + `<div style='margin:3em;line-height:32px;'><b style='color:red;'>Erreur.</b><br /><i style='color:grey;'>${this._converse.connfeedback.attributes.message}</i></div>` + ); + } + }); + } + }); - // Initialize rai plugin - converse.plugins.add('conversejs-rai', { - async initialize() { + // Initialize rai plugin + converse.plugins.add('conversejs-rai', { + async initialize() { - let userRooms = (await Promise.all([ - new Promise((resolve, reject) => { - const circleInterval = setInterval(async () => { - let retry = false; - let circles = []; - const user = await document.querySelector('sib-auth').getUser(); - const userProfile = await store.getData(user['@id'], this.context); - for (let circleMembership of await userProfile['circles.ldp:contains']) { - if (circleMembership) { - circles.push(await circleMembership['circle.jabberID']); - } else { - retry = true; + let userRooms = (await Promise.all([ + new Promise((resolve, reject) => { + const circleInterval = setInterval(async () => { + let retry = false; + let circles = []; + const user = await document.querySelector('sib-auth').getUser(); + const userProfile = await store.getData(user['@id'], this.context); + for (let circleMembership of await userProfile['circles.ldp:contains']) { + if (circleMembership) { + circles.push(await circleMembership['circle.jabberID']); + } else { + retry = true; + } } - } - if (retry) { - } else { - clearInterval(circleInterval); - resolve(circles); - } - }, 250); - }), - new Promise((resolve, reject) => { - const projectInterval = setInterval(async () => { - let retry = false; - let projects = []; - const user = await document.querySelector('sib-auth').getUser(); - const userProfile = await store.getData(user['@id'], this.context); - for (let projectMembership of await userProfile['projects.ldp:contains']) { - if (projectMembership) { - projects.push(await projectMembership['project.jabberID']); - } else { - retry = true; + if (!retry) { + clearInterval(circleInterval); + resolve(circles); } - } - if (retry) { - } else { - clearInterval(projectInterval); - resolve(projects); - } - }, 250); - }) - ])).flat(); + }, 250); + }), + new Promise((resolve, reject) => { + const projectInterval = setInterval(async () => { + let retry = false; + let projects = []; + const user = await document.querySelector('sib-auth').getUser(); + const userProfile = await store.getData(user['@id'], this.context); + for (let projectMembership of await userProfile['projects.ldp:contains']) { + if (projectMembership) { + projects.push(await projectMembership['project.jabberID']); + } else { + retry = true; + } + } + if (!retry) { + clearInterval(projectInterval); + resolve(projects); + } + }, 250); + }) + ])).flat(); - // @MattJ Here userRooms is an array of each jabberID the user is on. - this._converse.api.trigger('raiRoomsUpdated', userRooms); + // @MattJ Here userRooms is an array of each jabberID the user is on. + this._converse.api.trigger('raiRoomsUpdated', userRooms); - this._converse.api.listen.on('chatRoomActivityIndicators', function (jid) { - window.dispatchEvent(new CustomEvent('newMessage', { - detail: { - jid: jid - } - })); - }); - } - }); + this._converse.api.listen.on('chatRoomActivityIndicators', function (jid) { + window.dispatchEvent(new CustomEvent('newMessage', { + detail: { + jid: jid + } + })); + }); + } + }); - // Initialize deferred resolution plugin - converse.plugins.add('conversejs-sib-focused', { - initialize() { - this._converse.api.listen.on('chatBoxFocused', function() { - // Get the currently used solid-xmpp-chat & send read event of - const resource = Array.from(document.querySelectorAll('solid-xmpp-chat')) - .filter(el => {return el.shadowRoot.getElementById('conversejs')}) - .pop() - .component.resource; - window.dispatchEvent(new CustomEvent('read', { - detail: { - resource: resource - } - })); - }); - } - }); + // Initialize deferred resolution plugin + converse.plugins.add('conversejs-sib-focused', { + initialize() { + this._converse.api.listen.on('chatBoxFocused', function() { + // Get the currently used solid-xmpp-chat & send read event of + const resource = Array.from(document.querySelectorAll('solid-xmpp-chat')) + .filter(el => {return el.shadowRoot.getElementById('conversejs')}) + .pop() + .component.resource; + window.dispatchEvent(new CustomEvent('read', { + detail: { + resource: resource + } + })); + }); + } + }); - const user = await document.querySelector('sib-auth').getUser(); - const userProfile = await store.getData(user['@id'], this.context); - const jabberID = await userProfile['chatProfile.jabberID']; + const user = await document.querySelector('sib-auth').getUser(); + const userProfile = await store.getData(user['@id'], this.context); + const jabberID = await userProfile['chatProfile.jabberID']; - if(this.resource) { - if (await this.resource.jabberRoom) { - this.jid = await this.resource['jabberID']; + if(this.resource) { + if (await this.resource.jabberRoom) { + this.jid = await this.resource['jabberID']; + } else { + this.jid = await this.resource['chatProfile.jabberID']; + } } else { - this.jid = await this.resource['chatProfile.jabberID']; + this.jid = jabberID; } - } else { - this.jid = jabberID; - } - const idToken = await document.querySelector('sib-auth').getUserIdToken(); + const idToken = await document.querySelector('sib-auth').getUserIdToken(); - converse.initialize({ - 'assets_path': ComponentPath + '/dist/conversejs/', - 'authentication': this.element.dataset.authentication || 'login', - 'password': idToken, - 'allow_chat_pending_contacts': true, - 'allow_message_retraction': 'moderator', - 'allow_non_roster_messaging': true, - 'allow_dragresize': false, - 'allow_logout': false, - 'archived_messages_page_size': "30", - 'auto_list_rooms': true, - 'auto_login': this.element.dataset.autoLogin === 'true', - 'auto_join_on_invite': true, - 'auto_reconnect': true, - 'auto_register_muc_nickname': true, - 'websocket_url': this.element.dataset.websocketUrl || 'wss://jabber.happy-dev.fr/xmpp-websocket', - 'enable_smacks': true, - "clear_messages_on_reconnection": false, - "discover_connection_methods": false, - 'jid': jabberID.toLowerCase(), - 'i18n': this.element.dataset.i18n || 'fr', - 'loglevel': 'fatal', - 'message_archiving': 'always', - 'message_archiving_timeout': 60000, - 'muc_disable_slash_commands': true, - 'muc_hats': ['hats', 'vcard_roles', 'moderator'], - 'role_affiliation_hat_conversions': {'moderator': 'Administrateur'}, - 'muc_nickname_from_jid': true, - 'muc_fetch_members': true, - 'muc_show_info_messages': [], - 'play_sounds': false, - 'root': this.element.shadowRoot, - 'show_client_info': false, - 'show_desktop_notifications': false, - 'persistent_store': 'IndexedDB', - 'sounds_path': ComponentPath + '/dist/conversejs/', - 'show_send_button': false, - 'view_mode': 'fullscreen', - 'visible_toolbar_buttons': { - call: false, - spoiler: false, - emoji: true, - fileupload: false, // Not working in current Converse - toggle_occupants: false - }, - 'whitelisted_plugins': ['rai', 'conversejs-sib-disconnected', 'conversejs-sib-connected', 'conversejs-sib-focused', 'conversejs-changechat', 'conversejs-rai'], - }); + converse.initialize({ + 'assets_path': ComponentPath + '/dist/conversejs/', + 'authentication': this.element.dataset.authentication || 'login', + 'password': idToken, + 'allow_chat_pending_contacts': true, + 'allow_message_retraction': 'moderator', + 'allow_non_roster_messaging': true, + 'allow_dragresize': false, + 'allow_logout': false, + 'archived_messages_page_size': "30", + 'auto_list_rooms': true, + 'auto_login': this.element.dataset.autoLogin === 'true', + 'auto_join_on_invite': true, + 'auto_reconnect': true, + 'auto_register_muc_nickname': true, + 'websocket_url': this.element.dataset.websocketUrl || 'wss://jabber.happy-dev.fr/xmpp-websocket', + 'enable_smacks': true, + "clear_messages_on_reconnection": false, + "discover_connection_methods": false, + 'jid': jabberID.toLowerCase(), + 'i18n': this.element.dataset.i18n || 'fr', + 'loglevel': 'fatal', + 'message_archiving': 'always', + 'message_archiving_timeout': 60000, + 'muc_disable_slash_commands': true, + 'muc_hats': ['hats', 'vcard_roles', 'moderator'], + 'role_affiliation_hat_conversions': {'moderator': 'Administrateur'}, + 'muc_nickname_from_jid': true, + 'muc_fetch_members': true, + 'muc_show_info_messages': [], + 'play_sounds': false, + 'root': this.element.shadowRoot, + 'show_client_info': false, + 'show_desktop_notifications': false, + 'persistent_store': 'IndexedDB', + 'sounds_path': ComponentPath + '/dist/conversejs/', + 'show_send_button': false, + 'view_mode': 'fullscreen', + 'visible_toolbar_buttons': { + call: false, + spoiler: false, + emoji: true, + fileupload: false, // Not working in current Converse + toggle_occupants: false + }, + 'whitelisted_plugins': ['rai', 'conversejs-sib-disconnected', 'conversejs-sib-connected', 'conversejs-sib-focused', 'conversejs-changechat', 'conversejs-rai'], + }); - converse_sib.loaded_deferred.resolve(); + converse_sib.loaded_deferred.resolve(); + }, 0); } };