From 1000e0756b8521ac247c51426683596c46e2fef3 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Tue, 31 Jan 2023 21:08:16 +0100 Subject: [PATCH] feat: Improve detecting new streams Fixes #26. --- packages/moxxmpp/lib/src/connection.dart | 4 ++++ packages/moxxmpp/lib/src/events.dart | 3 +++ packages/moxxmpp/lib/src/managers/base.dart | 10 ++++++++++ packages/moxxmpp/lib/src/xeps/xep_0191.dart | 9 ++++++--- packages/moxxmpp/lib/src/xeps/xep_0280.dart | 18 ++++++------------ .../lib/src/xeps/xep_0363/xep_0363.dart | 13 ++++++++----- packages/moxxmpp/result | 1 - 7 files changed, 37 insertions(+), 21 deletions(-) delete mode 120000 packages/moxxmpp/result diff --git a/packages/moxxmpp/lib/src/connection.dart b/packages/moxxmpp/lib/src/connection.dart index 3fb5ae8..3b5f740 100644 --- a/packages/moxxmpp/lib/src/connection.dart +++ b/packages/moxxmpp/lib/src/connection.dart @@ -790,6 +790,10 @@ class XmppConnection { // Resolve the connection completion future _connectionCompleter?.complete(const XmppConnectionResult(true)); _connectionCompleter = null; + + // Tell consumers of the event stream that we're done with stream feature + // negotiations + await _sendEvent(StreamNegotiationsDoneEvent()); // Send out initial presence await getPresenceManager().sendInitialPresence(); diff --git a/packages/moxxmpp/lib/src/events.dart b/packages/moxxmpp/lib/src/events.dart index 82d2f78..180a6f9 100644 --- a/packages/moxxmpp/lib/src/events.dart +++ b/packages/moxxmpp/lib/src/events.dart @@ -246,3 +246,6 @@ class NonRecoverableErrorEvent extends XmppEvent { /// The error in question. final XmppError error; } + +/// Triggered when the stream negotiations are done. +class StreamNegotiationsDoneEvent extends XmppEvent {} diff --git a/packages/moxxmpp/lib/src/managers/base.dart b/packages/moxxmpp/lib/src/managers/base.dart index 30b74b4..b01b0b2 100644 --- a/packages/moxxmpp/lib/src/managers/base.dart +++ b/packages/moxxmpp/lib/src/managers/base.dart @@ -8,6 +8,7 @@ import 'package:moxxmpp/src/managers/namespaces.dart'; import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/xeps/xep_0030/types.dart'; import 'package:moxxmpp/src/xeps/xep_0030/xep_0030.dart'; +import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart'; abstract class XmppManagerBase { XmppManagerBase(this.id); @@ -111,6 +112,15 @@ abstract class XmppManagerBase { return handled; } + /// Returns true, if the current stream negotiations resulted in a new stream. Useful + /// for plugins to reset their cache in case of a new stream. + /// The value only makes sense after receiving a StreamNegotiationsDoneEvent. + Future isNewStream() async { + final sm = getAttributes().getManagerById(smManager); + + return sm?.streamResumed == false; + } + /// Sends a reply of the stanza in [data] with [type]. Replaces the original stanza's /// children with [children]. /// diff --git a/packages/moxxmpp/lib/src/xeps/xep_0191.dart b/packages/moxxmpp/lib/src/xeps/xep_0191.dart index f73bf14..5a14403 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0191.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0191.dart @@ -46,9 +46,12 @@ class BlockingManager extends XmppManagerBase { @override Future onXmppEvent(XmppEvent event) async { - if (event is StreamResumeFailedEvent) { - _gotSupported = false; - _supported = false; + if (event is StreamNegotiationsDoneEvent) { + final newStream = await isNewStream(); + if (newStream) { + _gotSupported = false; + _supported = false; + } } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0280.dart b/packages/moxxmpp/lib/src/xeps/xep_0280.dart index e111e29..f711bec 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0280.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0280.dart @@ -59,19 +59,13 @@ class CarbonsManager extends XmppManagerBase { @override Future onXmppEvent(XmppEvent event) async { - if (event is ServerDiscoDoneEvent && !_isEnabled) { - final attrs = getAttributes(); - - if (attrs.isFeatureSupported(carbonsXmlns)) { - logger.finest('Message carbons supported. Enabling...'); - await enableCarbons(); - logger.finest('Message carbons enabled'); - } else { - logger.info('Message carbons not supported.'); + if (event is StreamNegotiationsDoneEvent) { + // Reset disco cache info on a new stream + final newStream = await isNewStream(); + if (newStream) { + _gotSupported = false; + _supported = false; } - } else if (event is StreamResumeFailedEvent) { - _gotSupported = false; - _supported = false; } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0363/xep_0363.dart b/packages/moxxmpp/lib/src/xeps/xep_0363/xep_0363.dart index 055f630..df04a57 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0363/xep_0363.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0363/xep_0363.dart @@ -77,11 +77,14 @@ class HttpFileUploadManager extends XmppManagerBase { @override Future onXmppEvent(XmppEvent event) async { - if (event is StreamResumeFailedEvent) { - _gotSupported = false; - _supported = false; - _entityJid = null; - _maxUploadSize = null; + if (event is StreamNegotiationsDoneEvent) { + final newStream = await isNewStream(); + if (newStream) { + _gotSupported = false; + _supported = false; + _entityJid = null; + _maxUploadSize = null; + } } } diff --git a/packages/moxxmpp/result b/packages/moxxmpp/result deleted file mode 120000 index e635363..0000000 --- a/packages/moxxmpp/result +++ /dev/null @@ -1 +0,0 @@ -/nix/store/df027433sc5k93yfsm823wl42vijhrzn-lol-pub-cache \ No newline at end of file