You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nodebb/src/messaging.js

344 lines
9.3 KiB
JavaScript

11 years ago
'use strict';
8 years ago
var async = require('async');
var S = require('string');
var db = require('./database');
var user = require('./user');
var plugins = require('./plugins');
var meta = require('./meta');
var utils = require('./utils');
12 years ago
var Messaging = module.exports;
require('./messaging/data')(Messaging);
require('./messaging/create')(Messaging);
require('./messaging/delete')(Messaging);
require('./messaging/edit')(Messaging);
require('./messaging/rooms')(Messaging);
require('./messaging/unread')(Messaging);
require('./messaging/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);
}
db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', start, stop, next);
},
function (mids, next) {
if (!Array.isArray(mids) || !mids.length) {
return callback(null, []);
}
mids.forEach(function (mid, index) {
indices[mid] = start + index;
});
8 years ago
mids.reverse();
8 years ago
Messaging.getMessagesData(mids, uid, roomId, isNew, next);
},
function (messageData, next) {
messageData.forEach(function (messageData) {
messageData.index = indices[messageData.messageId.toString()];
});
next(null, messageData);
},
], callback);
};
function canGet(hook, callerUid, uid, callback) {
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);
}
12 years ago
var messageData = {
message: message,
parsed: parsed,
fromuid: fromuid,
8 years ago
uid: uid,
roomId: roomId,
isNew: isNew,
parsedMessage: parsed,
};
8 years ago
plugins.fireHook('filter:messaging.parse', messageData, function (err, messageData) {
callback(err, 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) {
if (messages && messages.length) {
next(null, parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + Messaging.newMessageCutoff);
} else {
next(null, true);
}
},
], callback);
};
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);
}
uids = uids.filter(function (value) {
return value && parseInt(value, 10) !== parseInt(uid, 10);
8 years ago
});
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'], next);
});
}, 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) {
room.users = results.users[index];
room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : room.users.length > 2;
room.unread = results.unread[index];
room.teaser = results.teasers[index];
room.users.forEach(function (userData) {
if (userData && parseInt(userData.uid, 10)) {
userData.status = user.getStatus(userData);
8 years ago
}
11 years ago
});
room.users = room.users.filter(function (user) {
return user && parseInt(user.uid, 10);
});
room.lastUser = room.users[0];
11 years ago
room.usernames = Messaging.generateUsernames(room.users, uid);
});
next(null, { rooms: results.roomData, nextStart: stop + 1 });
},
], callback);
};
Messaging.generateUsernames = function (users, excludeUid) {
users = users.filter(function (user) {
return user && parseInt(user.uid, 10) !== excludeUid;
});
return users.map(function (user) {
return user.username;
}).join(', ');
};
Messaging.getTeaser = function (uid, roomId, callback) {
var teaser;
async.waterfall([
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', 0, 0, next);
},
function (mids, next) {
if (!mids || !mids.length) {
return next(null, null);
}
Messaging.getMessageFields(mids[0], ['fromuid', 'content', 'timestamp'], next);
},
function (_teaser, next) {
teaser = _teaser;
if (!teaser) {
return callback();
}
if (teaser.content) {
teaser.content = S(teaser.content).stripTags().decodeHTMLEntities().s;
}
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);
};
Messaging.canMessageUser = function (uid, toUid, callback) {
8 years ago
if (parseInt(meta.config.disableChat, 10) === 1 || !uid || uid === toUid) {
return callback(new Error('[[error:chat-disabled]]'));
}
8 years ago
if (parseInt(uid, 10) === parseInt(toUid, 10)) {
return callback(new Error('[[error:cant-chat-with-yourself'));
}
async.waterfall([
function (next) {
user.exists(toUid, next);
},
function (exists, next) {
if (!exists) {
return callback(new Error('[[error:no-user]]'));
}
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
},
function (userData, next) {
if (parseInt(userData.banned, 10) === 1) {
return callback(new Error('[[error:user-banned]]'));
}
if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
return callback(new Error('[[error:email-not-confirmed-chat]]'));
}
async.parallel({
settings: async.apply(user.getSettings, toUid),
isAdmin: async.apply(user.isAdministrator, uid),
isFollowing: async.apply(user.isFollowing, toUid, uid),
}, next);
},
function (results, next) {
if (!results.settings.restrictChat || results.isAdmin || results.isFollowing) {
return next();
}
8 years ago
next(new Error('[[error:chat-restricted]]'));
},
], callback);
};
9 years ago
Messaging.canMessageRoom = function (uid, roomId, callback) {
8 years ago
if (parseInt(meta.config.disableChat, 10) === 1 || !uid) {
return callback(new Error('[[error:chat-disabled]]'));
}
9 years ago
async.waterfall([
function (next) {
Messaging.isUserInRoom(uid, roomId, next);
},
function (inRoom, next) {
if (!inRoom) {
return next(new Error('[[error:not-in-room]]'));
}
9 years ago
Messaging.getUserCountInRoom(roomId, next);
},
function (count, next) {
if (count < 2) {
return next(new Error('[[error:no-users-in-room]]'));
}
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
},
function (userData, next) {
if (parseInt(userData.banned, 10) === 1) {
return next(new Error('[[error:user-banned]]'));
}
if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
return next(new Error('[[error:email-not-confirmed-chat]]'));
10 years ago
}
next();
},
], callback);
};
9 years ago
Messaging.hasPrivateChat = function (uid, withUid, callback) {
if (parseInt(uid, 10) === parseInt(withUid, 10)) {
return callback(null, 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) {
return roomId && results.theirRooms.indexOf(roomId) !== -1;
});
9 years ago
if (!roomIds.length) {
return callback();
9 years ago
}
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <[email protected]> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
var index = 0;
var roomId = 0;
async.whilst(function () {
return index < roomIds.length && !roomId;
}, function (next) {
Messaging.getUserCountInRoom(roomIds[index], function (err, count) {
if (err) {
return next(err);
}
if (count === 2) {
roomId = roomIds[index];
next(null, roomId);
} else {
index += 1;
next();
}
});
}, function (err) {
next(err, roomId);
});
},
], callback);
};