participants.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. * 获取会话的成员列表
  18. *
  19. * @param sessionId
  20. */
  21. getParticipants(sessionId, handler) {
  22. let participant_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
  23. redis.existsAsync(participant_key).then(function (res) {
  24. if (res) {
  25. redis.zrangeAsync(participant_key, 0, -1).then(function (res) {
  26. handler(res);
  27. })
  28. } else {
  29. ParticipantRepo.findParticipants(sessionId, handler);
  30. }
  31. })
  32. }
  33. /**
  34. * 会话中是否存在指定成员
  35. *
  36. * @param sessionId
  37. * @param userId
  38. * @param handler
  39. */
  40. existsParticipant(sessionId, userId, handler) {
  41. let participant_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
  42. redis.existsAsync(participant_key).then(function (res) {
  43. if (res) {
  44. // get from redis
  45. redis.zrangeAsync(participant_key, 0, -1).then(function (res) {
  46. let exists = false;
  47. for (var j in res) {
  48. var value = res[j];
  49. if (value == userId) {
  50. exists = true;
  51. break;
  52. }
  53. }
  54. handler(null, exists);
  55. })
  56. } else {
  57. // get from mysql
  58. ParticipantRepo.existsParticipant(sessionId, userId, handler);
  59. }
  60. })
  61. }
  62. /**
  63. * 获取P2P成员所在会话
  64. *
  65. * @param patient
  66. * @param doctor
  67. */
  68. getSessionIdByParticipants(patient, doctor, handler) {
  69. ParticipantRepo.findSessionIdByParticipantIds(patient, doctor, handler);
  70. }
  71. /**
  72. * 将成员写入redis
  73. *
  74. * @param session_id 会话ID
  75. * @param users 用户集合
  76. * @param createDate 创建日期
  77. * @param handler 回调
  78. */
  79. saveParticipantsToRedis(session_id, users, createDate, handler) {
  80. let participants_key = super.makeRedisKey(REDIS_KEYS.Participants, session_id);
  81. let participants_role_key = super.makeRedisKey(REDIS_KEYS.ParticipantsRole, session_id);
  82. let _super = super.makeRedisKey;
  83. redis.hmsetAsync(participants_role_key,users).then(function(res){
  84. log.info("create participants role!");
  85. for (var j in users) {
  86. let user_session_key = _super(REDIS_KEYS.UserSessions, j);
  87. redis.zaddAsync(participants_key, createDate.getTime(), j).then(function (res) {
  88. return redis.zaddAsync(user_session_key, createDate.getTime(), session_id);
  89. }
  90. ).catch(function (err) {
  91. log.error("createParticipantsToRedis is fail error:" + err + ",session_id:" + session_id + ",users:" + JSON.stringify(users));
  92. handler(false);
  93. });
  94. }
  95. })
  96. handler(true);
  97. }
  98. /**
  99. * mysql成员创建
  100. *
  101. * @param session_id
  102. * @param users
  103. */
  104. saveParticipantsToMysql(session_id, users) {
  105. return ParticipantRepo.saveParticipantsToMysql(session_id, users);
  106. }
  107. /**
  108. * MUC成员创建
  109. * @param users
  110. */
  111. createMUCParticipants(users) {
  112. return true;
  113. }
  114. /**
  115. * 移除成员
  116. * @param sessionId
  117. * @param userId
  118. */
  119. deleteUser(sessionId, userId) {
  120. let self = this;
  121. let participants_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
  122. let user_session_key = super.makeRedisKey(REDIS_KEYS.UsersSessions, userId);
  123. //1.移除SESSION成员表中的成员信息
  124. redis.zremAsync(participants_key, userId).then(function (res) {
  125. log.info("remove participants:" + res);
  126. //2.删除对应人员的Session列表
  127. redis.zremAsync(user_session_key, sessionId).then(function (res) {
  128. log.info("remove user_session:" + res);
  129. //3.移除数据库中的人员记录
  130. self.deleteUserFromMysql(sessionId, userId);
  131. ModelUtil.emitData(self.eventEmitter, {"status": 200, "msg": "人员删除成功!"});
  132. }).catch(function (err) {
  133. ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
  134. })
  135. }).catch(function (err) {
  136. ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
  137. })
  138. }
  139. /**
  140. * 更新用户在MUC模式中的状态
  141. * @param sessionId 会话ID
  142. * @param user 用户
  143. * @param role 变更状态
  144. */
  145. updateUser(sessionId,user,role){
  146. let participants_role_key = super.makeRedisKey(REDIS_KEYS.ParticipantsRole, sessionId);
  147. redis.hsetAsync(sessionId,user,role).then(function(res){
  148. ParticipantRepo.updateParticipant(sessionId,user,role);
  149. })
  150. }
  151. /**
  152. * 添加讨论组成员
  153. * @param sessionId
  154. * @param user
  155. */
  156. pushUser(sessionId, user) {
  157. let self = this;
  158. let users = [];
  159. users.push(user);
  160. self.saveParticipantsToRedis(sessionId, users, new Date(), function (res) {
  161. if (res) {
  162. self.saveParticipantsToMysql(sessionId, users);
  163. } else {
  164. ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员添加失败!"});
  165. }
  166. })
  167. }
  168. /**
  169. * user从数据库中删除
  170. * @param sessionId 会话
  171. * @param user 用户
  172. */
  173. deleteUserFromMysql(sessionId, user) {
  174. ParticipantRepo.deleteUserFromMysql(sessionId, user);
  175. }
  176. }
  177. // Expose class
  178. module.exports = Participants;