add sendMessage function

This commit is contained in:
xyz
2025-06-18 18:21:23 +08:00
parent c50f574eed
commit 971ab64769
11 changed files with 319 additions and 9 deletions

View File

@ -268,6 +268,11 @@ namespace model
}
}
void DataCenter::sendTextMessageAsync(const QString& chatSessionId, const QString& content)
{
netClient.sendMessage(loginSessionId, chatSessionId, MessageType::TEXT_TYPE, content.toUtf8(), "");
}
ChatSessionInfo* DataCenter::findChatSessionById(const QString& chatSessionId)
{
if (chatSessionList == nullptr) {
@ -281,6 +286,46 @@ namespace model
return nullptr;
}
ChatSessionInfo* DataCenter::findChatSessionByUserId(const QString& userId)
{
if (chatSessionList == nullptr) {
return nullptr;
}
for (auto& info : *chatSessionList) {
if (info.userId == userId) {
return &info;
}
}
return nullptr;
}
void DataCenter::topCurrentChatSessionId(const ChatSessionInfo& chatSessionInfo)
{
if (chatSessionList == nullptr) {
return;
}
//把这个元素从列表中找到
auto it = chatSessionList->begin();
for (; it != chatSessionList->end(); ++it) {
if (it->chatSessionId == chatSessionInfo.chatSessionId) {
break;
}
}
if (it == chatSessionList->end()) {
//上面的循环没有找到匹配的元素,直接返回,正常来说,不会走这个逻辑
return;
}
//删除并重新插入头部
ChatSessionInfo backup = chatSessionInfo;
chatSessionList->erase(it);
//把备份的元素插入到头部
chatSessionList->push_front(backup);
}
void DataCenter::setCurrentChatSessionId(const QString& chatSessionId)
{
this->currentChatSessionId = chatSessionId;
@ -291,4 +336,10 @@ namespace model
return currentChatSessionId;
}
void DataCenter::addMessage(const Message& message)
{
QList<Message>& messageList = (*recentMessages)[message.chatSessionId];
messageList.push_back(message);
}
} //end namespace model

View File

@ -122,12 +122,26 @@ namespace model
QList<Message>* getRecentMessageList(const QString& chatSessionId);
void resetRecentMessageList(const QString& chatSessionId, std::shared_ptr<bite_im::GetRecentMsgRsp> resp);
//发送消息给服务器
void sendTextMessageAsync(const QString& chatSessionId, const QString& content);
///////////////////////////////////////////////////////////////////////////////////
///辅助函数
///////////////////////////////////////////////////////////////////////////////
//根据会话id查询会话信息
ChatSessionInfo* findChatSessionById(const QString& chatSessionId);
//根据用户ID查询会话信息
ChatSessionInfo* findChatSessionByUserId(const QString& userId);
//把指定的会话信息,放到列表头部
void topCurrentChatSessionId(const ChatSessionInfo& chatSessionInfo);
//设置/获取当前选中的会话
void setCurrentChatSessionId(const QString& chatSessionId);
const QString& getCurrentSessionId();
//添加消息到DataCenter中
void addMessage(const Message& message);
signals:
//自定义信号
void getMyselfDone();
@ -135,5 +149,7 @@ namespace model
void getChatSessionListDone();
void getApplyListDone();
void getRecentMessageListDone(const QString& chatSessionId);
void sendMessageDone(MessageType messageType, const QByteArray& content, const QString& extraInfo);
void updateLastMessage(const QString& chatSessionId);
};
} //end namespace model