diff --git a/src/plugins/sib-subscribe-to-rai.js b/src/plugins/sib-subscribe-to-rai.js index 986d8a4adff75879b6f221542e395befbd4ca8a4..ac9c05895f8e992b8c2ef691a94460db20adccdc 100644 --- a/src/plugins/sib-subscribe-to-rai.js +++ b/src/plugins/sib-subscribe-to-rai.js @@ -1,85 +1,69 @@ -import('../utils.js').then(utils => { - import(/* @vite-ignore */utils.coreVersion()).then(async (core) => { +import('../utils.js').then((utils) => { + import(/* @vite-ignore */ utils.coreVersion()).then(async (core) => { /** * Initialize rai plugin. */ - converse.plugins.add("sib-subscribe-to-rai", { - dependencies: ["converse-rai"], - initialize() { + converse.plugins.add('sib-subscribe-to-rai', { + dependencies: ['converse-rai'], + async initialize() { const { log } = converse.env; const _converse = this._converse; const { api } = _converse; - const getCircles = new Promise((resolve, reject) => { - const circleInterval = setInterval(async () => { - let retry = false; - let circles = []; - const user = await document.querySelector("sib-auth").getUser(); - const userProfile = await core.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; - } - } + /** + * Return the user's memberships as a set of jabberIDs. + * + * @param {'circles' | 'projects'} entity + * @returns {Promise<string[]>} + */ + const fetchMemberships = async (entity) => { + const user = await document.querySelector('sib-auth').getUser(); - if (!retry) { - clearInterval(circleInterval); - resolve(circles); - } - }, 250); - }); + // FIXME: The context is undefined + const userProfile = await core.store.getData( + user['@id'], + this.context + ); + const entities = await userProfile[entity]; - const getProjects = new Promise((resolve, reject) => { - const projectInterval = setInterval(async () => { - let retry = false; - let projects = []; - const user = await document.querySelector("sib-auth").getUser(); - const userProfile = await core.store.getData(user["@id"], this.context); - if ( - userProfile && - (await userProfile["projects.ldp:contains"]) && - (await userProfile["projects.ldp:contains"]) !== "undefined" - ) { - 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); - }); + const res = await core.store.fetchAuthn(entities['@id'], { + headers: { + depth: 1, + 'accept-model-fields': '["jabberID"]', + }, + }); + + const data = await res.json(); + return ( + data['ldp:contains']?.flatMap((item) => item['jabberID']) || [] + ); + }; const onConnected = async () => { - // @MattJ Here userRooms is an array of each jabberID the user is on. - let userRooms = (await Promise.all([getCircles, getProjects])).flat(); + const userRooms = ( + await Promise.all([ + fetchMemberships('circles'), + fetchMemberships('projects'), + ]) + ) + .flat() + .filter(Boolean); - log.info(`User rooms: ${userRooms.join(", ")}`); + log.info(`User rooms: ${userRooms.join(', ')}`); await api.rooms.subscribe(userRooms); }; // Send RAI subscriptions when connected to the XMPP endpoint - api.listen.on("connected", onConnected); - api.listen.on("reconnected", onConnected); + api.listen.on('connected', onConnected); + api.listen.on('reconnected', onConnected); // Set timeout so the event is sent once the document is loaded // Await document ready state so the event is properly registered - api.listen.on("chatRoomHasActivity", async (jid) => { + api.listen.on('chatRoomHasActivity', async (jid) => { await utils.documentReady(); setTimeout(() => { window.dispatchEvent( - new CustomEvent("newMessage", { + new CustomEvent('newMessage', { detail: { jid, },