소스 검색

清洗im聊天记录到redis

zhangdan 5 년 전
부모
커밋
8adb5ae46b
4개의 변경된 파일83개의 추가작업 그리고 3개의 파일을 삭제
  1. 2 2
      src/server/app.js
  2. 18 0
      src/server/endpoints/v2/message.endpoint.js
  3. 3 1
      src/server/include/endpoints.js
  4. 60 0
      src/server/models/messages/messages.js

+ 2 - 2
src/server/app.js

@ -36,8 +36,8 @@ app.set('view engine', 'jade');
// logger, body parser, cookie parser and view path
app.use(favicon(__dirname + '/public/favicon.ico', null));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json({limit:'50mb'}));
app.use(bodyParser.urlencoded({limit:'50mb',extended: true}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(methodOverride(null, {methods: ['GET', 'POST', 'PUT', 'DELETE']}));

+ 18 - 0
src/server/endpoints/v2/message.endpoint.js

@ -30,4 +30,22 @@ router.post(APIv2.Message.Send, function (req, res) {
    messages.sendMessage(payload);
});
router.get(APIv2.Message.DataMessage, function (req, res) {
    let sessionId = req.query.sessionId;
    let messages = new Messages();
    ControllerUtil.regModelEventHandler(messages, res);
    messages.dataMessage(sessionId);
});
router.post(APIv2.Message.CleanMessageToRedis, function (req, res) {
    let payload = req.body;
    let session_id_str= payload.sessionIdsStr;
    let messages = new Messages();
    ControllerUtil.regModelEventHandler(messages, res);
    //messages.dataMessage(session_id);
    messages.cleanMessageToReids(session_id_str);
});
module.exports = router;

+ 3 - 1
src/server/include/endpoints.js

@ -16,7 +16,9 @@ const APIv2 = {
    Message: {
        Base: '/api/v2/message',
        Send: '/send'                                                  //发送消息,和会话无关,内外网通信
        Send: '/send',       //发送消息,和会话无关,内外网通信
        DataMessage: '/dataMessage',
        CleanMessageToRedis: '/cleanMessageToRedis'
    },
    Users: {
        Base: '/api/v2/users',

+ 60 - 0
src/server/models/messages/messages.js

@ -262,6 +262,66 @@ class Messages extends RedisModel {
            }
        });
    }
    /**
     *
     * 清洗数据
     * @param message
     */
    dataMessage(sessionId) {
        let self = this;
        let messagesKey = RedisModel.makeRedisKey(REDIS_KEYS.Messages, sessionId);
        let message_timestamp_key = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
        MessageRepo.findBySessionId(sessionId, 0, config.sessionConfig.maxMessageCount, null, function (err, messages) {
            if (err) {
                ModelUtil.emitError(self.eventEmitter, {status: -1, message: "没有找到对应sessionId的聊天记录----"+sessionId});
                return;
            }
            let multi = redis.multi();
            if(messages){
                messages.forEach(function (message) {
                    let msgJson = {
                        id: message.id,
                        sender_id: message.sender_id,
                        sender_name: message.sender_name,
                        timestamp: ObjectUtil.timestampToLong(message.timestamp),
                        content_type: message.content_type,
                        content: message.content
                    };
                    multi = multi.hset(messagesKey, message.id, JSON.stringify(msgJson))
                        .zadd(message_timestamp_key, ObjectUtil.timestampToLong(message.timestamp), message.id);
                });
            }
            multi.execAsync()
                .then(function (res) {
                    ModelUtil.emitOK(self.eventEmitter, {status:200,message:"存入redis成功!"});
                })
                .catch(function (ex) {
                    log.error("Login failed while caching messages: ", ex);
                    ModelUtil.emitOK(self.eventEmitter, res);
                    return;
                });
        });
    }
    /**
     *
     * 清洗数据
     * @param message
     */
    cleanMessageToReids(session_id_str) {
        let self = this;
        console.log(session_id_str)
        let sessionArray=session_id_str.split(",");
        sessionArray.forEach(function (sessionId) {
            console.log(sessionId);
            self.dataMessage(sessionId);
        });
    }
}
// Expose class