session.endpoint.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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:
  81. * /sessions/recent?user_id=abc&date_span=7
  82. */
  83. router.get(APIv2.Sessions.RecentSessions, function (req, res) {
  84. ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
  85. let userId = req.query.user_id;
  86. let dateSpan = req.query.date_span;
  87. let sessions = new Sessions();
  88. ControllerUtil.regModelEventHandler(sessions, res);
  89. sessions.getRecentSessions(userId, dateSpan);
  90. });
  91. /**
  92. * 某个聊天记录置顶操作
  93. *
  94. * 请求URL /stick?user=3121&sessionId=testsessionmsg1
  95. */
  96. router.post(APIv2.Sessions.SessionSticky, function (req, res) {
  97. let user = req.query.user;
  98. let sessionId = req.query.sessionId;
  99. if (!user) {
  100. throw {httpStatus: 406, message: 'Missing user.'};
  101. }
  102. if (!sessionId) {
  103. throw {httpStatus: 406, message: 'Missing sessionId.'};
  104. }
  105. let sessions = new Sessions();
  106. ControllerUtil.regModelEventHandler(sessions, res);
  107. sessions.stickSession(sessionId, user);
  108. });
  109. /**
  110. * 取消置顶
  111. * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1
  112. */
  113. router.delete(APIv2.Sessions.SessionSticky, function (req, res) {
  114. let user = req.query.user;
  115. let sessionId = req.query.sessionId;
  116. if (!user) {
  117. throw {httpStatus: 406, message: 'Missing user.'};
  118. }
  119. if (!sessionId) {
  120. throw {httpStatus: 406, message: 'Missing sessionId.'};
  121. }
  122. let sessions = new Sessions();
  123. ControllerUtil.regModelEventHandler(sessions, res);
  124. sessions.cancelStickSession(sessionId, user);
  125. });
  126. /**
  127. * 获取会话成员。
  128. */
  129. router.get(APIv2.Sessions.Participants, function (req, res) {
  130. let sessionId = req.params.session_id;
  131. let participants = new Participants();
  132. ControllerUtil.regModelEventHandler(participants, res);
  133. participants.getParticipants(sessionId);
  134. });
  135. /**
  136. * 获取会话成员头像列表。
  137. */
  138. router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
  139. let sessionId = req.params.session_id;
  140. let participants = new Participants();
  141. ControllerUtil.regModelEventHandler(participants, res);
  142. participants.getParticipantsAvatar(sessionId);
  143. });
  144. /**
  145. * 增加成员
  146. * user:移除的人员
  147. * sessionId 会话ID
  148. */
  149. router.put(APIv2.Sessions.Participant, function (req, res) {
  150. let user = req.params.participant_id;
  151. if (!user) {
  152. throw {httpStatus: 406, message: 'Missing participant_id.'};
  153. }
  154. let sessionId = req.params.session_id;
  155. if (!sessionId) {
  156. throw {httpStatus: 406, message: 'Missing session_id.'};
  157. }
  158. let participants = new Participants();
  159. ControllerUtil.regModelEventHandler(participants, res);
  160. participants.addUser(sessionId, user);
  161. });
  162. /**
  163. * 移除成员
  164. * user:移除的人员
  165. * sessionId 会话ID
  166. */
  167. router.delete(APIv2.Sessions.Participant, function (req, res) {
  168. let user = req.query.user;
  169. if (!user) {
  170. throw {httpStatus: 406, message: 'Missing user.'};
  171. }
  172. let sessionId = req.query.sessionId;
  173. if (!sessionId) {
  174. throw {httpStatus: 406, message: 'Missing sessionId.'};
  175. }
  176. let participants = new Participants();
  177. ControllerUtil.regModelEventHandler(sessions, res);
  178. participants.removeUser(sessionId, user);
  179. });
  180. /**
  181. * 发送消息
  182. *
  183. * 请求URL:
  184. * /sessions/:session_id/messages
  185. *
  186. * 消息体:
  187. * {
  188. * sender_id: bea851fc,
  189. * sender_name: 李毅,
  190. * content_type: 1,
  191. * content: 李医生,你好
  192. * }
  193. */
  194. router.post(APIv2.Sessions.Messages, function (req, res) {
  195. let payload = req.body;
  196. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  197. if (!testing.pass) {
  198. throw {httpStatus: 406, message: testing.message}
  199. }
  200. let sessions = new Sessions();
  201. ControllerUtil.regModelEventHandler(sessions, res);
  202. payload.timestamp = new Date();
  203. sessions.saveMessageBySession(req.params.session_id, payload);
  204. });
  205. router.post(APIv2.Sessions.TopicMessages,function(req,res){
  206. let payload = req.body;
  207. let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content");
  208. if (!testing.pass) {
  209. throw {httpStatus: 406, message: testing.message}
  210. }
  211. let sessions = new Sessions();
  212. ControllerUtil.regModelEventHandler(sessions, res);
  213. payload.timestamp = new Date();
  214. sessions.sendTopicMessages(req.params.topic_id, payload);
  215. })
  216. /**
  217. * 获取消息
  218. *
  219. * 请求URL:
  220. * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20
  221. */
  222. router.get(APIv2.Sessions.Messages, function (req, res) {
  223. let startMsgId = req.query.start_msg_id;
  224. let endMsgId = req.query.end_msg_id;
  225. let pagesize = req.query.pagesize;
  226. let page = req.query.page;
  227. let user = req.query.user;
  228. let sessionId = req.query.session_id;
  229. let content_type = req.query.content_type;
  230. let isoffset = req.query.isoffset;
  231. if (!user) {
  232. throw {httpStatus: 406, message: 'Missing user.'};
  233. }
  234. if (!sessionId) {
  235. throw {httpStatus: 406, message: 'Missing session_id.'};
  236. }
  237. if(!pagesize){
  238. pagesize = 20;
  239. }
  240. if(!page){
  241. page = 1;
  242. }
  243. if(content_type){
  244. let messages = new Messages();
  245. ControllerUtil.regModelEventHandler(messages, res);
  246. messages.getMessageByType(sessionId,page,pagesize,content_type)
  247. }else{
  248. let sessions = new Sessions();
  249. ControllerUtil.regModelEventHandler(sessions, res);
  250. sessions.getMessages(sessionId, user, startMsgId,endMsgId,page,pagesize,isoffset);
  251. }
  252. });
  253. router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
  254. let sessionId = req.params.session_id;
  255. let userId = req.query.user_id;
  256. let sessions = new Sessions();
  257. ControllerUtil.regModelEventHandler(sessions, res);
  258. sessions.getSessionUnreadMessages(sessionId, userId);
  259. });
  260. /**
  261. * 获取所有会话未读消息数。
  262. *
  263. * 请求URL:/sessions/unread_message_count?user_id=xyz
  264. */
  265. router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) {
  266. let userId = req.query.user_id;
  267. let sessions = new Sessions();
  268. ControllerUtil.regModelEventHandler(sessions, res);
  269. sessions.getAllSessionsUnreadMessageCount(userId);
  270. });
  271. /**
  272. * 获取指定会话未读消息数
  273. *
  274. * 请求URL
  275. * /sessions/:session_id/unread_message_count?user_id=xyz
  276. */
  277. router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) {
  278. let sessionId = req.params.session_id;
  279. let userId = req.query.user_id;
  280. let sessions = new Sessions();
  281. ControllerUtil.regModelEventHandler(sessions, res);
  282. sessions.getSessionUnreadMessageCount(sessionId, userId);
  283. });
  284. module.exports = router;