ReceiptEventContent.fromJson constructor
ReceiptEventContent.fromJson(
- Map<String, dynamic> json
)
Implementation
factory ReceiptEventContent.fromJson(Map<String, dynamic> json) {
// Example data:
// {
// "$I": {
// "m.read": {
// "@user:example.org": {
// "ts": 1661384801651,
// "thread_id": "main" // because `I` is not in a thread, but is a threaded receipt
// }
// }
// },
// "$E": {
// "m.read": {
// "@user:example.org": {
// "ts": 1661384801651,
// "thread_id": "$A" // because `E` is in Thread `A`
// }
// }
// },
// "$D": {
// "m.read": {
// "@user:example.org": {
// "ts": 1661384801651
// // no `thread_id` because the receipt is *unthreaded*
// }
// }
// }
// }
final Map<String, Map<ReceiptType, Map<String, ReceiptData>>> receipts = {};
for (final eventIdEntry in json.entries) {
final eventId = eventIdEntry.key;
final contentForEventId = eventIdEntry.value;
if (!eventId.startsWith('\$') || contentForEventId is! Map) continue;
for (final receiptTypeEntry in contentForEventId.entries) {
if (receiptTypeEntry.key is! String) continue;
final receiptType = ReceiptType.values.fromString(receiptTypeEntry.key);
final contentForReceiptType = receiptTypeEntry.value;
if (receiptType == null || contentForReceiptType is! Map) continue;
for (final userIdEntry in contentForReceiptType.entries) {
final userId = userIdEntry.key;
final receiptContent = userIdEntry.value;
if (userId is! String ||
!userId.isValidSDNId ||
receiptContent is! Map) continue;
final ts = receiptContent['ts'];
final threadId = receiptContent['thread_id'];
if (ts is int && (threadId == null || threadId is String)) {
((receipts[eventId] ??= {})[receiptType] ??= {})[userId] =
ReceiptData(ts, threadId: threadId);
}
}
}
}
return ReceiptEventContent(receipts);
}