Commit b6d5077d authored by JC Brand's avatar JC Brand

Refactor `_converse.XMPPStatus` out into `headless/converse-status.js`

Also move some other methods out of `converse-core` into the plugins
that use them.
parent 1a7f58b5
......@@ -10,6 +10,7 @@
- Initial support for sending custom emojis. Currently only between Converse
instances. Still working out a wire protocol for compatibility with other clients.
To add custom emojis, edit the `emojis.json` file.
- Refactor some presence and status handling code from `converse-core` into `@converse/headless/converse-status`.
### Breaking changes
......@@ -26,6 +27,7 @@
* `_converse.api.rooms.create`
- The `show_only_online_users` setting has been removed.
- The order of certain events have now changed: `statusInitialized` is now triggered after `initialized` and `connected` and `reconnected`.
## 5.0.4 (2019-10-08)
- New config option [allow_message_corrections](https://conversejs.org/docs/html/configuration.html#allow-message-corrections)
......
......@@ -1138,15 +1138,14 @@
describe("A Message Counter", function () {
it("is incremented when the message is received and the window is not focused",
mock.initConverse(
['rosterGroupsFetched'], {},
async function (done, _converse) {
mock.initConverse(
['rosterGroupsFetched'], {},
async function (done, _converse) {
await test_utils.waitForRoster(_converse, 'current');
test_utils.openControlBox();
expect(_converse.msg_counter).toBe(0);
expect(document.title).toBe('Converse Tests');
const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const view = await test_utils.openChatBoxFor(_converse, sender_jid)
......@@ -1170,7 +1169,7 @@
await new Promise(resolve => view.once('messageInserted', resolve));
expect(_converse.incrementMsgCounter).toHaveBeenCalled();
expect(_converse.clearMsgCounter).not.toHaveBeenCalled();
expect(_converse.msg_counter).toBe(1);
expect(document.title).toBe('Messages (1) Converse Tests');
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
_converse.windowSate = previous_state;
done();
......@@ -1199,7 +1198,7 @@
await test_utils.waitForRoster(_converse, 'current');
test_utils.openControlBox();
expect(_converse.msg_counter).toBe(0);
expect(document.title).toBe('Converse Tests');
spyOn(_converse, 'incrementMsgCounter').and.callThrough();
_converse.saveWindowState(null, 'focus');
const message = 'This message will not increment the message counter';
......@@ -1213,7 +1212,7 @@
.c('active', {'xmlns': Strophe.NS.CHATSTATES}).tree();
await _converse.chatboxes.onMessage(msg);
expect(_converse.incrementMsgCounter).not.toHaveBeenCalled();
expect(_converse.msg_counter).toBe(0);
expect(document.title).toBe('Converse Tests');
done();
}));
......@@ -1224,7 +1223,7 @@
await test_utils.waitForRoster(_converse, 'current');
// initial state
expect(_converse.msg_counter).toBe(0);
expect(document.title).toBe('Converse Tests');
const message = 'This message will always increment the message counter from zero',
sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit',
msgFactory = function () {
......@@ -1244,12 +1243,12 @@
_converse.chatboxes.onMessage(msgFactory());
await u.waitUntil(() => _converse.api.chats.get().length === 2)
let view = _converse.chatboxviews.get(sender_jid);
expect(_converse.msg_counter).toBe(1);
expect(document.title).toBe('Messages (1) Converse Tests');
// come back to converse-chat page
_converse.saveWindowState(null, 'focus');
expect(u.isVisible(view.el)).toBeTruthy();
expect(_converse.msg_counter).toBe(0);
expect(document.title).toBe('Converse Tests');
// close chatbox and leave converse-chat page again
view.close();
......@@ -1260,7 +1259,7 @@
await u.waitUntil(() => _converse.api.chats.get().length === 2)
view = _converse.chatboxviews.get(sender_jid);
expect(u.isVisible(view.el)).toBeTruthy();
expect(_converse.msg_counter).toBe(1);
expect(document.title).toBe('Messages (1) Converse Tests');
done();
}));
});
......
......@@ -50,16 +50,17 @@
await test_utils.waitForRoster(_converse, 'current', 1);
iq = IQ_stanzas.pop();
IQ_stanzas.pop();
const disco_iq = IQ_stanzas.pop();
expect(Strophe.serialize(disco_iq)).toBe(
`<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
`<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`);
iq = IQ_stanzas.pop();
expect(Strophe.serialize(iq)).toBe(
`<iq from="romeo@montague.lit/orchard" id="${iq.getAttribute('id')}" to="montague.lit" type="get" xmlns="jabber:client">`+
`<query xmlns="http://jabber.org/protocol/disco#info"/></iq>`);
const disco_iq = IQ_stanzas.pop();
expect(Strophe.serialize(disco_iq)).toBe(
`<iq from="romeo@montague.lit" id="${disco_iq.getAttribute('id')}" to="romeo@montague.lit" type="get" xmlns="jabber:client">`+
`<pubsub xmlns="http://jabber.org/protocol/pubsub"><items node="eu.siacs.conversations.axolotl.devicelist"/></pubsub></iq>`);
expect(sent_stanzas.filter(s => (s.nodeName === 'r')).length).toBe(2);
expect(_converse.session.get('unacked_stanzas').length).toBe(5);
......
......@@ -7,6 +7,7 @@
/**
* @module converse-profile
*/
import "@converse/headless/converse-status";
import "@converse/headless/converse-vcard";
import "converse-modal";
import "formdata-polyfill";
......@@ -23,7 +24,7 @@ const u = converse.env.utils;
converse.plugins.add('converse-profile', {
dependencies: ["converse-modal", "converse-vcard", "converse-chatboxviews"],
dependencies: ["converse-status", "converse-modal", "converse-vcard", "converse-chatboxviews"],
initialize () {
/* The initialize function gets called as soon as the plugin is
......
......@@ -49,6 +49,33 @@ converse.plugins.add('converse-chatboxes', {
'privateChatsAutoJoined'
]);
let msg_counter = 0;
_converse.incrementMsgCounter = function () {
msg_counter += 1;
const title = document.title;
if (!title) {
return;
}
if (title.search(/^Messages \(\d+\) /) === -1) {
document.title = `Messages (${msg_counter}) ${title}`;
} else {
document.title = title.replace(/^Messages \(\d+\) /, `Messages (${msg_counter})`);
}
};
_converse.clearMsgCounter = function () {
msg_counter = 0;
const title = document.title;
if (!title) {
return;
}
if (title.search(/^Messages \(\d+\) /) !== -1) {
document.title = title.replace(/^Messages \(\d+\) /, "");
}
};
function openChat (jid) {
if (!utils.isValidJID(jid)) {
return _converse.log(
......@@ -1405,6 +1432,7 @@ converse.plugins.add('converse-chatboxes', {
_converse.api.listen.on('presencesInitialized', (reconnecting) => _converse.chatboxes.onConnected(reconnecting));
_converse.api.listen.on('reconnected', () => _converse.chatboxes.forEach(m => m.onReconnection()));
_converse.api.listen.on('windowStateChanged', d => (d.state === 'visible') && _converse.clearMsgCounter());
/************************ END Event Handlers ************************/
......
This diff is collapsed.
......@@ -6,6 +6,7 @@
/**
* @module converse-roster
*/
import "@converse/headless/converse-status";
import converse from "@converse/headless/converse-core";
const { Backbone, Strophe, $iq, $pres, dayjs, sizzle, _ } = converse.env;
......@@ -14,7 +15,7 @@ const u = converse.env.utils;
converse.plugins.add('converse-roster', {
dependencies: [],
dependencies: ['converse-status'],
initialize () {
/* The initialize function gets called as soon as the plugin is
......@@ -58,6 +59,21 @@ converse.plugins.add('converse-roster', {
};
/**
* Reject or cancel another user's subscription to our presence updates.
* @method rejectPresenceSubscription
* @private
* @memberOf _converse
* @param { String } jid - The Jabber ID of the user whose subscription is being canceled
* @param { String } message - An optional message to the user
*/
_converse.rejectPresenceSubscription = function (jid, message) {
const pres = $pres({to: jid, type: "unsubscribed"});
if (message && message !== "") { pres.c("status").t(message); }
_converse.api.send(pres);
};
/**
* Initialize the Bakcbone collections that represent the contats
* roster and the roster groups.
......@@ -91,6 +107,13 @@ converse.plugins.add('converse-roster', {
};
_converse.sendInitialPresence = function () {
if (_converse.send_initial_presence) {
_converse.xmppstatus.sendPresence();
}
};
/**
* Fetch all the roster groups, and then the roster contacts.
* Emit an event after fetching is done in each case.
......@@ -714,6 +737,7 @@ converse.plugins.add('converse-roster', {
_converse.api.trigger('contactRequest', this.create(user_data));
},
handleIncomingSubscription (presence) {
const jid = presence.getAttribute('from'),
bare_jid = Strophe.getBareJidFromJid(jid),
......@@ -972,6 +996,7 @@ converse.plugins.add('converse-roster', {
_converse.api.trigger('presencesInitialized', reconnecting);
});
_converse.api.listen.on('presencesInitialized', (reconnecting) => {
if (reconnecting) {
/**
......@@ -983,7 +1008,6 @@ converse.plugins.add('converse-roster', {
*/
_converse.api.trigger('rosterReadyAfterReconnection');
} else {
_converse.registerIntervalHandler();
_converse.initRoster();
}
_converse.roster.onConnected();
......
This diff is collapsed.
......@@ -6,6 +6,7 @@
/**
* @module converse-vcard
*/
import "./converse-status";
import converse from "./converse-core";
import tpl_vcard from "./templates/vcard.html";
......@@ -15,7 +16,7 @@ const u = converse.env.utils;
converse.plugins.add('converse-vcard', {
dependencies: ["converse-roster"],
dependencies: ["converse-status", "converse-roster"],
overrides: {
XMPPStatus: {
......@@ -162,10 +163,9 @@ converse.plugins.add('converse-vcard', {
_converse.vcards.browserStorage = _converse.createStore(id, _converse.config.get('storage'));
_converse.vcards.fetch();
}
_converse.api.listen.on('afterResourceBinding', _converse.initVCardCollection);
_converse.api.listen.on('statusInitialized', () => {
_converse.initVCardCollection();
const vcards = _converse.vcards;
if (_converse.session) {
const jid = _converse.session.get('bare_jid');
......
......@@ -14,6 +14,7 @@ import "./converse-pubsub"; // XEP-0060 Pubsub
import "./converse-roster"; // Contacts Roster
import "./converse-rsm"; // XEP-0059 Result Set management
import "./converse-smacks"; // XEP-0198 Stream Management
import "./converse-status"; // XEP-0199 XMPP Ping
import "./converse-vcard"; // XEP-0054 VCard-temp
/* END: Removable components */
......
......@@ -256,6 +256,7 @@
if (el) {
el.parentElement.removeChild(el);
}
document.title = "Converse Tests";
done();
}
await Promise.all((promise_names || []).map(_converse.api.waitUntil));
......
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