123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- /**
- * 会话成员模型。
- */
- "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();
- }
- /**
- * 获取会话的成员列表
- *
- * @param sessionId
- */
- getParticipants(sessionId, handler) {
- let participant_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
- redis.existsAsync(participant_key).then(function (res) {
- if (res) {
- redis.zrangeAsync(participant_key, 0, -1).then(function (res) {
- handler(res);
- })
- } else {
- ParticipantRepo.findParticipants(sessionId, handler);
- }
- })
- }
- /**
- * 会话中是否存在指定成员
- *
- * @param sessionId
- * @param userId
- * @param handler
- */
- existsParticipant(sessionId, userId, handler) {
- let participant_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
- redis.existsAsync(participant_key).then(function (res) {
- if (res) {
- // get from redis
- redis.zrangeAsync(participant_key, 0, -1).then(function (res) {
- let exists = false;
- for (var j in res) {
- var value = res[j];
- if (value == userId) {
- exists = true;
- break;
- }
- }
- handler(null, exists);
- })
- } else {
- // get from mysql
- ParticipantRepo.existsParticipant(sessionId, userId, handler);
- }
- })
- }
- /**
- * 获取P2P成员所在会话
- *
- * @param patient
- * @param doctor
- */
- getSessionIdByParticipants(patient, doctor, handler) {
- ParticipantRepo.findSessionIdByParticipantIds(patient, doctor, handler);
- }
- /**
- * 将成员写入redis
- *
- * @param session_id 会话ID
- * @param users 用户集合
- * @param createDate 创建日期
- * @param handler 回调
- */
- 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);
- });
- }
- handler(true);
- }
- /**
- * mysql成员创建
- *
- * @param session_id
- * @param users
- */
- saveParticipantsToMysql(session_id, users) {
- return ParticipantRepo.createParticipantsToMysql(session_id, users);
- }
- /**
- * MUC成员创建
- * @param users
- */
- createMUCParticipants(users) {
- return true;
- }
- /**
- * 移除成员
- * @param sessionId
- * @param userId
- */
- deleteUser(sessionId, userId) {
- let self = this;
- let participants_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
- let user_session_key = super.makeRedisKey(REDIS_KEYS.UsersSessions, userId);
- //1.移除SESSION成员表中的成员信息
- redis.zremAsync(participants_key, userId).then(function (res) {
- log.info("remove participants:" + res);
- //2.删除对应人员的Session列表
- redis.zremAsync(user_session_key, sessionId).then(function (res) {
- log.info("remove user_session:" + res);
- //3.移除数据库中的人员记录
- self.deleteUserFromMysql(sessionId, userId);
- ModelUtil.emitData(self.eventEmitter, {"status": 200, "msg": "人员删除成功!"});
- }).catch(function (err) {
- ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
- })
- }).catch(function (err) {
- ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
- })
- }
- /**
- * 添加讨论组成员
- * @param sessionId
- * @param user
- */
- pushUser(sessionId, user) {
- let self = this;
- let users = [];
- users.push(user);
- self.saveParticipantsToRedis(sessionId, users, new Date(), function (res) {
- if (res) {
- self.saveParticipantsToMysql(sessionId, users);
- } else {
- ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员添加失败!"});
- }
- })
- }
- /**
- * user从数据库中删除
- * @param sessionId 会话
- * @param user 用户
- */
- deleteUserFromMysql(sessionId, user) {
- ParticipantRepo.deleteUserFromMysql(sessionId, user);
- }
- }
- // Expose class
- module.exports = Participants;
|