/** * 同步群组聊天信息 * * @param arg NULL * * @return NULL */ void* CSyncCenter::doSyncGroupChat(void* arg) { m_bSyncGroupChatRuning = true; CDBManager* pDBManager = CDBManager::getInstance(); map<uint32_t, uint32_t> mapChangedGroup; do { mapChangedGroup.clear(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave"); if(pDBConn) { string strSql = "select id, lastChated from IMGroup where status=0 and lastChated >=" + int2string(m_pInstance->getLastUpdateGroup()); CResultSet* pResult = pDBConn->ExecuteQuery(strSql.c_str()); if(pResult) { while (pResult->Next()) { uint32_t nGroupId = pResult->GetInt("id"); uint32_t nLastChat = pResult->GetInt("lastChated"); if(nLastChat != 0) { mapChangedGroup[nGroupId] = nLastChat; } } delete pResult; } pDBManager->RelDBConn(pDBConn); } else { log("no db connection for teamtalk_slave"); } m_pInstance->updateLastUpdateGroup(time(NULL)); for (auto it=mapChangedGroup.begin(); it!=mapChangedGroup.end(); ++it) { uint32_t nGroupId =it->first; list<uint32_t> lsUsers; uint32_t nUpdate = it->second; CGroupModel::getInstance()->getGroupUser(nGroupId, lsUsers); for (auto it1=lsUsers.begin(); it1!=lsUsers.end(); ++it1) { uint32_t nUserId = *it1; uint32_t nSessionId = INVALID_VALUE; nSessionId = CSessionModel::getInstance()->getSessionId(nUserId, nGroupId, IM::BaseDefine::SESSION_TYPE_GROUP, true); if(nSessionId != INVALID_VALUE) { CSessionModel::getInstance()->updateSession(nSessionId, nUpdate); } else { CSessionModel::getInstance()->addSession(nUserId, nGroupId, IM::BaseDefine::SESSION_TYPE_GROUP); } } } // } while (!m_pInstance->m_pCondSync->waitTime(5*1000)); } while (m_pInstance->m_bSyncGroupChatWaitting && !(m_pInstance->m_pCondGroupChat->waitTime(5*1000))); // } while(m_pInstance->m_bSyncGroupChatWaitting); m_bSyncGroupChatRuning = false; return NULL; }
bool CInterLoginStrategy::doLogin(const std::string &strName, const std::string &strPass, IM::BaseDefine::UserInfo& user) { bool bRet = false; CDBManager* pDBManger = CDBManager::getInstance(); CDBConn* pDBConn = pDBManger->GetDBConn("teamtalk_slave"); if (pDBConn) { string strSql = "select * from IMUser where name='" + strName + "' and status=0"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if(pResultSet) { string strResult, strSalt; uint32_t nId, nGender, nDeptId, nStatus; string strNick, strAvatar, strEmail, strRealName, strTel, strDomain,strSignInfo; while (pResultSet->Next()) { nId = pResultSet->GetInt("id"); strResult = pResultSet->GetString("password"); strSalt = pResultSet->GetString("salt"); strNick = pResultSet->GetString("nick"); nGender = pResultSet->GetInt("sex"); strRealName = pResultSet->GetString("name"); strDomain = pResultSet->GetString("domain"); strTel = pResultSet->GetString("phone"); strEmail = pResultSet->GetString("email"); strAvatar = pResultSet->GetString("avatar"); nDeptId = pResultSet->GetInt("departId"); nStatus = pResultSet->GetInt("status"); strSignInfo = pResultSet->GetString("sign_info"); } string strInPass = strPass + strSalt; char szMd5[33]; CMd5::MD5_Calculate(strInPass.c_str(), strInPass.length(), szMd5); string strOutPass(szMd5); if(strOutPass == strResult) { bRet = true; user.set_user_id(nId); user.set_user_nick_name(strNick); user.set_user_gender(nGender); user.set_user_real_name(strRealName); user.set_user_domain(strDomain); user.set_user_tel(strTel); user.set_email(strEmail); user.set_avatar_url(strAvatar); user.set_department_id(nDeptId); user.set_status(nStatus); user.set_sign_info(strSignInfo); } delete pResultSet; } pDBManger->RelDBConn(pDBConn); } return bRet; }
/* * IMMessage 分表 * AddFriendShip() * if nFromId or nToId is ShopEmployee * GetShopId * Insert into IMMessage_ShopId%8 */ bool CMessageModel::sendMessage(uint32_t nRelateId, uint32_t nFromId, uint32_t nToId, IM::BaseDefine::MsgType nMsgType, uint32_t nCreateTime, uint32_t nMsgId, string& strMsgContent) { bool bRet =false; if (nFromId == 0 || nToId == 0) { log("invalied userId.%u->%u", nFromId, nToId); return bRet; } CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_master"); if (pDBConn) { string strTableName = "IMMessage_" + int2string(nRelateId % 8); string strSql = "insert into " + strTableName + " (`relateId`, `fromId`, `toId`, `msgId`, `content`, `status`, `type`, `created`, `updated`) values(?, ?, ?, ?, ?, ?, ?, ?, ?)"; // 必须在释放连接前delete CPrepareStatement对象,否则有可能多个线程操作mysql对象,会crash CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t nStatus = 0; uint32_t nType = nMsgType; uint32_t index = 0; pStmt->SetParam(index++, nRelateId); pStmt->SetParam(index++, nFromId); pStmt->SetParam(index++, nToId); pStmt->SetParam(index++, nMsgId); pStmt->SetParam(index++, strMsgContent); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nType); pStmt->SetParam(index++, nCreateTime); pStmt->SetParam(index++, nCreateTime); bRet = pStmt->ExecuteUpdate(); } delete pStmt; pDBManager->RelDBConn(pDBConn); if (bRet) { uint32_t nNow = (uint32_t) time(NULL); incMsgCount(nFromId, nToId); } else { log("insert message failed: %s", strSql.c_str()); } } else { log("no db connection for teamtalk_master"); } return bRet; }
/** * <#Description#> * * @param nFromId <#nFromId description#> * @param nToId <#nToId description#> * @param nMsgId <#nMsgId description#> * @param strMsgData <#strMsgData description#> * @param nMsgType <#nMsgType description#> * @param nStatus 0获取未被删除的,1获取所有的,默认获取未被删除的 */ void CMessageModel::getLastMsg(uint32_t nFromId, uint32_t nToId, uint32_t& nMsgId, string& strMsgData, IM::BaseDefine::MsgType& nMsgType, uint32_t nStatus) { uint32_t nRelateId = CRelationModel::getInstance()->getRelationId(nFromId, nToId, false); if (nRelateId != INVALID_VALUE) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave"); if (pDBConn) { string strTableName = "IMMessage_" + int2string(nRelateId % 8); string strSql = "select msgId,type,content from " + strTableName + " force index (idx_relateId_status_created) where relateId= " + int2string(nRelateId) + " and status = 0 order by created desc, id desc limit 1"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if (pResultSet) { while (pResultSet->Next()) { nMsgId = pResultSet->GetInt("msgId"); nMsgType = IM::BaseDefine::MsgType(pResultSet->GetInt("type")); if (nMsgType == IM::BaseDefine::MSG_TYPE_SINGLE_AUDIO) { // "[语音]"加密后的字符串 strMsgData = strAudioEnc; } else { strMsgData = pResultSet->GetString("content"); } } delete pResultSet; } else { log("no result set: %s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); } else { log("no db connection_slave"); } } else { log("no relation between %lu and %lu", nFromId, nToId); } }
bool CGroupModel::removeGroup(uint32_t nUserId, uint32_t nGroupId, list<uint32_t>& lsCurUserId) { bool bRet = false; CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_master"); set<uint32_t> setGroupUsers; if(pDBConn) { string strSql = "select creator from IMGroup where id="+int2string(nGroupId); CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if(pResultSet) { uint32_t nCreator; while (pResultSet->Next()) { nCreator = pResultSet->GetInt("creator"); } if(0 == nCreator || nCreator == nUserId) { //设置群组不可用。 strSql = "update IMGroup set status=0 where id="+int2string(nGroupId); bRet = pDBConn->ExecuteUpdate(strSql.c_str()); } pResultSet->Clear(); } if (bRet) { strSql = "select userId from IMGroupMember where groupId="+int2string(nGroupId); CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if(pResultSet) { while (pResultSet->Next()) { uint32_t nId = pResultSet->GetInt("userId"); setGroupUsers.insert(nId); } pResultSet->Clear(); } } pDBManager->RelDBConn(pDBConn); } if(bRet) { bRet = removeMember(nGroupId, setGroupUsers, lsCurUserId); } return bRet; }
bool CGroupModel::insertNewGroup(uint32_t nUserId, const string& strGroupName, const string& strGroupAvatar, uint32_t nGroupType, uint32_t nMemberCnt, uint32_t& nGroupId) { bool bRet = false; nGroupId = INVALID_VALUE; CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_master"); if (pDBConn) { string strSql = "insert into IMGroup(`name`, `avatar`, `creator`, `type`,`userCnt`, `status`, `version`, `lastChated`, `updated`, `created`) "\ "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t nCreated = (uint32_t)time(NULL); uint32_t index = 0; uint32_t nStatus = 0; uint32_t nVersion = 1; uint32_t nLastChat = 0; pStmt->SetParam(index++, strGroupName); pStmt->SetParam(index++, strGroupAvatar); pStmt->SetParam(index++, nUserId); pStmt->SetParam(index++, nGroupType); pStmt->SetParam(index++, nMemberCnt); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nVersion); pStmt->SetParam(index++, nLastChat); pStmt->SetParam(index++, nCreated); pStmt->SetParam(index++, nCreated); bRet = pStmt->ExecuteUpdate(); if(bRet) { nGroupId = pStmt->GetInsertId(); } } delete pStmt; pDBManager->RelDBConn(pDBConn); } else { log("no db connection for dffxIMDB_master"); } return bRet; }
void CMessageModel::getMessageDays(uint32_t nUserId, uint32_t nPeerId, list<string>& lsDay) { uint32_t nRelateId = CRelationModel::getInstance()->getRelationId(nUserId, nPeerId, false); if (nRelateId != INVALID_VALUE) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave"); if (pDBConn) { string strTableName = "IMMessage_" + int2string(nRelateId % 8); string strSql = "select date_format(from_unixtime(created), '%Y-%m-%d') as day from " + strTableName + " where relateId = " + int2string(nRelateId) + " and status = 0 group by day order by day desc"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if (pResultSet) { while (pResultSet->Next()) { lsDay.push_back(pResultSet->GetString("day")); } delete pResultSet; } else { log("no result set: %s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); } else { log("no db connection for teamtalk_slave"); } } else { log("no relation between %lu and %lu", nUserId, nPeerId); } }
bool CGroupModel::insertNewMember(uint32_t nGroupId, set<uint32_t>& setUsers) { bool bRet = false; uint32_t nUserCnt = (uint32_t)setUsers.size(); if(nGroupId != INVALID_VALUE && nUserCnt > 0) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_slave"); if (pDBConn) { uint32_t nCreated = (uint32_t)time(NULL); // 获取 已经存在群里的用户 string strClause; bool bFirst = true; for (auto it=setUsers.begin(); it!=setUsers.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } string strSql = "select userId from IMGroupMember where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); set<uint32_t> setHasUser; if(pResultSet) { while (pResultSet->Next()) { setHasUser.insert(pResultSet->GetInt("userId")); } pResultSet->Clear(); } else { log("no result for sql:%s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); pDBConn = pDBManager->GetDBConn("dffxIMDB_master"); if (pDBConn) { CacheManager* pCacheManager = CacheManager::getInstance(); CacheConn* pCacheConn = pCacheManager->GetCacheConn("group_member"); if (pCacheConn) { // 设置已经存在群中人的状态 if (!setHasUser.empty()) { strClause.clear(); bFirst = true; for (auto it=setHasUser.begin(); it!=setHasUser.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } strSql = "update IMGroupMember set status=0, updated="+int2string(nCreated)+" where groupId=" + int2string(nGroupId) + " and userId in (" + strClause + ")"; pDBConn->ExecuteUpdate(strSql.c_str()); } strSql = "insert into IMGroupMember(`groupId`, `userId`, `status`, `created`, `updated`) values\ (?,?,?,?,?)"; //插入新成员 auto it = setUsers.begin(); uint32_t nStatus = 0; uint32_t nIncMemberCnt = 0; for (;it != setUsers.end();) { uint32_t nUserId = *it; if(setHasUser.find(nUserId) == setHasUser.end()) { CPrepareStatement* pStmt = new CPrepareStatement(); if (pStmt->Init(pDBConn->GetMysql(), strSql)) { uint32_t index = 0; pStmt->SetParam(index++, nGroupId); pStmt->SetParam(index++, nUserId); pStmt->SetParam(index++, nStatus); pStmt->SetParam(index++, nCreated); pStmt->SetParam(index++, nCreated); pStmt->ExecuteUpdate(); ++nIncMemberCnt; delete pStmt; } else { setUsers.erase(it++); delete pStmt; continue; } } ++it; } if(nIncMemberCnt != 0) { strSql = "update IMGroup set userCnt=userCnt+" + int2string(nIncMemberCnt) + " where id="+int2string(nGroupId); pDBConn->ExecuteUpdate(strSql.c_str()); } //更新一份到redis中 string strKey = "group_member_"+int2string(nGroupId); for(auto it = setUsers.begin(); it!=setUsers.end(); ++it) { pCacheConn->hset(strKey, int2string(*it), int2string(nCreated)); } pCacheManager->RelCacheConn(pCacheConn); bRet = true; } else { log("no cache connection"); } pDBManager->RelDBConn(pDBConn); } else {
void CGroupModel::getGroupInfo(map<uint32_t,IM::BaseDefine::GroupVersionInfo>& mapGroupId, list<IM::BaseDefine::GroupInfo>& lsGroupInfo) { if (!mapGroupId.empty()) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("dffxIMDB_slave"); if (pDBConn) { string strClause; bool bFirst = true; for(auto it=mapGroupId.begin(); it!=mapGroupId.end(); ++it) { if(bFirst) { bFirst = false; strClause = int2string(it->first); } else { strClause += ("," + int2string(it->first)); } } string strSql = "select * from IMGroup where id in (" + strClause + ") order by updated desc"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if(pResultSet) { while (pResultSet->Next()) { uint32_t nGroupId = pResultSet->GetInt("id"); uint32_t nVersion = pResultSet->GetInt("version"); if(mapGroupId[nGroupId].version() < nVersion) { IM::BaseDefine::GroupInfo cGroupInfo; cGroupInfo.set_group_id(nGroupId); cGroupInfo.set_version(nVersion); cGroupInfo.set_group_name(pResultSet->GetString("name")); cGroupInfo.set_group_avatar(pResultSet->GetString("avatar")); IM::BaseDefine::GroupType nGroupType = IM::BaseDefine::GroupType(pResultSet->GetInt("type")); if(IM::BaseDefine::GroupType_IsValid(nGroupType)) { cGroupInfo.set_group_type(nGroupType); cGroupInfo.set_group_creator_id(pResultSet->GetInt("creator")); lsGroupInfo.push_back(cGroupInfo); } else { log("invalid groupType. groupId=%u, groupType=%u", nGroupId, nGroupType); } } } pResultSet->Clear(); } else { log("no result set for sql:%s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); if(!lsGroupInfo.empty()) { fillGroupMember(lsGroupInfo); } } else { log("no db connection for dffxIMDB_slave"); } } else { log("no ids in map"); } }
void CMessageModel::getMessageDuringTime(uint32_t nUserId, uint32_t nPeerId, uint32_t nStartTime, uint32_t nEndTime, uint32_t nOffset, uint32_t nCount, list<IM::BaseDefine::MsgInfo>& lsMsg) { uint32_t nRelateId = CRelationModel::getInstance()->getRelationId(nUserId, nPeerId, false); if (nRelateId != INVALID_VALUE) { CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave"); if (pDBConn) { string strTableName = "IMMessage_" + int2string(nRelateId % 8); string strSql; if (nCount == 0) { strSql = "select * from " + strTableName + " force index (idx_relateId_status_created) where relateId= " + int2string(nRelateId) + " and status = 0 and created >= " + int2string(nStartTime) + " and created <= " + int2string(nEndTime) + " order by created desc"; } else { strSql = "select * from " + strTableName + " force index (idx_relateId_status_created) where relateId= " + int2string(nRelateId) + " and status = 0 and created >= " + int2string(nStartTime) + " and created <= " + int2string(nEndTime) + " order by created desc limit " + int2string(nOffset) + "," + int2string(nCount); } CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if (pResultSet) { while (pResultSet->Next()) { IM::BaseDefine::MsgInfo cMsg; cMsg.set_msg_id(pResultSet->GetInt("msgId")); cMsg.set_from_session_id(pResultSet->GetInt("fromId")); cMsg.set_create_time(pResultSet->GetInt("created")); IM::BaseDefine::MsgType nMsgType = IM::BaseDefine::MsgType(pResultSet->GetInt("type")); if(IM::BaseDefine::MsgType_IsValid(nMsgType)) { cMsg.set_msg_type(nMsgType); cMsg.set_msg_data(pResultSet->GetString("content")); lsMsg.push_back(cMsg); } else { log("invalid msgType. userId=%u, peerId=%u, startTime=%u, endTime=%u, offset=%u, size=%u, msgType=%u, msgId=%u", nUserId, nPeerId, nStartTime, nEndTime, nOffset, nCount, nMsgType, pResultSet->GetInt("msgId")); } } delete pResultSet; } else { log("no result set: %s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); if (!lsMsg.empty()) { CAudioModel::getInstance()->readAudios(lsMsg); } } else { log("no db connection for teamtalk_slave"); } } else { log("no relation between %lu and %lu", nUserId, nPeerId); } }
void CMessageModel::getMsgByMsgId(uint32_t nUserId, uint32_t nPeerId, const list<uint32_t> &lsMsgId, list<IM::BaseDefine::MsgInfo> &lsMsg) { if(lsMsgId.empty()) { return ; } uint32_t nRelateId = CRelationModel::getInstance()->getRelationId(nUserId, nPeerId, false); if(nRelateId == INVALID_VALUE) { log("invalid relation id between %u and %u", nUserId, nPeerId); return; } CDBManager* pDBManager = CDBManager::getInstance(); CDBConn* pDBConn = pDBManager->GetDBConn("teamtalk_slave"); if (pDBConn) { string strTableName = "IMMessage_" + int2string(nRelateId % 8); string strClause ; bool bFirst = true; for(auto it= lsMsgId.begin(); it!=lsMsgId.end();++it) { if (bFirst) { bFirst = false; strClause = int2string(*it); } else { strClause += ("," + int2string(*it)); } } string strSql = "select * from " + strTableName + " where relateId=" + int2string(nRelateId) + " and status=0 and msgId in (" + strClause + ") order by created desc, id desc limit 100"; CResultSet* pResultSet = pDBConn->ExecuteQuery(strSql.c_str()); if (pResultSet) { while (pResultSet->Next()) { IM::BaseDefine::MsgInfo msg; msg.set_msg_id(pResultSet->GetInt("msgId")); msg.set_from_session_id(pResultSet->GetInt("fromId")); msg.set_create_time(pResultSet->GetInt("created")); IM::BaseDefine::MsgType nMsgType = IM::BaseDefine::MsgType(pResultSet->GetInt("type")); if(IM::BaseDefine::MsgType_IsValid(nMsgType)) { msg.set_msg_type(nMsgType); msg.set_msg_data(pResultSet->GetString("content")); lsMsg.push_back(msg); } else { log("invalid msgType. userId=%u, peerId=%u, msgType=%u, msgId=%u", nUserId, nPeerId, nMsgType, msg.msg_id()); } } delete pResultSet; } else { log("no result set for sql:%s", strSql.c_str()); } pDBManager->RelDBConn(pDBConn); if(!lsMsg.empty()) { CAudioModel::getInstance()->readAudios(lsMsg); } } else { log("no db connection for teamtalk_slave"); } }