sendMessage method
Posts a reply to id and appends it to the loaded thread. Answers whether
the backend accepted it.
attachments go up one multipart part each, and body may then be
empty. Without a file the request stays JSON.
Implementation
Future<bool> sendMessage(
Object id,
String body, {
List<MagicFile> attachments = const <MagicFile>[],
}) async {
final response = await _submit(
'/support/conversations/$id/messages',
data: {if (body.isNotEmpty || attachments.isEmpty) 'body': body},
action: 'sendMessage',
fallbackKey: 'magic_starter.support.reply_error',
attachments: attachments,
);
if (response == null) return false;
final loaded = _conversation;
if (loaded != null && loaded.id.toString() == id.toString()) {
final Object? payload = _payload(response);
if (payload is Map<String, dynamic>) {
_conversation = loaded.withMessage(
MagicStarterSupportMessage.fromMap(payload),
);
refreshUI();
} else {
// The server took the reply but answered something this client cannot
// read: read the thread again rather than report a failure the reader
// would retry, posting the reply twice.
Log.error(
'[MagicStarterSupportController.sendMessage] unreadable reply: '
'$payload',
);
await loadConversation(id);
}
}
return true;
}