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.
- **BREAKING**: `MessageManager.sendMessage` does not use `MessageDetails` anymore. Instead, use `TypedMap`.
- `MessageManager` now allows registering callbacks for adding data whenever a message is sent.
- **BREAKING**: `MessageEvent` now makes use of `TypedMap`.
## 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/roster/roster.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_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_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 {}
@ -75,60 +66,31 @@ class RosterUpdatedEvent extends XmppEvent {
/// Triggered when a message is received
class MessageEvent extends XmppEvent {
MessageEvent({
required this.body,
required this.fromJid,
required this.toJid,
required this.sid,
required this.isCarbon,
required this.deliveryReceiptRequested,
required this.isMarkable,
required this.encrypted,
required this.other,
this.originId,
this.stanzaIds,
this.error,
MessageEvent(
this.from,
this.to,
this.id,
this.extensions, {
this.type,
this.oob,
this.sfs,
this.sims,
this.reply,
this.chatState,
this.fun,
this.funReplacement,
this.funCancellation,
this.messageRetraction,
this.messageCorrectionId,
this.messageReactions,
this.messageProcessingHints,
this.stickerPackId,
this.error,
});
final StanzaError? error;
final String body;
final JID fromJid;
final JID toJid;
final String sid;
/// The from attribute of the message.
final JID from;
/// 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? originId;
final List<StanzaId>? stanzaIds;
final bool isCarbon;
final bool deliveryReceiptRequested;
final bool isMarkable;
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;
final StanzaError? error;
/// Data added by other handlers.
final TypedMap extensions;
}
/// 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/handlers.dart';
import 'package:moxxmpp/src/managers/namespaces.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/staging/file_upload_notification.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_0449.dart';
import 'package:moxxmpp/src/xeps/xep_0461.dart';
@ -80,48 +69,14 @@ class MessageManager extends XmppManagerBase {
Stanza _,
StanzaHandlerData state,
) 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(
MessageEvent(
body: body != null ? body.innerText() : '',
fromJid: JID.fromString(message.attributes['from']! as String),
toJid: JID.fromString(message.attributes['to']! as String),
sid: message.attributes['id']! as String,
originId: state.extensions.get<StableIdData>()?.originId,
stanzaIds: state.extensions.get<StableIdData>()?.stanzaIds,
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),
JID.fromString(state.stanza.attributes['from']! as String),
JID.fromString(state.stanza.attributes['to']! as String),
state.stanza.attributes['id']! as String,
state.extensions,
type: state.stanza.attributes['type'] as String?,
error: StanzaError.fromStanza(state.stanza),
),
);

View File

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

View File

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

View File

@ -160,7 +160,7 @@ void main() {
);
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.id, 'message-id1');
expect(reply.jid, JID.fromString('anna@example.com/tablet'));
@ -262,7 +262,7 @@ void main() {
);
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.id, 'message-id1');
expect(reply.jid, JID.fromString('anna@example.com/laptop'));