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?.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,11 +46,14 @@ 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) {
final newStream = await isNewStream();
if (newStream) {
_gotSupported = false; _gotSupported = false;
_supported = false; _supported = false;
} }
} }
}
Future<StanzaHandlerData> _blockPush(Stanza iq, StanzaHandlerData state) async { Future<StanzaHandlerData> _blockPush(Stanza iq, StanzaHandlerData state) async {
final block = iq.firstTag('block', xmlns: blockingXmlns)!; final block = iq.firstTag('block', xmlns: blockingXmlns)!;

View File

@ -59,21 +59,15 @@ 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...');
await enableCarbons();
logger.finest('Message carbons enabled');
} else {
logger.info('Message carbons not supported.');
}
} else if (event is StreamResumeFailedEvent) {
_gotSupported = false; _gotSupported = false;
_supported = false; _supported = false;
} }
} }
}
Future<StanzaHandlerData> _onMessageReceived(Stanza message, StanzaHandlerData state) async { Future<StanzaHandlerData> _onMessageReceived(Stanza message, StanzaHandlerData state) async {
final from = JID.fromString(message.attributes['from']! as String); final from = JID.fromString(message.attributes['from']! as String);

View File

@ -77,13 +77,16 @@ 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) {
final newStream = await isNewStream();
if (newStream) {
_gotSupported = false; _gotSupported = false;
_supported = false; _supported = false;
_entityJid = null; _entityJid = null;
_maxUploadSize = null; _maxUploadSize = null;
} }
} }
}
@override @override
Future<bool> isSupported() async { Future<bool> isSupported() async {

View File

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