sendUserMessage method
Sends UserMessage on this channel with params
.
It returns UserMessage with MessageSendingStatus.pending and
onCompleted
will be invoked once the message has been sent completely.
Channel event ChannelEventHandler.onMessageReceived will be invoked
on all other members' end.
NOTE that the pending message does not have a messageId.
Implementation
UserMessage sendUserMessage(
UserMessageParams params, {
OnUserMessageCallback? onCompleted,
}) {
if (params.message.isEmpty) {
throw InvalidParameterError();
}
// final req = ChannelUserMessageSendRequest(
// channelType: channelType,
// channelUrl: channelUrl,
// params: params,
// );
// final pending = req.pending;
// if (!_sdk.state.hasActiveUser) {
// final error = ConnectionRequiredError();
// pending
// ..errorCode = error.code
// ..sendingStatus = MessageSendingStatus.failed;
// if (onCompleted != null) onCompleted(pending, error);
// return pending;
// }
// pending.sendingStatus = MessageSendingStatus.pending;
// pending.sender = Sender.fromUser(_sdk.state.currentUser, this);
// _sdk.cmdManager.send(req).then((result) {
// if (result == null) return;
// final msg = BaseMessage.msgFromJson<UserMessage>(
// result.payload,
// type: result.cmd,
// );
// if (onCompleted != null && msg != null) onCompleted(msg, null);
// }).catchError((e) {
// // pending.errorCode = e?.code ?? ErrorCode.unknownError;
// pending
// ..errorCode = e.code
// ..sendingStatus = MessageSendingStatus.failed;
// if (onCompleted != null) onCompleted(pending, e);
// });
if (params.mentionedUserIds != null) {
if (params.mentionedUserIds!.isNotEmpty &&
params.isChannelMention == false) {
params.mentionType = MentionType.users;
}
}
final cmd = Command.buildUserMessage(
channelUrl,
params,
Uuid().v1(),
);
final pending = BaseMessage.msgFromJson<UserMessage>(
cmd.payload,
channelType: channelType,
type: cmd.cmd,
)!;
//return mentionedUsers if params include mentionedUsers
pending.mentionedUsers = params.mentionedUsers ?? [];
if (!_sdk.state.hasActiveUser) {
final error = ConnectionRequiredError();
pending
..errorCode = error.code
..sendingStatus = MessageSendingStatus.failed;
if (onCompleted != null) onCompleted(pending, error);
return pending;
}
pending.sendingStatus = MessageSendingStatus.pending;
pending.sender = Sender.fromUser(_sdk.state.currentUser, this);
_sdk.cmdManager.sendCommand(cmd).then((result) {
if (result == null) return;
result.payload['mentioned_users'] =
params.mentionedUsers?.map((e) => e.toJson()).toList();
final msg = BaseMessage.msgFromJson<UserMessage>(
result.payload,
type: result.cmd,
);
if (onCompleted != null && msg != null) onCompleted(msg, null);
}).catchError((e) {
pending
..errorCode = e?.code ?? ErrorCode.unknownError
..sendingStatus = MessageSendingStatus.failed;
if (onCompleted != null) onCompleted(pending, e);
});
return pending;
}