@@ -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 ) ;
0 commit comments