Make api.user.status API methods async

So that `_converse.xmppstatus` is defined before trying to set the
status.
This commit is contained in:
JC Brand 2020-09-04 12:51:03 +02:00
parent 4e7b29e417
commit c3d8f12050
8 changed files with 67 additions and 51 deletions

View File

@ -32,6 +32,7 @@ Soon we'll deprecate the latter, so prepare now.
- #2213: added CustomElement to converse.env - #2213: added CustomElement to converse.env
- Removed the mockups from the project. Recommended to use tests instead. - 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 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). - 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` - 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 - Bugfix. Handle stanza that clears the MUC subject

View File

@ -62,7 +62,8 @@ describe("Converse", function() {
describe("Automatic status change", 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; let i = 0;
// Usually initialized by registerIntervalHandler // Usually initialized by registerIntervalHandler
_converse.idle_seconds = 0; _converse.idle_seconds = 0;
@ -70,26 +71,26 @@ describe("Converse", function() {
_converse.api.settings.set('auto_away', 3); _converse.api.settings.set('auto_away', 3);
_converse.api.settings.set('auto_xa', 6); _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")) { while (i <= _converse.api.settings.get("auto_away")) {
_converse.onEverySecond(); i++; _converse.onEverySecond(); i++;
} }
expect(_converse.auto_changed_status).toBe(true); expect(_converse.auto_changed_status).toBe(true);
while (i <= _converse.auto_xa) { while (i <= _converse.auto_xa) {
expect(_converse.api.user.status.get()).toBe('away'); expect(await _converse.api.user.status.get()).toBe('away');
_converse.onEverySecond(); _converse.onEverySecond();
i++; 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); expect(_converse.auto_changed_status).toBe(true);
_converse.onUserActivity(); _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); expect(_converse.auto_changed_status).toBe(false);
// Check that it also works for the chat feature // Check that it also works for the chat feature
_converse.api.user.status.set('chat') await _converse.api.user.status.set('chat')
i = 0; i = 0;
while (i <= _converse.api.settings.get("auto_away")) { while (i <= _converse.api.settings.get("auto_away")) {
_converse.onEverySecond(); _converse.onEverySecond();
@ -97,36 +98,36 @@ describe("Converse", function() {
} }
expect(_converse.auto_changed_status).toBe(true); expect(_converse.auto_changed_status).toBe(true);
while (i <= _converse.auto_xa) { while (i <= _converse.auto_xa) {
expect(_converse.api.user.status.get()).toBe('away'); expect(await _converse.api.user.status.get()).toBe('away');
_converse.onEverySecond(); _converse.onEverySecond();
i++; 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); expect(_converse.auto_changed_status).toBe(true);
_converse.onUserActivity(); _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); expect(_converse.auto_changed_status).toBe(false);
// Check that it doesn't work for 'dnd' // Check that it doesn't work for 'dnd'
_converse.api.user.status.set('dnd'); await _converse.api.user.status.set('dnd');
i = 0; i = 0;
while (i <= _converse.api.settings.get("auto_away")) { while (i <= _converse.api.settings.get("auto_away")) {
_converse.onEverySecond(); _converse.onEverySecond();
i++; 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); expect(_converse.auto_changed_status).toBe(false);
while (i <= _converse.auto_xa) { while (i <= _converse.auto_xa) {
expect(_converse.api.user.status.get()).toBe('dnd'); expect(await _converse.api.user.status.get()).toBe('dnd');
_converse.onEverySecond(); _converse.onEverySecond();
i++; 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); expect(_converse.auto_changed_status).toBe(false);
_converse.onUserActivity(); _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); expect(_converse.auto_changed_status).toBe(false);
done(); done();
})); }));
@ -136,47 +137,51 @@ describe("Converse", function() {
describe("The \"status\" API", 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'); _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'); _converse.xmppstatus.set('status', 'dnd');
expect(_converse.api.user.status.get()).toBe('dnd'); expect(await _converse.api.user.status.get()).toBe('dnd');
done(); done();
})); }));
it("has a method for setting the user's availability", mock.initConverse((done, _converse) => { it("has a method for setting the user's availability", mock.initConverse(async (done, _converse) => {
_converse.api.user.status.set('away'); await _converse.api.user.status.set('away');
expect(_converse.xmppstatus.get('status')).toBe('away'); expect(await _converse.xmppstatus.get('status')).toBe('away');
_converse.api.user.status.set('dnd'); await _converse.api.user.status.set('dnd');
expect(_converse.xmppstatus.get('status')).toBe('dnd'); expect(await _converse.xmppstatus.get('status')).toBe('dnd');
_converse.api.user.status.set('xa'); await _converse.api.user.status.set('xa');
expect(_converse.xmppstatus.get('status')).toBe('xa'); expect(await _converse.xmppstatus.get('status')).toBe('xa');
_converse.api.user.status.set('chat'); await _converse.api.user.status.set('chat');
expect(_converse.xmppstatus.get('status')).toBe('chat'); expect(await _converse.xmppstatus.get('status')).toBe('chat');
expect(() => _converse.api.user.status.set('invalid')).toThrow( const promise = _converse.api.user.status.set('invalid')
new Error('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1') promise.catch(e => {
); expect(e.message).toBe('Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1');
done(); done();
});
})); }));
it("allows setting the status message as well", mock.initConverse((done, _converse) => { it("allows setting the status message as well", mock.initConverse(async (done, _converse) => {
_converse.api.user.status.set('away', "I'm in a meeting"); await _converse.api.user.status.set('away', "I'm in a meeting");
expect(_converse.xmppstatus.get('status')).toBe('away'); expect(_converse.xmppstatus.get('status')).toBe('away');
expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting"); expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
done(); done();
})); }));
it("has a method for getting the user's status message", mock.initConverse((done, _converse) => { it("has a method for getting the user's status message",
_converse.xmppstatus.set('status_message', undefined); mock.initConverse(['statusInitialized'], {}, async (done, _converse) => {
expect(_converse.api.user.status.message.get()).toBe(undefined); await _converse.xmppstatus.set('status_message', undefined);
_converse.xmppstatus.set('status_message', "I'm in a meeting"); expect(await _converse.api.user.status.message.get()).toBe(undefined);
expect(_converse.api.user.status.message.get()).toBe("I'm in a meeting"); 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(); 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.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"); expect(_converse.xmppstatus.get('status_message')).toBe("I'm in a meeting");
done(); done();
})); }));

View File

@ -8,7 +8,7 @@ const original_timeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
describe("Emojis", function () { describe("Emojis", function () {
describe("The emoji picker", 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)); afterEach(() => (jasmine.DEFAULT_TIMEOUT_INTERVAL = original_timeout));
it("can be opened by clicking a button in the chat toolbar", it("can be opened by clicking a button in the chat toolbar",

View File

@ -454,7 +454,7 @@ describe("Message Archive Management", function () {
})); }));
it("can be used to query for archived messages from a chat room", 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'; const room_jid = 'coven@chat.shakespeare.lit';
_converse.api.archive.query({'with': room_jid, 'groupchat': true}); _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", 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 room_jid = 'coven@chat.shakespeare.lit';
const promise = _converse.api.archive.query({'with': room_jid, 'groupchat': true, 'max':'10'}); const promise = _converse.api.archive.query({'with': room_jid, 'groupchat': true, 'max':'10'});

View File

@ -1,4 +1,4 @@
/*global mock */ /*global mock, converse */
const Strophe = converse.env.Strophe; const Strophe = converse.env.Strophe;
const u = converse.env.utils; const u = converse.env.utils;
@ -8,7 +8,8 @@ describe("XMPP Ping", function () {
describe("An IQ stanza", 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(` const ping = u.toStanza(`
<iq from="${_converse.domain}" <iq from="${_converse.domain}"
to="${_converse.jid}" id="s2c1" type="get"> to="${_converse.jid}" id="s2c1" type="get">

View File

@ -31,7 +31,7 @@ describe("A sent presence stanza", function () {
done(); 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'); let pres = _converse.xmppstatus.constructPresence('online', null, 'Hello world');
expect(pres.toLocaleString()).toBe( expect(pres.toLocaleString()).toBe(
`<presence xmlns="jabber:client">`+ `<presence xmlns="jabber:client">`+

View File

@ -1,4 +1,4 @@
/*global mock */ /*global mock, converse */
const u = converse.env.utils; const u = converse.env.utils;
@ -6,7 +6,7 @@ describe("The XMPPStatus model", function () {
it("won't send <show>online</show> when setting a custom status message", it("won't send <show>online</show> when setting a custom status message",
mock.initConverse(async (done, _converse) => { mock.initConverse(async (done, _converse) => {
_converse.xmppstatus.save({'status': 'online'}); await _converse.api.user.status.set('online');
spyOn(_converse.connection, 'send'); spyOn(_converse.connection, 'send');
_converse.api.user.status.message.set("I'm also happy!"); _converse.api.user.status.message.set("I'm also happy!");
await u.waitUntil(() => _converse.connection.send.calls.count()); await u.waitUntil(() => _converse.connection.send.calls.count());

View File

@ -229,6 +229,7 @@ converse.plugins.add('converse-status', {
if (_converse.shouldClearCache() && _converse.xmppstatus) { if (_converse.shouldClearCache() && _converse.xmppstatus) {
_converse.xmppstatus.destroy(); _converse.xmppstatus.destroy();
delete _converse.xmppstatus; delete _converse.xmppstatus;
api.promises.add(['statusInitialized']);
} }
}); });
@ -265,16 +266,19 @@ converse.plugins.add('converse-status', {
status: { status: {
/** /**
* Return the current user's availability status. * Return the current user's availability status.
* @async
* @method _converse.api.user.status.get * @method _converse.api.user.status.get
* @example _converse.api.user.status.get(); * @example _converse.api.user.status.get();
*/ */
get () { async get () {
await api.waitUntil('statusInitialized');
return _converse.xmppstatus.get('status'); return _converse.xmppstatus.get('status');
}, },
/** /**
* The user's status can be set to one of the following values: * The user's status can be set to one of the following values:
* *
* @async
* @method _converse.api.user.status.set * @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} value The user's chat status (e.g. 'away', 'dnd', 'offline', 'online', 'unavailable' or 'xa')
* @param {string} [message] A custom status message * @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');
* @example _converse.api.user.status.set('dnd', 'In a meeting'); * @example _converse.api.user.status.set('dnd', 'In a meeting');
*/ */
set (value, message) { async set (value, message) {
const data = {'status': value}; const data = {'status': value};
if (!Object.keys(_converse.STATUS_WEIGHTS).includes(value)) { if (!Object.keys(_converse.STATUS_WEIGHTS).includes(value)) {
throw new Error( throw new Error(
@ -292,6 +296,7 @@ converse.plugins.add('converse-status', {
if (isString(message)) { if (isString(message)) {
data.status_message = message; data.status_message = message;
} }
await api.waitUntil('statusInitialized');
_converse.xmppstatus.save(data); _converse.xmppstatus.save(data);
}, },
@ -303,19 +308,23 @@ converse.plugins.add('converse-status', {
*/ */
message: { message: {
/** /**
* @async
* @method _converse.api.user.status.message.get * @method _converse.api.user.status.message.get
* @returns {string} The status message * @returns {string} The status message
* @example const message = _converse.api.user.status.message.get() * @example const message = _converse.api.user.status.message.get()
*/ */
get () { async get () {
await api.waitUntil('statusInitialized');
return _converse.xmppstatus.get('status_message'); return _converse.xmppstatus.get('status_message');
}, },
/** /**
* @async
* @method _converse.api.user.status.message.set * @method _converse.api.user.status.message.set
* @param {string} status The status message * @param {string} status The status message
* @example _converse.api.user.status.message.set('In a meeting'); * @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 }); _converse.xmppstatus.save({ status_message: status });
} }
} }