sendUserMessage method
- UserMessageCreateParams params,
- {UserMessageHandler? handler}
Sends a user message.
Implementation
UserMessage sendUserMessage(
UserMessageCreateParams params, {
UserMessageHandler? handler,
}) {
sbLog.i(StackTrace.current, 'message: ${params.message}');
checkUnsupportedAction();
if (params.message.isEmpty) {
throw InvalidParameterException();
}
final cmd = Command.buildUserMessage(
channelUrl,
params,
const Uuid().v1(),
);
final pendingUserMessage =
BaseMessage.getMessageFromJsonWithChat<UserMessage>(
chat,
cmd.payload,
channelType: channelType,
commandType: cmd.cmd,
);
if (chat.chatContext.currentUser == null) {
final error = ConnectionRequiredException();
pendingUserMessage
..errorCode = error.code
..sendingStatus = SendingStatus.failed;
if (handler != null) handler(pendingUserMessage, error);
return pendingUserMessage;
}
pendingUserMessage.sendingStatus = SendingStatus.pending;
pendingUserMessage.sender =
Sender.fromUser(chat.chatContext.currentUser, this);
chat.commandManager.sendCommand(cmd).then((result) {
if (result == null) return;
final message = BaseMessage.getMessageFromJsonWithChat<UserMessage>(
chat,
result.payload,
commandType: result.cmd,
);
chat.collectionManager.onMessageSentByMe(message);
if (handler != null) handler(message, null);
}).catchError((e) {
sbLog.e(StackTrace.current, 'e: $e');
pendingUserMessage
..errorCode = e?.code ?? SendbirdError.unknownError
..sendingStatus = SendingStatus.failed;
if (handler != null) handler(pendingUserMessage, e);
});
return pendingUserMessage;
}