Commit 18730036 authored by JC Brand's avatar JC Brand

Add new plugin `converse-carbons`

Fixes a bug whereby carbons weren't being enabled because the listener
for `afterResourceBinding` was registered before `converse.initialize`
was called,(which means it was removed as soon as `cleanup` was called
inside `converse.initialize`).

This shows that event `api.listen` API calls can/should only be called
inside the `initialize` method of a plugin (or at least once plugins
have been initialized).
parent 2c042cc3
/**
* @module converse-carbons
* @copyright The Converse.js contributors
* @license Mozilla Public License (MPLv2)
* @description Implements support for XEP-0280 Message Carbons
*/
import log from '@converse/headless/log';
import { Strophe } from 'strophe.js/src/strophe';
import { _converse, api, converse } from "./converse-core";
/* Ask the XMPP server to enable Message Carbons
* See XEP-0280 https://xmpp.org/extensions/xep-0280.html#enabling
*/
function enableCarbons () {
if (!api.settings.get("message_carbons") || _converse.session?.get('carbons_enabled')) {
return;
}
const carbons_iq = new Strophe.Builder('iq', {
'from': _converse.connection.jid,
'id': 'enablecarbons',
'type': 'set'
})
.c('enable', {xmlns: Strophe.NS.CARBONS});
_converse.connection.addHandler((iq) => {
if (iq.querySelectorAll('error').length > 0) {
log.warn('An error occurred while trying to enable message carbons.');
} else {
_converse.session.save({'carbons_enabled': true});
log.debug('Message carbons have been enabled.');
}
}, null, "iq", null, "enablecarbons");
_converse.connection.send(carbons_iq);
}
converse.plugins.add('converse-carbons', {
initialize () {
api.settings.extend({
message_carbons: true
});
api.listen.on('afterResourceBinding', () => enableCarbons());
}
});
......@@ -82,13 +82,14 @@ const CORE_PLUGINS = [
'converse-bookmarks',
'converse-bosh',
'converse-caps',
'converse-chatboxes',
'converse-carbons',
'converse-chat',
'converse-chatboxes',
'converse-disco',
'converse-emoji',
'converse-headlines',
'converse-mam',
'converse-muc',
'converse-headlines',
'converse-ping',
'converse-pubsub',
'converse-roster',
......@@ -123,7 +124,6 @@ const DEFAULT_SETTINGS = {
'gl', 'he', 'hi', 'hu', 'id', 'it', 'ja', 'nb', 'nl', 'mr', 'oc',
'pl', 'pt', 'pt_BR', 'ro', 'ru', 'tr', 'uk', 'vi', 'zh_CN', 'zh_TW'
],
message_carbons: true,
nickname: undefined,
password: undefined,
persistent_store: 'localStorage',
......@@ -1339,33 +1339,6 @@ async function cleanup () {
}
function enableCarbons () {
/* Ask the XMPP server to enable Message Carbons
* See XEP-0280 https://xmpp.org/extensions/xep-0280.html#enabling
*/
if (!api.settings.get("message_carbons") || !_converse.session || _converse.session.get('carbons_enabled')) {
return;
}
const carbons_iq = new Strophe.Builder('iq', {
'from': _converse.connection.jid,
'id': 'enablecarbons',
'type': 'set'
})
.c('enable', {xmlns: Strophe.NS.CARBONS});
_converse.connection.addHandler((iq) => {
if (iq.querySelectorAll('error').length > 0) {
log.warn('An error occurred while trying to enable message carbons.');
} else {
_converse.session.save({'carbons_enabled': true});
log.debug('Message carbons have been enabled.');
}
}, null, "iq", null, "enablecarbons");
_converse.connection.send(carbons_iq);
}
api.listen.on('afterResourceBinding', () => enableCarbons());
function fetchLoginCredentials (wait=0) {
return new Promise(
debounce((resolve, reject) => {
......
......@@ -6,15 +6,16 @@ import "./converse-adhoc"; // XEP-0050 Ad Hoc Commands
import "./converse-bookmarks"; // XEP-0199 XMPP Ping
import "./converse-bosh"; // XEP-0206 BOSH
import "./converse-caps"; // XEP-0115 Entity Capabilities
import "./converse-carbons"; // XEP-0280 Message Carbons
import "./converse-chat"; // RFC-6121 Instant messaging
import "./converse-chatboxes";
import "./converse-chat"; // Support for one-on-one chats
import "./converse-disco"; // XEP-0030 Service discovery
import "./converse-headlines"; // Support for headline messages
import "./converse-mam"; // XEP-0313 Message Archive Management
import "./converse-muc"; // XEP-0045 Multi-user chat
import "./converse-headlines"; // Support for headline messages
import "./converse-ping"; // XEP-0199 XMPP Ping
import "./converse-pubsub"; // XEP-0060 Pubsub
import "./converse-roster"; // Contacts Roster
import "./converse-roster"; // RFC-6121 Contacts Roster
import "./converse-rsm"; // XEP-0059 Result Set management
import "./converse-smacks"; // XEP-0198 Stream Management
import "./converse-status"; // XEP-0199 XMPP Ping
......
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