From 67446285c1bdff6f61031a287fba446665cbaa54 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sat, 7 Jan 2023 18:36:18 +0100 Subject: [PATCH] feat: Refactor RosterPushEvent to RosterPushResult --- packages/moxxmpp/lib/src/events.dart | 7 ------- packages/moxxmpp/lib/src/roster/roster.dart | 19 +++++++++++------- packages/moxxmpp/lib/src/roster/state.dart | 2 +- packages/moxxmpp/test/roster_state_test.dart | 21 ++++++++++++-------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/packages/moxxmpp/lib/src/events.dart b/packages/moxxmpp/lib/src/events.dart index 2fdf6ba..3b674bf 100644 --- a/packages/moxxmpp/lib/src/events.dart +++ b/packages/moxxmpp/lib/src/events.dart @@ -54,13 +54,6 @@ class StreamResumedEvent extends XmppEvent { /// Triggered when stream resumption failed class StreamResumeFailedEvent extends XmppEvent {} -/// Triggered when a roster push is received -class RosterPushEvent extends XmppEvent { - RosterPushEvent({ required this.item, this.ver }); - final XmppRosterItem item; - final String? ver; -} - /// Triggered when the roster has been modified class RosterUpdatedEvent extends XmppEvent { RosterUpdatedEvent(this.removed, this.modified, this.added); diff --git a/packages/moxxmpp/lib/src/roster/roster.dart b/packages/moxxmpp/lib/src/roster/roster.dart index 879df6a..dc481de 100644 --- a/packages/moxxmpp/lib/src/roster/roster.dart +++ b/packages/moxxmpp/lib/src/roster/roster.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'package:moxxmpp/src/events.dart'; import 'package:moxxmpp/src/jid.dart'; import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; @@ -30,11 +29,17 @@ enum RosterRemovalResult { } class RosterRequestResult { - RosterRequestResult({ required this.items, this.ver }); + RosterRequestResult(this.items, this.ver); List items; String? ver; } +class RosterPushResult { + RosterPushResult(this.item, this.ver); + final XmppRosterItem item; + final String? ver; +} + /// A Stub feature negotiator for finding out whether roster versioning is supported. class RosterFeatureNegotiator extends XmppFeatureNegotiatorBase { RosterFeatureNegotiator() : _supported = false, super(11, false, rosterVersioningXmlns, rosterNegotiator); @@ -110,14 +115,14 @@ class RosterManager extends XmppManagerBase { unawaited( _stateManager.handleRosterPush( - RosterPushEvent( - item: XmppRosterItem( + RosterPushResult( + XmppRosterItem( jid: item.attributes['jid']! as String, subscription: item.attributes['subscription']! as String, ask: item.attributes['ask'] as String?, name: item.attributes['name'] as String?, ), - ver: query.attributes['ver'] as String?, + query.attributes['ver'] as String?, ), ), ); @@ -150,8 +155,8 @@ class RosterManager extends XmppManagerBase { } final result = RosterRequestResult( - items: items, - ver: rosterVersion, + items, + rosterVersion, ); unawaited( diff --git a/packages/moxxmpp/lib/src/roster/state.dart b/packages/moxxmpp/lib/src/roster/state.dart index 8240f1e..bc41e3d 100644 --- a/packages/moxxmpp/lib/src/roster/state.dart +++ b/packages/moxxmpp/lib/src/roster/state.dart @@ -126,7 +126,7 @@ abstract class BaseRosterStateManager { } /// Handles a roster push from the RosterManager. - Future handleRosterPush(RosterPushEvent event) async { + Future handleRosterPush(RosterPushResult event) async { await _lock.synchronized(() async { await _loadRosterCache(); diff --git a/packages/moxxmpp/test/roster_state_test.dart b/packages/moxxmpp/test/roster_state_test.dart index 94651af..510ca02 100644 --- a/packages/moxxmpp/test/roster_state_test.dart +++ b/packages/moxxmpp/test/roster_state_test.dart @@ -4,13 +4,15 @@ import 'package:test/test.dart'; void main() { test('Test receiving a roster push', () async { final rs = TestingRosterStateManager(null, []); + rs.register((_) {}); await rs.handleRosterPush( - RosterPushEvent( - item: XmppRosterItem( + RosterPushResult( + XmppRosterItem( jid: 'testuser@server.example', subscription: 'both', ), + null, ), ); @@ -23,11 +25,12 @@ void main() { // Receive another roster push await rs.handleRosterPush( - RosterPushEvent( - item: XmppRosterItem( + RosterPushResult( + XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'to', ), + null, ), ); @@ -40,11 +43,12 @@ void main() { // Remove one of the items await rs.handleRosterPush( - RosterPushEvent( - item: XmppRosterItem( + RosterPushResult( + XmppRosterItem( jid: 'testuser2@server2.example', subscription: 'remove', ), + null, ), ); @@ -62,11 +66,12 @@ void main() { test('Test a roster fetch', () async { final rs = TestingRosterStateManager(null, []); + rs.register((_) {}); // Fetch the roster await rs.handleRosterFetch( RosterRequestResult( - items: [ + [ XmppRosterItem( jid: 'testuser@server.example', subscription: 'both', @@ -80,7 +85,7 @@ void main() { subscription: 'from', ), ], - ver: 'aaaaaaaa', + 'aaaaaaaa', ), );