Commit c3d8f120 authored by JC Brand's avatar JC Brand

Make `api.user.status` API methods async

So that `_converse.xmppstatus` is defined before trying to set the
status.
parent 4e7b29e4
......@@ -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
......
......@@ -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();
}));
......
......@@ -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",
......
......@@ -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'});
......
/*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(`
<iq from="${_converse.domain}"
to="${_converse.jid}" id="s2c1" type="get">
......
......@@ -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(
`<presence xmlns="jabber:client">`+
......
/*global mock */
/*global mock, converse */
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",
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());
......
......@@ -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 });
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment