|
@ -15,7 +15,6 @@ let ParticipantRepo = require('../../repository/mysql/participant.repo');
|
|
|
|
|
|
let WechatClient = require("../client/wechat.client.js");
|
|
|
let AppClient = require("../client/app.client.js");
|
|
|
|
|
|
let configFile = require('../../include/commons').CONFIG_FILE;
|
|
|
let config = require('../../resources/config/' + configFile);
|
|
|
let redis = RedisClient.redisClient().connection;
|
|
@ -64,14 +63,12 @@ class Sessions extends RedisModel {
|
|
|
handler(err, null);
|
|
|
return;
|
|
|
}
|
|
|
;
|
|
|
ModelUtil.emitError(self.eventEmitter, {message: err, status: -1}, null);
|
|
|
} else {
|
|
|
if (handler) {
|
|
|
handler(null, res);
|
|
|
return;
|
|
|
}
|
|
|
;
|
|
|
ModelUtil.emitOK(self.eventEmitter, {status: 200, data: res});
|
|
|
}
|
|
|
});
|
|
@ -143,7 +140,7 @@ class Sessions extends RedisModel {
|
|
|
last_message_time: message.timestamp.getTime(),
|
|
|
last_content: message.content,
|
|
|
last_content_type: message.content_type
|
|
|
}
|
|
|
};
|
|
|
redis.hmsetAsync(sessionKey, session).then(function () {
|
|
|
Participants.saveParticipantsToRedis(sessionId, participantArray, createDate, function (res) {
|
|
|
handler(null, session);
|
|
@ -231,7 +228,6 @@ class Sessions extends RedisModel {
|
|
|
handler(err, null);
|
|
|
return;
|
|
|
}
|
|
|
;
|
|
|
callBeginTrans(session);
|
|
|
})
|
|
|
}
|
|
@ -264,7 +260,6 @@ class Sessions extends RedisModel {
|
|
|
handler(err, null);
|
|
|
return;
|
|
|
}
|
|
|
;
|
|
|
handler(null, session);
|
|
|
});
|
|
|
}
|
|
@ -359,69 +354,79 @@ class Sessions extends RedisModel {
|
|
|
})
|
|
|
},
|
|
|
// 遍历会话
|
|
|
function (sessionIds, callback) {
|
|
|
function (sessionIds) {
|
|
|
let sessionList = [];
|
|
|
sessionIds.forEach(function (sessionId) {
|
|
|
let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
|
|
|
let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
|
|
|
let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
|
|
|
let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
|
|
|
redis.multi()
|
|
|
.hgetall(sessionKey) // 会话实体
|
|
|
.hget(participantsRoleKey, userId) // 用户在此会话中的角色
|
|
|
.zscore(sessionParticipantsKey, userId) // 用户在此会话中最后一次获取未读消息的时间
|
|
|
.zrange(participantsKey, 0, -1)
|
|
|
.execAsync()
|
|
|
.then(function (res) {
|
|
|
let session = res[0];
|
|
|
let role = res[1];
|
|
|
let lastFetchTime = res[2];
|
|
|
let users = res[3];
|
|
|
let sessionName = "";
|
|
|
let otheruserId = "";
|
|
|
if (session.type == SESSION_TYPES.P2P) {
|
|
|
for (let j in users) {
|
|
|
if (users[j] != userId) {
|
|
|
otheruserId = users[j];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (!role) role = 0;
|
|
|
if (!lastFetchTime) lastFetchTime = new Date().getTime();
|
|
|
// 计算未读消息数
|
|
|
let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
|
|
|
redis.zcountAsync(messagesByTimestampKey, lastFetchTime, new Date().getTime())
|
|
|
.then(function (count) {
|
|
|
if (!otheruserId) otheruserId = userId;
|
|
|
ParticipantRepo.findNameById(otheruserId, function (err, res) {
|
|
|
if ((res && res.length == 0) || session.type != SESSION_TYPES.P2P) {
|
|
|
sessionName = session.name;
|
|
|
} else {
|
|
|
sessionName = res[0].name;
|
|
|
}
|
|
|
sessionList.push({
|
|
|
id: sessionId,
|
|
|
name: sessionName,
|
|
|
create_date: session.create_date,
|
|
|
last_content_type: session.last_content_type,
|
|
|
last_content: session.last_content,
|
|
|
sender_id: session.sender_id,
|
|
|
type: session.type,
|
|
|
sender_name: session.sender_name,
|
|
|
unread_count: count,
|
|
|
business_type: session.business_type,
|
|
|
my_role: role
|
|
|
});
|
|
|
if (sessionId === sessionIds[sessionIds.length - 1]) {
|
|
|
ModelUtil.emitOK(self.eventEmitter, sessionList);
|
|
|
let functionList =[];
|
|
|
for(var j = 0 ;j<sessionIds.length;j++){
|
|
|
let fun = function(index,callback){
|
|
|
if(!callback) {callback =index,index=0};
|
|
|
let sessionId = sessionIds[index]
|
|
|
let sessionKey = RedisModel.makeRedisKey(REDIS_KEYS.Session, sessionId);
|
|
|
let participantsRoleKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipantsRole, sessionId);
|
|
|
let sessionParticipantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants, sessionId);
|
|
|
let participantsKey = RedisModel.makeRedisKey(REDIS_KEYS.SessionParticipants,sessionId);
|
|
|
redis.multi()
|
|
|
.hgetall(sessionKey) // 会话实体
|
|
|
.hget(participantsRoleKey, userId) // 用户在此会话中的角色
|
|
|
.zscore(sessionParticipantsKey, userId) // 用户在此会话中最后一次获取未读消息的时间
|
|
|
.zrange(participantsKey,0,-1)
|
|
|
.execAsync()
|
|
|
.then(function (res) {
|
|
|
let session = res[0];
|
|
|
let role = res[1];
|
|
|
let lastFetchTime = res[2];
|
|
|
let users = res[3];
|
|
|
let sessionName="";
|
|
|
let otheruserId ="";
|
|
|
if(session.type==SESSION_TYPES.P2P){
|
|
|
for(var j in users){
|
|
|
if(users[j]!=userId){
|
|
|
otheruserId = users[j];
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
}).catch(function (err) {
|
|
|
logger.error("Get sessions failed: ", ex);
|
|
|
ModelUtil.emitError(self.eventEmitter, "Get sessions failed: " + err);
|
|
|
});
|
|
|
});
|
|
|
}
|
|
|
if(!role)role =0;
|
|
|
if(!lastFetchTime)lastFetchTime=new Date().getTime();
|
|
|
// 计算未读消息数
|
|
|
let messagesByTimestampKey = RedisModel.makeRedisKey(REDIS_KEYS.MessagesByTimestamp, sessionId);
|
|
|
redis.zcountAsync(messagesByTimestampKey, lastFetchTime, new Date().getTime())
|
|
|
.then(function (count) {
|
|
|
if(!otheruserId)otheruserId=userId;
|
|
|
ParticipantRepo.findNameById(otheruserId, function (err, res) {
|
|
|
if((res&&res.length==0)||session.type!=SESSION_TYPES.P2P){
|
|
|
sessionName = session.name;
|
|
|
}else{
|
|
|
sessionName = res[0].name;
|
|
|
}
|
|
|
sessionList.push({
|
|
|
id: sessionId,
|
|
|
name: sessionName,
|
|
|
create_date: session.create_date,
|
|
|
last_content_type: session.last_content_type,
|
|
|
last_content: session.last_content,
|
|
|
sender_id: session.sender_id,
|
|
|
type: session.type,
|
|
|
sender_name: session.sender_name,
|
|
|
unread_count: count,
|
|
|
business_type: session.business_type,
|
|
|
my_role: role
|
|
|
});
|
|
|
index=(parseInt(index)+1);
|
|
|
if(index==sessionIds.length){
|
|
|
ModelUtil.emitOK(self.eventEmitter, sessionList);
|
|
|
}else{
|
|
|
callback(null,index);
|
|
|
}
|
|
|
|
|
|
})
|
|
|
})
|
|
|
}).catch(function (err) {
|
|
|
logger.error("Get sessions failed: ", err);
|
|
|
});
|
|
|
}
|
|
|
functionList.push(fun);
|
|
|
};
|
|
|
async.waterfall(functionList);
|
|
|
}
|
|
|
]);
|
|
|
}
|
|
@ -631,7 +636,7 @@ class Sessions extends RedisModel {
|
|
|
ModelUtil.emitOK(self.eventEmitter, {count: count});
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
for (let j in res) {
|
|
|
if (res[j].type == SESSION_TYPES.SYSTEM) {
|
|
|
if (j == res.length - 1) {
|