/** * 会话成员模型。 */ "use strict"; let RedisModel = require('./../redis.model.js'); let ModelUtil = require('../../util/model.util'); let RedisClient = require('../../repository/redis/redis.client.js'); let ParticipantRepo = require('../../repository/mysql/participant.repo'); let log = require('../../util/log.js'); let redis = RedisClient.redisClient().connection; const REDIS_KEYS = require('../../include/commons').REDIS_KEYS; class Participants extends RedisModel { constructor() { super(); } /** * 获取会话的成员列表,直接从MySQL获取。 * * @param sessionId */ getParticipants(sessionId) { let self = this; ParticipantRepo.findAll(sessionId, function (err, participants) { if (err) { ModelUtil.emitError(self.eventEmitter, "Get session participants error", err); return; } ModelUtil.emitOK(self.eventEmitter, participants); }); } /** * 获取所有成员的头像。 * * @param sessionId */ getParticipantsAvatar(sessionId) { let self = this; ParticipantRepo.findAllAvatars(sessionId, function (err, participantsAvatars) { if (err) { ModelUtil.emitError(self.eventEmitter, "Get session participant's avatars error", err); return; } ModelUtil.emitOK(self.eventEmitter, participantsAvatars); }) } /** * 会话中是否存在指定成员 * * @param sessionId * @param userId * @param handler */ existsParticipant(sessionId, userId, handler) { let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId); redis.hgetAsync(participantsRoleKey, userId).then(function (res) { if (false) { // get from redis handler(null, true); } else { // get from mysql ParticipantRepo.existsParticipant(sessionId, userId, handler); } }) } /** * 获取P2P成员所在会话 * * @param patient * @param doctor * @param handler */ getSessionIdByParticipants(patient, doctor, handler) { ParticipantRepo.findSessionIdByParticipantIds(patient, doctor, handler); } /** * 将成员写入redis * * @param sessionId 会话ID * @param participantsArray 会话参与者集合 * @param createDate 创建日期 * @param handler 回调 */ static saveParticipantsToRedis(sessionId, participantsArray, createDate, handler) { // 构造会话,成员及成员角色zset, hash所需要的数据 let userSessions = {}; let sessionParticipants = []; let sessionParticipantsRoles = []; participantsArray.forEach(function (item) { let tokens = item.split(":"); userSessions[RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, tokens[0])] = [createDate.getTime(), sessionId]; sessionParticipants.push(createDate.getTime()); sessionParticipants.push(tokens[0]); sessionParticipantsRoles.push(tokens[0], tokens[1]); }); // 向会话成员、会话成员角色集合中添加数据 let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId); let sessionParticipantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId); let multi = redis.multi() .zadd(sessionParticipantsKey, sessionParticipants) .hmset(sessionParticipantsRoleKey, sessionParticipantsRoles); // 更新用户参与的会话列表 for (let key in userSessions) { multi = multi.zadd(key, userSessions[key]); } multi.execAsync() .then(function (res) { handler(true); }); } /** * mysql成员创建 * * @param sessionId * @param users * @param handler */ static saveParticipantsToMysql(sessionId, users, handler) { return ParticipantRepo.saveParticipantsToMysql(sessionId, users, handler); } /** * 移除成员 * @param sessionId * @param userId */ removeUser(sessionId, userId) { let self = this; let participants_key = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId); let user_session_key = RedisModel.makeRedisKey(REDIS_KEYS.UsersSessions, userId); // 移除会话中的成员信息,用户的Session信息及MySQL中的记录 redis.multi() .zrem(participants_key, userId) .zrem(user_session_key, sessionId) .execAsync() .then(function (res) { self.deleteUserFromMysql(sessionId, userId); ModelUtil.emitOK(self.eventEmitter, {}); }) .catch(function (err) { ModelUtil.emitError(self.eventEmitter, {message: "成员删除失败: " + err}); }); } /** * 更新用户在MUC模式中的状态 * @param sessionId 会话ID * @param user 用户 * @param role 变更状态 */ updateUser(sessionId, user, role) { let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId); redis.hsetAsync(participantsRoleKey, user, role) .then(function (res) { ParticipantRepo.updateParticipant(sessionId, user, role, function (err, res) { }); }) } /** * 添加讨论组成员 * @param sessionId * @param user */ addUser(sessionId, user) { let self = this; let users = [user]; self.saveParticipantsToRedis(sessionId, users, new Date(), function (res) { if (res) { self.saveParticipantsToMysql(sessionId, users); } else { ModelUtil.emitOK(self.eventEmitter, {message: "成员添加失败"}); } }) } /** * user从数据库中删除 * @param sessionId 会话 * @param user 用户 */ deleteUserFromMysql(sessionId, user) { ParticipantRepo.deleteUserFromMysql(sessionId, user); } } // Expose class module.exports = Participants;