feat: Allow clients to read Message Processing Hints

This commit is contained in:
PapaTutuWawa 2022-12-09 12:46:17 +01:00
parent d9e4a3c1d4
commit cc1b371198
4 changed files with 38 additions and 2 deletions

View File

@ -6,6 +6,7 @@ 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_0066.dart';
import 'package:moxxmpp/src/xeps/xep_0085.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_0359.dart';
import 'package:moxxmpp/src/xeps/xep_0385.dart'; import 'package:moxxmpp/src/xeps/xep_0385.dart';
import 'package:moxxmpp/src/xeps/xep_0424.dart'; import 'package:moxxmpp/src/xeps/xep_0424.dart';
@ -77,6 +78,7 @@ class MessageEvent extends XmppEvent {
this.messageRetraction, this.messageRetraction,
this.messageCorrectionId, this.messageCorrectionId,
this.messageReactions, this.messageReactions,
this.messageProcessingHints,
}); });
final StanzaError? error; final StanzaError? error;
final String body; final String body;
@ -100,6 +102,7 @@ class MessageEvent extends XmppEvent {
final MessageRetractionData? messageRetraction; final MessageRetractionData? messageRetraction;
final String? messageCorrectionId; final String? messageCorrectionId;
final MessageReactions? messageReactions; final MessageReactions? messageReactions;
final List<MessageProcessingHint>? messageProcessingHints;
final Map<String, dynamic> other; final Map<String, dynamic> other;
} }

View File

@ -13,6 +13,7 @@ import 'package:moxxmpp/src/xeps/xep_0085.dart';
import 'package:moxxmpp/src/xeps/xep_0184.dart'; import 'package:moxxmpp/src/xeps/xep_0184.dart';
import 'package:moxxmpp/src/xeps/xep_0308.dart'; import 'package:moxxmpp/src/xeps/xep_0308.dart';
import 'package:moxxmpp/src/xeps/xep_0333.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_0359.dart';
import 'package:moxxmpp/src/xeps/xep_0424.dart'; import 'package:moxxmpp/src/xeps/xep_0424.dart';
import 'package:moxxmpp/src/xeps/xep_0444.dart'; import 'package:moxxmpp/src/xeps/xep_0444.dart';
@ -40,6 +41,7 @@ class MessageDetails {
this.messageRetraction, this.messageRetraction,
this.lastMessageCorrectionId, this.lastMessageCorrectionId,
this.messageReactions, this.messageReactions,
this.messageProcessingHints,
}); });
final String to; final String to;
final String? body; final String? body;
@ -59,6 +61,7 @@ class MessageDetails {
final MessageRetractionData? messageRetraction; final MessageRetractionData? messageRetraction;
final String? lastMessageCorrectionId; final String? lastMessageCorrectionId;
final MessageReactions? messageReactions; final MessageReactions? messageReactions;
final List<MessageProcessingHint>? messageProcessingHints;
} }
class MessageManager extends XmppManagerBase { class MessageManager extends XmppManagerBase {
@ -84,6 +87,11 @@ class MessageManager extends XmppManagerBase {
final message = state.stanza; final message = state.stanza;
final body = message.firstTag('body'); final body = message.firstTag('body');
final hints = List<MessageProcessingHint>.empty(growable: true);
for (final element in message.findTagsByXmlns(messageProcessingHintsXmlns)) {
hints.add(messageProcessingHintFromXml(element));
}
getAttributes().sendEvent(MessageEvent( getAttributes().sendEvent(MessageEvent(
body: body != null ? body.innerText() : '', body: body != null ? body.innerText() : '',
fromJid: JID.fromString(message.attributes['from']! as String), fromJid: JID.fromString(message.attributes['from']! as String),
@ -106,6 +114,9 @@ class MessageManager extends XmppManagerBase {
messageRetraction: state.messageRetraction, messageRetraction: state.messageRetraction,
messageCorrectionId: state.lastMessageCorrectionSid, messageCorrectionId: state.lastMessageCorrectionSid,
messageReactions: state.messageReactions, messageReactions: state.messageReactions,
messageProcessingHints: hints.isEmpty ?
null :
hints,
other: state.other, other: state.other,
error: StanzaError.fromStanza(message), error: StanzaError.fromStanza(message),
),); ),);
@ -270,6 +281,12 @@ class MessageManager extends XmppManagerBase {
stanza.addChild(details.messageReactions!.toXml()); stanza.addChild(details.messageReactions!.toXml());
} }
if (details.messageProcessingHints != null) {
for (final hint in details.messageProcessingHints!) {
stanza.addChild(hint.toXml());
}
}
getAttributes().sendStanza(stanza, awaitable: false); getAttributes().sendStanza(stanza, awaitable: false);
} }
} }

View File

@ -129,6 +129,12 @@ class XMLNode {
}).toList(); }).toList();
} }
List<XMLNode> findTagsByXmlns(String xmlns) {
return children
.where((element) => element.attributes['xmlns'] == xmlns)
.toList();
}
/// Returns the inner text of the node. If none is set, returns the "". /// Returns the inner text of the node. If none is set, returns the "".
String innerText() { String innerText() {
return text ?? ''; return text ?? '';

View File

@ -8,8 +8,18 @@ enum MessageProcessingHint {
store, store,
} }
/// NOTE: We do not define a function for turning a Message Processing Hint element into MessageProcessingHint messageProcessingHintFromXml(XMLNode element) {
/// an enum value since the elements do not concern us as a client. switch (element.tag) {
case 'no-permanent-store': return MessageProcessingHint.noPermanentStore;
case 'no-store': return MessageProcessingHint.noStore;
case 'no-copy': return MessageProcessingHint.noCopies;
case 'store': return MessageProcessingHint.store;
}
assert(false, 'Invalid Message Processing Hint: ${element.tag}');
return MessageProcessingHint.noStore;
}
extension XmlExtension on MessageProcessingHint { extension XmlExtension on MessageProcessingHint {
XMLNode toXml() { XMLNode toXml() {
String tag; String tag;