session.endpoint.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  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 sessionType = req.query.type;
  76. let status = req.query.status;
  77. if (!page) {
  78. throw {httpStatus: 406, message: 'Missing page.'};
  79. }
  80. if (!size) {
  81. throw {httpStatus: 406, message: 'Missing size.'};
  82. }
  83. if (!userId) {
  84. throw {httpStatus: 406, message: 'Missing user.'};
  85. }
  86. let sessions = new Sessions();
  87. ControllerUtil.regModelEventHandler(sessions, res);
  88. //传入参数齐全走过滤方法
  89. if(status&&businessType&&userId&&size&&page){
  90. sessions.getUserStatusSessions(userId,status,sessionType,businessType,page,size);
  91. }else{
  92. sessions.getUserSessions(userId, page, size, businessType);
  93. }
  94. });
  95. /**
  96. * 按会话类型获取会话列表
  97. * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4
  98. */
  99. router.get(APIv2.Sessions.SessionListByType, function (req, res) {
  100. let page = req.query.page;
  101. let size = req.query.size;
  102. let userId = req.query.user_id;
  103. let type = req.query.type;
  104. let status = req.query.status;
  105. let name = req.query.name;
  106. if (!page) {
  107. throw {httpStatus: 406, message: 'Missing page.'};
  108. }
  109. if (!size) {
  110. throw {httpStatus: 406, message: 'Missing size.'};
  111. }
  112. if (!type) {
  113. throw {httpStatus: 406, message: 'Missing type.'};
  114. }
  115. if (!userId) {
  116. throw {httpStatus: 406, message: 'Missing user.'};
  117. }
  118. let sessions = new Sessions();
  119. ControllerUtil.regModelEventHandler(sessions, res);
  120. sessions.getUserSessionsByType(userId,type,page,size,status,name);
  121. });
  122. /**
  123. * 按会话类型获取会话总数
  124. * 请求URL /sessions/sessionCountByType?user_id=3121&type=4
  125. */
  126. router.get(APIv2.Sessions.SessionCountByType, function (req, res) {
  127. let userId = req.query.user_id;
  128. let type = req.query.type;
  129. if (!type) {
  130. throw {httpStatus: 406, message: 'Missing type.'};
  131. }
  132. if (!userId) {
  133. throw {httpStatus: 406, message: 'Missing user.'};
  134. }
  135. let sessions = new Sessions();
  136. ControllerUtil.regModelEventHandler(sessions, res);
  137. sessions.getSessionCountByType(userId,type);
  138. });
  139. router.get(APIv2.Sessions.Session,function(req,res){
  140. let sessionId = req.params.session_id;
  141. let userId = req.query.user_id;
  142. if (!sessionId) {
  143. throw {httpStatus: 406, message: 'Missing sessionId.'};
  144. }
  145. if (!userId) {
  146. throw {httpStatus: 406, message: 'Missing user.'};
  147. }
  148. let sessions = new Sessions();
  149. ControllerUtil.regModelEventHandler(sessions, res);
  150. sessions.getSession(sessionId,userId);
  151. });
  152. /**
  153. * 最近会话列表。
  154. *
  155. * URL:
  156. * /sessions/recent?user_id=abc&date_span=7
  157. */
  158. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  159. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  160. let userId = req.query.user_id;
  161. let dateSpan = req.query.date_span;
  162. let sessions = new Sessions();
  163. ControllerUtil.regModelEventHandler(sessions, res);
  164. sessions.getRecentSessions(userId, dateSpan);
  165. });
  166. /**
  167. * 判断会话是否存在。
  168. *
  169. * URL:
  170. * /sessions/isExist?session_id=abc
  171. */
  172. router.get(APIv2.Sessions.IsExist, function (req, res) {
  173. let sessionId = req.query.session_id;
  174. if (!sessionId) {
  175. throw {httpStatus: 406, message: 'Missing sessionId.'}
  176. }
  177. let sessions = new Sessions();
  178. ControllerUtil.regModelEventHandler(sessions, res);
  179. sessions.isExist(sessionId);
  180. });
  181. /**
  182. * 某个聊天记录置顶操作
  183. *
  184. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  185. */
  186. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  187. let user = req.query.user;
  188. let sessionId = req.query.sessionId;
  189. if (!user) {
  190. throw {httpStatus: 406, message: 'Missing user.'};
  191. }
  192. if (!sessionId) {
  193. throw {httpStatus: 406, message: 'Missing sessionId.'};
  194. }
  195. let sessions = new Sessions();
  196. ControllerUtil.regModelEventHandler(sessions, res);
  197. sessions.stickSession(sessionId, user);
  198. });
  199. /**
  200. * 某个聊天记录置顶操作
  201. *
  202. * 请求URL /status?status=1&sessionId=testsessionmsg1
  203. */
  204. router.post(APIv2.Sessions.SessionStatus, function (req, res) {
  205. let sessionId = req.query.sessionId;
  206. let status = req.query.status;
  207. if (!sessionId) {
  208. throw {httpStatus: 406, message: 'Missing sessionId.'};
  209. }
  210. if (!status) {
  211. throw {httpStatus: 406, message: 'Missing status.'};
  212. }
  213. let sessions = new Sessions();
  214. ControllerUtil.regModelEventHandler(sessions, res);
  215. sessions.updateSessionStatus(sessionId, status);
  216. });
  217. /**
  218. * 修改会话名称(修改redis和数据库)
  219. *
  220. * 请求URL /name?name=哈哈&sessionId=testsessionmsg1
  221. */
  222. router.post(APIv2.Sessions.SessionName, function (req, res) {
  223. let sessionId = req.body.sessionId;
  224. let name = req.body.name;
  225. if (!sessionId) {
  226. throw {httpStatus: 406, message: 'Missing sessionId.'};
  227. }
  228. if (!name) {
  229. throw {httpStatus: 406, message: 'Missing name.'};
  230. }
  231. let sessions = new Sessions();
  232. ControllerUtil.regModelEventHandler(sessions, res);
  233. sessions.updateSessionName(sessionId, name);
  234. });
  235. /**
  236. * 取消置顶
  237. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  238. */
  239. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  240. let user = req.query.user;
  241. let sessionId = req.query.sessionId;
  242. if (!user) {
  243. throw {httpStatus: 406, message: 'Missing user.'};
  244. }
  245. if (!sessionId) {
  246. throw {httpStatus: 406, message: 'Missing sessionId.'};
  247. }
  248. let sessions = new Sessions();
  249. ControllerUtil.regModelEventHandler(sessions, res);
  250. sessions.cancelStickSession(sessionId, user);
  251. });
  252. /**
  253. * 获取会话成员。
  254. */
  255. router.get(APIv2.Sessions.Participants, function (req, res) {
  256. let sessionId = req.params.session_id;
  257. let participants = new Participants();
  258. ControllerUtil.regModelEventHandler(participants, res);
  259. participants.getParticipants(sessionId);
  260. });
  261. /**
  262. * 获取会话成员头像列表。
  263. */
  264. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  265. let sessionId = req.params.session_id;
  266. let participants = new Participants();
  267. ControllerUtil.regModelEventHandler(participants, res);
  268. participants.getParticipantsAvatar(sessionId);
  269. });
  270. /**
  271. * 增加成员
  272. * user:增加的人员
  273. * sessionId 会话ID
  274. */
  275. router.put(APIv2.Sessions.Participant, function (req, res) {
  276. let user = req.params.participant_id;
  277. if (!user) {
  278. throw {httpStatus: 406, message: 'Missing participant_id.'};
  279. }
  280. let sessionId = req.params.session_id;
  281. if (!sessionId) {
  282. throw {httpStatus: 406, message: 'Missing session_id.'};
  283. }
  284. let participants = new Participants();
  285. ControllerUtil.regModelEventHandler(participants, res);
  286. participants.addUser(sessionId, user);
  287. });
  288. /**
  289. * 增加成员
  290. * user_id:增加的人员
  291. * old_user_id:删除的人员
  292. * session_id 会话ID
  293. */
  294. router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
  295. let payload = req.body;
  296. let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
  297. if (!testing.pass) {
  298. throw testing.message;
  299. }
  300. let participants = new Participants();
  301. ControllerUtil.regModelEventHandler(participants, res);
  302. participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
  303. });
  304. /**
  305. * 移除成员
  306. * user:移除的人员
  307. * sessionId 会话ID
  308. */
  309. router.delete(APIv2.Sessions.Participant, function (req, res) {
  310. let user = req.params.participant_id;
  311. if (!user) {
  312. throw {httpStatus: 406, message: 'Missing user.'};
  313. }
  314. let sessionId = req.params.session_id;
  315. if (!sessionId) {
  316. throw {httpStatus: 406, message: 'Missing sessionId.'};
  317. }
  318. let participants = new Participants();
  319. ControllerUtil.regModelEventHandler(participants, res);
  320. participants.removeUser(sessionId, user);
  321. });
  322. /**
  323. * 发送消息
  324. *
  325. * 请求URL:
  326. * /sessions/:session_id/messages
  327. *
  328. * 消息体:
  329. * {
  330. * sender_id: bea851fc,
  331. * sender_name: 李毅,
  332. * content_type: 1,
  333. * content: 李医生,你好
  334. * }
  335. */
  336. router.post(APIv2.Sessions.Messages, function (req, res) {
  337. let payload = req.body;
  338. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  339. if (!testing.pass) {
  340. throw {httpStatus: 406, message: testing.message}
  341. }
  342. if(payload.view&&payload.view==1){
  343. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  344. }
  345. // 消息的发送时间由服务端决定
  346. payload.timestamp = new Date((new Date().getTime()));
  347. let sessions = new Sessions();
  348. ControllerUtil.regModelEventHandler(sessions, res);
  349. sessions.saveMessageBySession(req.params.session_id, payload);
  350. });
  351. router.post(APIv2.Sessions.TopicMessages, function (req, res) {
  352. let payload = req.body;
  353. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  354. if (!testing.pass) {
  355. throw {httpStatus: 406, message: testing.message}
  356. }
  357. if(payload.view&&payload.view==1){
  358. throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
  359. }
  360. let sessions = new Sessions();
  361. ControllerUtil.regModelEventHandler(sessions, res);
  362. payload.timestamp = new Date((new Date().getTime()));
  363. sessions.sendTopicMessages(req.params.topic_id, payload);
  364. })
  365. /**
  366. * 获取消息
  367. *
  368. * 请求URL:
  369. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  370. */
  371. router.get(APIv2.Sessions.Messages, function (req, res) {
  372. let startMsgId = req.query.start_msg_id;
  373. let endMsgId = req.query.end_msg_id;
  374. let pagesize = req.query.pagesize;
  375. let page = req.query.page;
  376. let user = req.query.user;
  377. let sessionId = req.query.session_id;
  378. let content_type = req.query.content_type;
  379. let isoffset = req.query.isoffset;
  380. if (!user) {
  381. throw {httpStatus: 406, message: 'Missing user.'};
  382. }
  383. if (!sessionId) {
  384. throw {httpStatus: 406, message: 'Missing session_id.'};
  385. }
  386. if (!pagesize) {
  387. pagesize = 20;
  388. }
  389. if (!page) {
  390. page = 1;
  391. }
  392. if (content_type) {
  393. let messages = new Messages();
  394. ControllerUtil.regModelEventHandler(messages, res);
  395. messages.getMessageByType(sessionId, page, pagesize, content_type)
  396. } else {
  397. let sessions = new Sessions();
  398. ControllerUtil.regModelEventHandler(sessions, res);
  399. sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
  400. }
  401. });
  402. /**
  403. * 修改单条消息
  404. * session_id 会话ID
  405. * message_id:消息ID
  406. * content 消息内容
  407. */
  408. router.post(APIv2.Sessions.MessageUpdate, function (req, res) {
  409. let payload = req.body;
  410. let testing = ObjectUtil.fieldsCheck(payload,'session_type', 'session_id','message_id','content');
  411. if (!testing.pass) {
  412. throw testing.message;
  413. }
  414. let messages = new Messages();
  415. ControllerUtil.regModelEventHandler(messages, res);
  416. messages.updateMsgContent(payload.session_type, payload.session_id,payload.message_id, payload.content);
  417. });
  418. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  419. let sessionId = req.params.session_id;
  420. let userId = req.query.user_id;
  421. let sessions = new Sessions();
  422. ControllerUtil.regModelEventHandler(sessions, res);
  423. sessions.getSessionUnreadMessages(sessionId, userId);
  424. });
  425. /**
  426. * 获取所有会话未读消息数。
  427. *
  428. * 请求URL:/sessions/unread_message_count?user_id=xyz
  429. */
  430. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  431. let userId = req.query.user_id;
  432. let sessions = new Sessions();
  433. ControllerUtil.regModelEventHandler(sessions, res);
  434. sessions.getAllSessionsUnreadMessageCount(userId);
  435. });
  436. /**
  437. * 按会话类型获取未读消息数量 status不传为获取所有状态
  438. * 请求URL:/sessions/unread_message_count_type?user_id=xyz&type=18&status=0
  439. */
  440. router.get(APIv2.Sessions.SessionsUnreadMessageCountByType, function (req, res) {
  441. let userId = req.query.user_id;
  442. let type = req.query.type;
  443. let status = req.query.status;
  444. if (!type) {
  445. throw {httpStatus: 406, message: 'Missing type.'};
  446. }
  447. let sessions = new Sessions();
  448. ControllerUtil.regModelEventHandler(sessions, res);
  449. sessions.getAllSessionsUnreadMessageCountByType(userId,type,status);
  450. });
  451. /**
  452. * 获取指定会话未读消息数
  453. *
  454. * 请求URL
  455. * /sessions/:session_id/unread_message_count?user_id=xyz
  456. */
  457. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  458. let sessionId = req.params.session_id;
  459. let userId = req.query.user_id;
  460. let sessions = new Sessions();
  461. ControllerUtil.regModelEventHandler(sessions, res);
  462. sessions.getSessionUnreadMessageCount(sessionId, userId);
  463. });
  464. module.exports = router;