participants.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * @param handler
  71. */
  72. getSessionIdByParticipants(patient, doctor, handler) {
  73. ParticipantRepo.findSessionIdByParticipantIds(patient, doctor, handler);
  74. }
  75. /**
  76. * 将成员写入redis
  77. *
  78. * @param sessionId 会话ID
  79. * @param participantsArray 会话参与者集合
  80. * @param createDate 创建日期
  81. * @param handler 回调
  82. */
  83. static saveParticipantsToRedis(sessionId, participantsArray, createDate, handler) {
  84. // 构造会话,成员及成员角色zset, hash所需要的数据
  85. let userSessions = {};
  86. let sessionParticipants = [];
  87. let sessionParticipantsRoles = [];
  88. participantsArray.forEach(function (item) {
  89. let tokens = item.split(":");
  90. userSessions[RedisModel.makeRedisKey(REDIS_KEYS.UserSessions, tokens[0])] = [createDate.getTime(), sessionId];
  91. sessionParticipants.push(createDate.getTime());
  92. sessionParticipants.push(tokens[0]);
  93. sessionParticipantsRoles.push(tokens[0], tokens[1]);
  94. });
  95. // 向会话成员、会话成员角色集合中添加数据
  96. let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
  97. let sessionParticipantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
  98. let multi = redis.multi()
  99. .zadd(sessionParticipantsKey, sessionParticipants)
  100. .hmset(sessionParticipantsRoleKey, sessionParticipantsRoles);
  101. // 更新用户参与的会话列表
  102. for (let key in userSessions) {
  103. multi = multi.zadd(key, userSessions[key]);
  104. }
  105. multi.execAsync()
  106. .then(function (res) {
  107. handler(true);
  108. });
  109. }
  110. /**
  111. * mysql成员创建
  112. *
  113. * @param sessionId
  114. * @param users
  115. * @param handler
  116. */
  117. static saveParticipantsToMysql(sessionId, users, handler) {
  118. return ParticipantRepo.saveParticipantsToMysql(sessionId, users, handler);
  119. }
  120. /**
  121. * 移除成员
  122. * @param sessionId
  123. * @param userId
  124. */
  125. removeUser(sessionId, userId) {
  126. let self = this;
  127. let participants_key = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
  128. let user_session_key = RedisModel.makeRedisKey(REDIS_KEYS.UsersSessions, userId);
  129. // 移除会话中的成员信息,用户的Session信息及MySQL中的记录
  130. redis.multi()
  131. .zrem(participants_key, userId)
  132. .zrem(user_session_key, sessionId)
  133. .execAsync()
  134. .then(function (res) {
  135. self.deleteUserFromMysql(sessionId, userId);
  136. ModelUtil.emitOK(self.eventEmitter, {});
  137. })
  138. .catch(function (err) {
  139. ModelUtil.emitError(self.eventEmitter, {message: "成员删除失败: " + err});
  140. });
  141. }
  142. /**
  143. * 更新用户在MUC模式中的状态
  144. * @param sessionId 会话ID
  145. * @param user 用户
  146. * @param role 变更状态
  147. */
  148. updateUser(sessionId, user, role) {
  149. let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
  150. redis.hsetAsync(participantsRoleKey, user, role)
  151. .then(function (res) {
  152. ParticipantRepo.updateParticipant(sessionId, user, role, function (err, res) {
  153. });
  154. })
  155. }
  156. /**
  157. * 添加讨论组成员
  158. * @param sessionId
  159. * @param user
  160. */
  161. addUser(sessionId, user) {
  162. let self = this;
  163. let users = [user];
  164. self.saveParticipantsToRedis(sessionId, users, new Date(), function (res) {
  165. if (res) {
  166. self.saveParticipantsToMysql(sessionId, users);
  167. } else {
  168. ModelUtil.emitOK(self.eventEmitter, {message: "成员添加失败"});
  169. }
  170. })
  171. }
  172. /**
  173. * user从数据库中删除
  174. * @param sessionId 会话
  175. * @param user 用户
  176. */
  177. deleteUserFromMysql(sessionId, user) {
  178. ParticipantRepo.deleteUserFromMysql(sessionId, user);
  179. }
  180. }
  181. // Expose class
  182. module.exports = Participants;