1. 简介
管理mysql IMRecentSession表用户会话信息
2. mysql IMRecentSession表
CREATE TABLE `IMRecentSession` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId` int(11) unsigned NOT NULL COMMENT '用户id',
`peerId` int(11) unsigned NOT NULL COMMENT '对方id',
`type` tinyint(1) unsigned DEFAULT '0' COMMENT '类型,1-用户,2-群组',
`status` tinyint(1) unsigned DEFAULT '0' COMMENT '用户:0-正常, 1-用户A删除,群组:0-正常, 1-被删除',
`created` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '创建时间',
`updated` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '更新时间',
PRIMARY KEY (`id`),
KEY `idx_userId_peerId_status_updated` (`userId`,`peerId`,`status`,`updated`),
KEY `idx_userId_peerId_type` (`userId`,`peerId`,`type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3. IM::BaseDefine::ContactSessionInfo
message ContactSessionInfo{
required uint32 session_id = 1;
required SessionType session_type = 2;
required SessionStatusType session_status = 3;
required uint32 updated_time = 4;
required uint32 latest_msg_id = 5;
required bytes latest_msg_data = 6;
required MsgType latest_msg_type = 7;
required uint32 latest_msg_from_user_id = 8;
}
4. 类与接口
① void CSessionModel::getRecentSession(uint32_t nUserId, uint32_t lastTime, listIM::BaseDefine::ContactSessionInfo& lsContact)
通过查询mysql IMRecentSession获取和nUserId时间大于lastTime的会话信息(类型,1-用户,2-群组 用户:0-正常, 1-用户A删除,群组:0-正常, 1-被删除),
查询的结果存入lsContact
② uint32_t CSessionModel::getSessionId(uint32_t nUserId, uint32_t nPeerId, uint32_t nType, bool isAll)
从mysql IMRecentSession查询nUserId和nPeerId指定nType的会话id,isAll为false仅查询正常用户否则查询全部,
返回查询到的会话id
③ bool CSessionModel::updateSession(uint32_t nSessionId, uint32_t nUpdateTime)
更新mysql IMRecentSession nSessionId的会话时间(updated)
④ bool CSessionModel::removeSession(uint32_t nSessionId)
删除会话信息,实际未删除,
将mysql IMRecentSession指定nSessionId会话状态设置为删除
⑤ uint32_t CSessionModel::addSession(uint32_t nUserId, uint32_t nPeerId, uint32_t nType)
增加会话信息,若有则更新,若无则添加
⑥ void CSessionModel::fillSessionMsg(uint32_t nUserId, listIM::BaseDefine::ContactSessionInfo& lsContact)
填充lsContact里所有会话信息的消息内容,
获取消息内容需使用到CMessageModel
5. Tests/test_sessionmodel.cpp
RecentSession提供了两个接口getRecentSession和deleteRecentSession处理获取和删除最近会话请求pdu
该用例模拟win client,获取userid为1的会话信息
6. 源码
测试demo: tests/test_sessionmodel.cpp