feat: Refactor RosterPushEvent to RosterPushResult

This commit is contained in:
PapaTutuWawa 2023-01-07 18:36:18 +01:00
parent e12f4688d3
commit 67446285c1
4 changed files with 26 additions and 23 deletions

View File

@ -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);

View File

@ -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<XmppRosterItem> 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(

View File

@ -126,7 +126,7 @@ abstract class BaseRosterStateManager {
}
/// Handles a roster push from the RosterManager.
Future<void> handleRosterPush(RosterPushEvent event) async {
Future<void> handleRosterPush(RosterPushResult event) async {
await _lock.synchronized(() async {
await _loadRosterCache();

View File

@ -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',
),
);