Skip to content

Commit cdcaa92

Browse files
committed
Add a timeout option to the api.disco.refresh function
And use it to set a lower timeout for disco info calls when joining a MUC. Updates #3778
1 parent a77b508 commit cdcaa92

File tree

9 files changed

+57
-17
lines changed

9 files changed

+57
-17
lines changed

src/headless/plugins/disco/api.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ export default {
152152
* @method api.disco.info
153153
* @param {string} jid The Jabber ID of the entity to query
154154
* @param {string} [node] A specific node identifier associated with the JID
155+
* @param {import('./types').DiscoInfoOptions} [options]
155156
* @returns {promise} Promise which resolves once we have a result from the server.
156157
*/
157-
info(jid, node) {
158+
info(jid, node, options) {
158159
const attrs = { xmlns: Strophe.NS.DISCO_INFO };
159160
if (node) {
160161
attrs.node = node;
@@ -164,7 +165,7 @@ export default {
164165
'to': jid,
165166
'type': 'get',
166167
}).c('query', attrs);
167-
return api.sendIQ(info);
168+
return api.sendIQ(info, options?.timeout);
168169
},
169170

170171
/**
@@ -381,11 +382,12 @@ export default {
381382
* disco entity by refetching them from the server
382383
* @method api.disco.refresh
383384
* @param {string} jid The JID of the entity whose features are refreshed.
385+
* @param {import('./types').DiscoInfoOptions} [options]
384386
* @returns {Promise} A promise which resolves once the features have been refreshed
385387
* @example
386388
* await api.disco.refresh('room@conference.example.org');
387389
*/
388-
async refresh(jid) {
390+
async refresh(jid, options) {
389391
if (!jid) throw new TypeError('api.disco.refresh: You need to provide an entity JID');
390392

391393
await api.waitUntil('discoInitialized');
@@ -400,10 +402,10 @@ export default {
400402
if (!entity.waitUntilItemsFetched.isPending) {
401403
entity.waitUntilItemsFetched = getOpenPromise();
402404
}
403-
entity.queryInfo();
405+
entity.queryInfo(options);
404406
} else {
405407
// Create it if it doesn't exist
406-
entity = await api.disco.entities.create({ jid }, { ignore_cache: true });
408+
entity = await api.disco.entities.create({ jid }, { ignore_cache: true, timeout: options.timeout });
407409
}
408410
return entity.waitUntilItemsFetched;
409411
},

src/headless/plugins/disco/entity.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,12 @@ class DiscoEntity extends Model {
102102
api.trigger('discoExtensionFieldDiscovered', field);
103103
}
104104

105+
/**
106+
* @param {import('./types').FetchEntityFeaturesOptions} options
107+
*/
105108
async fetchFeatures(options) {
106109
if (options.ignore_cache) {
107-
await this.queryInfo();
110+
await this.queryInfo(options);
108111
} else {
109112
const store_id = this.features.browserStorage.name;
110113

@@ -144,10 +147,13 @@ class DiscoEntity extends Model {
144147
}
145148
}
146149

147-
async queryInfo() {
150+
/**
151+
* @param {import('./types').DiscoInfoOptions} [options]
152+
*/
153+
async queryInfo(options) {
148154
let stanza;
149155
try {
150-
stanza = await api.disco.info(this.get('jid'), null);
156+
stanza = await api.disco.info(this.get('jid'), null, options);
151157
} catch (iq) {
152158
if (u.isElement(iq)) {
153159
const e = await parseErrorStanza(iq);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type DiscoInfoOptions = {
2+
timeout?: number;
3+
}
4+
5+
export type FetchEntityFeaturesOptions = {
6+
timeout?: number;
7+
ignore_cache?: boolean;
8+
}

src/headless/plugins/muc/muc.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import MUCSession from './session';
5050

5151
const { u, stx } = converse.env;
5252

53+
const DISCO_INFO_TIMEOUT_ON_JOIN = 5000;
54+
5355
/**
5456
* Represents a groupchat conversation.
5557
*/
@@ -192,7 +194,9 @@ class MUC extends ModelWithVCard(ModelWithMessages(ColorAwareModel(ChatBoxBase))
192194
// Set this early, so we don't rejoin in onHiddenChange
193195
this.session.save('connection_status', ROOMSTATUS.CONNECTING);
194196

195-
const is_new = (await this.refreshDiscoInfo()) instanceof ItemNotFoundError;
197+
const result = await this.refreshDiscoInfo({ timeout: DISCO_INFO_TIMEOUT_ON_JOIN });
198+
const is_new = result instanceof ItemNotFoundError;
199+
196200
nick = await this.getAndPersistNickname(nick);
197201
if (!nick) {
198202
safeSave(this.session, { 'connection_status': ROOMSTATUS.NICKNAME_REQUIRED });
@@ -1254,10 +1258,11 @@ class MUC extends ModelWithVCard(ModelWithMessages(ColorAwareModel(ChatBoxBase))
12541258
* Refresh the disco identity, features and fields for this {@link MUC}.
12551259
* *features* are stored on the features {@link Model} attribute on this {@link MUC}.
12561260
* *fields* are stored on the config {@link Model} attribute on this {@link MUC}.
1261+
* @param {import('@converse/headless/plugins/disco/types').DiscoInfoOptions} [options]
12571262
* @returns {Promise}
12581263
*/
1259-
async refreshDiscoInfo() {
1260-
const result = await api.disco.refresh(this.get('jid'));
1264+
async refreshDiscoInfo(options) {
1265+
const result = await api.disco.refresh(this.get('jid'), options);
12611266
if (result instanceof StanzaError) {
12621267
return result;
12631268
}
@@ -1738,7 +1743,8 @@ class MUC extends ModelWithVCard(ModelWithMessages(ColorAwareModel(ChatBoxBase))
17381743
<iq to="${this.get('jid')}" type="get" xmlns="jabber:client">
17391744
<query xmlns="${Strophe.NS.DISCO_INFO}" node="x-roomuser-item"/>
17401745
</iq>`;
1741-
const result = await api.sendIQ(stanza, null, false);
1746+
1747+
const result = await api.sendIQ(stanza, DISCO_INFO_TIMEOUT_ON_JOIN, false);
17421748
if (u.isErrorObject(result)) {
17431749
throw result;
17441750
}

src/headless/shared/api/send.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default {
1919
send(stanza) {
2020
const { api } = _converse;
2121
if (!api.connection.connected()) {
22+
// TODO: queue unsent messages and send once we're connected again
2223
log.warn("Not sending stanza because we're not connected!");
2324
log.warn(Strophe.serialize(stanza));
2425
return;

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ declare namespace _default {
6565
* @method api.disco.info
6666
* @param {string} jid The Jabber ID of the entity to query
6767
* @param {string} [node] A specific node identifier associated with the JID
68+
* @param {import('./types').DiscoInfoOptions} [options]
6869
* @returns {promise} Promise which resolves once we have a result from the server.
6970
*/
70-
export function info(jid: string, node?: string): Promise<any>;
71+
export function info(jid: string, node?: string, options?: import("./types").DiscoInfoOptions): Promise<any>;
7172
/**
7273
* Query for items associated with an XMPP entity
7374
*
@@ -190,11 +191,12 @@ declare namespace _default {
190191
* disco entity by refetching them from the server
191192
* @method api.disco.refresh
192193
* @param {string} jid The JID of the entity whose features are refreshed.
194+
* @param {import('./types').DiscoInfoOptions} [options]
193195
* @returns {Promise} A promise which resolves once the features have been refreshed
194196
* @example
195197
* await api.disco.refresh('room@conference.example.org');
196198
*/
197-
export function refresh(jid: string): Promise<any>;
199+
export function refresh(jid: string, options?: import("./types").DiscoInfoOptions): Promise<any>;
198200
/**
199201
* Return all the features associated with a disco entity
200202
*

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,14 @@ declare class DiscoEntity extends Model {
4646
getFeature(feature: string): Promise<this>;
4747
onFeatureAdded(feature: any): void;
4848
onFieldAdded(field: any): void;
49-
fetchFeatures(options: any): Promise<void>;
50-
queryInfo(): Promise<void>;
49+
/**
50+
* @param {import('./types').FetchEntityFeaturesOptions} options
51+
*/
52+
fetchFeatures(options: import("./types").FetchEntityFeaturesOptions): Promise<void>;
53+
/**
54+
* @param {import('./types').DiscoInfoOptions} [options]
55+
*/
56+
queryInfo(options?: import("./types").DiscoInfoOptions): Promise<void>;
5157
/**
5258
* @param {Element} stanza
5359
*/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type DiscoInfoOptions = {
2+
timeout?: number;
3+
};
4+
export type FetchEntityFeaturesOptions = {
5+
timeout?: number;
6+
ignore_cache?: boolean;
7+
};
8+
//# sourceMappingURL=types.d.ts.map

src/headless/types/plugins/muc/muc.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,10 @@ declare class MUC extends MUC_base {
539539
* Refresh the disco identity, features and fields for this {@link MUC}.
540540
* *features* are stored on the features {@link Model} attribute on this {@link MUC}.
541541
* *fields* are stored on the config {@link Model} attribute on this {@link MUC}.
542+
* @param {import('@converse/headless/plugins/disco/types').DiscoInfoOptions} [options]
542543
* @returns {Promise}
543544
*/
544-
refreshDiscoInfo(): Promise<any>;
545+
refreshDiscoInfo(options?: import("@converse/headless/plugins/disco/types").DiscoInfoOptions): Promise<any>;
545546
/**
546547
* Fetch the *extended* MUC info from the server and cache it locally
547548
* https://xmpp.org/extensions/xep-0045.html#disco-roominfo

0 commit comments

Comments
 (0)