publish method
Implementation
void publish(String messageUid, String content, String type,
ThreadProtobuf currentThread) {
debugPrint(
'publish message content: $type, $content, ${currentThread.uid}');
// String? agentInfo = SpUtil.getString(BytedeskConstants.agentInfo);
// AgentJson agentJson = AgentJson.fromJson(jsonDecode(agentInfo!));
//
if (isConnected()) {
// 发送消息
protothread.Thread thread = protothread.Thread();
thread.uid = currentThread.uid!;
thread.type = currentThread.type!;
thread.topic = currentThread.topic!;
//
protouser.User threadUser = protouser.User();
threadUser.uid = currentThread.user!.uid!;
threadUser.nickname = currentThread.user!.nickname!;
threadUser.avatar = currentThread.user!.avatar!;
thread.user = threadUser;
//
protouser.User user = protouser.User();
user.uid = currentUid!;
user.nickname = nickname!;
user.avatar = avatar!;
//
var messageExtra = {
orgUid: orgUid!,
};
protomsg.Message message = protomsg.Message();
message.uid = messageUid;
message.type = type;
message.status = BytedeskConstants.MESSAGE_STATUS_SENDING;
message.createdAt = BytedeskUtils.formatedDateNow();
message.client = BytedeskUtils.client();
message.content = content;
message.extra = jsonEncode(messageExtra);
//
message.user = user;
message.thread = thread;
// 先通知界面本地显示聊天记录,然后再发送
// if (shouldInsertLocal) {
// 插入本地数据库
// if (messageProvider != null) {
// messageProvider.insert(message);
// }
// bytedeskEventBus.fire(ReceiveMessageEventBus(message));
// midList.add(message.mid!);
// }
//
final PayloadBuilder builder = PayloadBuilder();
// 注意:此函数为自行添加,原先库中没有
builder.addProtobuf(message.writeToBuffer());
mqttClient?.publishMessage(
currentThread.topic!, MqttQos.exactlyOnce, builder.payload!);
} else {
// TODO: 长连接断开,调用rest api发送消息
String? createdAt = BytedeskUtils.formatedDateNow();
String? client = BytedeskUtils.getClient();
//
ThreadProtobuf threadProtobuf = ThreadProtobuf(
uid: currentThread.uid,
topic: currentThread.topic,
type: currentThread.type,
user: UserProtobuf(
uid: currentThread.user!.uid,
nickname: currentThread.user!.nickname,
avatar: currentThread.user!.avatar,
));
//
UserProtobuf userProtobuf = UserProtobuf(
uid: currentUid,
nickname: nickname,
avatar: avatar,
);
//
var messageExtra = {
orgUid: orgUid!,
};
MessageProtobuf messageProtobuf = MessageProtobuf(
uid: messageUid,
type: type,
content: content,
status: BytedeskConstants.MESSAGE_STATUS_SENDING,
createdAt: createdAt,
client: client,
extra: jsonEncode(messageExtra),
//
thread: threadProtobuf,
user: userProtobuf,
);
//
String? jsonString = jsonEncode(messageProtobuf);
debugPrint("TODO: jsonString:$jsonString");
//MessageHttpApi().sendRestMessage(jsonString);
//
// BlocProvider.of<MessageBloc>(context)
// .add(SendMessageRestEvent(json: jsonString));
// // 插入本地数据库
// _messageProvider.insert(message);
}
}