feat: Improve detecting new streams

Fixes #26.
This commit is contained in:
PapaTutuWawa 2023-01-31 21:08:16 +01:00
parent 902b497526
commit 1000e0756b
7 changed files with 37 additions and 21 deletions

View File

@ -790,6 +790,10 @@ class XmppConnection {
// Resolve the connection completion future // Resolve the connection completion future
_connectionCompleter?.complete(const XmppConnectionResult(true)); _connectionCompleter?.complete(const XmppConnectionResult(true));
_connectionCompleter = null; _connectionCompleter = null;
// Tell consumers of the event stream that we're done with stream feature
// negotiations
await _sendEvent(StreamNegotiationsDoneEvent());
// Send out initial presence // Send out initial presence
await getPresenceManager().sendInitialPresence(); await getPresenceManager().sendInitialPresence();

View File

@ -246,3 +246,6 @@ class NonRecoverableErrorEvent extends XmppEvent {
/// The error in question. /// The error in question.
final XmppError error; final XmppError error;
} }
/// Triggered when the stream negotiations are done.
class StreamNegotiationsDoneEvent extends XmppEvent {}

View File

@ -8,6 +8,7 @@ import 'package:moxxmpp/src/managers/namespaces.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/xeps/xep_0030/types.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_0030/xep_0030.dart';
import 'package:moxxmpp/src/xeps/xep_0198/xep_0198.dart';
abstract class XmppManagerBase { abstract class XmppManagerBase {
XmppManagerBase(this.id); XmppManagerBase(this.id);
@ -111,6 +112,15 @@ abstract class XmppManagerBase {
return handled; 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<bool> isNewStream() async {
final sm = getAttributes().getManagerById<StreamManagementManager>(smManager);
return sm?.streamResumed == false;
}
/// Sends a reply of the stanza in [data] with [type]. Replaces the original stanza's /// Sends a reply of the stanza in [data] with [type]. Replaces the original stanza's
/// children with [children]. /// children with [children].
/// ///

View File

@ -46,9 +46,12 @@ class BlockingManager extends XmppManagerBase {
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
if (event is StreamResumeFailedEvent) { if (event is StreamNegotiationsDoneEvent) {
_gotSupported = false; final newStream = await isNewStream();
_supported = false; if (newStream) {
_gotSupported = false;
_supported = false;
}
} }
} }

View File

@ -59,19 +59,13 @@ class CarbonsManager extends XmppManagerBase {
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
if (event is ServerDiscoDoneEvent && !_isEnabled) { if (event is StreamNegotiationsDoneEvent) {
final attrs = getAttributes(); // Reset disco cache info on a new stream
final newStream = await isNewStream();
if (attrs.isFeatureSupported(carbonsXmlns)) { if (newStream) {
logger.finest('Message carbons supported. Enabling...'); _gotSupported = false;
await enableCarbons(); _supported = false;
logger.finest('Message carbons enabled');
} else {
logger.info('Message carbons not supported.');
} }
} else if (event is StreamResumeFailedEvent) {
_gotSupported = false;
_supported = false;
} }
} }

View File

@ -77,11 +77,14 @@ class HttpFileUploadManager extends XmppManagerBase {
@override @override
Future<void> onXmppEvent(XmppEvent event) async { Future<void> onXmppEvent(XmppEvent event) async {
if (event is StreamResumeFailedEvent) { if (event is StreamNegotiationsDoneEvent) {
_gotSupported = false; final newStream = await isNewStream();
_supported = false; if (newStream) {
_entityJid = null; _gotSupported = false;
_maxUploadSize = null; _supported = false;
_entityJid = null;
_maxUploadSize = null;
}
} }
} }

View File

@ -1 +0,0 @@
/nix/store/df027433sc5k93yfsm823wl42vijhrzn-lol-pub-cache