From 0ec3777f44dda0d9e0c8d689ee97f5af22342527 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Tue, 6 Jun 2023 15:14:19 +0200 Subject: [PATCH] feat(xep): Managers register the sending callback --- .../moxxmpp/lib/src/managers/namespaces.dart | 1 + packages/moxxmpp/lib/src/message.dart | 46 +++++++--- packages/moxxmpp/lib/src/namespaces.dart | 2 +- .../staging/file_upload_notification.dart | 62 +++++++++++++ packages/moxxmpp/lib/src/xeps/xep_0066.dart | 21 +++++ packages/moxxmpp/lib/src/xeps/xep_0085.dart | 25 ++++-- packages/moxxmpp/lib/src/xeps/xep_0184.dart | 34 ++++--- packages/moxxmpp/lib/src/xeps/xep_0308.dart | 29 ++++-- packages/moxxmpp/lib/src/xeps/xep_0333.dart | 17 ++++ packages/moxxmpp/lib/src/xeps/xep_0334.dart | 45 +++++++++- packages/moxxmpp/lib/src/xeps/xep_0359.dart | 21 +++-- packages/moxxmpp/lib/src/xeps/xep_0385.dart | 2 + packages/moxxmpp/lib/src/xeps/xep_0424.dart | 73 ++++++++------- packages/moxxmpp/lib/src/xeps/xep_0444.dart | 29 ++++-- packages/moxxmpp/lib/src/xeps/xep_0447.dart | 51 ++++++----- packages/moxxmpp/lib/src/xeps/xep_0449.dart | 43 +++++---- packages/moxxmpp/lib/src/xeps/xep_0461.dart | 89 +++++++++++-------- packages/moxxmpp/test/helpers/manager.dart | 44 ++++----- packages/moxxmpp/test/xeps/xep_0030_test.dart | 3 +- packages/moxxmpp/test/xeps/xep_0060_test.dart | 12 ++- packages/moxxmpp/test/xeps/xep_0115_test.dart | 48 +++++----- packages/moxxmpp/test/xeps/xep_0449_test.dart | 13 +-- packages/moxxmpp/test/xeps/xep_0461_test.dart | 6 +- 23 files changed, 498 insertions(+), 218 deletions(-) diff --git a/packages/moxxmpp/lib/src/managers/namespaces.dart b/packages/moxxmpp/lib/src/managers/namespaces.dart index 03e40e0..713493d 100644 --- a/packages/moxxmpp/lib/src/managers/namespaces.dart +++ b/packages/moxxmpp/lib/src/managers/namespaces.dart @@ -31,3 +31,4 @@ const lastMessageCorrectionManager = 'org.moxxmpp.lastmessagecorrectionmanager'; const messageReactionsManager = 'org.moxxmpp.messagereactionsmanager'; const stickersManager = 'org.moxxmpp.stickersmanager'; const entityCapabilitiesManager = 'org.moxxmpp.entitycapabilities'; +const messageProcessingHintManager = 'org.moxxmpp.messageprocessinghint'; diff --git a/packages/moxxmpp/lib/src/message.dart b/packages/moxxmpp/lib/src/message.dart index 1995cb5..701d8ce 100644 --- a/packages/moxxmpp/lib/src/message.dart +++ b/packages/moxxmpp/lib/src/message.dart @@ -40,15 +40,6 @@ class MessageBodyData { text: body, ); } - - static List messageSendingCallback(TypedMap extensions) { - if (extensions.get() != null) { - return []; - } - - final data = extensions.get(); - return data != null ? [data.toXML()] : []; - } } class MessageIdData { @@ -113,11 +104,16 @@ class MessageDetails { } class MessageManager extends XmppManagerBase { - MessageManager(this.messageSendingCallbacks) : super(messageManager); + MessageManager() : super(messageManager); /// A list of callbacks that are called when a message is sent in order to add /// appropriate child elements. - final List messageSendingCallbacks; + final List _messageSendingCallbacks = + List.empty(growable: true); + + void registerMessageSendingCallback(MessageSendingCallback callback) { + _messageSendingCallbacks.add(callback); + } @override List getIncomingStanzaHandlers() => [ @@ -190,7 +186,7 @@ class MessageManager extends XmppManagerBase { to: to.toString(), id: extensions.get()?.id, type: 'chat', - children: messageSendingCallbacks + children: _messageSendingCallbacks .map((c) => c(extensions)) .flattened .toList(), @@ -387,4 +383,30 @@ class MessageManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + if (extensions.get() != null) { + return []; + } + if (extensions.get() != null) { + return []; + } + if (extensions.get() != null) { + return []; + } + if (extensions.get() != null) { + return []; + } + + final data = extensions.get(); + return data != null ? [data.toXML()] : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/namespaces.dart b/packages/moxxmpp/lib/src/namespaces.dart index f0be542..eb98e63 100644 --- a/packages/moxxmpp/lib/src/namespaces.dart +++ b/packages/moxxmpp/lib/src/namespaces.dart @@ -96,7 +96,7 @@ const httpFileUploadXmlns = 'urn:xmpp:http:upload:0'; // XEP-0372 const referenceXmlns = 'urn:xmpp:reference:0'; -// XEP-380 +// XEP-0380 const emeXmlns = 'urn:xmpp:eme:0'; const emeOtr = 'urn:xmpp:otr:0'; const emeLegacyOpenPGP = 'jabber:x:encrypted'; diff --git a/packages/moxxmpp/lib/src/xeps/staging/file_upload_notification.dart b/packages/moxxmpp/lib/src/xeps/staging/file_upload_notification.dart index 51d60ff..c429cc8 100644 --- a/packages/moxxmpp/lib/src/xeps/staging/file_upload_notification.dart +++ b/packages/moxxmpp/lib/src/xeps/staging/file_upload_notification.dart @@ -2,8 +2,11 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; +import 'package:moxxmpp/src/stringxml.dart'; +import 'package:moxxmpp/src/util/typed_map.dart'; import 'package:moxxmpp/src/xeps/xep_0446.dart'; /// NOTE: Specified by https://github.com/PapaTutuWawa/custom-xeps/blob/master/xep-xxxx-file-upload-notifications.md @@ -15,6 +18,16 @@ class FileUploadNotificationData { /// The file metadata indicated in the upload notification. final FileMetadataData metadata; + + XMLNode toXML() { + return XMLNode.xmlns( + tag: 'file-upload', + xmlns: fileUploadNotificationXmlns, + children: [ + metadata.toXML(), + ], + ); + } } /// Indicates that a file upload has been cancelled. @@ -23,6 +36,16 @@ class FileUploadNotificationCancellationData { /// The id of the upload notifiaction that is cancelled. final String id; + + XMLNode toXML() { + return XMLNode.xmlns( + tag: 'cancelled', + xmlns: fileUploadNotificationXmlns, + attributes: { + 'id': id, + }, + ); + } } /// Indicates that a file upload has been completed. @@ -31,6 +54,16 @@ class FileUploadNotificationReplacementData { /// The id of the upload notifiaction that is replaced. final String id; + + XMLNode toXML() { + return XMLNode.xmlns( + tag: 'replaces', + xmlns: fileUploadNotificationXmlns, + attributes: { + 'id': id, + }, + ); + } } class FileUploadNotificationManager extends XmppManagerBase { @@ -107,4 +140,33 @@ class FileUploadNotificationManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + final fun = extensions.get(); + if (fun != null) { + return [fun.toXML()]; + } + + final cancel = extensions.get(); + if (cancel != null) { + return [cancel.toXML()]; + } + + final replace = extensions.get(); + if (replace != null) { + return [replace.toXML()]; + } + + return []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0066.dart b/packages/moxxmpp/lib/src/xeps/xep_0066.dart index a008be3..5b0d51f 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0066.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0066.dart @@ -2,9 +2,11 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; +import 'package:moxxmpp/src/util/typed_map.dart'; /// A data class representing the jabber:x:oob tag. class OOBData { @@ -65,4 +67,23 @@ class OOBManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + data.toXML(), + ] + : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0085.dart b/packages/moxxmpp/lib/src/xeps/xep_0085.dart index 62c788e..1074696 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0085.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0085.dart @@ -2,6 +2,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -53,11 +54,6 @@ enum ChatState { xmlns: chatStateXmlns, ); } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null ? [data.toXML()] : []; - } } class ChatStateManager extends XmppManagerBase { @@ -109,4 +105,23 @@ class ChatStateManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + data.toXML(), + ] + : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0184.dart b/packages/moxxmpp/lib/src/xeps/xep_0184.dart index aa8e5a5..76aa678 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0184.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0184.dart @@ -4,6 +4,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -25,15 +26,6 @@ class MessageDeliveryReceiptData { xmlns: deliveryXmlns, ); } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null - ? [ - data.toXML(), - ] - : []; - } } class MessageDeliveryReceivedData { @@ -49,11 +41,6 @@ class MessageDeliveryReceivedData { attributes: {'id': id}, ); } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null ? [data.toXML()] : []; - } } class MessageDeliveryReceiptManager extends XmppManagerBase { @@ -116,4 +103,23 @@ class MessageDeliveryReceiptManager extends XmppManagerBase { ); return state..done = true; } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + data.toXML(), + ] + : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0308.dart b/packages/moxxmpp/lib/src/xeps/xep_0308.dart index 7621841..d38ddf8 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0308.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0308.dart @@ -2,6 +2,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -22,15 +23,6 @@ class LastMessageCorrectionData { }, ); } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null - ? [ - data.toXML(), - ] - : []; - } } class LastMessageCorrectionManager extends XmppManagerBase { @@ -64,4 +56,23 @@ class LastMessageCorrectionManager extends XmppManagerBase { LastMessageCorrectionData(edit.attributes['id']! as String), ); } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + data.toXML(), + ] + : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0333.dart b/packages/moxxmpp/lib/src/xeps/xep_0333.dart index 5858d20..99b6289 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0333.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0333.dart @@ -4,9 +4,11 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; +import 'package:moxxmpp/src/util/typed_map.dart'; class ChatMarkerData { const ChatMarkerData(this.isMarkable); @@ -79,4 +81,19 @@ class ChatMarkerManager extends XmppManagerBase { return state..done = true; } + + // TODO: Implement + List _messageSendingCallback(TypedMap extensions) { + return []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0334.dart b/packages/moxxmpp/lib/src/xeps/xep_0334.dart index 5b372a6..401c83f 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0334.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0334.dart @@ -1,4 +1,10 @@ +import 'package:moxxmpp/src/managers/base.dart'; +import 'package:moxxmpp/src/managers/data.dart'; +import 'package:moxxmpp/src/managers/handlers.dart'; +import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; +import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/util/typed_map.dart'; @@ -46,8 +52,35 @@ enum MessageProcessingHint { xmlns: messageProcessingHintsXmlns, ); } +} - static List messageSendingCallback(TypedMap extensions) { +class MessageProcessingHintManager extends XmppManagerBase { + MessageProcessingHintManager() : super(messageProcessingHintManager); + + @override + Future isSupported() async => true; + + @override + List getIncomingStanzaHandlers() => [ + StanzaHandler( + stanzaTag: 'message', + tagXmlns: messageProcessingHintsXmlns, + callback: _onMessage, + // Before the message handler + priority: -99, + ), + ]; + + // TODO: Test + Future _onMessage( + Stanza stanza, + StanzaHandlerData state, + ) async { + final element = stanza.findTagsByXmlns(messageProcessingHintsXmlns).first; + return state..extensions.set(MessageProcessingHint.fromName(element.tag)); + } + + List _messageSendingCallback(TypedMap extensions) { final data = extensions.get(); return data != null ? [ @@ -55,4 +88,14 @@ enum MessageProcessingHint { ] : []; } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0359.dart b/packages/moxxmpp/lib/src/xeps/xep_0359.dart index 4576efd..5cf561c 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0359.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0359.dart @@ -3,6 +3,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -65,11 +66,6 @@ class StableIdData { if (stanzaIds != null) ...stanzaIds!.map((s) => s.toXML()), ]; } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null ? data.toXML() : []; - } } class StableIdManager extends XmppManagerBase { @@ -126,4 +122,19 @@ class StableIdManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null ? data.toXML() : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0385.dart b/packages/moxxmpp/lib/src/xeps/xep_0385.dart index bbf66d0..6f03339 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0385.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0385.dart @@ -70,7 +70,9 @@ StatelessMediaSharingData parseSIMSElement(XMLNode node) { ); } +@Deprecated('Not maintained') class SIMSManager extends XmppManagerBase { + @Deprecated('Not maintained') SIMSManager() : super(simsManager); @override diff --git a/packages/moxxmpp/lib/src/xeps/xep_0424.dart b/packages/moxxmpp/lib/src/xeps/xep_0424.dart index 08e8bc5..4b3e8b8 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0424.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0424.dart @@ -2,6 +2,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -15,37 +16,6 @@ class MessageRetractionData { /// The id of the message that is retracted. final String id; - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null - ? [ - XMLNode.xmlns( - tag: 'apply-to', - xmlns: fasteningXmlns, - attributes: { - 'id': data.id, - }, - children: [ - XMLNode.xmlns( - tag: 'retract', - xmlns: messageRetractionXmlns, - ), - ], - ), - if (data.fallback != null) - XMLNode( - tag: 'body', - text: data.fallback, - ), - if (data.fallback != null) - XMLNode.xmlns( - tag: 'fallback', - xmlns: fallbackIndicationXmlns, - ), - ] - : []; - } } class MessageRetractionManager extends XmppManagerBase { @@ -92,4 +62,45 @@ class MessageRetractionManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + XMLNode.xmlns( + tag: 'apply-to', + xmlns: fasteningXmlns, + attributes: { + 'id': data.id, + }, + children: [ + XMLNode.xmlns( + tag: 'retract', + xmlns: messageRetractionXmlns, + ), + ], + ), + if (data.fallback != null) + XMLNode( + tag: 'body', + text: data.fallback, + ), + if (data.fallback != null) + XMLNode.xmlns( + tag: 'fallback', + xmlns: fallbackIndicationXmlns, + ), + ] + : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0444.dart b/packages/moxxmpp/lib/src/xeps/xep_0444.dart index 282ec74..dd85dc6 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0444.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0444.dart @@ -2,6 +2,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -27,15 +28,6 @@ class MessageReactions { }).toList(), ); } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null - ? [ - data.toXML(), - ] - : []; - } } class MessageReactionsManager extends XmppManagerBase { @@ -76,4 +68,23 @@ class MessageReactionsManager extends XmppManagerBase { ), ); } + + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + data.toXML(), + ] + : []; + } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0447.dart b/packages/moxxmpp/lib/src/xeps/xep_0447.dart index be9b474..57aeb80 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0447.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0447.dart @@ -3,6 +3,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -119,26 +120,6 @@ class StatelessFileSharingData { source is StatelessFileSharingUrlSource, ) as StatelessFileSharingUrlSource?; } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - if (data == null) { - return []; - } - - // TODO(Unknown): Consider all sources? - final source = data.sources.first; - OOBData? oob; - if (source is StatelessFileSharingUrlSource && data.includeOOBFallback) { - // SFS recommends OOB as a fallback - oob = OOBData(source.url, null); - } - - return [ - data.toXML(), - if (oob != null) oob.toXML(), - ]; - } } class SFSManager extends XmppManagerBase { @@ -159,6 +140,26 @@ class SFSManager extends XmppManagerBase { @override Future isSupported() async => true; + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + if (data == null) { + return []; + } + + // TODO(Unknown): Consider all sources? + final source = data.sources.first; + OOBData? oob; + if (source is StatelessFileSharingUrlSource && data.includeOOBFallback) { + // SFS recommends OOB as a fallback + oob = OOBData(source.url, null); + } + + return [ + data.toXML(), + if (oob != null) oob.toXML(), + ]; + } + Future _onMessage( Stanza message, StanzaHandlerData state, @@ -170,4 +171,14 @@ class SFSManager extends XmppManagerBase { StatelessFileSharingData.fromXML(sfs), ); } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0449.dart b/packages/moxxmpp/lib/src/xeps/xep_0449.dart index efda5a5..be2da81 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0449.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0449.dart @@ -4,6 +4,7 @@ import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/rfcs/rfc_4790.dart'; import 'package:moxxmpp/src/stanza.dart'; @@ -236,22 +237,6 @@ class StickersData { /// The metadata of the sticker. final StatelessFileSharingData sticker; - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null - ? [ - XMLNode.xmlns( - tag: 'sticker', - xmlns: stickersXmlns, - attributes: { - 'pack': data.stickerPackId, - }, - ), - data.sticker.toXML(), - ] - : []; - } } class StickersManager extends XmppManagerBase { @@ -285,6 +270,22 @@ class StickersManager extends XmppManagerBase { ); } + List _messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + XMLNode.xmlns( + tag: 'sticker', + xmlns: stickersXmlns, + attributes: { + 'pack': data.stickerPackId, + }, + ), + data.sticker.toXML(), + ] + : []; + } + /// Publishes the StickerPack [pack] to the PubSub node of [jid]. If specified, then /// [accessModel] will be used as the PubSub node's access model. /// @@ -350,4 +351,14 @@ class StickersManager extends XmppManagerBase { return Result(stickerPack); } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(_messageSendingCallback); + } } diff --git a/packages/moxxmpp/lib/src/xeps/xep_0461.dart b/packages/moxxmpp/lib/src/xeps/xep_0461.dart index 95af407..0cb0b23 100644 --- a/packages/moxxmpp/lib/src/xeps/xep_0461.dart +++ b/packages/moxxmpp/lib/src/xeps/xep_0461.dart @@ -1,8 +1,10 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:moxxmpp/src/jid.dart'; import 'package:moxxmpp/src/managers/base.dart'; import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/namespaces.dart'; +import 'package:moxxmpp/src/message.dart'; import 'package:moxxmpp/src/namespaces.dart'; import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stringxml.dart'; @@ -51,44 +53,6 @@ class ReplyData { return body!.replaceRange(start!, end, ''); } - - static List messageSendingCallback(TypedMap extensions) { - final data = extensions.get(); - return data != null - ? [ - XMLNode.xmlns( - tag: 'reply', - xmlns: replyXmlns, - attributes: { - // The to attribute is optional - if (data.jid != null) 'to': data.jid!.toString(), - - 'id': data.id, - }, - ), - if (data.body != null) - XMLNode( - tag: 'body', - text: data.body, - ), - if (data.body != null) - XMLNode.xmlns( - tag: 'fallback', - xmlns: fallbackXmlns, - attributes: {'for': replyXmlns}, - children: [ - XMLNode( - tag: 'body', - attributes: { - 'start': data.start!.toString(), - 'end': data.end!.toString(), - }, - ), - ], - ), - ] - : []; - } } /// Internal class describing how to build a message with a quote fallback body. @@ -138,6 +102,45 @@ class MessageRepliesManager extends XmppManagerBase { @override Future isSupported() async => true; + @visibleForTesting + List messageSendingCallback(TypedMap extensions) { + final data = extensions.get(); + return data != null + ? [ + XMLNode.xmlns( + tag: 'reply', + xmlns: replyXmlns, + attributes: { + // The to attribute is optional + if (data.jid != null) 'to': data.jid!.toString(), + + 'id': data.id, + }, + ), + if (data.body != null) + XMLNode( + tag: 'body', + text: data.body, + ), + if (data.body != null) + XMLNode.xmlns( + tag: 'fallback', + xmlns: fallbackXmlns, + attributes: {'for': replyXmlns}, + children: [ + XMLNode( + tag: 'body', + attributes: { + 'start': data.start!.toString(), + 'end': data.end!.toString(), + }, + ), + ], + ), + ] + : []; + } + Future _onMessage( Stanza stanza, StanzaHandlerData state, @@ -167,4 +170,14 @@ class MessageRepliesManager extends XmppManagerBase { ), ); } + + @override + Future postRegisterCallback() async { + await super.postRegisterCallback(); + + // Register the sending callback + getAttributes() + .getManagerById(messageManager) + ?.registerMessageSendingCallback(messageSendingCallback); + } } diff --git a/packages/moxxmpp/test/helpers/manager.dart b/packages/moxxmpp/test/helpers/manager.dart index a3c02b7..b50381b 100644 --- a/packages/moxxmpp/test/helpers/manager.dart +++ b/packages/moxxmpp/test/helpers/manager.dart @@ -41,27 +41,31 @@ class TestingManagerHolder { return _managers[id] as T?; } - Future register(XmppManagerBase manager) async { - manager.register( - XmppManagerAttributes( - sendStanza: _sendStanza, - getConnection: () => XmppConnection( - TestingReconnectionPolicy(), - AlwaysConnectedConnectivityManager(), - ClientToServerNegotiator(), - socket, + Future register(List managers) async { + for (final manager in managers) { + manager.register( + XmppManagerAttributes( + sendStanza: _sendStanza, + getConnection: () => XmppConnection( + TestingReconnectionPolicy(), + AlwaysConnectedConnectivityManager(), + ClientToServerNegotiator(), + socket, + ), + getConnectionSettings: () => settings, + sendNonza: (_) {}, + sendEvent: sentEvents.add, + getSocket: () => socket, + getNegotiatorById: getNegotiatorNullStub, + getFullJID: () => jid, + getManagerById: _getManagerById, ), - getConnectionSettings: () => settings, - sendNonza: (_) {}, - sendEvent: sentEvents.add, - getSocket: () => socket, - getNegotiatorById: getNegotiatorNullStub, - getFullJID: () => jid, - getManagerById: _getManagerById, - ), - ); + ); + _managers[manager.id] = manager; + } - await manager.postRegisterCallback(); - _managers[manager.id] = manager; + for (final manager in managers) { + await manager.postRegisterCallback(); + } } } diff --git a/packages/moxxmpp/test/xeps/xep_0030_test.dart b/packages/moxxmpp/test/xeps/xep_0030_test.dart index f7ec19f..69f8d24 100644 --- a/packages/moxxmpp/test/xeps/xep_0030_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0030_test.dart @@ -120,8 +120,7 @@ void main() { final ecm = EntityCapabilitiesManager(''); final dm = DiscoManager([]); - await tm.register(dm); - await tm.register(ecm); + await tm.register([dm, ecm]); // Inject a capability hash into the cache final aliceJid = JID.fromString('alice@example.org/abc123'); diff --git a/packages/moxxmpp/test/xeps/xep_0060_test.dart b/packages/moxxmpp/test/xeps/xep_0060_test.dart index 44d56a0..da31d14 100644 --- a/packages/moxxmpp/test/xeps/xep_0060_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0060_test.dart @@ -55,8 +55,10 @@ void main() { () async { final manager = PubSubManager(); final tm = TestingManagerHolder(); - await tm.register(StubbedDiscoManager(false)); - await tm.register(manager); + await tm.register([ + StubbedDiscoManager(false), + manager, + ]); final result = await manager.preprocessPublishOptions( JID.fromString('pubsub.server.example.org'), @@ -72,8 +74,10 @@ void main() { () async { final manager = PubSubManager(); final tm = TestingManagerHolder(); - await tm.register(StubbedDiscoManager(true)); - await tm.register(manager); + await tm.register([ + StubbedDiscoManager(true), + manager, + ]); final result = await manager.preprocessPublishOptions( JID.fromString('pubsub.server.example.org'), diff --git a/packages/moxxmpp/test/xeps/xep_0115_test.dart b/packages/moxxmpp/test/xeps/xep_0115_test.dart index 6febc7c..3daf321 100644 --- a/packages/moxxmpp/test/xeps/xep_0115_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0115_test.dart @@ -319,8 +319,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register(StubbedDiscoManager()); - await tm.register(manager); + await tm.register([ + StubbedDiscoManager(), + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -352,8 +354,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register(StubbedDiscoManager()); - await tm.register(manager); + await tm.register([ + StubbedDiscoManager(), + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -385,10 +389,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register( + await tm.register([ StubbedDiscoManager()..multipleEqualIdentities = true, - ); - await tm.register(manager); + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -420,10 +424,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register( + await tm.register([ StubbedDiscoManager()..multipleEqualFeatures = true, - ); - await tm.register(manager); + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -455,10 +459,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register( + await tm.register([ StubbedDiscoManager()..multipleExtendedFormsWithSameType = true, - ); - await tm.register(manager); + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -490,10 +494,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register( + await tm.register([ StubbedDiscoManager()..invalidExtension1 = true, - ); - await tm.register(manager); + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -527,10 +531,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register( + await tm.register([ StubbedDiscoManager()..invalidExtension2 = true, - ); - await tm.register(manager); + manager, + ]); await manager.onPresence( PresenceReceivedEvent( @@ -564,10 +568,10 @@ void main() { final tm = TestingManagerHolder(); final manager = EntityCapabilitiesManager(''); - await tm.register( + await tm.register([ StubbedDiscoManager()..invalidExtension3 = true, - ); - await tm.register(manager); + manager, + ]); await manager.onPresence( PresenceReceivedEvent( diff --git a/packages/moxxmpp/test/xeps/xep_0449_test.dart b/packages/moxxmpp/test/xeps/xep_0449_test.dart index a322e7f..c58d972 100644 --- a/packages/moxxmpp/test/xeps/xep_0449_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0449_test.dart @@ -234,10 +234,7 @@ void main() { }); test('Test sending a sticker', () async { - final manager = MessageManager([ - StatelessFileSharingData.messageSendingCallback, - StickersData.messageSendingCallback, - ]); + final manager = MessageManager(); final holder = TestingManagerHolder( stubSocket: StubTCPSocket([ StanzaExpectation( @@ -265,7 +262,11 @@ void main() { ), ]), ); - await holder.register(manager); + await holder.register([ + manager, + StickersManager(), + SFSManager(), + ]); await manager.sendMessage2( JID.fromString('user@example.org'), @@ -360,7 +361,7 @@ void main() { password: 'aaaa', ); await conn.registerManagers([ - MessageManager([]), + MessageManager(), SFSManager(), StickersManager(), ]); diff --git a/packages/moxxmpp/test/xeps/xep_0461_test.dart b/packages/moxxmpp/test/xeps/xep_0461_test.dart index 07df8b0..12da342 100644 --- a/packages/moxxmpp/test/xeps/xep_0461_test.dart +++ b/packages/moxxmpp/test/xeps/xep_0461_test.dart @@ -45,7 +45,7 @@ void main() { }); test('Test calling the message sending callback', () { - final result = ReplyData.messageSendingCallback( + final result = MessageRepliesManager().messageSendingCallback( TypedMap() ..set( ReplyData.fromQuoteData( @@ -129,7 +129,7 @@ void main() { password: 'aaaa', ); await conn.registerManagers([ - MessageManager([]), + MessageManager(), MessageRepliesManager(), ]); await conn.registerFeatureNegotiators([ @@ -228,7 +228,7 @@ void main() { password: 'aaaa', ); await conn.registerManagers([ - MessageManager([]), + MessageManager(), MessageRepliesManager(), ]); await conn.registerFeatureNegotiators([