session.endpoint.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 http = require('http');
  11. let log = require('../../util/log.js');
  12. let ObjectUtil = require("../../util/object.util.js");
  13. let ControllerUtil = require('../../util/controller.util');
  14. let Sessions = require('../../models/sessions/sessions');
  15. let Participants = require('../../models/sessions/participants');
  16. const SESSION_TYPES = require('../../include/commons').SESSION_TYPES;
  17. const APIv2 = require('../../include/endpoints').APIv2;
  18. /**
  19. * 创建会话。对MUC会话而言,需要创建的是议题,如果是第一次创建议题,那应该附带创建会话,而不是直接创建会话。
  20. *
  21. * session_type:1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组
  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 = null;
  44. if (payload.session_type == SESSION_TYPES.GROUP) {
  45. if(!payload.hasOwnProperty('session_id') && payload.session_id.trim().length > 0){
  46. throw {httpStatus: 406, message: 'Field "session_id" is necessary and cannot be empty for GROUP session.'};
  47. }
  48. sessionId = payload.session_id;
  49. }
  50. let sessions = new Sessions();
  51. ControllerUtil.regModelEventHandler(sessions, res);
  52. sessions.createSession(sessionId, sessionName, sessionType, participants);
  53. });
  54. /**
  55. * 获取会话列表
  56. *
  57. * 请求URL /sessions?user_id=3121&page=0&size=10
  58. */
  59. router.get("/", function (req, res) {
  60. let page = req.query.page;
  61. let size = req.query.size;
  62. let userId = req.query.user_id;
  63. if (!page) {
  64. throw {httpStatus: 406, message: 'Missing page.'};
  65. }
  66. if (!size) {
  67. throw {httpStatus: 406, message: 'Missing size.'};
  68. }
  69. if (!userId) {
  70. throw {httpStatus: 406, message: 'Missing user.'};
  71. }
  72. let sessions = new Sessions();
  73. ControllerUtil.regModelEventHandler(sessions, res);
  74. sessions.getUserSessions(userId, page, size);
  75. });
  76. /**
  77. * 某个聊天记录置顶操作
  78. *
  79. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  80. */
  81. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  82. let user = req.query.user;
  83. let sessionId = req.query.sessionId;
  84. if (!user) {
  85. throw {httpStatus: 406, message: 'Missing user.'};
  86. }
  87. if (!sessionId) {
  88. throw {httpStatus: 406, message: 'Missing sessionId.'};
  89. }
  90. let sessions = new Sessions();
  91. ControllerUtil.regModelEventHandler(sessions, res);
  92. sessions.stickSession(sessionId, user);
  93. });
  94. /**
  95. * 取消置顶
  96. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  97. */
  98. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  99. let user = req.query.user;
  100. let sessionId = req.query.sessionId;
  101. if (!user) {
  102. throw {httpStatus: 406, message: 'Missing user.'};
  103. }
  104. if (!sessionId) {
  105. throw {httpStatus: 406, message: 'Missing sessionId.'};
  106. }
  107. let sessions = new Sessions();
  108. ControllerUtil.regModelEventHandler(sessions, res);
  109. sessions.cancelStickSession(sessionId, user);
  110. });
  111. /**
  112. * 移除成员
  113. * user:移除的人员
  114. * sessionId 会话ID
  115. */
  116. router.delete(APIv2.Sessions.Participant, function (req, res) {
  117. let user = req.query.user;
  118. if (!user) {
  119. throw {httpStatus: 406, message: 'Missing user.'};
  120. }
  121. let sessionId = req.query.sessionId;
  122. if (!sessionId) {
  123. throw {httpStatus: 406, message: 'Missing sessionId.'};
  124. }
  125. let participants = new Participants();
  126. ControllerUtil.regModelEventHandler(sessions, res);
  127. participants.deleteUser(sessionId, user);
  128. });
  129. /**
  130. * 增加成员
  131. * user:移除的人员
  132. * sessionId 会话ID
  133. */
  134. router.put(APIv2.Sessions.Participant, function (req, res) {
  135. let user = req.query.user;
  136. if (!user) {
  137. throw {httpStatus: 406, message: 'Missing user.'};
  138. }
  139. let sessionId = req.query.sessionId;
  140. if (!sessionId) {
  141. throw {httpStatus: 406, message: 'Missing sessionId.'};
  142. }
  143. let participants = new Participants();
  144. ControllerUtil.regModelEventHandler(sessions, res);
  145. participants.pushUser(sessionId, user);
  146. });
  147. /**
  148. * 发送消息
  149. *
  150. * 请求URL:
  151. * /sessions/:session_id/messages
  152. *
  153. * 消息体:
  154. * {
  155. * sender_id: bea851fc,
  156. * sender_name: 李毅,
  157. * content_type: 1,
  158. * content: 李医生,你好
  159. * }
  160. */
  161. router.post(APIv2.Sessions.Messages, function (req, res) {
  162. let payload = req.body;
  163. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  164. if (!testing.pass) {
  165. throw {httpStatus: 406, message: testing.message}
  166. }
  167. let sessions = new Sessions();
  168. ControllerUtil.regModelEventHandler(sessions, res);
  169. payload.timestamp = new Date();
  170. sessions.saveMessageBySession(req.params.session_id, payload);
  171. });
  172. /**
  173. * 获取消息
  174. *
  175. * 请求URL:
  176. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  177. */
  178. router.get(APIv2.Sessions.Messages, function (req, res) {
  179. let stratmsgid = req.query.stratmsgid;
  180. let endmsgid = req.query.endmsgid;
  181. let user = req.query.user;
  182. let sessionId = req.query.sessionId;
  183. if (!stratmsgid) {
  184. throw {httpStatus: 406, message: 'Missing stratmsgid.'};
  185. }
  186. if (!endmsgid) {
  187. throw {httpStatus: 406, message: 'Missing endmsgid.'};
  188. }
  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.getMessages(sessionId, user, stratmsgid,endmsgid);
  198. });
  199. /**
  200. * 获取所有会话未读消息数。
  201. *
  202. * 请求URL:/sessions/unread_message_count?user_id=xyz
  203. */
  204. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  205. let userId = req.query.user_id;
  206. let sessions = new Sessions();
  207. ControllerUtil.regModelEventHandler(sessions, res);
  208. sessions.getAllSessionsUnreadMessageCount();
  209. });
  210. /**
  211. * 获取指定会话未读消息数
  212. *
  213. * 请求URL
  214. * /sessions/:session_id/unread_message_count?user_id=xyz
  215. */
  216. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  217. let sessionId = req.params.session_id;
  218. let userId = req.query.user_id;
  219. let sessions = new Sessions();
  220. ControllerUtil.regModelEventHandler(sessions, res);
  221. sessions.getSessionUnreadMessageCount(sessionId, userId);
  222. });
  223. module.exports = router;