Explorar el Código

清洗im最后获取信息时间到redis

zhangdan hace 5 años
padre
commit
08a20e859c

+ 4 - 5
src/server/endpoints/v2/message.endpoint.js

@ -38,14 +38,13 @@ router.get(APIv2.Message.DataMessage, function (req, res) {
    messages.dataMessage(sessionId);
});
router.post(APIv2.Message.CleanMessageToRedis, function (req, res) {
    let payload = req.body;
    let session_id_str= payload.sessionIdsStr;
router.get(APIv2.Message.CleanMessageLastFetchTimeToRedis, function (req, res) {
    let sessionId = req.query.sessionId;
    let userId = req.query.userId;
    let messages = new Messages();
    ControllerUtil.regModelEventHandler(messages, res);
    //messages.dataMessage(session_id);
    messages.cleanMessageToReids(session_id_str);
    messages.cleanMessageLastFetchTime(sessionId,userId);
});
module.exports = router;

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

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

+ 20 - 11
src/server/models/messages/messages.js

@ -6,6 +6,7 @@
let MessageRepo = require('../../repository/mysql/message.repo');
let RedisModel = require('./../redis.model.js');
let SessionRepo = require('../../repository/mysql/session.repo');
let ParticipantRepo = require('../../repository/mysql/participant.repo');
let RedisClient = require('../../repository/redis/redis.client.js');
let ModelUtil = require('../../util/model.util');
var ObjectUtil = require("../../util/object.util.js");
@ -308,18 +309,26 @@ class Messages extends RedisModel {
        });
    }
    /**
     *
     * 清洗数据
     * @param message
     */
    cleanMessageToReids(session_id_str) {
    cleanMessageLastFetchTime(sessionId,userId) {
        let self = this;
        console.log(session_id_str)
        let sessionArray=session_id_str.split(",");
        sessionArray.forEach(function (sessionId) {
            console.log(sessionId);
            self.dataMessage(sessionId);
        ParticipantRepo.findLastFetchTime(sessionId,userId,function(err,res){
            if (err) {
                logger.error(err);
                return;
            } else {
                let last_fetch_time = new Date(res[0].last_fetch_time).getTime();
                last_fetch_time = last_fetch_time + 1;
                let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
                redis.zaddAsync(participantsKey, last_fetch_time, userId)
                    .then(function (res) {
                        ModelUtil.emitOK(self.eventEmitter, {status:200,message:"存入redis成功!"});
                    })
                    .catch(function (err) {
                        logger.error("Update participant last fetch time failed: ", err);
                        ModelUtil.emitOK(self.eventEmitter, res);
                        return;
                    });
            }
        });
    }
}

+ 9 - 0
src/server/repository/mysql/participant.repo.js

@ -301,6 +301,15 @@ class ParticipantRepo {
            }
        });
    }
    static findLastFetchTime(sessionId,userId,handler){
        let sql = "select last_fetch_time from participants p where p.session_id = ? and p.participant_id=?";
        ImDb.execQuery({
            "sql": sql,
            "args": [sessionId,userId],
            "handler": handler
        });
    }
}
module.exports = ParticipantRepo;