diff --git a/src/plugins/converse-rai.js b/src/plugins/converse-rai.js index fb72df1e5b2adb9de11c01901941379d87e29b6f..b7a1b25393e03f564db153579b697fcf897ea027 100644 --- a/src/plugins/converse-rai.js +++ b/src/plugins/converse-rai.js @@ -22,18 +22,57 @@ converse.plugins.add('converse-rai', { const conn_status = this.session.get('connection_status'); const { api } = this.__super__._converse; - if (this.get('hidden') && conn_status === converse.ROOMSTATUS.ENTERED) { - if (api.settings.get('muc_subscribe_to_rai') && this.getOwnAffiliation() !== 'none') { - if (conn_status !== converse.ROOMSTATUS.DISCONNECTED) { - this.sendMarkerForLastMessage('received', true); - await this.leave(); - await this.close(); - } - } + if (this.get('hidden') + && conn_status === converse.ROOMSTATUS.ENTERED + && api.settings.get('muc_subscribe_to_rai') + && this.getOwnAffiliation() !== 'none' + ) { + this.sendMarkerForLastMessage('received', true); + await this.leave(); + await this.close(); } else if (conn_status === converse.ROOMSTATUS.DISCONNECTED) { await this.rejoin(); } }, + + /** + * Subscribe to RAI if connected to the room, but it's hidden. + * @private + * @method _converse.ChatRoom#onConnectionStatusChanged + */ + async onConnectionStatusChanged() { + const conn_status = this.session.get('connection_status'); + const { api } = this.__super__._converse; + + if (this.get('hidden') + && conn_status === converse.ROOMSTATUS.ENTERED + && api.settings.get('muc_subscribe_to_rai') + && this.getOwnAffiliation() !== 'none' + ) { + await this.leave(); + await this.close(); + this.enableRAI(); + } else { + await this.__super__.onConnectionStatusChanged.apply(this, arguments); + } + }, + + /** + * Ensures that the user is subscribed to XEP-0437 Room Activity Indicators + * if `muc_subscribe_to_rai` is set to `true`. + * Only affiliated users can subscribe to RAI, but this method doesn't + * check whether the current user is affiliated because it's intended to be + * called after the MUC has been left and we don't have that information + * anymore. + * @private + * @method _converse.ChatRoom#enableRAI + */ + enableRAI() { + const { api } = this.__super__._converse; + if (api.settings.get('muc_subscribe_to_rai')) { + api.rooms.subscribe(this.get('jid')); + } + }, }, ChatBox: { /** @@ -55,10 +94,8 @@ converse.plugins.add('converse-rai', { const _converse = this._converse; const { api } = _converse; - // Register namespace Strophe.addNamespace('RAI', 'urn:xmpp:rai:0'); - // Register settings api.settings.extend({ muc_subscribe_to_rai: false, }); @@ -73,7 +110,8 @@ converse.plugins.add('converse-rai', { */ subscribe(jids) { if (!api.settings.get('muc_subscribe_to_rai')) { - console.error('Can\'t subscribe to RAI, this feature is not enabled'); + log.error(`You must enable the 'muc_subscribe_to_rai' option before subscribing to a MUC.`); + return; } if (typeof jids === 'string') { @@ -87,7 +125,7 @@ converse.plugins.add('converse-rai', { 'xmlns': Strophe.NS.RAI, }); api.send(rai); - console.log('Sent RAI stanza for muc_domain', muc_domain, rai.toString()); + log.debug(`Sent RAI stanza for domain "${muc_domain}"`); }); }, });