feat(core): Rework [MessageEvent]

This commit is contained in:
PapaTutuWawa 2023-06-06 16:19:16 +02:00
parent 4d76b9f57a
commit 10a5046431
6 changed files with 39 additions and 117 deletions

View File

@ -13,6 +13,7 @@
- Sent stanzas are now kept in a queue until sent. - Sent stanzas are now kept in a queue until sent.
- **BREAKING**: `MessageManager.sendMessage` does not use `MessageDetails` anymore. Instead, use `TypedMap`. - **BREAKING**: `MessageManager.sendMessage` does not use `MessageDetails` anymore. Instead, use `TypedMap`.
- `MessageManager` now allows registering callbacks for adding data whenever a message is sent. - `MessageManager` now allows registering callbacks for adding data whenever a message is sent.
- **BREAKING**: `MessageEvent` now makes use of `TypedMap`.
## 0.3.1 ## 0.3.1

View File

@ -4,19 +4,10 @@ import 'package:moxxmpp/src/jid.dart';
import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/data.dart';
import 'package:moxxmpp/src/roster/roster.dart'; import 'package:moxxmpp/src/roster/roster.dart';
import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stanza.dart';
import 'package:moxxmpp/src/util/typed_map.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_0060/xep_0060.dart'; import 'package:moxxmpp/src/xeps/xep_0060/xep_0060.dart';
import 'package:moxxmpp/src/xeps/xep_0066.dart';
import 'package:moxxmpp/src/xeps/xep_0084.dart'; import 'package:moxxmpp/src/xeps/xep_0084.dart';
import 'package:moxxmpp/src/xeps/xep_0085.dart';
import 'package:moxxmpp/src/xeps/xep_0334.dart';
import 'package:moxxmpp/src/xeps/xep_0359.dart';
import 'package:moxxmpp/src/xeps/xep_0385.dart';
import 'package:moxxmpp/src/xeps/xep_0424.dart';
import 'package:moxxmpp/src/xeps/xep_0444.dart';
import 'package:moxxmpp/src/xeps/xep_0446.dart';
import 'package:moxxmpp/src/xeps/xep_0447.dart';
import 'package:moxxmpp/src/xeps/xep_0461.dart';
abstract class XmppEvent {} abstract class XmppEvent {}
@ -75,60 +66,31 @@ class RosterUpdatedEvent extends XmppEvent {
/// Triggered when a message is received /// Triggered when a message is received
class MessageEvent extends XmppEvent { class MessageEvent extends XmppEvent {
MessageEvent({ MessageEvent(
required this.body, this.from,
required this.fromJid, this.to,
required this.toJid, this.id,
required this.sid, this.extensions, {
required this.isCarbon,
required this.deliveryReceiptRequested,
required this.isMarkable,
required this.encrypted,
required this.other,
this.originId,
this.stanzaIds,
this.error,
this.type, this.type,
this.oob, this.error,
this.sfs,
this.sims,
this.reply,
this.chatState,
this.fun,
this.funReplacement,
this.funCancellation,
this.messageRetraction,
this.messageCorrectionId,
this.messageReactions,
this.messageProcessingHints,
this.stickerPackId,
}); });
final StanzaError? error;
final String body; /// The from attribute of the message.
final JID fromJid; final JID from;
final JID toJid;
final String sid; /// The to attribute of the message.
final JID to;
/// The id attribute of the message.
final String id;
/// The type attribute of the message.
final String? type; final String? type;
final String? originId;
final List<StanzaId>? stanzaIds; final StanzaError? error;
final bool isCarbon;
final bool deliveryReceiptRequested; /// Data added by other handlers.
final bool isMarkable; final TypedMap extensions;
final OOBData? oob;
final StatelessFileSharingData? sfs;
final StatelessMediaSharingData? sims;
final ReplyData? reply;
final ChatState? chatState;
final FileMetadataData? fun;
final String? funReplacement;
final String? funCancellation;
final bool encrypted;
final MessageRetractionData? messageRetraction;
final String? messageCorrectionId;
final MessageReactions? messageReactions;
final List<MessageProcessingHint>? messageProcessingHints;
final String? stickerPackId;
final Map<String, dynamic> other;
} }
/// Triggered when a client responds to our delivery receipt request /// Triggered when a client responds to our delivery receipt request

View File

@ -5,21 +5,10 @@ import 'package:moxxmpp/src/managers/base.dart';
import 'package:moxxmpp/src/managers/data.dart'; import 'package:moxxmpp/src/managers/data.dart';
import 'package:moxxmpp/src/managers/handlers.dart'; import 'package:moxxmpp/src/managers/handlers.dart';
import 'package:moxxmpp/src/managers/namespaces.dart'; import 'package:moxxmpp/src/managers/namespaces.dart';
import 'package:moxxmpp/src/namespaces.dart';
import 'package:moxxmpp/src/stanza.dart'; import 'package:moxxmpp/src/stanza.dart';
import 'package:moxxmpp/src/stringxml.dart'; import 'package:moxxmpp/src/stringxml.dart';
import 'package:moxxmpp/src/util/typed_map.dart'; import 'package:moxxmpp/src/util/typed_map.dart';
import 'package:moxxmpp/src/xeps/staging/file_upload_notification.dart';
import 'package:moxxmpp/src/xeps/xep_0066.dart'; import 'package:moxxmpp/src/xeps/xep_0066.dart';
import 'package:moxxmpp/src/xeps/xep_0085.dart';
import 'package:moxxmpp/src/xeps/xep_0184.dart';
import 'package:moxxmpp/src/xeps/xep_0280.dart';
import 'package:moxxmpp/src/xeps/xep_0333.dart';
import 'package:moxxmpp/src/xeps/xep_0334.dart';
import 'package:moxxmpp/src/xeps/xep_0359.dart';
import 'package:moxxmpp/src/xeps/xep_0385.dart';
import 'package:moxxmpp/src/xeps/xep_0424.dart';
import 'package:moxxmpp/src/xeps/xep_0444.dart';
import 'package:moxxmpp/src/xeps/xep_0447.dart'; import 'package:moxxmpp/src/xeps/xep_0447.dart';
import 'package:moxxmpp/src/xeps/xep_0449.dart'; import 'package:moxxmpp/src/xeps/xep_0449.dart';
import 'package:moxxmpp/src/xeps/xep_0461.dart'; import 'package:moxxmpp/src/xeps/xep_0461.dart';
@ -80,48 +69,14 @@ class MessageManager extends XmppManagerBase {
Stanza _, Stanza _,
StanzaHandlerData state, StanzaHandlerData state,
) async { ) async {
final message = state.stanza;
final body = message.firstTag('body');
final hints = List<MessageProcessingHint>.empty(growable: true);
for (final element
in message.findTagsByXmlns(messageProcessingHintsXmlns)) {
hints.add(MessageProcessingHint.fromName(element.tag));
}
getAttributes().sendEvent( getAttributes().sendEvent(
MessageEvent( MessageEvent(
body: body != null ? body.innerText() : '', JID.fromString(state.stanza.attributes['from']! as String),
fromJid: JID.fromString(message.attributes['from']! as String), JID.fromString(state.stanza.attributes['to']! as String),
toJid: JID.fromString(message.attributes['to']! as String), state.stanza.attributes['id']! as String,
sid: message.attributes['id']! as String, state.extensions,
originId: state.extensions.get<StableIdData>()?.originId, type: state.stanza.attributes['type'] as String?,
stanzaIds: state.extensions.get<StableIdData>()?.stanzaIds, error: StanzaError.fromStanza(state.stanza),
isCarbon: state.extensions.get<CarbonsData>()?.isCarbon ?? false,
deliveryReceiptRequested: state.extensions
.get<MessageDeliveryReceiptData>()
?.receiptRequested ??
false,
isMarkable: state.extensions.get<ChatMarkerData>()?.isMarkable ?? false,
type: message.attributes['type'] as String?,
oob: state.extensions.get<OOBData>(),
sfs: state.extensions.get<StatelessFileSharingData>(),
sims: state.extensions.get<StatelessMediaSharingData>(),
reply: state.extensions.get<ReplyData>(),
chatState: state.extensions.get<ChatState>(),
fun: state.extensions.get<FileUploadNotificationData>()?.metadata,
funReplacement:
state.extensions.get<FileUploadNotificationReplacementData>()?.id,
funCancellation:
state.extensions.get<FileUploadNotificationCancellationData>()?.id,
encrypted: state.encrypted,
messageRetraction: state.extensions.get<MessageRetractionData>(),
messageCorrectionId: state.extensions.get<MessageRetractionData>()?.id,
messageReactions: state.extensions.get<MessageReactions>(),
messageProcessingHints: hints.isEmpty ? null : hints,
stickerPackId: state.extensions.get<StickersData>()?.stickerPackId,
other: {},
error: StanzaError.fromStanza(message),
), ),
); );

View File

@ -76,11 +76,13 @@ class MessageProcessingHintManager extends XmppManagerBase {
Stanza stanza, Stanza stanza,
StanzaHandlerData state, StanzaHandlerData state,
) async { ) async {
// TODO(Unknown): Do we need to consider multiple hints?
final element = stanza.findTagsByXmlns(messageProcessingHintsXmlns).first; final element = stanza.findTagsByXmlns(messageProcessingHintsXmlns).first;
return state..extensions.set(MessageProcessingHint.fromName(element.tag)); return state..extensions.set(MessageProcessingHint.fromName(element.tag));
} }
List<XMLNode> _messageSendingCallback(TypedMap extensions) { List<XMLNode> _messageSendingCallback(TypedMap extensions) {
// TODO(Unknown): Do we need to consider multiple hints?
final data = extensions.get<MessageProcessingHint>(); final data = extensions.get<MessageProcessingHint>();
return data != null return data != null
? [ ? [

View File

@ -405,10 +405,12 @@ void main() {
); );
await Future<void>.delayed(const Duration(seconds: 2)); await Future<void>.delayed(const Duration(seconds: 2));
expect(messageEvent?.stickerPackId, 'EpRv28DHHzFrE4zd+xaNpVb4'); final sticker = messageEvent!.extensions.get<StickersData>()!;
expect(messageEvent?.sfs!.metadata.desc, '😘'); final sfs = messageEvent!.extensions.get<StatelessFileSharingData>()!;
expect(sticker.stickerPackId, 'EpRv28DHHzFrE4zd+xaNpVb4');
expect(sfs.metadata.desc, '😘');
expect( expect(
messageEvent?.sfs!.sources.first is StatelessFileSharingUrlSource, sfs.sources.first is StatelessFileSharingUrlSource,
true, true,
); );
}); });

View File

@ -160,7 +160,7 @@ void main() {
); );
await Future<void>.delayed(const Duration(seconds: 2)); await Future<void>.delayed(const Duration(seconds: 2));
final reply = messageEvent!.reply!; final reply = messageEvent!.extensions.get<ReplyData>()!;
expect(reply.withoutFallback, 'Great idea!'); expect(reply.withoutFallback, 'Great idea!');
expect(reply.id, 'message-id1'); expect(reply.id, 'message-id1');
expect(reply.jid, JID.fromString('anna@example.com/tablet')); expect(reply.jid, JID.fromString('anna@example.com/tablet'));
@ -262,7 +262,7 @@ void main() {
); );
await Future<void>.delayed(const Duration(seconds: 2)); await Future<void>.delayed(const Duration(seconds: 2));
final reply = messageEvent!.reply!; final reply = messageEvent!.extensions.get<ReplyData>()!;
expect(reply.withoutFallback, 'Great idea!'); expect(reply.withoutFallback, 'Great idea!');
expect(reply.id, 'message-id1'); expect(reply.id, 'message-id1');
expect(reply.jid, JID.fromString('anna@example.com/laptop')); expect(reply.jid, JID.fromString('anna@example.com/laptop'));