session.endpoint.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. * users 讨论组包含的用户标示
  22. * sessionId 会话ID
  23. *
  24. * 请求URL: /sessions
  25. * 参数:
  26. * {
  27. * session_id: 会话ID,非必须,仅对群消息使用。
  28. * session_type: 会话类型,必须。
  29. * session_name: 会话名称,
  30. * participants: 此会话的成员列表,格式:["userId1:role", "userId2:role"],用户的ID及角色。
  31. * }
  32. */
  33. router.post("/", function (req, res) {
  34. let payload = req.body;
  35. let testing = ObjectUtil.fieldsCheck(payload, 'participants', 'session_name', 'session_type');
  36. if (!testing.pass) {
  37. throw testing.message;
  38. }
  39. let participants = payload.participants;
  40. let sessionName = payload.session_name;
  41. let sessionType = payload.session_type;
  42. let sessionId = payload.session_id;
  43. let sessions = new Sessions();
  44. let participantArray = [];
  45. participants = JSON.parse(participants);
  46. for (let j in participants) {
  47. participantArray.push(j + ":" + participants[j]);
  48. }
  49. ControllerUtil.regModelEventHandler(sessions, res);
  50. sessions.createSession(sessionId, sessionName, sessionType, participantArray);
  51. });
  52. /**
  53. * 获取会话列表
  54. *
  55. * 请求URL /sessions?user_id=3121&page=0&size=10
  56. */
  57. router.get("/", function (req, res) {
  58. let page = req.query.page;
  59. let size = req.query.size;
  60. let userId = req.query.user_id;
  61. let businessType = req.query.business_type;
  62. if (!page) {
  63. throw {httpStatus: 406, message: 'Missing page.'};
  64. }
  65. if (!size) {
  66. throw {httpStatus: 406, message: 'Missing size.'};
  67. }
  68. if (!userId) {
  69. throw {httpStatus: 406, message: 'Missing user.'};
  70. }
  71. let sessions = new Sessions();
  72. ControllerUtil.regModelEventHandler(sessions, res);
  73. sessions.getUserSessions(userId, page, size, businessType);
  74. });
  75. /**
  76. * 最近会话列表。
  77. *
  78. * URL:
  79. * /sessions/recent?user_id=abc&date_span=7
  80. */
  81. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  82. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  83. let userId = req.query.user_id;
  84. let dateSpan = req.query.date_span;
  85. let sessions = new Sessions();
  86. ControllerUtil.regModelEventHandler(sessions, res);
  87. sessions.getRecentSessions(userId, dateSpan);
  88. });
  89. /**
  90. * 某个聊天记录置顶操作
  91. *
  92. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  93. */
  94. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  95. let user = req.query.user;
  96. let sessionId = req.query.sessionId;
  97. if (!user) {
  98. throw {httpStatus: 406, message: 'Missing user.'};
  99. }
  100. if (!sessionId) {
  101. throw {httpStatus: 406, message: 'Missing sessionId.'};
  102. }
  103. let sessions = new Sessions();
  104. ControllerUtil.regModelEventHandler(sessions, res);
  105. sessions.stickSession(sessionId, user);
  106. });
  107. /**
  108. * 取消置顶
  109. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  110. */
  111. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  112. let user = req.query.user;
  113. let sessionId = req.query.sessionId;
  114. if (!user) {
  115. throw {httpStatus: 406, message: 'Missing user.'};
  116. }
  117. if (!sessionId) {
  118. throw {httpStatus: 406, message: 'Missing sessionId.'};
  119. }
  120. let sessions = new Sessions();
  121. ControllerUtil.regModelEventHandler(sessions, res);
  122. sessions.cancelStickSession(sessionId, user);
  123. });
  124. /**
  125. * 获取会话成员。
  126. */
  127. router.get(APIv2.Sessions.Participants, function (req, res) {
  128. let sessionId = req.params.session_id;
  129. let participants = new Participants();
  130. ControllerUtil.regModelEventHandler(participants, res);
  131. participants.getParticipants(sessionId);
  132. });
  133. /**
  134. * 获取会话成员头像列表。
  135. */
  136. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  137. let sessionId = req.params.session_id;
  138. let participants = new Participants();
  139. ControllerUtil.regModelEventHandler(participants, res);
  140. participants.getParticipantsAvatar(sessionId);
  141. });
  142. /**
  143. * 增加成员
  144. * user:增加的人员
  145. * sessionId 会话ID
  146. */
  147. router.put(APIv2.Sessions.Participant, function (req, res) {
  148. let user = req.params.participant_id;
  149. if (!user) {
  150. throw {httpStatus: 406, message: 'Missing participant_id.'};
  151. }
  152. let sessionId = req.params.session_id;
  153. if (!sessionId) {
  154. throw {httpStatus: 406, message: 'Missing session_id.'};
  155. }
  156. let participants = new Participants();
  157. ControllerUtil.regModelEventHandler(participants, res);
  158. participants.addUser(sessionId, user);
  159. });
  160. /**
  161. * 移除成员
  162. * user:移除的人员
  163. * sessionId 会话ID
  164. */
  165. router.delete(APIv2.Sessions.Participant, function (req, res) {
  166. let user = req.query.user;
  167. if (!user) {
  168. throw {httpStatus: 406, message: 'Missing user.'};
  169. }
  170. let sessionId = req.query.sessionId;
  171. if (!sessionId) {
  172. throw {httpStatus: 406, message: 'Missing sessionId.'};
  173. }
  174. let participants = new Participants();
  175. ControllerUtil.regModelEventHandler(sessions, res);
  176. participants.removeUser(sessionId, user);
  177. });
  178. /**
  179. * 发送消息
  180. *
  181. * 请求URL:
  182. * /sessions/:session_id/messages
  183. *
  184. * 消息体:
  185. * {
  186. * sender_id: bea851fc,
  187. * sender_name: 李毅,
  188. * content_type: 1,
  189. * content: 李医生,你好
  190. * }
  191. */
  192. router.post(APIv2.Sessions.Messages, function (req, res) {
  193. let payload = req.body;
  194. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  195. if (!testing.pass) {
  196. throw {httpStatus: 406, message: testing.message}
  197. }
  198. let sessions = new Sessions();
  199. ControllerUtil.regModelEventHandler(sessions, res);
  200. payload.timestamp = new Date();
  201. sessions.saveMessageBySession(req.params.session_id, payload);
  202. });
  203. router.post(APIv2.Sessions.TopicMessages, function (req, res) {
  204. let payload = req.body;
  205. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  206. if (!testing.pass) {
  207. throw {httpStatus: 406, message: testing.message}
  208. }
  209. let sessions = new Sessions();
  210. ControllerUtil.regModelEventHandler(sessions, res);
  211. payload.timestamp = new Date();
  212. sessions.sendTopicMessages(req.params.topic_id, payload);
  213. })
  214. /**
  215. * 获取消息
  216. *
  217. * 请求URL:
  218. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  219. */
  220. router.get(APIv2.Sessions.Messages, function (req, res) {
  221. let startMsgId = req.query.start_msg_id;
  222. let endMsgId = req.query.end_msg_id;
  223. let pagesize = req.query.pagesize;
  224. let page = req.query.page;
  225. let user = req.query.user;
  226. let sessionId = req.query.session_id;
  227. let content_type = req.query.content_type;
  228. let isoffset = req.query.isoffset;
  229. if (!user) {
  230. throw {httpStatus: 406, message: 'Missing user.'};
  231. }
  232. if (!sessionId) {
  233. throw {httpStatus: 406, message: 'Missing session_id.'};
  234. }
  235. if (!pagesize) {
  236. pagesize = 20;
  237. }
  238. if (!page) {
  239. page = 1;
  240. }
  241. if (content_type) {
  242. let messages = new Messages();
  243. ControllerUtil.regModelEventHandler(messages, res);
  244. messages.getMessageByType(sessionId, page, pagesize, content_type)
  245. } else {
  246. let sessions = new Sessions();
  247. ControllerUtil.regModelEventHandler(sessions, res);
  248. sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
  249. }
  250. });
  251. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  252. let sessionId = req.params.session_id;
  253. let userId = req.query.user_id;
  254. let sessions = new Sessions();
  255. ControllerUtil.regModelEventHandler(sessions, res);
  256. sessions.getSessionUnreadMessages(sessionId, userId);
  257. });
  258. /**
  259. * 获取所有会话未读消息数。
  260. *
  261. * 请求URL:/sessions/unread_message_count?user_id=xyz
  262. */
  263. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  264. let userId = req.query.user_id;
  265. let sessions = new Sessions();
  266. ControllerUtil.regModelEventHandler(sessions, res);
  267. sessions.getAllSessionsUnreadMessageCount(userId);
  268. });
  269. /**
  270. * 获取指定会话未读消息数
  271. *
  272. * 请求URL
  273. * /sessions/:session_id/unread_message_count?user_id=xyz
  274. */
  275. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  276. let sessionId = req.params.session_id;
  277. let userId = req.query.user_id;
  278. let sessions = new Sessions();
  279. ControllerUtil.regModelEventHandler(sessions, res);
  280. sessions.getSessionUnreadMessageCount(sessionId, userId);
  281. });
  282. module.exports = router;