Explorar el Código

disco: Rely on the `items` property on the entity to get items

Instead of the `parent_jids` property on potential entity items.
JC Brand hace 1 mes
padre
commit
a3a16d3426

+ 23 - 20
src/headless/plugins/disco/api.js

@@ -208,33 +208,35 @@ export default {
             async find(feature, jid) {
                 await api.waitUntil('discoInitialized');
                 const disco_entities = /** @type {DiscoEntities} */ (_converse.state.disco_entities);
-                if (!disco_entities) {
-                    return [];
-                }
-                let candidates = [];
+                if (!disco_entities) return [];
+
+                const candidates = [];
                 if (jid) {
                     const entity = await api.disco.entities.get(jid, true);
-                    if (entity) candidates.push(entity);
-                    const items = await api.disco.entities.items(jid);
-                    candidates.push(...items);
+                    if (entity) {
+                        const items = await api.disco.entities.items(jid);
+                        candidates.push(entity, ...items);
+                    }
                 } else {
-                    const bare = _converse.session.get('bare_jid');
-                    const bare_entity = await api.disco.entities.get(bare, true);
+                    const bare_jid = _converse.session.get('bare_jid');
+                    const bare_entity = await api.disco.entities.get(bare_jid, true);
                     if (bare_entity) candidates.push(bare_entity);
-                    const domain = Strophe.getDomainFromJid(bare);
+
+                    const domain = Strophe.getDomainFromJid(bare_jid);
                     const domain_entity = await api.disco.entities.get(domain, true);
-                    if (domain_entity) candidates.push(domain_entity);
-                    const items = await api.disco.entities.items(domain);
-                    candidates.push(...items);
+                    if (domain_entity) {
+                        const items = await api.disco.entities.items(domain);
+                        candidates.push(domain_entity, ...items);
+                    }
                 }
-                // Deduplicate by JID
-                const unique = Array.from(new Map(candidates.map(e => [e.get('jid'), e])).values());
+
+                const unique = Array.from(new Map(candidates.map((e) => [e.get('jid'), e])).values());
                 const checks = unique.map(async (entity) => {
                     const has = await entity.getFeature(feature);
                     return has ? entity : null;
                 });
                 const results = await Promise.all(checks);
-                return results.filter(e => e);
+                return results.filter((e) => e);
             },
 
             /**
@@ -265,18 +267,20 @@ export default {
             },
 
             /**
-             * Return any disco items advertised on this entity
+             * Return the disco items advertised on this entity
              *
              * @method api.disco.entities.items
              * @param {string} jid - The Jabber ID of the entity for which we want to fetch items
+             * @returns {Promise<DiscoEntity[]>}
              * @example api.disco.entities.items(jid);
              */
             async items(jid) {
                 const entity = await api.disco.entities.get(jid);
                 if (entity) {
                     await entity.waitUntilItemsFetched;
-                    const disco_entities = /** @type {DiscoEntities} */ (_converse.state.disco_entities);
-                    return disco_entities.filter((e) => e.get('parent_jids')?.includes(jid));
+
+                    const item_jids = entity.get('items') || [];
+                    return Promise.all(item_jids.map(/** @param {string} jid */ (jid) => api.disco.entities.get(jid)));
                 }
                 return [];
             },
@@ -343,7 +347,6 @@ export default {
                 }
 
                 const items = await api.disco.entities.items(jid);
-
                 const promises = [entity.getFeature(feature), ...items.map((i) => i.getFeature(feature))];
                 const result = await Promise.all(promises);
                 return result.filter((f) => f instanceof Object);

+ 3 - 2
src/headless/types/plugins/disco/api.d.ts

@@ -99,13 +99,14 @@ declare namespace _default {
              */
             function get(jid: string, create?: boolean): Promise<import("./entity").default | import("./entities").default | undefined>;
             /**
-             * Return any disco items advertised on this entity
+             * Return the disco items advertised on this entity
              *
              * @method api.disco.entities.items
              * @param {string} jid - The Jabber ID of the entity for which we want to fetch items
+             * @returns {Promise<DiscoEntity[]>}
              * @example api.disco.entities.items(jid);
              */
-            function items(jid: string): Promise<any>;
+            function items(jid: string): Promise<import("./entity").default[]>;
             /**
              * Create a new  disco entity. It's identity and features
              * will automatically be fetched from cache or from the

+ 2 - 5
src/plugins/disco-views/disco-browser.js

@@ -75,13 +75,10 @@ class DiscoBrowser extends CustomElement {
             }
         }
         const features = entity.features?.map((f) => f.get('var')) || [];
-        const identities = entity.identities || [];
-        const item_jids = entity.get('items') || [];
-        const items = await Promise.all(item_jids.map(/** @param {string} jid */ (jid) => api.disco.entities.get(jid)));
         return {
             features: features.toSorted?.() || features,
-            identities,
-            items,
+            identities: entity.identities || [],
+            items: await api.disco.entities.items(entity_jid),
         };
     }
 }