From b1da6e5a538fba335e82f6892e2ffe3cabe21183 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Tue, 23 May 2023 17:28:20 +0200 Subject: [PATCH] chore(xep): Move DiscoManager from String to JID --- packages/moxxmpp/CHANGELOG.md | 3 +++ packages/moxxmpp/lib/src/jid.dart | 5 ++++ packages/moxxmpp/lib/src/managers/base.dart | 2 +- .../moxxmpp/lib/src/xeps/xep_0030/cache.dart | 4 ++-- .../lib/src/xeps/xep_0030/helpers.dart | 9 ++++---- .../moxxmpp/lib/src/xeps/xep_0030/types.dart | 4 ++-- .../lib/src/xeps/xep_0030/xep_0030.dart | 23 ++++++------------- .../lib/src/xeps/xep_0060/xep_0060.dart | 16 ++++++------- packages/moxxmpp/lib/src/xeps/xep_0084.dart | 7 +++--- packages/moxxmpp/lib/src/xeps/xep_0115.dart | 4 ++-- packages/moxxmpp/lib/src/xeps/xep_0359.dart | 2 +- .../lib/src/xeps/xep_0384/xep_0384.dart | 8 +++---- packages/moxxmpp/lib/src/xeps/xep_0449.dart | 2 +- packages/moxxmpp/test/xeps/xep_0030_test.dart | 10 ++++---- packages/moxxmpp/test/xeps/xep_0060_test.dart | 10 ++++---- packages/moxxmpp/test/xeps/xep_0115_test.dart | 2 +- 16 files changed, 55 insertions(+), 56 deletions(-) diff --git a/packages/moxxmpp/CHANGELOG.md b/packages/moxxmpp/CHANGELOG.md index ac9937c..e34e692 100644 --- a/packages/moxxmpp/CHANGELOG.md +++ b/packages/moxxmpp/CHANGELOG.md @@ -6,6 +6,9 @@ - The `DiscoManager` now only handled entity capabilities if a `EntityCapabilityManager` is registered. - The `EntityCapabilityManager` now verifies and validates its data before caching. - **BREAKING**: Added the `resumed` parameter to `StreamNegotiationsDoneEvent`. Use this to check if the current stream is new or resumed instead of using the `ConnectionStateChangedEvent`. +- **BREAKING**: Remove `DiscoManager.discoInfoCapHashQuery`. +- **BREAKING**: The entity argument of `DiscoManager.discoInfoQuery` and `DiscoManager.discoItemsQuery` are now `JID` instead of `String`. +- **BREAKING**: `PubSubManager` and `UserAvatarManager` now use `JID` instead of `String`. ## 0.3.1 diff --git a/packages/moxxmpp/lib/src/jid.dart b/packages/moxxmpp/lib/src/jid.dart index b3daf56..d46d1f7 100644 --- a/packages/moxxmpp/lib/src/jid.dart +++ b/packages/moxxmpp/lib/src/jid.dart @@ -63,6 +63,11 @@ class JID { /// Converts the JID into one with a resource part of [resource]. JID withResource(String resource) => JID(local, domain, resource); + /// Convert the JID into the JID of the domain. For example, converts alice@example.org/abc123 to example.org. + JID toDomain() { + return JID('', domain, ''); + } + /// Compares the JID with [other]. This function assumes that JID and [other] /// are bare, i.e. only the domain- and localparts are compared. If [ensureBare] /// is optionally set to true, then [other] MUST be bare. Otherwise, false is returned. diff --git a/packages/moxxmpp/lib/src/managers/base.dart b/packages/moxxmpp/lib/src/managers/base.dart index 8170675..351919d 100644 --- a/packages/moxxmpp/lib/src/managers/base.dart +++ b/packages/moxxmpp/lib/src/managers/base.dart @@ -45,7 +45,7 @@ abstract class XmppManagerBase { ); final result = await dm!.discoInfoQuery( - _managerAttributes.getConnectionSettings().jid.domain, + _managerAttributes.getConnectionSettings().jid.toDomain(), shouldEncrypt: false, ); if (result.isType()) { diff --git a/packages/moxxmpp/lib/src/xeps/xep_0030/cache.dart b/packages/moxxmpp/lib/src/xeps/xep_0030/cache.dart index 7a97896..1d0c57a 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0030/cache.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0030/cache.dart @@ -1,4 +1,5 @@ import 'package:meta/meta.dart'; +import 'package:moxxmpp/src/jid.dart'; @internal @immutable @@ -6,8 +7,7 @@ class DiscoCacheKey { const DiscoCacheKey(this.jid, this.node); /// The JID we're requesting disco data from. - // TODO(Unknown): Replace with JID - final String jid; + final JID jid; /// Optionally the node we are requesting from. final String? node; diff --git a/packages/moxxmpp/lib/src/xeps/xep_0030/helpers.dart b/packages/moxxmpp/lib/src/xeps/xep_0030/helpers.dart index 5b6810c..9ec65aa 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0030/helpers.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0030/helpers.dart @@ -1,12 +1,13 @@ +import 'package:moxxmpp/src/jid.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; // TODO(PapaTutuWawa): Move types into types.dart -Stanza buildDiscoInfoQueryStanza(String entity, String? node) { +Stanza buildDiscoInfoQueryStanza(JID entity, String? node) { return Stanza.iq( - to: entity, + to: entity.toString(), type: 'get', children: [ XMLNode.xmlns( @@ -18,9 +19,9 @@ Stanza buildDiscoInfoQueryStanza(String entity, String? node) { ); } -Stanza buildDiscoItemsQueryStanza(String entity, {String? node}) { +Stanza buildDiscoItemsQueryStanza(JID entity, {String? node}) { return Stanza.iq( - to: entity, + to: entity.toString(), type: 'get', children: [ XMLNode.xmlns( diff --git a/packages/moxxmpp/lib/src/xeps/xep_0030/types.dart b/packages/moxxmpp/lib/src/xeps/xep_0030/types.dart index 3da89c1..18c3d34 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0030/types.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0030/types.dart @@ -108,13 +108,13 @@ class DiscoInfo { @immutable class DiscoItem { const DiscoItem({required this.jid, this.node, this.name}); - final String jid; + final JID jid; final String? node; final String? name; XMLNode toXml() { final attributes = { - 'jid': jid, + 'jid': jid.toString(), }; if (node != null) { attributes['node'] = node!; diff --git a/packages/moxxmpp/lib/src/xeps/xep_0030/xep_0030.dart b/packages/moxxmpp/lib/src/xeps/xep_0030/xep_0030.dart index 84eca04..8558da3 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0030/xep_0030.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0030/xep_0030.dart @@ -253,7 +253,7 @@ class DiscoManager extends XmppManagerBase { /// [shouldCache] indicates whether the successful result of the disco#info query /// should be cached (true) or not(false). Future> discoInfoQuery( - String entity, { + JID entity, { String? node, bool shouldEncrypt = true, bool shouldCache = true, @@ -272,7 +272,7 @@ class DiscoManager extends XmppManagerBase { } else { // Check if we know entity capabilities if (ecm != null && node == null) { - info = await ecm.getCachedDiscoInfoFromJid(JID.fromString(entity)); + info = await ecm.getCachedDiscoInfoFromJid(entity); if (info != null) { return null; } @@ -312,7 +312,7 @@ class DiscoManager extends XmppManagerBase { final result = Result( DiscoInfo.fromQuery( query, - JID.fromString(entity), + entity, ), ); await _exitDiscoInfoCriticalSection(cacheKey, result, shouldCache); @@ -321,7 +321,7 @@ class DiscoManager extends XmppManagerBase { /// Sends a disco items query to the (full) jid [entity], optionally with node=[node]. Future>> discoItemsQuery( - String entity, { + JID entity, { String? node, bool shouldEncrypt = true, }) async { @@ -357,7 +357,7 @@ class DiscoManager extends XmppManagerBase { .findTags('item') .map( (node) => DiscoItem( - jid: node.attributes['jid']! as String, + jid: JID.fromString(node.attributes['jid']! as String), node: node.attributes['node'] as String?, name: node.attributes['name'] as String?, ), @@ -369,18 +369,9 @@ class DiscoManager extends XmppManagerBase { return result; } - /// Queries information about a jid based on its node and capability hash. - Future> discoInfoCapHashQuery( - String jid, - String node, - String ver, - ) async { - return discoInfoQuery(jid, node: '$node#$ver'); - } - Future>> performDiscoSweep() async { final attrs = getAttributes(); - final serverJid = attrs.getConnectionSettings().jid.domain; + final serverJid = attrs.getConnectionSettings().jid.toDomain(); final infoResults = List.empty(growable: true); final result = await discoInfoQuery(serverJid); if (result.isType()) { @@ -423,7 +414,7 @@ class DiscoManager extends XmppManagerBase { /// A wrapper function around discoInfoQuery: Returns true if the entity with JID /// [entity] supports the disco feature [feature]. If not, returns false. Future supportsFeature(JID entity, String feature) async { - final info = await discoInfoQuery(entity.toString()); + final info = await discoInfoQuery(entity); if (info.isType()) return false; return info.get().features.contains(feature); diff --git a/packages/moxxmpp/lib/src/xeps/xep_0060/xep_0060.dart b/packages/moxxmpp/lib/src/xeps/xep_0060/xep_0060.dart index f1254c6..bc66404 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0060/xep_0060.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0060/xep_0060.dart @@ -117,7 +117,7 @@ class PubSubManager extends XmppManagerBase { return state.copyWith(done: true); } - Future _getNodeItemCount(String jid, String node) async { + Future _getNodeItemCount(JID jid, String node) async { final dm = getAttributes().getManagerById(discoManager)!; final response = await dm.discoItemsQuery(jid, node: node); var count = 0; @@ -136,7 +136,7 @@ class PubSubManager extends XmppManagerBase { // with the requested configuration. @visibleForTesting Future preprocessPublishOptions( - String jid, + JID jid, String node, PubSubPublishOptions options, ) async { @@ -264,7 +264,7 @@ class PubSubManager extends XmppManagerBase { /// Publish [payload] to the PubSub node [node] on JID [jid]. Returns true if it /// was successful. False otherwise. Future> publish( - String jid, + JID jid, String node, XMLNode payload, { String? id, @@ -280,7 +280,7 @@ class PubSubManager extends XmppManagerBase { } Future> _publish( - String jid, + JID jid, String node, XMLNode payload, { String? id, @@ -296,7 +296,7 @@ class PubSubManager extends XmppManagerBase { final result = await getAttributes().sendStanza( Stanza.iq( type: 'set', - to: jid, + to: jid.toString(), children: [ XMLNode.xmlns( tag: 'pubsub', @@ -481,7 +481,7 @@ class PubSubManager extends XmppManagerBase { } Future> configure( - String jid, + JID jid, String node, PubSubPublishOptions options, ) async { @@ -491,7 +491,7 @@ class PubSubManager extends XmppManagerBase { final form = await attrs.sendStanza( Stanza.iq( type: 'get', - to: jid, + to: jid.toString(), children: [ XMLNode.xmlns( tag: 'pubsub', @@ -515,7 +515,7 @@ class PubSubManager extends XmppManagerBase { final submit = await attrs.sendStanza( Stanza.iq( type: 'set', - to: jid, + to: jid.toString(), children: [ XMLNode.xmlns( tag: 'pubsub', diff --git a/packages/moxxmpp/lib/src/xeps/xep_0084.dart b/packages/moxxmpp/lib/src/xeps/xep_0084.dart index dad4bbe..491a1b5 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0084.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0084.dart @@ -1,4 +1,5 @@ import 'package:moxxmpp/src/events.dart'; +import 'package:moxxmpp/src/jid.dart'; import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; import 'package:moxxmpp/src/namespaces.dart'; @@ -106,7 +107,7 @@ class UserAvatarManager extends XmppManagerBase { ) async { final pubsub = _getPubSubManager(); final result = await pubsub.publish( - getAttributes().getFullJID().toBare().toString(), + getAttributes().getFullJID().toBare(), userAvatarDataXmlns, XMLNode.xmlns( tag: 'data', @@ -133,7 +134,7 @@ class UserAvatarManager extends XmppManagerBase { ) async { final pubsub = _getPubSubManager(); final result = await pubsub.publish( - getAttributes().getFullJID().toBare().toString(), + getAttributes().getFullJID().toBare(), userAvatarMetadataXmlns, XMLNode.xmlns( tag: 'metadata', @@ -178,7 +179,7 @@ class UserAvatarManager extends XmppManagerBase { /// Returns the PubSub Id of an avatar after doing a disco#items query. /// Note that this assumes that there is only one (1) item published on /// the node. - Future> getAvatarId(String jid) async { + Future> getAvatarId(JID jid) async { final disco = getAttributes().getManagerById(discoManager)! as DiscoManager; final response = await disco.discoItemsQuery( jid, diff --git a/packages/moxxmpp/lib/src/xeps/xep_0115.dart b/packages/moxxmpp/lib/src/xeps/xep_0115.dart index f528439..75fefb3 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0115.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0115.dart @@ -181,7 +181,7 @@ class EntityCapabilitiesManager extends XmppManagerBase { final dm = getAttributes().getManagerById(discoManager)!; final discoRequest = await dm.discoInfoQuery( - event.jid.toString(), + event.jid, node: capabilityNode, ); if (discoRequest.isType()) { @@ -194,7 +194,7 @@ class EntityCapabilitiesManager extends XmppManagerBase { await dm.addCachedDiscoInfo( MapEntry( DiscoCacheKey( - event.jid.toString(), + event.jid, null, ), discoInfo, diff --git a/packages/moxxmpp/lib/src/xeps/xep_0359.dart b/packages/moxxmpp/lib/src/xeps/xep_0359.dart index 21c10e3..3033b21 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0359.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0359.dart @@ -68,7 +68,7 @@ class StableIdManager extends XmppManagerBase { logger.finest('Found stanza Id tag'); final attrs = getAttributes(); final disco = attrs.getManagerById(discoManager)!; - final result = await disco.discoInfoQuery(from.toString()); + final result = await disco.discoInfoQuery(from); if (result.isType()) { final info = result.get(); logger.finest('Got info for ${from.toString()}'); diff --git a/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart b/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart index b3d3491..658b69d 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0384/xep_0384.dart @@ -609,7 +609,7 @@ abstract class BaseOmemoManager extends XmppManagerBase { ); final deviceListPublish = await pm.publish( - bareJid.toString(), + bareJid, omemoDevicesXmlns, newDeviceList, id: 'current', @@ -621,7 +621,7 @@ abstract class BaseOmemoManager extends XmppManagerBase { } final deviceBundlePublish = await pm.publish( - bareJid.toString(), + bareJid, omemoBundlesXmlns, bundleToXML(bundle), id: '${bundle.id}', @@ -646,7 +646,7 @@ abstract class BaseOmemoManager extends XmppManagerBase { /// On failure, returns an OmemoError. Future> supportsOmemo(JID jid) async { final dm = getAttributes().getManagerById(discoManager)!; - final items = await dm.discoItemsQuery(jid.toBare().toString()); + final items = await dm.discoItemsQuery(jid.toBare()); if (items.isType()) return Result(UnknownOmemoError()); @@ -686,7 +686,7 @@ abstract class BaseOmemoManager extends XmppManagerBase { .toList(), ); final publishResult = await pm.publish( - jid.toString(), + jid, omemoDevicesXmlns, newPayload, id: 'current', diff --git a/packages/moxxmpp/lib/src/xeps/xep_0449.dart b/packages/moxxmpp/lib/src/xeps/xep_0449.dart index 07d5127..fb95432 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0449.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0449.dart @@ -267,7 +267,7 @@ class StickersManager extends XmppManagerBase { final pm = getAttributes().getManagerById(pubsubManager)!; return pm.publish( - jid.toBare().toString(), + jid.toBare(), stickersXmlns, pack.toXML(), id: pack.id, diff --git a/packages/moxxmpp/test/xeps/xep_0030_test.dart b/packages/moxxmpp/test/xeps/xep_0030_test.dart index 521dcb8..f38255f 100644 --- a/packages/moxxmpp/test/xeps/xep_0030_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0030_test.dart @@ -95,14 +95,12 @@ void main() { await Future.delayed(const Duration(seconds: 3)); final jid = JID.fromString('romeo@montague.lit/orchard'); - final result1 = disco.discoInfoQuery(jid.toString()); - final result2 = disco.discoInfoQuery(jid.toString()); + final result1 = disco.discoInfoQuery(jid); + final result2 = disco.discoInfoQuery(jid); await Future.delayed(const Duration(seconds: 1)); expect( - disco.infoTracker - .getRunningTasks(DiscoCacheKey(jid.toString(), null)) - .length, + disco.infoTracker.getRunningTasks(DiscoCacheKey(jid, null)).length, 1, ); fakeSocket.injectRawXml( @@ -140,7 +138,7 @@ void main() { ); // Query Alice's device - final result = await dm.discoInfoQuery(aliceJid.toString()); + final result = await dm.discoInfoQuery(aliceJid); expect(result.isType(), false); expect(tm.sentStanzas, 0); }); diff --git a/packages/moxxmpp/test/xeps/xep_0060_test.dart b/packages/moxxmpp/test/xeps/xep_0060_test.dart index ee58272..e559dbc 100644 --- a/packages/moxxmpp/test/xeps/xep_0060_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0060_test.dart @@ -12,7 +12,7 @@ class StubbedDiscoManager extends DiscoManager { @override Future> discoInfoQuery( - String entity, { + JID entity, { String? node, bool shouldEncrypt = true, bool shouldCache = true, @@ -32,7 +32,7 @@ class StubbedDiscoManager extends DiscoManager { @override Future>> discoItemsQuery( - String entity, { + JID entity, { String? node, bool shouldEncrypt = true, }) async { @@ -59,7 +59,7 @@ void main() { await tm.register(manager); final result = await manager.preprocessPublishOptions( - 'pubsub.server.example.org', + JID.fromString('pubsub.server.example.org'), 'urn:xmpp:omemo:2:bundles', const PubSubPublishOptions(maxItems: 'max'), ); @@ -76,7 +76,7 @@ void main() { await tm.register(manager); final result = await manager.preprocessPublishOptions( - 'pubsub.server.example.org', + JID.fromString('pubsub.server.example.org'), 'urn:xmpp:omemo:2:bundles', const PubSubPublishOptions(maxItems: 'max'), ); @@ -182,7 +182,7 @@ void main() { final item = XMLNode(tag: 'test-item'); final result = await connection.getManagerById(pubsubManager)!.publish( - 'pubsub.server.example.org', + JID.fromString('pubsub.server.example.org'), 'princely_musings', item, id: 'current', diff --git a/packages/moxxmpp/test/xeps/xep_0115_test.dart b/packages/moxxmpp/test/xeps/xep_0115_test.dart index f28aecc..6febc7c 100644 --- a/packages/moxxmpp/test/xeps/xep_0115_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0115_test.dart @@ -27,7 +27,7 @@ class StubbedDiscoManager extends DiscoManager { @override Future> discoInfoQuery( - String entity, { + JID entity, { String? node, bool shouldEncrypt = true, bool shouldCache = true,