session.endpoint.js 8.8 KB

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