session.endpoint.js 16 KB

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