瀏覽代碼

新增活跃成员

yeshijie 4 年之前
父節點
當前提交
7ee1ea3ade

+ 17 - 0
src/server/endpoints/v2/session.endpoint.js

@ -457,6 +457,23 @@ router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
    participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
});
/**
 * 增加活跃成员
 * user_id:增加的人员
 * old_user_id:删除的人员
 * session_id 会话ID
 */
router.post(APIv2.Sessions.ParticipantUpdateNew, function (req, res) {
    let payload = req.body;
    let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
    if (!testing.pass) {
        throw testing.message;
    }
    let participants = new Participants();
    ControllerUtil.regModelEventHandler(participants, res);
    participants.updateSessionUser0(payload.user_id, payload.old_user_id, payload.session_id);
});
/**
 * 移除成员

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

@ -65,6 +65,7 @@ const APIv2 = {
        SessionUnreadMessages: '/:session_id/messages/unread',          // 会话未读消息
        ParticipantUpdate:'/:session_id/participant/update',            //更新成员=删除旧成员,新增新成员
        ParticipantUpdateNew:'/:session_id/participant/updateNew',            //更新成员=删除旧成员,新增新活跃成员
        Participants: '/:session_id/participants',                      // 会话所有成员
        ParticipantsAvatar: '/:session_id/participants/avatars',        // 会话所有成员头像
        Participant: '/:session_id/participants/:participant_id',       // 会话单个成员(多个participant_id 用英文逗号间隔)

+ 53 - 0
src/server/models/sessions/participants.js

@ -426,6 +426,59 @@ class Participants extends RedisModel {
            }
        })
    }
    /**
     * 新增活跃成员
     * @param userId
     * @param oldUserId
     * @param sessionId
     */
    updateSessionUser0(userId,oldUserId,sessionId){
        let self = this;
        let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
        SessionRepo.findOne(sessionId,function(err,res){
            if(err){
                ModelUtil.emitOK(self.eventEmitter, {status:-1,message: "会话查询失败!"});
                return;
            }
            if(res && res.length!=1){
                ModelUtil.emitOK(self.eventEmitter, {status:200,message: "用户未创建咨询!"});
                return;
            }else{
                let session = res[0];
                if(oldUserId){
                    redis.hgetAsync(participantsRoleKey,oldUserId).then(function(role){
                        if(!role)role = 0;
                        self.deleteUserFromMysql(session.id,userId,function(err,addResult){
                            if(err){
                                ModelUtil.emitOK(self.eventEmitter, {status:-1,message: "会话成员添加失败!"});
                                return;
                            }
                            self.removeUser(session.id,oldUserId,function(err,res){
                                if(err){
                                    ModelUtil.emitOK(self.eventEmitter, {status:-1,message: "会话成员移除失败!"});
                                    return;
                                }else{
                                    ModelUtil.emitOK(self.eventEmitter, {status:200,message: "成员变更成功!"});
                                    return;
                                }
                            })
                        });
                    })
                }else{
                    self.addUser(session.id,userId,"0",function(err,addResult){
                        if(!err){
                            ModelUtil.emitOK(self.eventEmitter, {status:200,message: "成员变更成功!"});
                            return;
                        }else{
                            ModelUtil.emitOK(self.eventEmitter, {status:-1,message: "成员变更失败!"});
                            return;
                        }
                    });
                }
            }
        })
    }
}
// Expose class