|
|
|
@ -1,16 +1,15 @@
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
const validator = require('validator');
|
|
|
|
|
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var user = require('../user');
|
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
var utils = require('../utils');
|
|
|
|
|
const db = require('../database');
|
|
|
|
|
const user = require('../user');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const meta = require('../meta');
|
|
|
|
|
const utils = require('../utils');
|
|
|
|
|
|
|
|
|
|
var Messaging = module.exports;
|
|
|
|
|
const Messaging = module.exports;
|
|
|
|
|
|
|
|
|
|
require('./data')(Messaging);
|
|
|
|
|
require('./create')(Messaging);
|
|
|
|
@ -21,38 +20,27 @@ require('./unread')(Messaging);
|
|
|
|
|
require('./notifications')(Messaging);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Messaging.getMessages = function (params, callback) {
|
|
|
|
|
var uid = params.uid;
|
|
|
|
|
var roomId = params.roomId;
|
|
|
|
|
var isNew = params.isNew || false;
|
|
|
|
|
var start = params.hasOwnProperty('start') ? params.start : 0;
|
|
|
|
|
var stop = parseInt(start, 10) + ((params.count || 50) - 1);
|
|
|
|
|
|
|
|
|
|
var indices = {};
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
canGet('filter:messaging.canGetMessages', params.callerUid, params.uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (canGet, next) {
|
|
|
|
|
if (!canGet) {
|
|
|
|
|
return callback(null, null);
|
|
|
|
|
Messaging.getMessages = async (params) => {
|
|
|
|
|
const isNew = params.isNew || false;
|
|
|
|
|
const start = params.hasOwnProperty('start') ? params.start : 0;
|
|
|
|
|
const stop = parseInt(start, 10) + ((params.count || 50) - 1);
|
|
|
|
|
|
|
|
|
|
const indices = {};
|
|
|
|
|
const ok = await canGet('filter:messaging.canGetMessages', params.callerUid, params.uid);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', start, stop, next);
|
|
|
|
|
},
|
|
|
|
|
function (mids, next) {
|
|
|
|
|
|
|
|
|
|
const mids = await db.getSortedSetRevRange('uid:' + params.uid + ':chat:room:' + params.roomId + ':mids', start, stop);
|
|
|
|
|
if (!mids.length) {
|
|
|
|
|
return callback(null, []);
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mids.forEach(function (mid, index) {
|
|
|
|
|
indices[mid] = start + index;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
mids.reverse();
|
|
|
|
|
|
|
|
|
|
Messaging.getMessagesData(mids, uid, roomId, isNew, next);
|
|
|
|
|
},
|
|
|
|
|
function (messageData, next) {
|
|
|
|
|
let messageData = await Messaging.getMessagesData(mids, params.uid, params.roomId, isNew);
|
|
|
|
|
messageData.forEach(function (messageData) {
|
|
|
|
|
messageData.index = indices[messageData.messageId.toString()];
|
|
|
|
|
});
|
|
|
|
@ -62,28 +50,22 @@ Messaging.getMessages = function (params, callback) {
|
|
|
|
|
return (!messageData.deleted || messageData.fromuid === parseInt(params.uid, 10));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
next(null, messageData);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
return messageData;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function canGet(hook, callerUid, uid, callback) {
|
|
|
|
|
plugins.fireHook(hook, {
|
|
|
|
|
async function canGet(hook, callerUid, uid) {
|
|
|
|
|
const data = await plugins.fireHook(hook, {
|
|
|
|
|
callerUid: callerUid,
|
|
|
|
|
uid: uid,
|
|
|
|
|
canGet: parseInt(callerUid, 10) === parseInt(uid, 10),
|
|
|
|
|
}, function (err, data) {
|
|
|
|
|
callback(err, data ? data.canGet : false);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) {
|
|
|
|
|
plugins.fireHook('filter:parse.raw', message, function (err, parsed) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
return data ? data.canGet : false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var messageData = {
|
|
|
|
|
Messaging.parse = async (message, fromuid, uid, roomId, isNew) => {
|
|
|
|
|
const parsed = await plugins.fireHook('filter:parse.raw', message);
|
|
|
|
|
let messageData = {
|
|
|
|
|
message: message,
|
|
|
|
|
parsed: parsed,
|
|
|
|
|
fromuid: fromuid,
|
|
|
|
@ -93,70 +75,39 @@ Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) {
|
|
|
|
|
parsedMessage: parsed,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('filter:messaging.parse', messageData, function (err, messageData) {
|
|
|
|
|
callback(err, messageData ? messageData.parsedMessage : '');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
messageData = await plugins.fireHook('filter:messaging.parse', messageData);
|
|
|
|
|
return messageData ? messageData.parsedMessage : '';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.isNewSet = function (uid, roomId, timestamp, callback) {
|
|
|
|
|
var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.getSortedSetRevRangeWithScores(setKey, 0, 0, next);
|
|
|
|
|
},
|
|
|
|
|
function (messages, next) {
|
|
|
|
|
Messaging.isNewSet = async (uid, roomId, timestamp) => {
|
|
|
|
|
const setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
|
|
|
|
|
const messages = await db.getSortedSetRevRangeWithScores(setKey, 0, 0);
|
|
|
|
|
if (messages && messages.length) {
|
|
|
|
|
next(null, parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + Messaging.newMessageCutoff);
|
|
|
|
|
} else {
|
|
|
|
|
next(null, true);
|
|
|
|
|
return parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + Messaging.newMessageCutoff;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Messaging.getRecentChats = function (callerUid, uid, start, stop, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
canGet('filter:messaging.canGetRecentChats', callerUid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (canGet, next) {
|
|
|
|
|
if (!canGet) {
|
|
|
|
|
return callback(null, null);
|
|
|
|
|
}
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':chat:rooms', start, stop, next);
|
|
|
|
|
},
|
|
|
|
|
function (roomIds, next) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
roomData: function (next) {
|
|
|
|
|
Messaging.getRoomsData(roomIds, next);
|
|
|
|
|
},
|
|
|
|
|
unread: function (next) {
|
|
|
|
|
db.isSortedSetMembers('uid:' + uid + ':chat:rooms:unread', roomIds, next);
|
|
|
|
|
},
|
|
|
|
|
users: function (next) {
|
|
|
|
|
async.map(roomIds, function (roomId, next) {
|
|
|
|
|
db.getSortedSetRevRange('chat:room:' + roomId + ':uids', 0, 9, function (err, uids) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
Messaging.getRecentChats = async (callerUid, uid, start, stop) => {
|
|
|
|
|
const ok = await canGet('filter:messaging.canGetRecentChats', callerUid, uid);
|
|
|
|
|
if (!ok) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const roomIds = await db.getSortedSetRevRange('uid:' + uid + ':chat:rooms', start, stop);
|
|
|
|
|
const results = await utils.promiseParallel({
|
|
|
|
|
roomData: Messaging.getRoomsData(roomIds),
|
|
|
|
|
unread: db.isSortedSetMembers('uid:' + uid + ':chat:rooms:unread', roomIds),
|
|
|
|
|
users: Promise.all(roomIds.map(async (roomId) => {
|
|
|
|
|
let uids = await db.getSortedSetRevRange('chat:room:' + roomId + ':uids', 0, 9);
|
|
|
|
|
uids = uids.filter(function (value) {
|
|
|
|
|
return value && parseInt(value, 10) !== parseInt(uid, 10);
|
|
|
|
|
});
|
|
|
|
|
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'], next);
|
|
|
|
|
return await user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline']);
|
|
|
|
|
})),
|
|
|
|
|
teasers: Promise.all(roomIds.map(async roomId => Messaging.getTeaser(uid, roomId))),
|
|
|
|
|
});
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
teasers: function (next) {
|
|
|
|
|
async.map(roomIds, function (roomId, next) {
|
|
|
|
|
Messaging.getTeaser(uid, roomId, next);
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (results, next) {
|
|
|
|
|
|
|
|
|
|
results.roomData.forEach(function (room, index) {
|
|
|
|
|
if (room) {
|
|
|
|
|
room.users = results.users[index];
|
|
|
|
@ -179,21 +130,16 @@ Messaging.getRecentChats = function (callerUid, uid, start, stop, callback) {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
results.roomData = results.roomData.filter(Boolean);
|
|
|
|
|
|
|
|
|
|
next(null, { rooms: results.roomData, nextStart: stop + 1 });
|
|
|
|
|
},
|
|
|
|
|
function (ref, next) {
|
|
|
|
|
plugins.fireHook('filter:messaging.getRecentChats', {
|
|
|
|
|
const ref = { rooms: results.roomData, nextStart: stop + 1 };
|
|
|
|
|
return await plugins.fireHook('filter:messaging.getRecentChats', {
|
|
|
|
|
rooms: ref.rooms,
|
|
|
|
|
nextStart: ref.nextStart,
|
|
|
|
|
uid: uid,
|
|
|
|
|
callerUid: callerUid,
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.generateUsernames = function (users, excludeUid) {
|
|
|
|
|
Messaging.generateUsernames = (users, excludeUid) => {
|
|
|
|
|
users = users.filter(function (user) {
|
|
|
|
|
return user && parseInt(user.uid, 10) !== excludeUid;
|
|
|
|
|
});
|
|
|
|
@ -202,211 +148,149 @@ Messaging.generateUsernames = function (users, excludeUid) {
|
|
|
|
|
}).join(', ');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.getTeaser = function (uid, roomId, callback) {
|
|
|
|
|
var teaser;
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Messaging.getLatestUndeletedMessage(uid, roomId, next);
|
|
|
|
|
},
|
|
|
|
|
function (mid, next) {
|
|
|
|
|
Messaging.getTeaser = async (uid, roomId) => {
|
|
|
|
|
const mid = await Messaging.getLatestUndeletedMessage(uid, roomId);
|
|
|
|
|
if (!mid) {
|
|
|
|
|
return callback(null, null);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
Messaging.getMessageFields(mid, ['fromuid', 'content', 'timestamp'], next);
|
|
|
|
|
},
|
|
|
|
|
function (_teaser, next) {
|
|
|
|
|
teaser = _teaser;
|
|
|
|
|
const teaser = await Messaging.getMessageFields(mid, ['fromuid', 'content', 'timestamp']);
|
|
|
|
|
if (!teaser.fromuid) {
|
|
|
|
|
return callback(null, null);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
user.blocks.is(teaser.fromuid, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (blocked, next) {
|
|
|
|
|
const blocked = await user.blocks.is(teaser.fromuid, uid);
|
|
|
|
|
if (blocked) {
|
|
|
|
|
return callback(null, null);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
teaser.user = await user.getUserFields(teaser.fromuid, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline']);
|
|
|
|
|
if (teaser.content) {
|
|
|
|
|
teaser.content = utils.stripHTMLTags(utils.decodeHTMLEntities(teaser.content));
|
|
|
|
|
teaser.content = validator.escape(String(teaser.content));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
teaser.timestampISO = utils.toISOString(teaser.timestamp);
|
|
|
|
|
user.getUserFields(teaser.fromuid, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'], next);
|
|
|
|
|
},
|
|
|
|
|
function (user, next) {
|
|
|
|
|
teaser.user = user;
|
|
|
|
|
plugins.fireHook('filter:messaging.getTeaser', { teaser: teaser }, function (err, data) {
|
|
|
|
|
next(err, data.teaser);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
const payload = await plugins.fireHook('filter:messaging.getTeaser', { teaser: teaser });
|
|
|
|
|
return payload.teaser;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.getLatestUndeletedMessage = function (uid, roomId, callback) {
|
|
|
|
|
var done = false;
|
|
|
|
|
var latestMid = null;
|
|
|
|
|
var index = 0;
|
|
|
|
|
var mids;
|
|
|
|
|
async.doWhilst(
|
|
|
|
|
function (next) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (_next) {
|
|
|
|
|
db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', index, index, _next);
|
|
|
|
|
},
|
|
|
|
|
function (_mids, _next) {
|
|
|
|
|
mids = _mids;
|
|
|
|
|
if (!mids.length) {
|
|
|
|
|
done = true;
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
Messaging.getMessageFields(mids[0], ['deleted', 'system'], _next);
|
|
|
|
|
},
|
|
|
|
|
function (states, _next) {
|
|
|
|
|
Messaging.getLatestUndeletedMessage = async (uid, roomId) => {
|
|
|
|
|
let done = false;
|
|
|
|
|
let latestMid = null;
|
|
|
|
|
let index = 0;
|
|
|
|
|
let mids;
|
|
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
mids = await db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', index, index);
|
|
|
|
|
if (mids.length) {
|
|
|
|
|
const states = await Messaging.getMessageFields(mids[0], ['deleted', 'system']);
|
|
|
|
|
done = !states.deleted && !states.system;
|
|
|
|
|
if (done) {
|
|
|
|
|
latestMid = mids[0];
|
|
|
|
|
}
|
|
|
|
|
index += 1;
|
|
|
|
|
_next();
|
|
|
|
|
},
|
|
|
|
|
], next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
next(null, !done);
|
|
|
|
|
},
|
|
|
|
|
function (err) {
|
|
|
|
|
callback(err, parseInt(latestMid, 10));
|
|
|
|
|
} else {
|
|
|
|
|
done = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return latestMid;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.canMessageUser = function (uid, toUid, callback) {
|
|
|
|
|
Messaging.canMessageUser = async (uid, toUid) => {
|
|
|
|
|
if (meta.config.disableChat || uid <= 0 || uid === toUid) {
|
|
|
|
|
return callback(new Error('[[error:chat-disabled]]'));
|
|
|
|
|
throw new Error('[[error:chat-disabled]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (parseInt(uid, 10) === parseInt(toUid, 10)) {
|
|
|
|
|
return callback(new Error('[[error:cant-chat-with-yourself'));
|
|
|
|
|
throw new Error('[[error:cant-chat-with-yourself');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
user.exists(toUid, next);
|
|
|
|
|
},
|
|
|
|
|
function (exists, next) {
|
|
|
|
|
const exists = await user.exists(toUid);
|
|
|
|
|
if (!exists) {
|
|
|
|
|
return callback(new Error('[[error:no-user]]'));
|
|
|
|
|
throw new Error('[[error:no-user]]');
|
|
|
|
|
}
|
|
|
|
|
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
|
|
|
|
|
},
|
|
|
|
|
function (userData, next) {
|
|
|
|
|
|
|
|
|
|
const userData = await user.getUserFields(uid, ['banned', 'email:confirmed']);
|
|
|
|
|
if (userData.banned) {
|
|
|
|
|
return callback(new Error('[[error:user-banned]]'));
|
|
|
|
|
throw new Error('[[error:user-banned]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (meta.config.requireEmailConfirmation && !userData['email:confirmed']) {
|
|
|
|
|
return callback(new Error('[[error:email-not-confirmed-chat]]'));
|
|
|
|
|
throw new Error('[[error:email-not-confirmed-chat]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
|
|
|
|
settings: async.apply(user.getSettings, toUid),
|
|
|
|
|
isAdmin: async.apply(user.isAdministrator, uid),
|
|
|
|
|
isModerator: async.apply(user.isModeratorOfAnyCategory, uid),
|
|
|
|
|
isFollowing: async.apply(user.isFollowing, toUid, uid),
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (results, next) {
|
|
|
|
|
const results = await utils.promiseParallel({
|
|
|
|
|
settings: user.getSettings(toUid),
|
|
|
|
|
isAdmin: user.isAdministrator(uid),
|
|
|
|
|
isModerator: user.isModeratorOfAnyCategory(uid),
|
|
|
|
|
isFollowing: user.isFollowing(toUid, uid),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (results.settings.restrictChat && !results.isAdmin && !results.isModerator && !results.isFollowing) {
|
|
|
|
|
return next(new Error('[[error:chat-restricted]]'));
|
|
|
|
|
throw new Error('[[error:chat-restricted]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('static:messaging.canMessageUser', {
|
|
|
|
|
await plugins.fireHook('static:messaging.canMessageUser', {
|
|
|
|
|
uid: uid,
|
|
|
|
|
toUid: toUid,
|
|
|
|
|
}, function (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.canMessageRoom = function (uid, roomId, callback) {
|
|
|
|
|
Messaging.canMessageRoom = async (uid, roomId) => {
|
|
|
|
|
if (meta.config.disableChat || uid <= 0) {
|
|
|
|
|
return callback(new Error('[[error:chat-disabled]]'));
|
|
|
|
|
throw new Error('[[error:chat-disabled]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Messaging.isUserInRoom(uid, roomId, next);
|
|
|
|
|
},
|
|
|
|
|
function (inRoom, next) {
|
|
|
|
|
const inRoom = await Messaging.isUserInRoom(uid, roomId);
|
|
|
|
|
if (!inRoom) {
|
|
|
|
|
return next(new Error('[[error:not-in-room]]'));
|
|
|
|
|
throw new Error('[[error:not-in-room]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
|
|
|
|
|
},
|
|
|
|
|
function (userData, next) {
|
|
|
|
|
const userData = await user.getUserFields(uid, ['banned', 'email:confirmed']);
|
|
|
|
|
if (userData.banned) {
|
|
|
|
|
return next(new Error('[[error:user-banned]]'));
|
|
|
|
|
throw new Error('[[error:user-banned]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (meta.config.requireEmailConfirmation && !userData['email:confirmed']) {
|
|
|
|
|
return next(new Error('[[error:email-not-confirmed-chat]]'));
|
|
|
|
|
throw new Error('[[error:email-not-confirmed-chat]]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('static:messaging.canMessageRoom', {
|
|
|
|
|
await plugins.fireHook('static:messaging.canMessageRoom', {
|
|
|
|
|
uid: uid,
|
|
|
|
|
roomId: roomId,
|
|
|
|
|
}, function (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.hasPrivateChat = function (uid, withUid, callback) {
|
|
|
|
|
Messaging.hasPrivateChat = async (uid, withUid) => {
|
|
|
|
|
if (parseInt(uid, 10) === parseInt(withUid, 10)) {
|
|
|
|
|
return callback(null, 0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
async.parallel({
|
|
|
|
|
myRooms: async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':chat:rooms', 0, -1),
|
|
|
|
|
theirRooms: async.apply(db.getSortedSetRevRange, 'uid:' + withUid + ':chat:rooms', 0, -1),
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (results, next) {
|
|
|
|
|
var roomIds = results.myRooms.filter(function (roomId) {
|
|
|
|
|
|
|
|
|
|
const results = await utils.promiseParallel({
|
|
|
|
|
myRooms: db.getSortedSetRevRange('uid:' + uid + ':chat:rooms', 0, -1),
|
|
|
|
|
theirRooms: db.getSortedSetRevRange('uid:' + withUid + ':chat:rooms', 0, -1),
|
|
|
|
|
});
|
|
|
|
|
const roomIds = results.myRooms.filter(function (roomId) {
|
|
|
|
|
return roomId && results.theirRooms.includes(roomId);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!roomIds.length) {
|
|
|
|
|
return callback();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var index = 0;
|
|
|
|
|
var roomId = 0;
|
|
|
|
|
async.whilst(function (next) {
|
|
|
|
|
next(null, index < roomIds.length && !roomId);
|
|
|
|
|
}, function (next) {
|
|
|
|
|
Messaging.getUserCountInRoom(roomIds[index], function (err, count) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
while (index < roomIds.length && !roomId) {
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
const count = await Messaging.getUserCountInRoom(roomIds[index]);
|
|
|
|
|
if (count === 2) {
|
|
|
|
|
roomId = roomIds[index];
|
|
|
|
|
next(null, roomId);
|
|
|
|
|
} else {
|
|
|
|
|
index += 1;
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, function (err) {
|
|
|
|
|
next(err, roomId);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return roomId;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Messaging.async = require('../promisify')(Messaging);
|
|
|
|
|