sendAction method

Future<SendActionResponse> sendAction (
  1. Message message,
  2. Map<String, dynamic> formData
)

Send action for a specific message of this channel

Implementation

Future<SendActionResponse> sendAction(
  Message message,
  Map<String, dynamic> formData,
) async {
  _checkInitialized();

  final messageId = message.id;
  final response = await _client.post('/messages/$messageId/action', data: {
    'id': id,
    'type': type,
    'form_data': formData,
    'message_id': messageId,
  });

  final res = _client.decode(response.data, SendActionResponse.fromJson);

  if (res.message != null) {
    state.addMessage(res.message);
  } else {
    final oldIndex = state.messages?.indexWhere((m) => m.id == messageId);

    Message oldMessage;
    if (oldIndex != null && oldIndex != -1) {
      oldMessage = state.messages[oldIndex];
      state.updateChannelState(state._channelState.copyWith(
        messages: state.messages..remove(oldMessage),
      ));
    } else {
      oldMessage = state.threads.values
          .expand((messages) => messages)
          .firstWhere((m) => m.id == messageId, orElse: () => null);
      if (oldMessage?.parentId != null) {
        final parentMessage = state.messages.firstWhere(
          (element) => element.id == oldMessage.parentId,
          orElse: () => null,
        );
        if (parentMessage != null) {
          state.addMessage(parentMessage.copyWith(
              replyCount: parentMessage.replyCount - 1));
        }
        state.updateThreadInfo(oldMessage.parentId,
            state.threads[oldMessage.parentId]..remove(oldMessage));
      }
    }

    await _client.offlineStorage?.deleteMessages([messageId]);
  }

  return res;
}