123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536 |
- /**
- * 会话接口。
- *
- * author: Sand
- * since: 12/15/2016
- */
- "use strict";
- let express = require('express');
- let router = express.Router();
- let ObjectUtil = require("../../util/object.util.js");
- let ControllerUtil = require('../../util/controller.util');
- let Sessions = require('../../models/sessions/sessions');
- let Messages = require('../../models/messages/messages');
- 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表示临时讨论组
- * (咨询类型:1三师咨询,2视频咨询,3图文咨询,4公共咨询,5病友圈 6、患者名医咨询 7医生名医咨询 8续方咨询 9居民直接咨询专科 10医生发起的求助 11上门服务咨询)
- * 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 = payload.session_id;
- let sessions = new Sessions();
- let participantArray = [];
- ///是否视频会议的会话
- let videoconferencing = false;
- participants = JSON.parse(participants);
- for (let j in participants) {
- participantArray.push(j + ":" + participants[j]);
- }
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.createSession(sessionId, sessionName, sessionType, participantArray);
- //视频会议的会话 szx add 20181121
- if (!payload.hasOwnProperty('videoconferencing') && payload.videoconferencing == 1){
- videoconferencing = true;
- //发送广播,给相关的与会人员
- for (let j in participants) {
- let participant = participants[j];
- let socketClient = ClientCache.findById(participant.userId);
- socketClient.emt("startVideoconference",{"session_id":sessionId});
- }
-
- }
- });
- /**
- * 获取会话列表
- *
- * 请求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;
- let businessType = req.query.business_type;
- let sessionType = req.query.type;
- let status = req.query.status;
- 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);
- //传入参数齐全走过滤方法
- if(status&&businessType&&userId&&size&&page){
- sessions.getUserStatusSessions(userId,status,sessionType,businessType,page,size);
- }else{
- sessions.getUserSessions(userId, page, size, businessType);
- }
- });
- /**
- * 按会话类型获取会话列表
- * 请求URL /sessions/sessionListByType?user_id=3121&page=0&size=10&type=4
- */
- router.get(APIv2.Sessions.SessionListByType, function (req, res) {
- let page = req.query.page;
- let size = req.query.size;
- let userId = req.query.user_id;
- let type = req.query.type;
- let status = req.query.status;
- let name = req.query.name;
- if (!page) {
- throw {httpStatus: 406, message: 'Missing page.'};
- }
- if (!size) {
- throw {httpStatus: 406, message: 'Missing size.'};
- }
- if (!type) {
- throw {httpStatus: 406, message: 'Missing type.'};
- }
- if (!userId) {
- throw {httpStatus: 406, message: 'Missing user.'};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getUserSessionsByType(userId,type,page,size,status,name);
- });
- /**
- * 按会话类型获取会话总数
- * 请求URL /sessions/sessionCountByType?user_id=3121&type=4
- */
- router.get(APIv2.Sessions.SessionCountByType, function (req, res) {
- let userId = req.query.user_id;
- let type = req.query.type;
- if (!type) {
- throw {httpStatus: 406, message: 'Missing type.'};
- }
- if (!userId) {
- throw {httpStatus: 406, message: 'Missing user.'};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getSessionCountByType(userId,type);
- });
- router.get(APIv2.Sessions.Session,function(req,res){
- let sessionId = req.params.session_id;
- let userId = req.query.user_id;
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing sessionId.'};
- }
- if (!userId) {
- throw {httpStatus: 406, message: 'Missing user.'};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getSession(sessionId,userId);
- });
- /**
- * 最近会话列表。
- *
- * URL:
- * /sessions/recent?user_id=abc&date_span=7
- */
- router.get(APIv2.Sessions.RecentSessions, function (req, res) {
- ControllerUtil.checkRequestQueryParams(req, ['user_id', 'date_span']);
- let userId = req.query.user_id;
- let dateSpan = req.query.date_span;
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getRecentSessions(userId, dateSpan);
- });
- /**
- * 判断会话是否存在。
- *
- * URL:
- * /sessions/isExist?session_id=abc
- */
- router.get(APIv2.Sessions.IsExist, function (req, res) {
- let sessionId = req.query.session_id;
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing sessionId.'}
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.isExist(sessionId);
- });
- /**
- * 某个聊天记录置顶操作
- *
- * 请求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 /status?status=1&sessionId=testsessionmsg1
- */
- router.post(APIv2.Sessions.SessionStatus, function (req, res) {
- let sessionId = req.query.sessionId;
- let status = req.query.status;
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing sessionId.'};
- }
- if (!status) {
- throw {httpStatus: 406, message: 'Missing status.'};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.updateSessionStatus(sessionId, status);
- });
- /**
- * 修改会话名称(修改redis和数据库)
- *
- * 请求URL /name?name=哈哈&sessionId=testsessionmsg1
- */
- router.post(APIv2.Sessions.SessionName, function (req, res) {
- let sessionId = req.body.sessionId;
- let name = req.body.name;
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing sessionId.'};
- }
- if (!name) {
- throw {httpStatus: 406, message: 'Missing name.'};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.updateSessionName(sessionId, name);
- });
- /**
- * 取消置顶
- * 请求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);
- });
- /**
- * 获取会话成员。
- */
- router.get(APIv2.Sessions.Participants, function (req, res) {
- let sessionId = req.params.session_id;
- let participants = new Participants();
- ControllerUtil.regModelEventHandler(participants, res);
- participants.getParticipants(sessionId);
- });
- /**
- * 获取会话成员头像列表。
- */
- router.get(APIv2.Sessions.ParticipantsAvatar, function (req, res) {
- let sessionId = req.params.session_id;
- let participants = new Participants();
- ControllerUtil.regModelEventHandler(participants, res);
- participants.getParticipantsAvatar(sessionId);
- });
- /**
- * 增加成员
- * user:增加的人员
- * sessionId 会话ID
- */
- router.put(APIv2.Sessions.Participant, function (req, res) {
- let user = req.params.participant_id;
- if (!user) {
- throw {httpStatus: 406, message: 'Missing participant_id.'};
- }
- let sessionId = req.params.session_id;
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing session_id.'};
- }
- let participants = new Participants();
- ControllerUtil.regModelEventHandler(participants, res);
- participants.addUser(sessionId, user);
- });
- /**
- * 增加成员
- * user_id:增加的人员
- * old_user_id:删除的人员
- * session_id 会话ID
- */
- router.post(APIv2.Sessions.ParticipantUpdate, function (req, res) {
- let payload = req.body;
- let testing = ObjectUtil.fieldsCheck(payload, 'user_id', 'session_id');
- if (!testing.pass) {
- throw testing.message;
- }
- let participants = new Participants();
- ControllerUtil.regModelEventHandler(participants, res);
- participants.updateSessionUser(payload.user_id, payload.old_user_id, payload.session_id);
- });
- /**
- * 移除成员
- * user:移除的人员
- * sessionId 会话ID
- */
- router.delete(APIv2.Sessions.Participant, function (req, res) {
- let user = req.params.participant_id;
- if (!user) {
- throw {httpStatus: 406, message: 'Missing user.'};
- }
- let sessionId = req.params.session_id;
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing sessionId.'};
- }
- let participants = new Participants();
- ControllerUtil.regModelEventHandler(participants, res);
- participants.removeUser(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}
- }
- if(payload.view&&payload.view==1){
- throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
- }
- // 消息的发送时间由服务端决定
- payload.timestamp = new Date((new Date().getTime()));
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.saveMessageBySession(req.params.session_id, payload);
- });
- router.post(APIv2.Sessions.TopicMessages, 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}
- }
- if(payload.view&&payload.view==1){
- throw {httpStatus: 406, message: "观察者模式,不能发送消息!"};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- payload.timestamp = new Date((new Date().getTime()));
- sessions.sendTopicMessages(req.params.topic_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 startMsgId = req.query.start_msg_id;
- let endMsgId = req.query.end_msg_id;
- let pagesize = req.query.pagesize;
- let page = req.query.page;
- let user = req.query.user;
- let sessionId = req.query.session_id;
- let content_type = req.query.content_type;
- let isoffset = req.query.isoffset;
- if (!user) {
- throw {httpStatus: 406, message: 'Missing user.'};
- }
- if (!sessionId) {
- throw {httpStatus: 406, message: 'Missing session_id.'};
- }
- if (!pagesize) {
- pagesize = 20;
- }
- if (!page) {
- page = 1;
- }
- if (content_type) {
- let messages = new Messages();
- ControllerUtil.regModelEventHandler(messages, res);
- messages.getMessageByType(sessionId, page, pagesize, content_type)
- } else {
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getMessages(sessionId, user, startMsgId, endMsgId, page, pagesize, isoffset);
- }
- });
- /**
- * 修改单条消息
- * session_id 会话ID
- * message_id:消息ID
- * content 消息内容
- */
- router.post(APIv2.Sessions.MessageUpdate, function (req, res) {
- let payload = req.body;
- let testing = ObjectUtil.fieldsCheck(payload,'session_type', 'session_id','message_id','content');
- if (!testing.pass) {
- throw testing.message;
- }
- let messages = new Messages();
- ControllerUtil.regModelEventHandler(messages, res);
- messages.updateMsgContent(payload.session_type, payload.session_id,payload.message_id, payload.content);
- });
- router.get(APIv2.Sessions.SessionUnreadMessages, function (req, res) {
- let sessionId = req.params.session_id;
- let userId = req.query.user_id;
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getSessionUnreadMessages(sessionId, userId);
- });
- /**
- * 获取所有会话未读消息数。
- *
- * 请求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(userId);
- });
- /**
- * 按会话类型获取未读消息数量 status不传为获取所有状态
- * 请求URL:/sessions/unread_message_count_type?user_id=xyz&type=18&status=0
- */
- router.get(APIv2.Sessions.SessionsUnreadMessageCountByType, function (req, res) {
- let userId = req.query.user_id;
- let type = req.query.type;
- let status = req.query.status;
- if (!type) {
- throw {httpStatus: 406, message: 'Missing type.'};
- }
- let sessions = new Sessions();
- ControllerUtil.regModelEventHandler(sessions, res);
- sessions.getAllSessionsUnreadMessageCountByType(userId,type,status);
- });
- /**
- * 获取指定会话未读消息数
- *
- * 请求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;
|