@@ -202,20 +202,39 @@ export default {
202202 *
203203 * @method api.disco.entities.find
204204 * @param {string } feature The feature var to search for.
205- * @returns {Promise<DiscoEntity|null> } The matching DiscoEntity instance or null if not found.
205+ * @param {string } [jid] The entity JID whose subtree to search. If omitted, own bare JID and domain are queried.
206+ * @returns {Promise<DiscoEntity[]> } An array of matching DiscoEntity instances.
206207 */
207- async find ( feature ) {
208+ async find ( feature , jid ) {
208209 await api . waitUntil ( 'discoInitialized' ) ;
209210 const disco_entities = /** @type {DiscoEntities } */ ( _converse . state . disco_entities ) ;
210211 if ( ! disco_entities ) {
211212 return [ ] ;
212213 }
213- const checks = disco_entities . map ( async ( entity ) => {
214+ let candidates = [ ] ;
215+ if ( jid ) {
216+ 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 ) ;
220+ } else {
221+ const bare = _converse . session . get ( 'bare_jid' ) ;
222+ const bare_entity = await api . disco . entities . get ( bare , true ) ;
223+ if ( bare_entity ) candidates . push ( bare_entity ) ;
224+ const domain = Strophe . getDomainFromJid ( bare ) ;
225+ 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 ) ;
229+ }
230+ // Deduplicate by JID
231+ const unique = Array . from ( new Map ( candidates . map ( e => [ e . get ( 'jid' ) , e ] ) ) . values ( ) ) ;
232+ const checks = unique . map ( async ( entity ) => {
214233 const has = await entity . getFeature ( feature ) ;
215234 return has ? entity : null ;
216235 } ) ;
217236 const results = await Promise . all ( checks ) ;
218- return results . filter ( ( e ) => e instanceof Object ) ;
237+ return results . filter ( e => e ) ;
219238 } ,
220239
221240 /**
@@ -254,10 +273,12 @@ export default {
254273 */
255274 async items ( jid ) {
256275 const entity = await api . disco . entities . get ( jid ) ;
257- await entity . waitUntilItemsFetched ;
258-
259- const disco_entities = /** @type {DiscoEntities } */ ( _converse . state . disco_entities ) ;
260- return disco_entities . filter ( ( e ) => e . get ( 'parent_jids' ) ?. includes ( jid ) ) ;
276+ if ( entity ) {
277+ 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 ) ) ;
280+ }
281+ return [ ] ;
261282 } ,
262283
263284 /**
0 commit comments