From c14c2b3b59aae8e9972a753ad2729dd1d9e0079c Mon Sep 17 00:00:00 2001 From: JC Brand Date: Mon, 20 Jun 2022 22:43:51 +0200 Subject: [PATCH] Carbons: check for server support before enabling --- src/headless/plugins/chat/utils.js | 10 +++- src/headless/plugins/smacks/tests/smacks.js | 52 ++++++++++----------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/headless/plugins/chat/utils.js b/src/headless/plugins/chat/utils.js index ea5e14a28..0f865cee5 100644 --- a/src/headless/plugins/chat/utils.js +++ b/src/headless/plugins/chat/utils.js @@ -164,10 +164,18 @@ export async function enableCarbons (reconnecting) { return; } + const domain = Strophe.getDomainFromJid(_converse.bare_jid); + const supported = await api.disco.supports(Strophe.NS.CARBONS, domain); + + if (!supported) { + log.warn("Not enabling carbons because it's not supported!"); + return; + } + const iq = new Strophe.Builder('iq', { 'from': _converse.connection.jid, 'type': 'set' - }).c('enable', {xmlns: Strophe.NS.CARBONS}); + }).c('enable', {xmlns: Strophe.NS.CARBONS}); const result = await api.sendIQ(iq, null, false); if (result === null) { diff --git a/src/headless/plugins/smacks/tests/smacks.js b/src/headless/plugins/smacks/tests/smacks.js index 827c235df..042ca6f40 100644 --- a/src/headless/plugins/smacks/tests/smacks.js +++ b/src/headless/plugins/smacks/tests/smacks.js @@ -30,38 +30,36 @@ describe("XEP-0198 Stream Management", function () { _converse.connection._dataRecv(mock.createRequest(result)); expect(_converse.session.get('smacks_enabled')).toBe(true); + await mock.waitUntilDiscoConfirmed( + _converse, + "montague.lit", + [], + [Strophe.NS.CARBONS] + ); + let IQ_stanzas = _converse.connection.IQ_stanzas; await u.waitUntil(() => IQ_stanzas.length === 5); - expect(Strophe.serialize(IQ_stanzas[0])).toBe( - ``); - - const carbons_response = $iq({ - 'type': 'result', - 'from': _converse.bare_jid, - 'to': _converse.jid, - 'id': IQ_stanzas[0].getAttribute('id') - }); - _converse.connection._dataRecv(mock.createRequest(carbons_response)); - - const disco_iq = IQ_stanzas[1]; + const disco_iq = IQ_stanzas[0]; expect(Strophe.serialize(disco_iq)).toBe( ``+ ``); - expect(Strophe.serialize(IQ_stanzas[2])).toBe( - ``); + expect(Strophe.serialize(IQ_stanzas[1])).toBe( + ``); await mock.waitForRoster(_converse, 'current', 1); - const omemo_iq = IQ_stanzas[3]; + const omemo_iq = IQ_stanzas[2]; expect(Strophe.serialize(omemo_iq)).toBe( ``+ ``); - expect(Strophe.serialize(IQ_stanzas[4])).toBe( - ``+ + expect(Strophe.serialize(IQ_stanzas[3])).toBe( + ``+ ``); + expect(Strophe.serialize(IQ_stanzas[4])).toBe( + ``); await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'presence')).length); @@ -77,7 +75,7 @@ describe("XEP-0198 Stream Management", function () { let r = u.toStanza(``); _converse.connection._dataRecv(mock.createRequest(r)); - // "h" is 2 because we received two IQ responses, for carbons and the roster + // "h" is 3 because we received two IQ responses, for disco and the roster ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a')).pop()); expect(Strophe.serialize(ack)).toBe(''); @@ -95,13 +93,14 @@ describe("XEP-0198 Stream Management", function () { .c('feature', {'var': 'http://jabber.org/protocol/disco#items'}); _converse.connection._dataRecv(mock.createRequest(disco_result)); - ack = u.toStanza(``); + ack = u.toStanza(``); _converse.connection._dataRecv(mock.createRequest(ack)); - expect(_converse.session.get('unacked_stanzas').length).toBe(3); + expect(_converse.session.get('unacked_stanzas').length).toBe(4); - expect(_converse.session.get('unacked_stanzas')[0]).toBe(Strophe.serialize(IQ_stanzas[3])); - expect(_converse.session.get('unacked_stanzas')[1]).toBe(Strophe.serialize(IQ_stanzas[4])); - expect(_converse.session.get('unacked_stanzas')[2]).toBe( + expect(_converse.session.get('unacked_stanzas')[0]).toBe(Strophe.serialize(IQ_stanzas[2])); + expect(_converse.session.get('unacked_stanzas')[1]).toBe(Strophe.serialize(IQ_stanzas[3])); + expect(_converse.session.get('unacked_stanzas')[2]).toBe(Strophe.serialize(IQ_stanzas[4])); + expect(_converse.session.get('unacked_stanzas')[3]).toBe( `0`+ ``+ ``); @@ -109,9 +108,9 @@ describe("XEP-0198 Stream Management", function () { r = u.toStanza(``); _converse.connection._dataRecv(mock.createRequest(r)); - ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a' && s.getAttribute('h') === '2')).pop()); + ack = await u.waitUntil(() => sent_stanzas.filter(s => (s.nodeName === 'a' && s.getAttribute('h') === '3')).pop()); - expect(Strophe.serialize(ack)).toBe(''); + expect(Strophe.serialize(ack)).toBe(''); await _converse.api.waitUntil('rosterInitialized'); // test session resumption @@ -134,8 +133,7 @@ describe("XEP-0198 Stream Management", function () { // Test that unacked stanzas get resent out let iq = IQ_stanzas.pop(); expect(Strophe.serialize(iq)).toBe( - ``+ - ``); + ``); iq = IQ_stanzas.pop(); expect(Strophe.serialize(iq)).toBe(