getPlaintextFromOobAttachments function
Future<List<Result<DidcommPlaintextMessage, String> > >
getPlaintextFromOobAttachments(
- OutOfBandMessage message, {
- List<
String> ? expectedAttachments,
Resolves the attachments and returns them as PlainTextMessages Each message is tried to be resolved. An error is reported for each message if it could not be resolved or parsed.
Implementation
Future<List<Result<DidcommPlaintextMessage, String>>>
getPlaintextFromOobAttachments(OutOfBandMessage message,
{List<String>? expectedAttachments}) async {
List<Result<DidcommPlaintextMessage, String>> res = [];
if (message.attachments!.isNotEmpty) {
for (var a in message.attachments!) {
bool isOk = true;
if (a.data.json == null) {
try {
await a.data.resolveData();
} catch (e) {
res.add(Result.Error(
"Could not resolve attachment due to `${e.toString()}` "
"(Code: 348923084)"));
isOk = false;
}
}
if (isOk) {
try {
var plain = DidcommPlaintextMessage.fromJson(a.data.json!);
plain.from ??= message.from;
res.add(Result.Ok(plain));
} catch (e) {
res.add(Result.Error("Could not parse message from OOB "
"attachment due to `${e.toString()}` "
"(Code: 4853094)"));
}
}
}
}
// filter by type (optionally)
if (expectedAttachments != null) {
for (var i = 0; i < res.length; i++) {
if (res[i].isOk) {
DidcommPlaintextMessage r = res[i].unrwap();
if (!expectedAttachments.contains(r.type)) {
res[i] = Result.Error(
"Attachment is of type `${r.type}` but expected one of "
"`${expectedAttachments.join(', ')}` (Code: 3583457348)");
}
}
}
}
return res;
}