Skip to content

Commit a3a16d3

Browse files
committed
disco: Rely on the items property on the entity to get items
Instead of the `parent_jids` property on potential entity items.
1 parent 076cd8a commit a3a16d3

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/headless/plugins/disco/api.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,33 +208,35 @@ export default {
208208
async find(feature, jid) {
209209
await api.waitUntil('discoInitialized');
210210
const disco_entities = /** @type {DiscoEntities} */ (_converse.state.disco_entities);
211-
if (!disco_entities) {
212-
return [];
213-
}
214-
let candidates = [];
211+
if (!disco_entities) return [];
212+
213+
const candidates = [];
215214
if (jid) {
216215
const entity = await api.disco.entities.get(jid, true);
217-
if (entity) candidates.push(entity);
218-
const items = await api.disco.entities.items(jid);
219-
candidates.push(...items);
216+
if (entity) {
217+
const items = await api.disco.entities.items(jid);
218+
candidates.push(entity, ...items);
219+
}
220220
} else {
221-
const bare = _converse.session.get('bare_jid');
222-
const bare_entity = await api.disco.entities.get(bare, true);
221+
const bare_jid = _converse.session.get('bare_jid');
222+
const bare_entity = await api.disco.entities.get(bare_jid, true);
223223
if (bare_entity) candidates.push(bare_entity);
224-
const domain = Strophe.getDomainFromJid(bare);
224+
225+
const domain = Strophe.getDomainFromJid(bare_jid);
225226
const domain_entity = await api.disco.entities.get(domain, true);
226-
if (domain_entity) candidates.push(domain_entity);
227-
const items = await api.disco.entities.items(domain);
228-
candidates.push(...items);
227+
if (domain_entity) {
228+
const items = await api.disco.entities.items(domain);
229+
candidates.push(domain_entity, ...items);
230+
}
229231
}
230-
// Deduplicate by JID
231-
const unique = Array.from(new Map(candidates.map(e => [e.get('jid'), e])).values());
232+
233+
const unique = Array.from(new Map(candidates.map((e) => [e.get('jid'), e])).values());
232234
const checks = unique.map(async (entity) => {
233235
const has = await entity.getFeature(feature);
234236
return has ? entity : null;
235237
});
236238
const results = await Promise.all(checks);
237-
return results.filter(e => e);
239+
return results.filter((e) => e);
238240
},
239241

240242
/**
@@ -265,18 +267,20 @@ export default {
265267
},
266268

267269
/**
268-
* Return any disco items advertised on this entity
270+
* Return the disco items advertised on this entity
269271
*
270272
* @method api.disco.entities.items
271273
* @param {string} jid - The Jabber ID of the entity for which we want to fetch items
274+
* @returns {Promise<DiscoEntity[]>}
272275
* @example api.disco.entities.items(jid);
273276
*/
274277
async items(jid) {
275278
const entity = await api.disco.entities.get(jid);
276279
if (entity) {
277280
await entity.waitUntilItemsFetched;
278-
const disco_entities = /** @type {DiscoEntities} */ (_converse.state.disco_entities);
279-
return disco_entities.filter((e) => e.get('parent_jids')?.includes(jid));
281+
282+
const item_jids = entity.get('items') || [];
283+
return Promise.all(item_jids.map(/** @param {string} jid */ (jid) => api.disco.entities.get(jid)));
280284
}
281285
return [];
282286
},
@@ -343,7 +347,6 @@ export default {
343347
}
344348

345349
const items = await api.disco.entities.items(jid);
346-
347350
const promises = [entity.getFeature(feature), ...items.map((i) => i.getFeature(feature))];
348351
const result = await Promise.all(promises);
349352
return result.filter((f) => f instanceof Object);

src/headless/types/plugins/disco/api.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ declare namespace _default {
9999
*/
100100
function get(jid: string, create?: boolean): Promise<import("./entity").default | import("./entities").default | undefined>;
101101
/**
102-
* Return any disco items advertised on this entity
102+
* Return the disco items advertised on this entity
103103
*
104104
* @method api.disco.entities.items
105105
* @param {string} jid - The Jabber ID of the entity for which we want to fetch items
106+
* @returns {Promise<DiscoEntity[]>}
106107
* @example api.disco.entities.items(jid);
107108
*/
108-
function items(jid: string): Promise<any>;
109+
function items(jid: string): Promise<import("./entity").default[]>;
109110
/**
110111
* Create a new disco entity. It's identity and features
111112
* will automatically be fetched from cache or from the

src/plugins/disco-views/disco-browser.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,10 @@ class DiscoBrowser extends CustomElement {
7575
}
7676
}
7777
const features = entity.features?.map((f) => f.get('var')) || [];
78-
const identities = entity.identities || [];
79-
const item_jids = entity.get('items') || [];
80-
const items = await Promise.all(item_jids.map(/** @param {string} jid */ (jid) => api.disco.entities.get(jid)));
8178
return {
8279
features: features.toSorted?.() || features,
83-
identities,
84-
items,
80+
identities: entity.identities || [],
81+
items: await api.disco.entities.items(entity_jid),
8582
};
8683
}
8784
}

0 commit comments

Comments
 (0)