From c3d8f12050475b2b541f6f4fcb4c3d18558ebe90 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Fri, 4 Sep 2020 12:51:03 +0200 Subject: [PATCH] Make `api.user.status` API methods async So that `_converse.xmppstatus` is defined before trying to set the status. --- CHANGES.md | 1 + spec/converse.js | 83 +++++++++++++++++---------------- spec/emojis.js | 2 +- spec/mam.js | 4 +- spec/ping.js | 5 +- spec/presence.js | 2 +- spec/xmppstatus.js | 4 +- src/headless/converse-status.js | 17 +++++-- 8 files changed, 67 insertions(+), 51 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d4ab8aaee..9f017e30f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -32,6 +32,7 @@ Soon we'll deprecate the latter, so prepare now. - #2213: added CustomElement to converse.env - Removed the mockups from the project. Recommended to use tests instead. - The API method `api.settings.update` has been deprecated in favor of `api.settings.extend`. +- The API methods under the `api.user.status` namespace are now asynchronous and need to be `await`ed. - Filter roster contacts via all available information (JID, nickname and VCard full name). - Allow ignoring of bootstrap modules at build using environment variable. For xample: `export BOOTSTRAP_IGNORE_MODULES="Modal,Dropdown" && make dist` - Bugfix. Handle stanza that clears the MUC subject diff --git a/spec/converse.js b/spec/converse.js index 4d4650105..5860cc9cd 100644 --- a/spec/converse.js +++ b/spec/converse.js @@ -62,7 +62,8 @@ describe("Converse", function() { describe("Automatic status change", function () { - it("happens when the client is idle for long enough", mock.initConverse((done, _converse) => { + it("happens when the client is idle for long enough", + mock.initConverse(['initialized'], {}, async (done, _converse) => { let i = 0; // Usually initialized by registerIntervalHandler _converse.idle_seconds = 0; @@ -70,26 +71,26 @@ describe("Converse", function() { _converse.api.settings.set('auto_away', 3); _converse.api.settings.set('auto_xa', 6); - expect(_converse.api.user.status.get()).toBe('online'); + expect(await _converse.api.user.status.get()).toBe('online'); while (i <= _converse.api.settings.get("auto_away")) { _converse.onEverySecond(); i++; } expect(_converse.auto_changed_status).toBe(true); while (i <= _converse.auto_xa) { - expect(_converse.api.user.status.get()).toBe('away'); + expect(await _converse.api.user.status.get()).toBe('away'); _converse.onEverySecond(); i++; } - expect(_converse.api.user.status.get()).toBe('xa'); + expect(await _converse.api.user.status.get()).toBe('xa'); expect(_converse.auto_changed_status).toBe(true); _converse.onUserActivity(); - expect(_converse.api.user.status.get()).toBe('online'); + expect(await _converse.api.user.status.get()).toBe('online'); expect(_converse.auto_changed_status).toBe(false); // Check that it also works for the chat feature - _converse.api.user.status.set('chat') + await _converse.api.user.status.set('chat') i = 0; while (i <= _converse.api.settings.get("auto_away")) { _converse.onEverySecond(); @@ -97,36 +98,36 @@ describe("Converse", function() { } expect(_converse.auto_changed_status).toBe(true); while (i <= _converse.auto_xa) { - expect(_converse.api.user.status.get()).toBe('away'); + expect(await _converse.api.user.status.get()).toBe('away'); _converse.onEverySecond(); i++; } - expect(_converse.api.user.status.get()).toBe('xa'); + expect(await _converse.api.user.status.get()).toBe('xa'); expect(_converse.auto_changed_status).toBe(true); _converse.onUserActivity(); - expect(_converse.api.user.status.get()).toBe('online'); + expect(await _converse.api.user.status.get()).toBe('online'); expect(_converse.auto_changed_status).toBe(false); // Check that it doesn't work for 'dnd' - _converse.api.user.status.set('dnd'); + await _converse.api.user.status.set('dnd'); i = 0; while (i <= _converse.api.settings.get("auto_away")) { _converse.onEverySecond(); i++; } - expect(_converse.api.user.status.get()).toBe('dnd'); + expect(await _converse.api.user.status.get()).toBe('dnd'); expect(_converse.auto_changed_status).toBe(false); while (i <= _converse.auto_xa) { - expect(_converse.api.user.status.get()).toBe('dnd'); + expect(await _converse.api.user.status.get()).toBe('dnd'); _converse.onEverySecond(); i++; } - expect(_converse.api.user.status.get()).toBe('dnd'); + expect(await _converse.api.user.status.get()).toBe('dnd'); expect(_converse.auto_changed_status).toBe(false); _converse.onUserActivity(); - expect(_converse.api.user.status.get()).toBe('dnd'); + expect(await _converse.api.user.status.get()).toBe('dnd'); expect(_converse.auto_changed_status).toBe(false); done(); })); @@ -136,47 +137,51 @@ describe("Converse", function() { describe("The \"status\" API", function () { - it("has a method for getting the user's availability", mock.initConverse((done, _converse) => { + it("has a method for getting the user's availability", + mock.initConverse(['statusInitialized'], {}, async(done, _converse) => { _converse.xmppstatus.set('status', 'online'); - expect(_converse.api.user.status.get()).toBe('online'); + expect(await _converse.api.user.status.get()).toBe('online'); _converse.xmppstatus.set('status', 'dnd'); - expect(_converse.api.user.status.get()).toBe('dnd'); + expect(await _converse.api.user.status.get()).toBe('dnd'); done(); })); - it("has a method for setting the user's availability", mock.initConverse((done, _converse) => { - _converse.api.user.status.set('away'); - expect(_converse.xmppstatus.get('status')).toBe('away'); - _converse.api.user.status.set('dnd'); - expect(_converse.xmppstatus.get('status')).toBe('dnd'); - _converse.api.user.status.set('xa'); - expect(_converse.xmppstatus.get('status')).toBe('xa'); - _converse.api.user.status.set('chat'); - expect(_converse.xmppstatus.get('status')).toBe('chat'); - expect(() => _converse.api.user.status.set('invalid')).toThrow( - new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1') - ); - done(); + it("has a method for setting the user's availability", mock.initConverse(async (done, _converse) => { + await _converse.api.user.status.set('away'); + expect(await _converse.xmppstatus.get('status')).toBe('away'); + await _converse.api.user.status.set('dnd'); + expect(await _converse.xmppstatus.get('status')).toBe('dnd'); + await _converse.api.user.status.set('xa'); + expect(await _converse.xmppstatus.get('status')).toBe('xa'); + await _converse.api.user.status.set('chat'); + expect(await _converse.xmppstatus.get('status')).toBe('chat'); + const promise = _converse.api.user.status.set('invalid') + promise.catch(e => { + expect(e.message).toBe('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1'); + done(); + }); })); - it("allows setting the status message as well", mock.initConverse((done, _converse) => { - _converse.api.user.status.set('away', "I'm in a meeting"); + it("allows setting the status message as well", mock.initConverse(async (done, _converse) => { + await _converse.api.user.status.set('away', "I'm in a meeting"); expect(_converse.xmppstatus.get('status')).toBe('away'); expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting"); done(); })); - it("has a method for getting the user's status message", mock.initConverse((done, _converse) => { - _converse.xmppstatus.set('status_message', undefined); - expect(_converse.api.user.status.message.get()).toBe(undefined); - _converse.xmppstatus.set('status_message', "I'm in a meeting"); - expect(_converse.api.user.status.message.get()).toBe("I'm in a meeting"); + it("has a method for getting the user's status message", + mock.initConverse(['statusInitialized'], {}, async (done, _converse) => { + await _converse.xmppstatus.set('status_message', undefined); + expect(await _converse.api.user.status.message.get()).toBe(undefined); + await _converse.xmppstatus.set('status_message', "I'm in a meeting"); + expect(await _converse.api.user.status.message.get()).toBe("I'm in a meeting"); done(); })); - it("has a method for setting the user's status message", mock.initConverse((done, _converse) => { + it("has a method for setting the user's status message", + mock.initConverse(['statusInitialized'], {}, async (done, _converse) => { _converse.xmppstatus.set('status_message', undefined); - _converse.api.user.status.message.set("I'm in a meeting"); + await _converse.api.user.status.message.set("I'm in a meeting"); expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting"); done(); })); diff --git a/spec/emojis.js b/spec/emojis.js index 703478fc1..627db8b68 100644 --- a/spec/emojis.js +++ b/spec/emojis.js @@ -8,7 +8,7 @@ const original_timeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; describe("Emojis", function () { describe("The emoji picker", function () { - beforeEach(() => (jasmine.DEFAULT_TIMEOUT_INTERVAL = 7000)); + beforeEach(() => (jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000)); afterEach(() => (jasmine.DEFAULT_TIMEOUT_INTERVAL = original_timeout)); it("can be opened by clicking a button in the chat toolbar", diff --git a/spec/mam.js b/spec/mam.js index 779300bc4..b6af3b164 100644 --- a/spec/mam.js +++ b/spec/mam.js @@ -454,7 +454,7 @@ describe("Message Archive Management", function () { })); it("can be used to query for archived messages from a chat room", - mock.initConverse([], {}, async function (done, _converse) { + mock.initConverse(['statusInitialized'], {}, async function (done, _converse) { const room_jid = 'coven@chat.shakespeare.lit'; _converse.api.archive.query({'with': room_jid, 'groupchat': true}); @@ -479,7 +479,7 @@ describe("Message Archive Management", function () { })); it("checks whether returned MAM messages from a MUC room are from the right JID", - mock.initConverse([], {}, async function (done, _converse) { + mock.initConverse(['statusInitialized'], {}, async function (done, _converse) { const room_jid = 'coven@chat.shakespeare.lit'; const promise = _converse.api.archive.query({'with': room_jid, 'groupchat': true, 'max':'10'}); diff --git a/spec/ping.js b/spec/ping.js index 5bd5ae10f..e15ecc9a8 100644 --- a/spec/ping.js +++ b/spec/ping.js @@ -1,4 +1,4 @@ -/*global mock */ +/*global mock, converse */ const Strophe = converse.env.Strophe; const u = converse.env.utils; @@ -8,7 +8,8 @@ describe("XMPP Ping", function () { describe("An IQ stanza", function () { - it("is returned when converse.js gets pinged", mock.initConverse((done, _converse) => { + it("is returned when converse.js gets pinged", + mock.initConverse(['statusInitialized'], {}, (done, _converse) => { const ping = u.toStanza(` diff --git a/spec/presence.js b/spec/presence.js index 2a114b54b..2a212417d 100644 --- a/spec/presence.js +++ b/spec/presence.js @@ -31,7 +31,7 @@ describe("A sent presence stanza", function () { done(); })); - it("has a given priority", mock.initConverse((done, _converse) => { + it("has a given priority", mock.initConverse(['statusInitialized'], {}, (done, _converse) => { let pres = _converse.xmppstatus.constructPresence('online', null, 'Hello world'); expect(pres.toLocaleString()).toBe( ``+ diff --git a/spec/xmppstatus.js b/spec/xmppstatus.js index 116329aa9..94acdec0e 100644 --- a/spec/xmppstatus.js +++ b/spec/xmppstatus.js @@ -1,4 +1,4 @@ -/*global mock */ +/*global mock, converse */ const u = converse.env.utils; @@ -6,7 +6,7 @@ describe("The XMPPStatus model", function () { it("won't send online when setting a custom status message", mock.initConverse(async (done, _converse) => { - _converse.xmppstatus.save({'status': 'online'}); + await _converse.api.user.status.set('online'); spyOn(_converse.connection, 'send'); _converse.api.user.status.message.set("I'm also happy!"); await u.waitUntil(() => _converse.connection.send.calls.count()); diff --git a/src/headless/converse-status.js b/src/headless/converse-status.js index 582d25741..9d699279e 100644 --- a/src/headless/converse-status.js +++ b/src/headless/converse-status.js @@ -229,6 +229,7 @@ converse.plugins.add('converse-status', { if (_converse.shouldClearCache() && _converse.xmppstatus) { _converse.xmppstatus.destroy(); delete _converse.xmppstatus; + api.promises.add(['statusInitialized']); } }); @@ -265,16 +266,19 @@ converse.plugins.add('converse-status', { status: { /** * Return the current user's availability status. + * @async * @method _converse.api.user.status.get * @example _converse.api.user.status.get(); */ - get () { + async get () { + await api.waitUntil('statusInitialized'); return _converse.xmppstatus.get('status'); }, /** * The user's status can be set to one of the following values: * + * @async * @method _converse.api.user.status.set * @param {string} value The user's chat status (e.g. 'away', 'dnd', 'offline', 'online', 'unavailable' or 'xa') * @param {string} [message] A custom status message @@ -282,7 +286,7 @@ converse.plugins.add('converse-status', { * @example _converse.api.user.status.set('dnd'); * @example _converse.api.user.status.set('dnd', 'In a meeting'); */ - set (value, message) { + async set (value, message) { const data = {'status': value}; if (!Object.keys(_converse.STATUS_WEIGHTS).includes(value)) { throw new Error( @@ -292,6 +296,7 @@ converse.plugins.add('converse-status', { if (isString(message)) { data.status_message = message; } + await api.waitUntil('statusInitialized'); _converse.xmppstatus.save(data); }, @@ -303,19 +308,23 @@ converse.plugins.add('converse-status', { */ message: { /** + * @async * @method _converse.api.user.status.message.get * @returns {string} The status message * @example const message = _converse.api.user.status.message.get() */ - get () { + async get () { + await api.waitUntil('statusInitialized'); return _converse.xmppstatus.get('status_message'); }, /** + * @async * @method _converse.api.user.status.message.set * @param {string} status The status message * @example _converse.api.user.status.message.set('In a meeting'); */ - set (status) { + async set (status) { + await api.waitUntil('statusInitialized'); _converse.xmppstatus.save({ status_message: status }); } }