vor 8 Jahren
Ursprung
Commit
bec2fd2c4c

+ 7 - 3
src/server/endpoints/session.endpoint.js

@ -108,9 +108,9 @@ router.post(APIv1.Sessions.UnStickSession,function(req,res){
 * sessionType:1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组
 * users 讨论组包含的用户标示
 * sessionId 会话ID
 * 请求URL /create?sessionId=0&users=10&name=3121&sessionType=2
 * 请求URL /create?sessionId=0&users={10:1,20:1}&name=3121&sessionType=2
 */
router.post(APIv1.Sessions.CreateSession,function(req,res){
router.get(APIv1.Sessions.CreateSession,function(req,res){
    let sessionId = req.query.sessionId;
    let users = req.query.users;
    let name = req.query.name;
@ -151,10 +151,14 @@ router.post(APIv1.Sessions.SendMsg,function(req,res){
    if (!sessionId) {
        throw {httpStatus: 406, message: 'Missing sessionId.'};
    }
    let type = req.query.type;
    if (!type) {
        throw {httpStatus: 406, message: 'Missing type.'};
    }
    let sessions = new Sessions();
    ControllerUtil.regModelEventHandler(sessions, res);
    message.timestamp=new Date();
    sessions.saveMessageBySession(message,sessionId);
    sessions.saveMessageBySession(message,sessionId,type);
});

+ 28 - 13
src/server/models/sessions/participants.js

@ -57,7 +57,6 @@ class Participants extends RedisModel {
                            break;
                        }
                    }
                    handler(null, exists);
                })
            } else {
@ -88,17 +87,21 @@ class Participants extends RedisModel {
     */
    saveParticipantsToRedis(session_id, users, createDate, handler) {
        let participants_key = super.makeRedisKey(REDIS_KEYS.Participants, session_id);
        for (var j in users) {
            let user_session_key = super.makeRedisKey(REDIS_KEYS.UserSessions, users[j]);
            redis.zaddAsync(participants_key, createDate.getTime(), users[j]).then(function (res) {
                    return redis.zaddAsync(user_session_key, createDate.getTime(), session_id);
                }
            ).catch(function (err) {
                log.error("createParticipantsToRedis is fail error:" + err + ",session_id:" + session_id + ",users:" + users.join(","));
                handler(false);
            });
        }
        let participants_role_key = super.makeRedisKey(REDIS_KEYS.ParticipantsRole, session_id);
        let _super = super.makeRedisKey;
        redis.hmsetAsync(participants_role_key,users).then(function(res){
            log.info("create participants role!");
            for (var j in users) {
                let user_session_key = _super(REDIS_KEYS.UserSessions, j);
                redis.zaddAsync(participants_key, createDate.getTime(), j).then(function (res) {
                        return redis.zaddAsync(user_session_key, createDate.getTime(), session_id);
                    }
                ).catch(function (err) {
                    log.error("createParticipantsToRedis is fail error:" + err + ",session_id:" + session_id + ",users:" + JSON.stringify(users));
                    handler(false);
                });
            }
        })
        handler(true);
    }
@ -109,7 +112,7 @@ class Participants extends RedisModel {
     * @param users
     */
    saveParticipantsToMysql(session_id, users) {
        return ParticipantRepo.createParticipantsToMysql(session_id, users);
        return ParticipantRepo.saveParticipantsToMysql(session_id, users);
    }
    /**
@ -149,6 +152,18 @@ class Participants extends RedisModel {
        })
    }
    /**
     * 更新用户在MUC模式中的状态
     * @param sessionId 会话ID
     * @param user 用户
     * @param role 变更状态
     */
    updateUser(sessionId,user,role){
        let participants_role_key = super.makeRedisKey(REDIS_KEYS.ParticipantsRole, sessionId);
        redis.hsetAsync(sessionId,user,role).then(function(res){
                ParticipantRepo.updateParticipant(sessionId,user,role);
        })
    }
    /**
     * 添加讨论组成员
     * @param sessionId

+ 45 - 26
src/server/models/sessions/sessions.js

@ -16,6 +16,7 @@ const Commons = require('../../include/commons');
let configFile = require('../../include/commons').CONFIG_FILE;
let config = require('../../resources/config/' + configFile);
let SessionRepo = require('../../repository/mysql/session.repo');
let ParticipantRepo = require('../../repository/mysql/participant.repo');
let mongoose = require('mongoose');
class Sessions extends RedisModel {
@ -26,39 +27,57 @@ class Sessions extends RedisModel {
    /**
     * 创建会话
     *
     * type = 1 sessionid = md5(patientId); MUC
     * type  = 2 sessionId = hash(user1,user2); P2P
     * type = 3 sessionId = groupid; 团队群聊
     * @param sessionId 会话ID
     * @param name 会话名称
     * @param type 会话类型
     * @param users 会话成员
     */
    createSession(sessionId, name, type, users) {
        if (type == 2) {
        }
        let self = this;
        let createDate = new Date();
        let session_key = super.makeRedisKey(RedisKeys.Session, sessionId);
        let participants = new Participants();
        // 将session加入redis
        participants.saveParticipantsToRedis(sessionId, users.split(","), createDate, function (res) {
            if (!res) {
                modelUtil.emitData(self.eventEmitter, {"status": -1, "msg": res});
            } else {
                let messages = {};
                messages.senderId = "system";
                messages.senderName = "系统消息";
                messages.timestamp = createDate;
                messages.content = "";
                messages.contentType = "1";
                self.updateLastContent(session_key, type, name, messages);
                modelUtil.emitData(self.eventEmitter, {"status": 200, "msg": "session create success!"});
                self.saveSessionToMysql(sessionId, name, type, createDate);
                participants.saveParticipantsToMysql(sessionId, users.split(",")); //创建session成员到数据库
        let _super = super.makeRedisKey;
        users = eval("["+users+"]")[0];
        if (type == 2) {//P2P消息用hash校验
            var userArray=[];
            for(var key in users){
                userArray.push(key);
            }
        })
            if(userArray.length>2){
                modelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "会话人数超过2个无法创建P2P会话!"});
                return false;
            }
            ParticipantRepo.findSessionIdByParticipantIds(userArray[0],userArray[0],function(err,res){
                sessionId = res;
                callcreate(sessionId);
            })
        }else{
            callcreate();
        }
        function  callcreate(){
            let createDate = new Date();
            let session_key = _super(RedisKeys.Session, sessionId);
            let participants = new Participants();
            // 将session加入redis
            participants.saveParticipantsToRedis(sessionId, users, createDate, function (res) {
                if (!res) {
                    modelUtil.emitData(self.eventEmitter, {"status": -1, "msg": res});
                } else {
                    let messages = {};
                    messages.senderId = "system";
                    messages.senderName = "系统消息";
                    messages.timestamp = createDate;
                    messages.content = "";
                    messages.contentType = "1";
                    self.updateLastContent(session_key, type, name, messages);
                    modelUtil.emitData(self.eventEmitter, {"status": 200, "msg": "session create success!"});
                    self.saveSessionToMysql(sessionId, name, type, createDate);
                    participants.saveParticipantsToMysql(sessionId, users); //创建session成员到数据库
                }
            })
        }
    }
    /**
@ -273,7 +292,7 @@ class Sessions extends RedisModel {
        let message_id = mongoose.Types.ObjectId().toString();
        let session_type = 0;
        let name = "";
        participants.existsParticipant(sessionId, messages.senderId, function (res) {
        participants.existsParticipant(sessionId, message.senderId, function (res) {
            //校验发送成员是都在讨论组
            if (res) {
                redis.hmgetAsync(session_key, ["type", "name"]).then(function (res) {

+ 28 - 7
src/server/repository/mysql/participant.repo.js

@ -34,6 +34,27 @@ class ParticipantRepo {
        });
    }
    /**
     * 获取会话的成员列表
     *
     * @param sessionId
     * @param handler
     */
    static updateParticipant(sessionId, participant_id,role) {
        let sql = "update participants set participant_role = ? where session_id = ? and participant_id = ?";
        ImDb.execQuery({
            "sql": sql,
            "args": [role,sessionId,participant_id],
            "handler": function (err, res) {
                if (err) {
                    log.error("updateParticipant is fail error: " + err);
                }else{
                    log.info("updateParticipant is success" );
                }
            }
        });
    }
    /**
     * 获取P2P成员所在会话。将成员的ID排序后取哈希值即可。
     *
@ -77,7 +98,7 @@ class ParticipantRepo {
                if (err) {
                    log.error("existsUser is fail error: " + err);
                }
                handler(res[0].count);
                handler(err,res[0].count);
            }
        });
@ -86,16 +107,16 @@ class ParticipantRepo {
    /**
     * mysql成员创建
     *
     * @param userIds
     * @param users JSON
     */
    static saveParticipantsToMysql(session_id, userIds) {
    static saveParticipantsToMysql(session_id, users) {
        let sql = "insert into " + DB_TABLES.Participants + " (session_id,participant_id,participant_role,receiving) VALUES "
        let args = [];
        for (var j in userIds) {
        for (var j in users) {
            sql += "(?,?,?,?),";
            args.push(session_id);
            args.push(userIds[j]);
            args.push(0);
            args.push(j);
            args.push(users[j]);
            args.push(0);
        }
@ -105,7 +126,7 @@ class ParticipantRepo {
            "args": args,
            "handler": function (err, res) {
                if (err) {
                    log.error("createParticipantsForMysql is fail error: " + err + ",session_id:" + session_id + ",users:" + userIds.join(","));
                    log.error("createParticipantsForMysql is fail error: " + err + ",session_id:" + session_id + ",users:" + JSON.stringify(users));
                } else {
                    return res;
                }