participants.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * 会话成员模型。
  3. */
  4. "use strict";
  5. let RedisModel = require('./../redis.model.js');
  6. let ModelUtil = require('../../util/model.util');
  7. let RedisClient = require('../../repository/redis/redis.client.js');
  8. let ParticipantRepo = require('../../repository/mysql/participant.repo');
  9. let log = require('../../util/log.js');
  10. let redis = RedisClient.redisClient().connection;
  11. const REDIS_KEYS = require('../../include/commons').REDIS_KEYS;
  12. class Participants extends RedisModel {
  13. constructor() {
  14. super();
  15. }
  16. /**
  17. * 获取会话的成员列表,直接从MySQL获取。
  18. *
  19. * @param sessionId
  20. */
  21. getParticipants(sessionId) {
  22. let self = this;
  23. ParticipantRepo.findAll(sessionId, function (err, participants) {
  24. if(err){
  25. ModelUtil.emitError(self.eventEmitter, "Get session participants error", err);
  26. return;
  27. }
  28. ModelUtil.emitOK(self.eventEmitter, participants);
  29. });
  30. }
  31. /**
  32. * 获取所有成员的头像。
  33. *
  34. * @param sessionId
  35. */
  36. getParticipantsAvatar(sessionId){
  37. let self = this;
  38. ParticipantRepo.findAllAvatars(sessionId, function (err, participantsAvatars) {
  39. if(err){
  40. ModelUtil.emitError(self.eventEmitter, "Get session participant's avatars error", err);
  41. return;
  42. }
  43. ModelUtil.emitOK(self.eventEmitter, participantsAvatars);
  44. })
  45. }
  46. /**
  47. * 会话中是否存在指定成员
  48. *
  49. * @param sessionId
  50. * @param userId
  51. * @param handler
  52. */
  53. existsParticipant(sessionId, userId, handler) {
  54. let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
  55. redis.hgetAsync(participantsRoleKey, userId).then(function (res) {
  56. if (false) {
  57. // get from redis
  58. handler(null, true);
  59. } else {
  60. // get from mysql
  61. ParticipantRepo.existsParticipant(sessionId, userId, handler);
  62. }
  63. })
  64. }
  65. /**
  66. * 获取P2P成员所在会话
  67. *
  68. * @param patient
  69. * @param doctor
  70. */
  71. getSessionIdByParticipants(patient, doctor, handler) {
  72. ParticipantRepo.findSessionIdByParticipantIds(patient, doctor, handler);
  73. }
  74. /**
  75. * 将成员写入redis
  76. *
  77. * @param sessionId 会话ID
  78. * @param participantsArray 会话参与者集合
  79. * @param createDate 创建日期
  80. * @param handler 回调
  81. */
  82. static saveParticipantsToRedis(sessionId, participantsArray, createDate, handler) {
  83. // 构造会话,成员及成员角色zset, hash所需要的数据
  84. let userSessions = {};
  85. let sessionParticipants = [];
  86. let sessionParticipantsRoles = [];
  87. participantsArray.forEach(function (item) {
  88. let tokens = item.split(":");
  89. userSessions[RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, tokens[0])] = [createDate.getTime(), sessionId];
  90. sessionParticipants.push(createDate.getTime());
  91. sessionParticipants.push(tokens[0]);
  92. sessionParticipantsRoles.push(tokens[0], tokens[1]);
  93. });
  94. // 向会话成员、会话成员角色集合中添加数据
  95. let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
  96. let sessionParticipantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
  97. let multi = redis.multi()
  98. .zadd(sessionParticipantsKey, sessionParticipants)
  99. .hmset(sessionParticipantsRoleKey, sessionParticipantsRoles);
  100. // 更新用户参与的会话列表
  101. for(let key in userSessions){
  102. multi = multi.zadd(key, userSessions[key]);
  103. }
  104. multi.execAsync()
  105. .then(function (res) {
  106. handler(true);
  107. });
  108. }
  109. /**
  110. * mysql成员创建
  111. *
  112. * @param sessionId
  113. * @param users
  114. * @param handler
  115. */
  116. static saveParticipantsToMysql(sessionId, users, handler) {
  117. return ParticipantRepo.saveParticipantsToMysql(sessionId, users, handler);
  118. }
  119. /**
  120. * 移除成员
  121. * @param sessionId
  122. * @param userId
  123. */
  124. deleteUser(sessionId, userId) {
  125. let self = this;
  126. let participants_key = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
  127. let user_session_key = RedisModel.makeRedisKey(REDIS_KEYS.UsersSessions, userId);
  128. //1.移除SESSION成员表中的成员信息
  129. redis.zremAsync(participants_key, userId).then(function (res) {
  130. log.info("remove participants:" + res);
  131. //2.删除对应人员的Session列表
  132. redis.zremAsync(user_session_key, sessionId).then(function (res) {
  133. log.info("remove user_session:" + res);
  134. //3.移除数据库中的人员记录
  135. self.deleteUserFromMysql(sessionId, userId);
  136. ModelUtil.emitOK(self.eventEmitter, {"status": 200, "msg": "人员删除成功!"});
  137. }).catch(function (err) {
  138. ModelUtil.emitOK(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
  139. })
  140. }).catch(function (err) {
  141. ModelUtil.emitOK(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
  142. })
  143. }
  144. /**
  145. * 更新用户在MUC模式中的状态
  146. * @param sessionId 会话ID
  147. * @param user 用户
  148. * @param role 变更状态
  149. */
  150. updateUser(sessionId, user, role) {
  151. let participants_role_key = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
  152. redis.hsetAsync(sessionId, user, role).then(function (res) {
  153. ParticipantRepo.updateParticipant(sessionId, user, role,function(err,res){
  154. });
  155. })
  156. }
  157. /**
  158. * 添加讨论组成员
  159. * @param sessionId
  160. * @param user
  161. */
  162. addUser(sessionId, user) {
  163. let self = this;
  164. let users = [];
  165. users.push(user);
  166. self.saveParticipantsToRedis(sessionId, users, new Date(), function (res) {
  167. if (res) {
  168. self.saveParticipantsToMysql(sessionId, users);
  169. } else {
  170. ModelUtil.emitOK(self.eventEmitter, {"status": -1, "msg": "人员添加失败!"});
  171. }
  172. })
  173. }
  174. /**
  175. * user从数据库中删除
  176. * @param sessionId 会话
  177. * @param user 用户
  178. */
  179. deleteUserFromMysql(sessionId, user) {
  180. ParticipantRepo.deleteUserFromMysql(sessionId, user);
  181. }
  182. }
  183. // Expose class
  184. module.exports = Participants;