participants.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. for (var j in users) {
  82. let user_session_key = super.makeRedisKey(REDIS_KEYS.UserSessions, users[j]);
  83. redis.zaddAsync(participants_key, createDate.getTime(), users[j]).then(function (res) {
  84. return redis.zaddAsync(user_session_key, createDate.getTime(), session_id);
  85. }
  86. ).catch(function (err) {
  87. log.error("createParticipantsToRedis is fail error:" + err + ",session_id:" + session_id + ",users:" + users.join(","));
  88. handler(false);
  89. });
  90. }
  91. handler(true);
  92. }
  93. /**
  94. * mysql成员创建
  95. *
  96. * @param session_id
  97. * @param users
  98. */
  99. saveParticipantsToMysql(session_id, users) {
  100. return ParticipantRepo.createParticipantsToMysql(session_id, users);
  101. }
  102. /**
  103. * MUC成员创建
  104. * @param users
  105. */
  106. createMUCParticipants(users) {
  107. return true;
  108. }
  109. /**
  110. * 移除成员
  111. * @param sessionId
  112. * @param userId
  113. */
  114. deleteUser(sessionId, userId) {
  115. let self = this;
  116. let participants_key = super.makeRedisKey(REDIS_KEYS.Participants, sessionId);
  117. let user_session_key = super.makeRedisKey(REDIS_KEYS.UsersSessions, userId);
  118. //1.移除SESSION成员表中的成员信息
  119. redis.zremAsync(participants_key, userId).then(function (res) {
  120. log.info("remove participants:" + res);
  121. //2.删除对应人员的Session列表
  122. redis.zremAsync(user_session_key, sessionId).then(function (res) {
  123. log.info("remove user_session:" + res);
  124. //3.移除数据库中的人员记录
  125. self.deleteUserFromMysql(sessionId, userId);
  126. ModelUtil.emitData(self.eventEmitter, {"status": 200, "msg": "人员删除成功!"});
  127. }).catch(function (err) {
  128. ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
  129. })
  130. }).catch(function (err) {
  131. ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员删除失败!" + err});
  132. })
  133. }
  134. /**
  135. * 添加讨论组成员
  136. * @param sessionId
  137. * @param user
  138. */
  139. pushUser(sessionId, user) {
  140. let self = this;
  141. let users = [];
  142. users.push(user);
  143. self.saveParticipantsToRedis(sessionId, users, new Date(), function (res) {
  144. if (res) {
  145. self.saveParticipantsToMysql(sessionId, users);
  146. } else {
  147. ModelUtil.emitData(self.eventEmitter, {"status": -1, "msg": "人员添加失败!"});
  148. }
  149. })
  150. }
  151. /**
  152. * user从数据库中删除
  153. * @param sessionId 会话
  154. * @param user 用户
  155. */
  156. deleteUserFromMysql(sessionId, user) {
  157. ParticipantRepo.deleteUserFromMysql(sessionId, user);
  158. }
  159. }
  160. // Expose class
  161. module.exports = Participants;