sendMessage method

  1. @override
Future<DeliveryStatus> sendMessage(
  1. Message m
)
override

Sends m to the backend and returns its DeliveryStatus.

Implementation

@override
Future<DeliveryStatus> sendMessage(Message m) async {
  try {
    final res = await http.post(
      '${config.baseUrl}/messages',
      body: jsonEncode({
        'localId': m.id,
        'conversationId': m.conversationId,
        'authorId': m.authorId,
        'type': m.type,
        'payload': m.payload.toJson(),
        'createdAt': m.createdAt.toIso8601String(),
        if (m.replyToId != null) 'replyToId': m.replyToId,
      }),
      headers: config.defaultHeaders,
    );
    if (!res.isOk) return DeliveryStatus.fail('HTTP ${res.statusCode}');
    final remoteId = res.json['id'] as String? ?? m.id;
    return DeliveryStatus.ok(remoteId);
  } catch (e) {
    return DeliveryStatus.fail(e.toString());
  }
}