/** * 会话接口。 * * author: Sand * since: 12/15/2016 */ "use strict"; let express = require('express'); let router = express.Router(); let http = require('http'); let log = require('../../util/log.js'); let ObjectUtil = require("../../util/object.util.js"); let ControllerUtil = require('../../util/controller.util'); let Sessions = require('../../models/sessions/sessions'); let Participants = require('../../models/sessions/participants'); const SESSION_TYPES = require('../../include/commons').SESSION_TYPES; const APIv2 = require('../../include/endpoints').APIv2; /** * 创建会话。对MUC会话而言,需要创建的是议题,如果是第一次创建议题,那应该附带创建会话,而不是直接创建会话。 * * session_type:1表示MUC会话,2表示P2P,3表示群会话,4表示临时讨论组 * users 讨论组包含的用户标示 * sessionId 会话ID * * 请求URL: /sessions * 参数: * { * session_id: 会话ID,非必须,仅对群消息使用。 * session_type: 会话类型,必须。 * session_name: 会话名称, * participants: 此会话的成员列表,格式:["userId1:role", "userId2:role"],用户的ID及角色。 * } */ router.post("/", function (req, res) { let payload = req.body; let testing = ObjectUtil.fieldsCheck(payload, 'participants', 'session_name', 'session_type'); if (!testing.pass) { throw testing.message; } let participants = payload.participants; let sessionName = payload.session_name; let sessionType = payload.session_type; let sessionId = null; if (payload.session_type == SESSION_TYPES.GROUP) { if(!payload.hasOwnProperty('session_id') && payload.session_id.trim().length > 0){ throw {httpStatus: 406, message: 'Field "session_id" is necessary and cannot be empty for GROUP session.'}; } sessionId = payload.session_id; } let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.createSession(sessionId, sessionName, sessionType, participants); }); /** * 获取会话列表 * * 请求URL /sessions?user_id=3121&page=0&size=10 */ router.get("/", function (req, res) { let page = req.query.page; let size = req.query.size; let userId = req.query.user_id; if (!page) { throw {httpStatus: 406, message: 'Missing page.'}; } if (!size) { throw {httpStatus: 406, message: 'Missing size.'}; } if (!userId) { throw {httpStatus: 406, message: 'Missing user.'}; } let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.getUserSessions(userId, page, size); }); /** * 某个聊天记录置顶操作 * * 请求URL /stick?user=3121&sessionId=testsessionmsg1 */ router.post(APIv2.Sessions.SessionSticky, function (req, res) { let user = req.query.user; let sessionId = req.query.sessionId; if (!user) { throw {httpStatus: 406, message: 'Missing user.'}; } if (!sessionId) { throw {httpStatus: 406, message: 'Missing sessionId.'}; } let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.stickSession(sessionId, user); }); /** * 取消置顶 * 请求URL /cancelStick?user=3121&sessionId=testsessionmsg1 */ router.delete(APIv2.Sessions.SessionSticky, function (req, res) { let user = req.query.user; let sessionId = req.query.sessionId; if (!user) { throw {httpStatus: 406, message: 'Missing user.'}; } if (!sessionId) { throw {httpStatus: 406, message: 'Missing sessionId.'}; } let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.cancelStickSession(sessionId, user); }); /** * 移除成员 * user:移除的人员 * sessionId 会话ID */ router.delete(APIv2.Sessions.Participant, function (req, res) { let user = req.query.user; if (!user) { throw {httpStatus: 406, message: 'Missing user.'}; } let sessionId = req.query.sessionId; if (!sessionId) { throw {httpStatus: 406, message: 'Missing sessionId.'}; } let participants = new Participants(); ControllerUtil.regModelEventHandler(sessions, res); participants.deleteUser(sessionId, user); }); /** * 增加成员 * user:移除的人员 * sessionId 会话ID */ router.put(APIv2.Sessions.Participant, function (req, res) { let user = req.query.user; if (!user) { throw {httpStatus: 406, message: 'Missing user.'}; } let sessionId = req.query.sessionId; if (!sessionId) { throw {httpStatus: 406, message: 'Missing sessionId.'}; } let participants = new Participants(); ControllerUtil.regModelEventHandler(sessions, res); participants.pushUser(sessionId, user); }); /** * 发送消息 * * 请求URL: * /sessions/:session_id/messages * * 消息体: * { * sender_id: bea851fc, * sender_name: 李毅, * content_type: 1, * content: 李医生,你好 * } */ router.post(APIv2.Sessions.Messages, function (req, res) { let payload = req.body; let testing = ObjectUtil.fieldsCheck(payload, "sender_id", "sender_name", "content_type", "content"); if (!testing.pass) { throw {httpStatus: 406, message: testing.message} } let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); payload.timestamp = new Date(); sessions.saveMessageBySession(req.params.session_id, payload); }); /** * 获取消息 * * 请求URL: * /sessions/:session_id/messages?session_id=blabla&user_id=abc&start_message_id=100&end_message_id=20 */ router.get(APIv2.Sessions.Messages, function (req, res) { let stratmsgid = req.query.stratmsgid; let endmsgid = req.query.endmsgid; let user = req.query.user; let sessionId = req.query.sessionId; if (!stratmsgid) { throw {httpStatus: 406, message: 'Missing stratmsgid.'}; } if (!endmsgid) { throw {httpStatus: 406, message: 'Missing endmsgid.'}; } if (!user) { throw {httpStatus: 406, message: 'Missing user.'}; } if (!sessionId) { throw {httpStatus: 406, message: 'Missing sessionId.'}; } let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.getMessages(sessionId, user, stratmsgid,endmsgid); }); /** * 获取所有会话未读消息数。 * * 请求URL:/sessions/unread_message_count?user_id=xyz */ router.get(APIv2.Sessions.SessionsUnreadMessageCount, function (req, res) { let userId = req.query.user_id; let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.getAllSessionsUnreadMessageCount(); }); /** * 获取指定会话未读消息数 * * 请求URL * /sessions/:session_id/unread_message_count?user_id=xyz */ router.get(APIv2.Sessions.SessionUnreadMessageCount, function (req, res) { let sessionId = req.params.session_id; let userId = req.query.user_id; let sessions = new Sessions(); ControllerUtil.regModelEventHandler(sessions, res); sessions.getSessionUnreadMessageCount(sessionId, userId); }); module.exports = router;