sendUserMessage method
UserMessage
sendUserMessage(
- UserMessageCreateParams params, {
- UserMessageHandler? handler,
- int? resendMessageId,
Sends a user message.
Implementation
UserMessage sendUserMessage(
UserMessageCreateParams params, {
UserMessageHandler? handler,
int? resendMessageId,
}) {
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 =
RootMessage.getMessageFromJsonWithChat<UserMessage>(
chat,
cmd.payload,
channelType: channelType,
commandType: cmd.cmd,
) as UserMessage;
if (this is GroupChannel) {
pendingUserMessage.messageId = resendMessageId ?? 0;
for (final messageCollection
in chat.collectionManager.baseMessageCollections) {
if (messageCollection.baseChannel.channelUrl == channelUrl) {
if (resendMessageId != null) {
chat.collectionManager.sendEventsToMessageCollection(
messageCollection: messageCollection,
baseChannel: this,
eventSource: CollectionEventSource.localMessageResendStarted,
sendingStatus: SendingStatus.pending,
updatedMessages: [pendingUserMessage],
);
} else {
chat.collectionManager.sendEventsToMessageCollection(
messageCollection: messageCollection,
baseChannel: this,
eventSource: CollectionEventSource.localMessagePendingCreated,
sendingStatus: SendingStatus.pending,
addedMessages: [pendingUserMessage],
);
}
break;
}
}
}
if (chat.chatContext.currentUser == null) {
final error = ConnectionRequiredException();
pendingUserMessage
..errorCode = error.code
..sendingStatus = SendingStatus.failed
..messageId = resendMessageId ?? DateTime.now().millisecondsSinceEpoch;
if (this is GroupChannel) {
for (final messageCollection
in chat.collectionManager.baseMessageCollections) {
if (messageCollection.baseChannel.channelUrl == channelUrl) {
chat.collectionManager.sendEventsToMessageCollection(
messageCollection: messageCollection,
baseChannel: this,
eventSource: CollectionEventSource.localMessageFailed,
sendingStatus: SendingStatus.failed,
updatedMessages: [pendingUserMessage],
);
break;
}
}
}
if (handler != null) {
handler(pendingUserMessage, error);
}
return pendingUserMessage;
}
pendingUserMessage.sendingStatus = SendingStatus.pending;
pendingUserMessage.sender =
Sender.fromUser(chat.chatContext.currentUser, this);
pendingUserMessage.messageCreateParams = params;
runZonedGuarded(() {
chat.commandManager.sendCommand(cmd).then((result) async {
if (result == null) {
return;
}
final message = RootMessage.getMessageFromJsonWithChat<UserMessage>(
chat,
result.payload,
commandType: result.cmd,
) as UserMessage;
chat.collectionManager.onMessageSentByMe(
channel: this,
pendingMessage: pendingUserMessage,
sentMessage: message,
);
if (handler != null) {
handler(message, null);
}
});
}, (e, s) {
sbLog.e(StackTrace.current, 'e: $e');
if (e is SendbirdException) {
pendingUserMessage
..errorCode = e.code ?? SendbirdError.unknownError
..sendingStatus = SendingStatus.failed
..messageId =
resendMessageId ?? DateTime.now().millisecondsSinceEpoch;
if (this is GroupChannel) {
for (final messageCollection
in chat.collectionManager.baseMessageCollections) {
if (messageCollection.baseChannel.channelUrl == channelUrl) {
chat.collectionManager.sendEventsToMessageCollection(
messageCollection: messageCollection,
baseChannel: this,
eventSource: CollectionEventSource.localMessageFailed,
sendingStatus: SendingStatus.failed,
updatedMessages: [pendingUserMessage],
);
break;
}
}
}
if (handler != null) {
handler(pendingUserMessage, e);
}
}
});
return pendingUserMessage;
}