session.endpoint.js 15 KB

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