session.endpoint.js 10 KB

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