From a9ce9acff2e6db68ff133ced7f3c40a9ac79a6cf Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 26 Mar 2019 11:31:11 +0100 Subject: [PATCH] Make sure `nickname` config option takes precedence above other nickname sources. --- CHANGES.md | 1 + dist/converse.js | 79 ++++++++++++++++----------------- docs/source/configuration.rst | 4 ++ spec/muc.js | 44 ++++++++++++++++++ spec/roster.js | 2 +- src/converse-muc-views.js | 9 +--- src/headless/converse-core.js | 7 ++- src/headless/converse-muc.js | 35 ++++++++------- src/headless/converse-roster.js | 26 +++++------ 9 files changed, 124 insertions(+), 83 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 34f91ab2a..0537064e5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ - New config setting [show_client_info](https://conversejs.org/docs/html/configuration.html#show-client-info) - Render inline images served over HTTP if Converse itself was loaded on an unsecured (HTTP) page. - Document new API method [sendMessage](https://conversejs.org/docs/html/api/-_converse.ChatBox.html#sendMessage) +- Make sure `nickname` passed in via `_converse.initialize` has first preference as MUC nickname - #1149: With `xhr_user_search_url`, contact requests are not being sent out - #1213: Switch roster filter input and icons - #1327: fix False mentions positives in URLs and Email addresses diff --git a/dist/converse.js b/dist/converse.js index 71fced76c..e3cb80955 100644 --- a/dist/converse.js +++ b/dist/converse.js @@ -53767,12 +53767,6 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].plugins this.roomspanel.model.fetch(); this.el.querySelector('.controlbox-pane').insertAdjacentElement('beforeEnd', this.roomspanel.render().el); - if (!this.roomspanel.model.get('nick')) { - this.roomspanel.model.save({ - nick: _converse.xmppstatus.vcard.get('nickname') || Strophe.getNodeFromJid(_converse.bare_jid) - }); - } - _converse.emit('roomsPanelRendered'); }, @@ -55017,7 +55011,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].plugins }, onReservedNickNotFound(message) { - const nick = this.model.getDefaultNick(); + const nick = _converse.getDefaultMUCNickname(); if (nick) { this.join(nick); @@ -55038,7 +55032,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_6__["default"].plugins if (_converse.muc_nickname_from_jid) { const nick = presence.getAttribute('from').split('/')[1]; - if (nick === this.model.getDefaultNick()) { + if (nick === _converse.getDefaultMUCNickname()) { this.join(nick + '-2'); } else { const del = nick.lastIndexOf("-"); @@ -64109,11 +64103,12 @@ _converse.initialize = async function (settings, callback) { _converse.onStatusInitialized(reconnecting); } else { const id = `converse.xmppstatus-${_converse.bare_jid}`; - this.xmppstatus = new this.XMPPStatus({ + _converse.xmppstatus = new this.XMPPStatus({ 'id': id }); - this.xmppstatus.browserStorage = new Backbone.BrowserStorage.session(id); - this.xmppstatus.fetch({ + _converse.xmppstatus.browserStorage = new Backbone.BrowserStorage.session(id); + + _converse.xmppstatus.fetch({ 'success': _lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.partial(_converse.onStatusInitialized, reconnecting), 'error': _lodash_noconflict__WEBPACK_IMPORTED_MODULE_4___default.a.partial(_converse.onStatusInitialized, reconnecting) }); @@ -66646,6 +66641,20 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc _converse.router.route('converse/room?jid=:jid', openRoom); + _converse.getDefaultMUCNickname = function () { + if (!_converse.xmppstatus) { + throw new Error("Can't call _converse.getNickname before the statusInitialized has been fired."); + } + + const nick = _converse.nickname || _converse.xmppstatus.vcard.get('nickname'); + + if (nick) { + return nick; + } else if (_converse.muc_nickname_from_jid) { + return Strophe.unescapeNode(Strophe.getNodeFromJid(_converse.bare_jid)); + } + }; + _converse.openChatRoom = function (jid, settings, bring_to_foreground) { /* Opens a groupchat, making sure that certain attributes * are correct, for example that the "type" is set to @@ -67382,21 +67391,6 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc this.getJidsWithAffiliations(affiliations).then(old_members => this.setAffiliations(deltaFunc(members, old_members))).then(() => this.occupants.fetchMembers()).catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR)); }, - getDefaultNick() { - /* The default nickname (used when muc_nickname_from_jid is true) - * is the node part of the user's JID. - * We put this in a separate method so that it can be - * overridden by plugins. - */ - const nick = _converse.xmppstatus.vcard.get('nickname'); - - if (nick) { - return nick; - } else if (_converse.muc_nickname_from_jid) { - return Strophe.unescapeNode(Strophe.getNodeFromJid(_converse.bare_jid)); - } - }, - async checkForReservedNick() { /* Use service-discovery to ask the XMPP server whether * this user has a reserved nickname for this groupchat. @@ -67956,8 +67950,11 @@ _converse_core__WEBPACK_IMPORTED_MODULE_3__["default"].plugins.add('converse-muc }); _converse.RoomsPanelModel = Backbone.Model.extend({ - defaults: { - 'muc_domain': '' + defaults: function defaults() { + return { + 'muc_domain': '', + 'nick': _converse.getDefaultMUCNickname() + }; } }); @@ -68831,11 +68828,11 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins subscribe(message) { /* Send a presence subscription request to this roster contact - * - * Parameters: - * (String) message - An optional message to explain the - * reason for the subscription request. - */ + * + * Parameters: + * (String) message - An optional message to explain the + * reason for the subscription request. + */ const pres = $pres({ to: this.get('jid'), type: "subscribe" @@ -68845,7 +68842,7 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins pres.c("status").t(message).up(); } - const nick = _converse.xmppstatus.vcard.get('nickname') || _converse.xmppstatus.vcard.get('fullname'); + const nick = _converse.nickname || _converse.xmppstatus.vcard.get('nickname') || _converse.xmppstatus.vcard.get('fullname'); if (nick) { pres.c('nick', { @@ -68862,10 +68859,10 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins ackSubscribe() { /* Upon receiving the presence stanza of type "subscribed", - * the user SHOULD acknowledge receipt of that subscription - * state notification by sending a presence stanza of type - * "subscribe" to the contact - */ + * the user SHOULD acknowledge receipt of that subscription + * state notification by sending a presence stanza of type + * "subscribe" to the contact + */ _converse.api.send($pres({ 'type': 'subscribe', 'to': this.get('jid') @@ -68892,9 +68889,9 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins unauthorize(message) { /* Unauthorize this contact's presence subscription - * Parameters: - * (String) message - Optional message to send to the person being unauthorized - */ + * Parameters: + * (String) message - Optional message to send to the person being unauthorized + */ _converse.rejectPresenceSubscription(this.get('jid'), message); return this; diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index 3b0ad2c43..2409f2f97 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -1042,6 +1042,10 @@ This setting allows you to specify the nickname for the current user. The nickname will be included in presence requests to other users and will also be used as the default nickname when entering MUC chatrooms. +This value will have first preference (i.e. used instead of any other nickname source, +such as the VCard `nickname` value). + + notify_all_room_messages ------------------------ diff --git a/spec/muc.js b/spec/muc.js index 0f19a4bb5..4432748bb 100644 --- a/spec/muc.js +++ b/spec/muc.js @@ -3971,6 +3971,11 @@ let name_input = modal.el.querySelector('input[name="chatroom"]'); expect(name_input.placeholder).toBe('name@conference.example.org'); + const label_nick = modal.el.querySelector('label[for="nickname"]'); + expect(label_nick.textContent).toBe('Optional nickname:'); + const nick_input = modal.el.querySelector('input[name="nickname"]'); + expect(nick_input.value).toBe(''); + expect(modal.el.querySelector('.modal-title').textContent).toBe('Enter a new Groupchat'); spyOn(_converse.ChatRoom.prototype, 'getRoomFeatures').and.callFake(() => Promise.resolve()); roomspanel.delegateEvents(); // We need to rebind all events otherwise our spy won't be called @@ -3988,6 +3993,45 @@ done(); })); + it("uses the JID node if muc_nickname_from_jid is set to true", + mock.initConverse( + null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'muc_nickname_from_jid': true}, + async function (done, _converse) { + + test_utils.openControlBox(); + await test_utils.waitForRoster(_converse, 'current', 0); + const roomspanel = _converse.chatboxviews.get('controlbox').roomspanel; + roomspanel.el.querySelector('.show-add-muc-modal').click(); + test_utils.closeControlBox(_converse); + const modal = roomspanel.add_room_modal; + await test_utils.waitUntil(() => u.isVisible(modal.el), 1000) + const label_nick = modal.el.querySelector('label[for="nickname"]'); + expect(label_nick.textContent).toBe('Optional nickname:'); + const nick_input = modal.el.querySelector('input[name="nickname"]'); + expect(nick_input.value).toBe('dummy'); + done(); + })); + + + it("uses the nickname passed in to converse.initialize", + mock.initConverse( + null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'nickname': 'st.nick'}, + async function (done, _converse) { + + test_utils.openControlBox(); + await test_utils.waitForRoster(_converse, 'current', 0); + const roomspanel = _converse.chatboxviews.get('controlbox').roomspanel; + roomspanel.el.querySelector('.show-add-muc-modal').click(); + test_utils.closeControlBox(_converse); + const modal = roomspanel.add_room_modal; + await test_utils.waitUntil(() => u.isVisible(modal.el), 1000) + const label_nick = modal.el.querySelector('label[for="nickname"]'); + expect(label_nick.textContent).toBe('Optional nickname:'); + const nick_input = modal.el.querySelector('input[name="nickname"]'); + expect(nick_input.value).toBe('st.nick'); + done(); + })); + it("doesn't require the domain when muc_domain is set", mock.initConverse( null, ['rosterGroupsFetched', 'chatBoxesFetched'], {'muc_domain': 'muc.example.org'}, diff --git a/spec/roster.js b/spec/roster.js index d318d6ad0..cc4f7ddaf 100644 --- a/spec/roster.js +++ b/spec/roster.js @@ -1262,7 +1262,7 @@ */ expect(_converse.roster.pluck('jid').length).toBe(0); - var stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'}); + let stanza = $pres({from: 'data@enterprise/resource', type: 'subscribe'}); _converse.connection._dataRecv(test_utils.createRequest(stanza)); test_utils.waitUntil(function () { return $('a:contains("Contact requests")').length; diff --git a/src/converse-muc-views.js b/src/converse-muc-views.js index f888b2edb..9b756a7b4 100644 --- a/src/converse-muc-views.js +++ b/src/converse-muc-views.js @@ -77,11 +77,6 @@ converse.plugins.add('converse-muc-views', { this.el.querySelector('.controlbox-pane').insertAdjacentElement( 'beforeEnd', this.roomspanel.render().el); - if (!this.roomspanel.model.get('nick')) { - this.roomspanel.model.save({ - nick: _converse.xmppstatus.vcard.get('nickname') || Strophe.getNodeFromJid(_converse.bare_jid) - }); - } _converse.emit('roomsPanelRendered'); }, @@ -1259,7 +1254,7 @@ converse.plugins.add('converse-muc-views', { }, onReservedNickNotFound (message) { - const nick = this.model.getDefaultNick(); + const nick = _converse.getDefaultMUCNickname(); if (nick) { this.join(nick); } else { @@ -1278,7 +1273,7 @@ converse.plugins.add('converse-muc-views', { */ if (_converse.muc_nickname_from_jid) { const nick = presence.getAttribute('from').split('/')[1]; - if (nick === this.model.getDefaultNick()) { + if (nick === _converse.getDefaultMUCNickname()) { this.join(nick + '-2'); } else { const del= nick.lastIndexOf("-"); diff --git a/src/headless/converse-core.js b/src/headless/converse-core.js index ae165afaf..8936dfd07 100644 --- a/src/headless/converse-core.js +++ b/src/headless/converse-core.js @@ -108,7 +108,6 @@ _converse.keycodes = { META_RIGHT: 93 }; - // Module-level constants _converse.STATUS_WEIGHTS = { 'offline': 6, @@ -847,9 +846,9 @@ _converse.initialize = async function (settings, callback) { _converse.onStatusInitialized(reconnecting); } else { const id = `converse.xmppstatus-${_converse.bare_jid}`; - this.xmppstatus = new this.XMPPStatus({'id': id}); - this.xmppstatus.browserStorage = new Backbone.BrowserStorage.session(id); - this.xmppstatus.fetch({ + _converse.xmppstatus = new this.XMPPStatus({'id': id}); + _converse.xmppstatus.browserStorage = new Backbone.BrowserStorage.session(id); + _converse.xmppstatus.fetch({ 'success': _.partial(_converse.onStatusInitialized, reconnecting), 'error': _.partial(_converse.onStatusInitialized, reconnecting) }); diff --git a/src/headless/converse-muc.js b/src/headless/converse-muc.js index d3b3a0560..cb19def27 100644 --- a/src/headless/converse-muc.js +++ b/src/headless/converse-muc.js @@ -140,6 +140,18 @@ converse.plugins.add('converse-muc', { _converse.router.route('converse/room?jid=:jid', openRoom); + _converse.getDefaultMUCNickname = function () { + if (!_converse.xmppstatus) { + throw new Error("Can't call _converse.getNickname before the statusInitialized has been fired."); + } + const nick = _converse.nickname || _converse.xmppstatus.vcard.get('nickname'); + if (nick) { + return nick; + } else if (_converse.muc_nickname_from_jid) { + return Strophe.unescapeNode(Strophe.getNodeFromJid(_converse.bare_jid)); + } + } + _converse.openChatRoom = function (jid, settings, bring_to_foreground) { /* Opens a groupchat, making sure that certain attributes * are correct, for example that the "type" is set to @@ -777,20 +789,6 @@ converse.plugins.add('converse-muc', { .catch(_.partial(_converse.log, _, Strophe.LogLevel.ERROR)); }, - getDefaultNick () { - /* The default nickname (used when muc_nickname_from_jid is true) - * is the node part of the user's JID. - * We put this in a separate method so that it can be - * overridden by plugins. - */ - const nick = _converse.xmppstatus.vcard.get('nickname'); - if (nick) { - return nick; - } else if (_converse.muc_nickname_from_jid) { - return Strophe.unescapeNode(Strophe.getNodeFromJid(_converse.bare_jid)); - } - }, - async checkForReservedNick () { /* Use service-discovery to ask the XMPP server whether * this user has a reserved nickname for this groupchat. @@ -1274,9 +1272,12 @@ converse.plugins.add('converse-muc', { _converse.RoomsPanelModel = Backbone.Model.extend({ - defaults: { - 'muc_domain': '', - }, + defaults: function () { + return { + 'muc_domain': '', + 'nick': _converse.getDefaultMUCNickname() + } + } }); diff --git a/src/headless/converse-roster.js b/src/headless/converse-roster.js index cb89778a6..3d65b6a43 100644 --- a/src/headless/converse-roster.js +++ b/src/headless/converse-roster.js @@ -245,16 +245,16 @@ converse.plugins.add('converse-roster', { subscribe (message) { /* Send a presence subscription request to this roster contact - * - * Parameters: - * (String) message - An optional message to explain the - * reason for the subscription request. - */ + * + * Parameters: + * (String) message - An optional message to explain the + * reason for the subscription request. + */ const pres = $pres({to: this.get('jid'), type: "subscribe"}); if (message && message !== "") { pres.c("status").t(message).up(); } - const nick = _converse.xmppstatus.vcard.get('nickname') || _converse.xmppstatus.vcard.get('fullname'); + const nick = _converse.nickname || _converse.xmppstatus.vcard.get('nickname') || _converse.xmppstatus.vcard.get('fullname'); if (nick) { pres.c('nick', {'xmlns': Strophe.NS.NICK}).t(nick).up(); } @@ -265,10 +265,10 @@ converse.plugins.add('converse-roster', { ackSubscribe () { /* Upon receiving the presence stanza of type "subscribed", - * the user SHOULD acknowledge receipt of that subscription - * state notification by sending a presence stanza of type - * "subscribe" to the contact - */ + * the user SHOULD acknowledge receipt of that subscription + * state notification by sending a presence stanza of type + * "subscribe" to the contact + */ _converse.api.send($pres({ 'type': 'subscribe', 'to': this.get('jid') @@ -291,9 +291,9 @@ converse.plugins.add('converse-roster', { unauthorize (message) { /* Unauthorize this contact's presence subscription - * Parameters: - * (String) message - Optional message to send to the person being unauthorized - */ + * Parameters: + * (String) message - Optional message to send to the person being unauthorized + */ _converse.rejectPresenceSubscription(this.get('jid'), message); return this; },