From 62ef18a027d98a51cd3db9bce474069e7826e26a Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 20 Feb 2020 17:11:07 +0100 Subject: [PATCH] muc_fetch_members now accepts an array of affiliations --- CHANGES.md | 1 + docs/source/configuration.rst | 5 +++-- spec/muc.js | 29 +++++++++++++++++++++++++---- src/headless/converse-muc.js | 14 +++++++------- tests/utils.js | 10 +++++++--- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b504ca12c..13d0b270a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,6 +3,7 @@ ## 7.0.0 (Unreleased) +- [muc_fetch_members](https://conversejs.org/docs/html/configuration.html#muc-fetch-members) now also accepts an array of affiliations to fetch. - Replace Backbone with [Skeletor](https://github.com/skeletorjs/skeletor) - Start using [lit-html](https://lit-html.polymer-project.org/) instead of lodash for templating. - Bugfix. Handle stanza that clears the MUC subject diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index 9a16b2551..45d11f498 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -1079,6 +1079,8 @@ muc_fetch_members * Default: ``true`` +* Possible values: Array containing any of the following: ``['member', 'admin', 'owner']`` + Determines whether Converse.js will fetch the member lists for a MUC (multi-user chat) when the user first enters it. @@ -1091,8 +1093,7 @@ The member lists consists of three lists of users who have the affiliations ``member``, ``admin`` and ``owner`` respectively. By fetching member lists, Converse.js will always show these users as -participants of the MUC, which makes it behave a bit more like modern chat -apps. +participants of the MUC, giving them a permanent "presence" in the MUC. muc_history_max_stanzas diff --git a/spec/muc.js b/spec/muc.js index d41273ca8..4a91e81dd 100644 --- a/spec/muc.js +++ b/spec/muc.js @@ -362,12 +362,11 @@ ['rosterGroupsFetched'], {'muc_fetch_members': true}, async function (done, _converse) { - const sent_IQs = _converse.connection.IQ_stanzas; + let sent_IQs = _converse.connection.IQ_stanzas; const muc_jid = 'lounge@montague.lit'; - spyOn(_converse.ChatRoomOccupants.prototype, 'fetchMembers').and.callThrough(); await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo'); let view = _converse.chatboxviews.get(muc_jid); - expect(view.model.occupants.fetchMembers).toHaveBeenCalled(); + expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(3); // Check in reverse order that we requested all three lists const owner_iq = sent_IQs.pop(); @@ -387,11 +386,33 @@ ``+ ``+ ``); + view.close(); + _converse.connection.IQ_stanzas = []; + sent_IQs = _converse.connection.IQ_stanzas; _converse.muc_fetch_members = false; await test_utils.openAndEnterChatRoom(_converse, 'orchard@montague.lit', 'romeo'); view = _converse.chatboxviews.get('orchard@montague.lit'); - expect(view.model.occupants.fetchMembers.calls.count()).toBe(1); + expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(0); + await view.close(); + + _converse.connection.IQ_stanzas = []; + sent_IQs = _converse.connection.IQ_stanzas; + _converse.muc_fetch_members = ['admin']; + await test_utils.openAndEnterChatRoom(_converse, 'courtyard@montague.lit', 'romeo'); + view = _converse.chatboxviews.get('courtyard@montague.lit'); + expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(1); + expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation="admin"]')).length).toBe(1); + view.close(); + + _converse.connection.IQ_stanzas = []; + sent_IQs = _converse.connection.IQ_stanzas; + _converse.muc_fetch_members = ['owner']; + await test_utils.openAndEnterChatRoom(_converse, 'garden@montague.lit', 'romeo'); + view = _converse.chatboxviews.get('garden@montague.lit'); + expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation]')).length).toBe(1); + expect(sent_IQs.filter(iq => iq.querySelector('query item[affiliation="owner"]')).length).toBe(1); + view.close(); done(); })); diff --git a/src/headless/converse-muc.js b/src/headless/converse-muc.js index 57e5084e2..437b93a69 100644 --- a/src/headless/converse-muc.js +++ b/src/headless/converse-muc.js @@ -460,9 +460,7 @@ converse.plugins.add('converse-muc', { async onConnectionStatusChanged () { if (this.session.get('connection_status') === converse.ROOMSTATUS.ENTERED) { - if (_converse.muc_fetch_members) { - await this.occupants.fetchMembers(); - } + await this.occupants.fetchMembers(); await this.fetchMessages(); await this.clearMessageQueue(); /** @@ -1339,9 +1337,7 @@ converse.plugins.add('converse-muc', { const aff_lists = await Promise.all(all_affiliations.map(a => this.getAffiliationList(a))); const old_members = aff_lists.reduce((acc, val) => (u.isErrorObject(val) ? acc: [...val, ...acc]), []); await this.setAffiliations(muc_utils.computeAffiliationsDelta(true, false, members, old_members)); - if (_converse.muc_fetch_members) { - return this.occupants.fetchMembers(); - } + await this.occupants.fetchMembers(); }, /** @@ -2228,7 +2224,11 @@ converse.plugins.add('converse-muc', { }, async fetchMembers () { - const all_affiliations = ['member', 'admin', 'owner']; + const affs = _converse.muc_fetch_members; + const all_affiliations = Array.isArray(affs) ? affs : (affs ? ['member', 'admin', 'owner'] : []); + if (affs.length === 0) { + return; + } const aff_lists = await Promise.all(all_affiliations.map(a => this.chatroom.getAffiliationList(a))); const new_members = aff_lists.reduce((acc, val) => (u.isErrorObject(val) ? acc : [...val, ...acc]), []); const known_affiliations = all_affiliations.filter(a => !u.isErrorObject(aff_lists[all_affiliations.indexOf(a)])); diff --git a/tests/utils.js b/tests/utils.js index e266c102e..925c6030f 100644 --- a/tests/utils.js +++ b/tests/utils.js @@ -204,6 +204,9 @@ utils.returnMemberLists = async function (_converse, muc_jid, members=[], affiliations=['member', 'owner', 'admin']) { + if (affiliations.length === 0) { + return; + } const stanzas = _converse.connection.IQ_stanzas; if (affiliations.includes('member')) { @@ -302,9 +305,10 @@ await room_creation_promise; const view = _converse.chatboxviews.get(muc_jid); await u.waitUntil(() => (view.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED)); - if (_converse.muc_fetch_members) { - await utils.returnMemberLists(_converse, muc_jid, members); - } + + const affs = _converse.muc_fetch_members; + const all_affiliations = Array.isArray(affs) ? affs : (affs ? ['member', 'admin', 'owner'] : []); + await utils.returnMemberLists(_converse, muc_jid, members, all_affiliations); await view.model.messages.fetched; };