session.endpoint.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /**
  2. * 会话接口。
  3. *
  4. * author: Sand
  5. * since: 12/15/2016
  6. */
  7. "use strict";
  8. let express = require('express');
  9. let router = express.Router();
  10. let ObjectUtil = require("../../util/object.util.js");
  11. let ControllerUtil = require('../../util/controller.util');
  12. let Sessions = require('../../models/sessions/sessions');
  13. let Messages = require('../../models/messages/messages');
  14. let Participants = require('../../models/sessions/participants');
  15. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  16. const APIv2 = require('../../include/endpoints').APIv2;
  17. /**
  18. * 创建会话。对MUC会话而言,需要创建的是议题,如果是第一次创建议题,那应该附带创建会话,而不是直接创建会话。
  19. *
  20. * session_type:1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组
  21. * (咨询类型:1三师咨询,2视频咨询,3图文咨询,4公共咨询,5病友圈 6、患者名医咨询 7医生名医咨询 8续方咨询 9居民直接咨询专科 10医生发起的求助 11上门服务咨询)
  22. * users 讨论组包含的用户标示
  23. * sessionId 会话ID
  24. *
  25. * 请求URL: /sessions
  26. * 参数:
  27. * {
  28. * session_id: 会话ID,非必须,仅对群消息使用。
  29. * session_type: 会话类型,必须。
  30. * session_name: 会话名称,
  31. * participants: 此会话的成员列表,格式:["userId1:role", "userId2:role"],用户的ID及角色。
  32. * }
  33. */
  34. router.post("/", function (req, res) {
  35. let payload = req.body;
  36. let testing = ObjectUtil.fieldsCheck(payload, 'participants', 'session_name', 'session_type');
  37. if (!testing.pass) {
  38. throw testing.message;
  39. }
  40. let participants = payload.participants;
  41. let sessionName = payload.session_name;
  42. let sessionType = payload.session_type;
  43. let sessionId = payload.session_id;
  44. let sessions = new Sessions();
  45. let participantArray = [];
  46. ///是否视频会议的会话
  47. let videoconferencing = false;
  48. participants = JSON.parse(participants);
  49. for (let j in participants) {
  50. participantArray.push(j + ":" + participants[j]);
  51. }
  52. ControllerUtil.regModelEventHandler(sessions, res);
  53. sessions.createSession(sessionId, sessionName, sessionType, participantArray);
  54. //视频会议的会话 szx add 20181121
  55. if (!payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
  56. videoconferencing = true;
  57. //发送广播,给相关的与会人员
  58. for (let j in participants) {
  59. let participant = participants[j];
  60. let socketClient = ClientCache.findById(participant.userId);
  61. socketClient.emt("startVideoconference",{"session_id":sessionId});
  62. }
  63. }
  64. });
  65. /**
  66. * 获取会话列表
  67. *
  68. * 请求URL /sessions?user_id=3121&page=0&size=10
  69. */
  70. router.get("/", function (req, res) {
  71. let page = req.query.page;
  72. let size = req.query.size;
  73. let userId = req.query.user_id;
  74. let businessType = req.query.business_type;
  75. let status = req.query.status;
  76. if (!page) {
  77. throw {httpStatus: 406, message: 'Missing page.'};
  78. }
  79. if (!size) {
  80. throw {httpStatus: 406, message: 'Missing size.'};
  81. }
  82. if (!userId) {
  83. throw {httpStatus: 406, message: 'Missing user.'};
  84. }
  85. let sessions = new Sessions();
  86. ControllerUtil.regModelEventHandler(sessions, res);
  87. //传入参数齐全走过滤方法
  88. if(status&&businessType&&userId&&size&&page){
  89. sessions.getUserStatusSessions(userId,status,businessType,page,size);
  90. }else{
  91. sessions.getUserSessions(userId, page, size, businessType);
  92. }
  93. });
  94. /**
  95. * 按会话类型获取会话列表
  96. * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4
  97. */
  98. router.get(APIv2.Sessions.SessionListByType, function (req, res) {
  99. let page = req.query.page;
  100. let size = req.query.size;
  101. let userId = req.query.user_id;
  102. let type = req.query.type;
  103. if (!page) {
  104. throw {httpStatus: 406, message: 'Missing page.'};
  105. }
  106. if (!size) {
  107. throw {httpStatus: 406, message: 'Missing size.'};
  108. }
  109. if (!type) {
  110. throw {httpStatus: 406, message: 'Missing type.'};
  111. }
  112. if (!userId) {
  113. throw {httpStatus: 406, message: 'Missing user.'};
  114. }
  115. let sessions = new Sessions();
  116. ControllerUtil.regModelEventHandler(sessions, res);
  117. sessions.getUserSessionsByType(userId,type,page,size);
  118. });
  119. /**
  120. * 按会话类型获取会话总数
  121. * 请求URL /sessions/sessionCountByType?user_id=3121&type=4
  122. */
  123. router.get(APIv2.Sessions.SessionCountByType, function (req, res) {
  124. let userId = req.query.user_id;
  125. let type = req.query.type;
  126. if (!type) {
  127. throw {httpStatus: 406, message: 'Missing type.'};
  128. }
  129. if (!userId) {
  130. throw {httpStatus: 406, message: 'Missing user.'};
  131. }
  132. let sessions = new Sessions();
  133. ControllerUtil.regModelEventHandler(sessions, res);
  134. sessions.getSessionCountByType(userId,type);
  135. });
  136. router.get(APIv2.Sessions.Session,function(req,res){
  137. let sessionId = req.params.session_id;
  138. let userId = req.query.user_id;
  139. if (!sessionId) {
  140. throw {httpStatus: 406, message: 'Missing sessionId.'};
  141. }
  142. if (!userId) {
  143. throw {httpStatus: 406, message: 'Missing user.'};
  144. }
  145. let sessions = new Sessions();
  146. ControllerUtil.regModelEventHandler(sessions, res);
  147. sessions.getSession(sessionId,userId);
  148. });
  149. /**
  150. * 最近会话列表。
  151. *
  152. * URL:
  153. * /sessions/recent?user_id=abc&date_span=7
  154. */
  155. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  156. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  157. let userId = req.query.user_id;
  158. let dateSpan = req.query.date_span;
  159. let sessions = new Sessions();
  160. ControllerUtil.regModelEventHandler(sessions, res);
  161. sessions.getRecentSessions(userId, dateSpan);
  162. });
  163. /**
  164. * 判断会话是否存在。
  165. *
  166. * URL:
  167. * /sessions/isExist?session_id=abc
  168. */
  169. router.get(APIv2.Sessions.IsExist, function (req, res) {
  170. let sessionId = req.query.session_id;
  171. if (!sessionId) {
  172. throw {httpStatus: 406, message: 'Missing sessionId.'}
  173. }
  174. let sessions = new Sessions();
  175. ControllerUtil.regModelEventHandler(sessions, res);
  176. sessions.isExist(sessionId);
  177. });
  178. /**
  179. * 某个聊天记录置顶操作
  180. *
  181. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  182. */
  183. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  184. let user = req.query.user;
  185. let sessionId = req.query.sessionId;
  186. if (!user) {
  187. throw {httpStatus: 406, message: 'Missing user.'};
  188. }
  189. if (!sessionId) {
  190. throw {httpStatus: 406, message: 'Missing sessionId.'};
  191. }
  192. let sessions = new Sessions();
  193. ControllerUtil.regModelEventHandler(sessions, res);
  194. sessions.stickSession(sessionId, user);
  195. });
  196. /**
  197. * 某个聊天记录置顶操作
  198. *
  199. * 请求URL /status?status=1&sessionId=testsessionmsg1
  200. */
  201. router.post(APIv2.Sessions.SessionStatus, function (req, res) {
  202. let sessionId = req.query.sessionId;
  203. let status = req.query.status;
  204. if (!sessionId) {
  205. throw {httpStatus: 406, message: 'Missing sessionId.'};
  206. }
  207. if (!status) {
  208. throw {httpStatus: 406, message: 'Missing status.'};
  209. }
  210. let sessions = new Sessions();
  211. ControllerUtil.regModelEventHandler(sessions, res);
  212. sessions.updateSessionStatus(sessionId, status);
  213. });
  214. /**
  215. * 修改会话名称(修改redis和数据库)
  216. *
  217. * 请求URL /name?name=哈哈&sessionId=testsessionmsg1
  218. */
  219. router.post(APIv2.Sessions.SessionName, function (req, res) {
  220. let sessionId = req.body.sessionId;
  221. let name = req.body.name;
  222. if (!sessionId) {
  223. throw {httpStatus: 406, message: 'Missing sessionId.'};
  224. }
  225. if (!name) {
  226. throw {httpStatus: 406, message: 'Missing name.'};
  227. }
  228. let sessions = new Sessions();
  229. ControllerUtil.regModelEventHandler(sessions, res);
  230. sessions.updateSessionName(sessionId, name);
  231. });
  232. /**
  233. * 取消置顶
  234. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  235. */
  236. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  237. let user = req.query.user;
  238. let sessionId = req.query.sessionId;
  239. if (!user) {
  240. throw {httpStatus: 406, message: 'Missing user.'};
  241. }
  242. if (!sessionId) {
  243. throw {httpStatus: 406, message: 'Missing sessionId.'};
  244. }
  245. let sessions = new Sessions();
  246. ControllerUtil.regModelEventHandler(sessions, res);
  247. sessions.cancelStickSession(sessionId, user);
  248. });
  249. /**
  250. * 获取会话成员。
  251. */
  252. router.get(APIv2.Sessions.Participants, function (req, res) {
  253. let sessionId = req.params.session_id;
  254. let participants = new Participants();
  255. ControllerUtil.regModelEventHandler(participants, res);
  256. participants.getParticipants(sessionId);
  257. });
  258. /**
  259. * 获取会话成员头像列表。
  260. */
  261. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  262. let sessionId = req.params.session_id;
  263. let participants = new Participants();
  264. ControllerUtil.regModelEventHandler(participants, res);
  265. participants.getParticipantsAvatar(sessionId);
  266. });
  267. /**
  268. * 增加成员
  269. * user:增加的人员
  270. * sessionId 会话ID
  271. */
  272. router.put(APIv2.Sessions.Participant, function (req, res) {
  273. let user = req.params.participant_id;
  274. if (!user) {
  275. throw {httpStatus: 406, message: 'Missing participant_id.'};
  276. }
  277. let sessionId = req.params.session_id;
  278. if (!sessionId) {
  279. throw {httpStatus: 406, message: 'Missing session_id.'};
  280. }
  281. let participants = new Participants();
  282. ControllerUtil.regModelEventHandler(participants, res);
  283. participants.addUser(sessionId, user);
  284. });
  285. /**
  286. * 增加成员
  287. * user_id:增加的人员
  288. * old_user_id:删除的人员
  289. * session_id 会话ID
  290. */
  291. router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
  292. let payload = req.body;
  293. let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
  294. if (!testing.pass) {
  295. throw testing.message;
  296. }
  297. let participants = new Participants();
  298. ControllerUtil.regModelEventHandler(participants, res);
  299. participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
  300. });
  301. /**
  302. * 移除成员
  303. * user:移除的人员
  304. * sessionId 会话ID
  305. */
  306. router.delete(APIv2.Sessions.Participant, function (req, res) {
  307. let user = req.params.participant_id;
  308. if (!user) {
  309. throw {httpStatus: 406, message: 'Missing user.'};
  310. }
  311. let sessionId = req.params.session_id;
  312. if (!sessionId) {
  313. throw {httpStatus: 406, message: 'Missing sessionId.'};
  314. }
  315. let participants = new Participants();
  316. ControllerUtil.regModelEventHandler(participants, res);
  317. participants.removeUser(sessionId, user);
  318. });
  319. /**
  320. * 发送消息
  321. *
  322. * 请求URL:
  323. * /sessions/:session_id/messages
  324. *
  325. * 消息体:
  326. * {
  327. * sender_id: bea851fc,
  328. * sender_name: 李毅,
  329. * content_type: 1,
  330. * content: 李医生,你好
  331. * }
  332. */
  333. router.post(APIv2.Sessions.Messages, function (req, res) {
  334. let payload = req.body;
  335. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  336. if (!testing.pass) {
  337. throw {httpStatus: 406, message: testing.message}
  338. }
  339. if(payload.view&&payload.view==1){
  340. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  341. }
  342. // 消息的发送时间由服务端决定
  343. payload.timestamp = new Date((new Date().getTime()));
  344. let sessions = new Sessions();
  345. ControllerUtil.regModelEventHandler(sessions, res);
  346. sessions.saveMessageBySession(req.params.session_id, payload);
  347. });
  348. router.post(APIv2.Sessions.TopicMessages, function (req, res) {
  349. let payload = req.body;
  350. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  351. if (!testing.pass) {
  352. throw {httpStatus: 406, message: testing.message}
  353. }
  354. if(payload.view&&payload.view==1){
  355. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  356. }
  357. let sessions = new Sessions();
  358. ControllerUtil.regModelEventHandler(sessions, res);
  359. payload.timestamp = new Date((new Date().getTime()));
  360. sessions.sendTopicMessages(req.params.topic_id, payload);
  361. })
  362. /**
  363. * 获取消息
  364. *
  365. * 请求URL:
  366. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  367. */
  368. router.get(APIv2.Sessions.Messages, function (req, res) {
  369. let startMsgId = req.query.start_msg_id;
  370. let endMsgId = req.query.end_msg_id;
  371. let pagesize = req.query.pagesize;
  372. let page = req.query.page;
  373. let user = req.query.user;
  374. let sessionId = req.query.session_id;
  375. let content_type = req.query.content_type;
  376. let isoffset = req.query.isoffset;
  377. if (!user) {
  378. throw {httpStatus: 406, message: 'Missing user.'};
  379. }
  380. if (!sessionId) {
  381. throw {httpStatus: 406, message: 'Missing session_id.'};
  382. }
  383. if (!pagesize) {
  384. pagesize = 20;
  385. }
  386. if (!page) {
  387. page = 1;
  388. }
  389. if (content_type) {
  390. let messages = new Messages();
  391. ControllerUtil.regModelEventHandler(messages, res);
  392. messages.getMessageByType(sessionId, page, pagesize, content_type)
  393. } else {
  394. let sessions = new Sessions();
  395. ControllerUtil.regModelEventHandler(sessions, res);
  396. sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
  397. }
  398. });
  399. /**
  400. * 修改单条消息
  401. * session_id 会话ID
  402. * message_id:消息ID
  403. * content 消息内容
  404. */
  405. router.post(APIv2.Sessions.MessageUpdate, function (req, res) {
  406. let payload = req.body;
  407. let testing = ObjectUtil.fieldsCheck(payload,'session_type', 'session_id','message_id','content');
  408. if (!testing.pass) {
  409. throw testing.message;
  410. }
  411. let messages = new Messages();
  412. ControllerUtil.regModelEventHandler(messages, res);
  413. messages.updateMsgContent(payload.session_type, payload.session_id,payload.message_id, payload.content);
  414. });
  415. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  416. let sessionId = req.params.session_id;
  417. let userId = req.query.user_id;
  418. let sessions = new Sessions();
  419. ControllerUtil.regModelEventHandler(sessions, res);
  420. sessions.getSessionUnreadMessages(sessionId, userId);
  421. });
  422. /**
  423. * 获取所有会话未读消息数。
  424. *
  425. * 请求URL:/sessions/unread_message_count?user_id=xyz
  426. */
  427. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  428. let userId = req.query.user_id;
  429. let sessions = new Sessions();
  430. ControllerUtil.regModelEventHandler(sessions, res);
  431. sessions.getAllSessionsUnreadMessageCount(userId);
  432. });
  433. /**
  434. * 获取指定会话未读消息数
  435. *
  436. * 请求URL
  437. * /sessions/:session_id/unread_message_count?user_id=xyz
  438. */
  439. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  440. let sessionId = req.params.session_id;
  441. let userId = req.query.user_id;
  442. let sessions = new Sessions();
  443. ControllerUtil.regModelEventHandler(sessions, res);
  444. sessions.getSessionUnreadMessageCount(sessionId, userId);
  445. });
  446. module.exports = router;