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

@ -791,6 +791,10 @@ class XmppConnection {
_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();
}

View File

@ -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 {}

View File

@ -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<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
/// children with [children].
///

View File

@ -46,9 +46,12 @@ class BlockingManager extends XmppManagerBase {
@override
Future<void> 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;
}
}
}

View File

@ -59,19 +59,13 @@ class CarbonsManager extends XmppManagerBase {
@override
Future<void> 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;
}
}

View File

@ -77,11 +77,14 @@ class HttpFileUploadManager extends XmppManagerBase {
@override
Future<void> 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;
}
}
}

View File

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