void CMessageModel::getUnreadMsgCount(uint32_t nUserId, uint32_t &nTotalCnt, list<IM::BaseDefine::UnreadInfo>& lsUnreadCount) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("unread"); if (pCacheConn) { map<string, string> mapUnread; string strKey = "unread_" + int2string(nUserId); bool bRet = pCacheConn->hgetAll(strKey, mapUnread); pCacheManager->RelCacheConn(pCacheConn); if(bRet) { IM::BaseDefine::UnreadInfo cUnreadInfo; for (auto it = mapUnread.begin(); it != mapUnread.end(); it++) { cUnreadInfo.set_session_id(atoi(it->first.c_str())); cUnreadInfo.set_unread_cnt(atoi(it->second.c_str())); cUnreadInfo.set_session_type(IM::BaseDefine::SESSION_TYPE_SINGLE); uint32_t nMsgId = 0; string strMsgData; IM::BaseDefine::MsgType nMsgType; getLastMsg(cUnreadInfo.session_id(), nUserId, nMsgId, strMsgData, nMsgType); if(IM::BaseDefine::MsgType_IsValid(nMsgType)) { cUnreadInfo.set_latest_msg_id(nMsgId); cUnreadInfo.set_latest_msg_data(strMsgData); cUnreadInfo.set_latest_msg_type(nMsgType); cUnreadInfo.set_latest_msg_from_user_id(cUnreadInfo.session_id()); lsUnreadCount.push_back(cUnreadInfo); nTotalCnt += cUnreadInfo.unread_cnt(); } else { log("invalid msgType. userId=%u, peerId=%u, msgType=%u", nUserId, cUnreadInfo.session_id(), nMsgType); } } } else { log("hgetall %s failed!", strKey.c_str()); } } else { log("no cache connection for unread"); } }
void getUnreadMsgCounter(CImPdu* pPdu, uint32_t conn_uuid) { IM::Message::IMUnreadMsgCntReq msg; IM::Message::IMUnreadMsgCntRsp msgResp; if(msg.ParseFromArray(pPdu->GetBodyData(), pPdu->GetBodyLength())) { CImPdu* pPduResp = new CImPdu; uint32_t nUserId = msg.user_id(); list<IM::BaseDefine::UnreadInfo> lsUnreadCount; uint32_t nTotalCnt = 0; CMessageModel::getInstance()->getUnreadMsgCount(nUserId, nTotalCnt, lsUnreadCount); CGroupMessageModel::getInstance()->getUnreadMsgCount(nUserId, nTotalCnt, lsUnreadCount); msgResp.set_user_id(nUserId); msgResp.set_total_cnt(nTotalCnt); for(auto it= lsUnreadCount.begin(); it!=lsUnreadCount.end(); ++it) { IM::BaseDefine::UnreadInfo* pInfo = msgResp.add_unreadinfo_list(); // *pInfo = *it; pInfo->set_session_id(it->session_id()); pInfo->set_session_type(it->session_type()); pInfo->set_unread_cnt(it->unread_cnt()); pInfo->set_latest_msg_id(it->latest_msg_id()); pInfo->set_latest_msg_data(it->latest_msg_data()); pInfo->set_latest_msg_type(it->latest_msg_type()); pInfo->set_latest_msg_from_user_id(it->latest_msg_from_user_id()); } log("userId=%d, unreadCnt=%u, totalCount=%u", nUserId, msgResp.unreadinfo_list_size(), nTotalCnt); msgResp.set_attach_data(msg.attach_data()); pPduResp->SetPBMsg(&msgResp); pPduResp->SetSeqNum(pPdu->GetSeqNum()); pPduResp->SetServiceId(IM::BaseDefine::SID_MSG); pPduResp->SetCommandId(IM::BaseDefine::CID_MSG_UNREAD_CNT_RESPONSE); CProxyConn::AddResponsePdu(conn_uuid, pPduResp); } else { log("parse pb failed"); } }