mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 16:30:34 +01:00
messaging refactor
This commit is contained in:
201
src/messaging.js
201
src/messaging.js
@@ -2,7 +2,6 @@
|
||||
|
||||
|
||||
var async = require('async');
|
||||
var winston = require('winston');
|
||||
var S = require('string');
|
||||
|
||||
var db = require('./database');
|
||||
@@ -10,37 +9,19 @@ var user = require('./user');
|
||||
var plugins = require('./plugins');
|
||||
var meta = require('./meta');
|
||||
var utils = require('../public/src/utils');
|
||||
var notifications = require('./notifications');
|
||||
var userNotifications = require('./user/notifications');
|
||||
|
||||
(function (Messaging) {
|
||||
var Messaging = module.exports;
|
||||
|
||||
require('./messaging/create')(Messaging);
|
||||
require('./messaging/delete')(Messaging);
|
||||
require('./messaging/edit')(Messaging);
|
||||
require('./messaging/rooms')(Messaging);
|
||||
require('./messaging/unread')(Messaging);
|
||||
require('./messaging/notifications')(Messaging);
|
||||
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.getMessageField = function (mid, field, callback) {
|
||||
Messaging.getMessageFields(mid, [field], function (err, fields) {
|
||||
callback(err, fields ? fields[field] : null);
|
||||
});
|
||||
};
|
||||
|
||||
Messaging.getMessageFields = function (mid, fields, callback) {
|
||||
db.getObjectFields('message:' + mid, fields, callback);
|
||||
};
|
||||
|
||||
Messaging.setMessageField = function (mid, field, content, callback) {
|
||||
db.setObjectField('message:' + mid, field, content, callback);
|
||||
};
|
||||
|
||||
Messaging.setMessageFields = function (mid, data, callback) {
|
||||
db.setObject('message:' + mid, data, callback);
|
||||
};
|
||||
|
||||
Messaging.getMessages = function (params, callback) {
|
||||
Messaging.getMessages = function (params, callback) {
|
||||
var uid = params.uid;
|
||||
var roomId = params.roomId;
|
||||
var isNew = params.isNew || false;
|
||||
@@ -50,7 +31,7 @@ var userNotifications = require('./user/notifications');
|
||||
var indices = {};
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
canGetMessages(params.callerUid, params.uid, next);
|
||||
canGet('filter:messaging.canGetMessages', params.callerUid, params.uid, next);
|
||||
},
|
||||
function (canGet, next) {
|
||||
if (!canGet) {
|
||||
@@ -78,126 +59,19 @@ var userNotifications = require('./user/notifications');
|
||||
next(null, messageData);
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
function canGetMessages(callerUid, uid, callback) {
|
||||
plugins.fireHook('filter:messaging.canGetMessages', {
|
||||
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.getMessagesData = function (mids, uid, roomId, isNew, callback) {
|
||||
|
||||
var keys = mids.map(function (mid) {
|
||||
return 'message:' + mid;
|
||||
});
|
||||
|
||||
var messages;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObjects(keys, next);
|
||||
},
|
||||
function (_messages, next) {
|
||||
messages = _messages.map(function (msg, idx) {
|
||||
if (msg) {
|
||||
msg.messageId = parseInt(mids[idx], 10);
|
||||
}
|
||||
return msg;
|
||||
}).filter(Boolean);
|
||||
|
||||
var uids = messages.map(function (msg) {
|
||||
return msg && msg.fromuid;
|
||||
});
|
||||
|
||||
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], next);
|
||||
},
|
||||
function (users, next) {
|
||||
messages.forEach(function (message, index) {
|
||||
message.fromUser = users[index];
|
||||
var self = parseInt(message.fromuid, 10) === parseInt(uid, 10);
|
||||
message.self = self ? 1 : 0;
|
||||
message.timestampISO = utils.toISOString(message.timestamp);
|
||||
message.newSet = false;
|
||||
message.roomId = String(message.roomId || roomId);
|
||||
if (message.hasOwnProperty('edited')) {
|
||||
message.editedISO = new Date(parseInt(message.edited, 10)).toISOString();
|
||||
}
|
||||
});
|
||||
|
||||
async.map(messages, function (message, next) {
|
||||
Messaging.parse(message.content, message.fromuid, uid, roomId, isNew, function (err, result) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
message.content = result;
|
||||
message.cleanedContent = S(result).stripTags().decodeHTMLEntities().s;
|
||||
next(null, message);
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
function (messages, next) {
|
||||
if (messages.length > 1) {
|
||||
// Add a spacer in between messages with time gaps between them
|
||||
messages = messages.map(function (message, index) {
|
||||
// Compare timestamps with the previous message, and check if a spacer needs to be added
|
||||
if (index > 0 && parseInt(message.timestamp, 10) > parseInt(messages[index - 1].timestamp, 10) + (1000 * 60 * 5)) {
|
||||
// If it's been 5 minutes, this is a new set of messages
|
||||
message.newSet = true;
|
||||
} else if (index > 0 && message.fromuid !== messages[index - 1].fromuid) {
|
||||
// If the previous message was from the other person, this is also a new set
|
||||
message.newSet = true;
|
||||
}
|
||||
|
||||
return message;
|
||||
});
|
||||
|
||||
next(undefined, messages);
|
||||
} else if (messages.length === 1) {
|
||||
// For single messages, we don't know the context, so look up the previous message and compare
|
||||
var key = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
|
||||
async.waterfall([
|
||||
async.apply(db.sortedSetRank, key, messages[0].messageId),
|
||||
function (index, next) {
|
||||
// Continue only if this isn't the first message in sorted set
|
||||
if (index > 0) {
|
||||
db.getSortedSetRange(key, index - 1, index - 1, next);
|
||||
} else {
|
||||
messages[0].newSet = true;
|
||||
return next(undefined, messages);
|
||||
}
|
||||
},
|
||||
function (mid, next) {
|
||||
Messaging.getMessageFields(mid, ['fromuid', 'timestamp'], next);
|
||||
}
|
||||
], function (err, fields) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (
|
||||
(parseInt(messages[0].timestamp, 10) > parseInt(fields.timestamp, 10) + (1000 * 60 * 5)) ||
|
||||
(parseInt(messages[0].fromuid, 10) !== parseInt(fields.fromuid, 10))
|
||||
) {
|
||||
// If it's been 5 minutes, this is a new set of messages
|
||||
messages[0].newSet = true;
|
||||
}
|
||||
|
||||
next(undefined, messages);
|
||||
});
|
||||
} else {
|
||||
next(null, []);
|
||||
}
|
||||
}
|
||||
], callback);
|
||||
|
||||
};
|
||||
|
||||
Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) {
|
||||
Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) {
|
||||
plugins.fireHook('filter:parse.raw', message, function (err, parsed) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
@@ -217,9 +91,9 @@ var userNotifications = require('./user/notifications');
|
||||
callback(err, messageData ? messageData.parsedMessage : '');
|
||||
});
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
Messaging.isNewSet = function (uid, roomId, timestamp, callback) {
|
||||
Messaging.isNewSet = function (uid, roomId, timestamp, callback) {
|
||||
var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
|
||||
|
||||
async.waterfall([
|
||||
@@ -234,13 +108,13 @@ var userNotifications = require('./user/notifications');
|
||||
}
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
Messaging.getRecentChats = function (callerUid, uid, start, stop, callback) {
|
||||
Messaging.getRecentChats = function (callerUid, uid, start, stop, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
canGetRecentChats(callerUid, uid, next);
|
||||
canGet('filter:messaging.canGetRecentChats', callerUid, uid, next);
|
||||
},
|
||||
function (canGet, next) {
|
||||
if (!canGet) {
|
||||
@@ -299,28 +173,18 @@ var userNotifications = require('./user/notifications');
|
||||
next(null, {rooms: results.roomData, nextStart: stop + 1});
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
Messaging.generateUsernames = function (users, excludeUid) {
|
||||
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(', ');
|
||||
};
|
||||
};
|
||||
|
||||
function canGetRecentChats(callerUid, uid, callback) {
|
||||
plugins.fireHook('filter:messaging.canGetRecentChats', {
|
||||
callerUid: callerUid,
|
||||
uid: uid,
|
||||
canGet: parseInt(callerUid, 10) === parseInt(uid, 10)
|
||||
}, function (err, data) {
|
||||
callback(err, data ? data.canGet : false);
|
||||
});
|
||||
}
|
||||
|
||||
Messaging.getTeaser = function (uid, roomId, callback) {
|
||||
Messaging.getTeaser = function (uid, roomId, callback) {
|
||||
var teaser;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
@@ -349,9 +213,9 @@ var userNotifications = require('./user/notifications');
|
||||
next(null, teaser);
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
Messaging.canMessageUser = function (uid, toUid, callback) {
|
||||
Messaging.canMessageUser = function (uid, toUid, callback) {
|
||||
if (parseInt(meta.config.disableChat) === 1 || !uid || uid === toUid) {
|
||||
return callback(new Error('[[error:chat-disabled]]'));
|
||||
}
|
||||
@@ -393,9 +257,9 @@ var userNotifications = require('./user/notifications');
|
||||
next(new Error('[[error:chat-restricted]]'));
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
Messaging.canMessageRoom = function (uid, roomId, callback) {
|
||||
Messaging.canMessageRoom = function (uid, roomId, callback) {
|
||||
if (parseInt(meta.config.disableChat) === 1 || !uid) {
|
||||
return callback(new Error('[[error:chat-disabled]]'));
|
||||
}
|
||||
@@ -430,9 +294,9 @@ var userNotifications = require('./user/notifications');
|
||||
next();
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
};
|
||||
|
||||
Messaging.hasPrivateChat = function (uid, withUid, callback) {
|
||||
Messaging.hasPrivateChat = function (uid, withUid, callback) {
|
||||
if (parseInt(uid, 10) === parseInt(withUid, 10)) {
|
||||
return callback(null, 0);
|
||||
}
|
||||
@@ -474,7 +338,4 @@ var userNotifications = require('./user/notifications');
|
||||
});
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
|
||||
|
||||
}(exports));
|
||||
};
|
||||
|
||||
136
src/messaging/data.js
Normal file
136
src/messaging/data.js
Normal file
@@ -0,0 +1,136 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var S = require('string');
|
||||
|
||||
var db = require('../database');
|
||||
var user = require('../user');
|
||||
var utils = require('../../public/src/utils');
|
||||
|
||||
module.exports = function (Messaging) {
|
||||
|
||||
Messaging.getMessageField = function (mid, field, callback) {
|
||||
Messaging.getMessageFields(mid, [field], function (err, fields) {
|
||||
callback(err, fields ? fields[field] : null);
|
||||
});
|
||||
};
|
||||
|
||||
Messaging.getMessageFields = function (mid, fields, callback) {
|
||||
db.getObjectFields('message:' + mid, fields, callback);
|
||||
};
|
||||
|
||||
Messaging.setMessageField = function (mid, field, content, callback) {
|
||||
db.setObjectField('message:' + mid, field, content, callback);
|
||||
};
|
||||
|
||||
Messaging.setMessageFields = function (mid, data, callback) {
|
||||
db.setObject('message:' + mid, data, callback);
|
||||
};
|
||||
|
||||
Messaging.getMessagesData = function (mids, uid, roomId, isNew, callback) {
|
||||
|
||||
var messages;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
var keys = mids.map(function (mid) {
|
||||
return 'message:' + mid;
|
||||
});
|
||||
|
||||
db.getObjects(keys, next);
|
||||
},
|
||||
function (_messages, next) {
|
||||
messages = _messages.map(function (msg, idx) {
|
||||
if (msg) {
|
||||
msg.messageId = parseInt(mids[idx], 10);
|
||||
}
|
||||
return msg;
|
||||
}).filter(Boolean);
|
||||
|
||||
var uids = messages.map(function (msg) {
|
||||
return msg && msg.fromuid;
|
||||
});
|
||||
|
||||
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], next);
|
||||
},
|
||||
function (users, next) {
|
||||
messages.forEach(function (message, index) {
|
||||
message.fromUser = users[index];
|
||||
var self = parseInt(message.fromuid, 10) === parseInt(uid, 10);
|
||||
message.self = self ? 1 : 0;
|
||||
message.timestampISO = utils.toISOString(message.timestamp);
|
||||
message.newSet = false;
|
||||
message.roomId = String(message.roomId || roomId);
|
||||
if (message.hasOwnProperty('edited')) {
|
||||
message.editedISO = new Date(parseInt(message.edited, 10)).toISOString();
|
||||
}
|
||||
});
|
||||
|
||||
async.map(messages, function (message, next) {
|
||||
Messaging.parse(message.content, message.fromuid, uid, roomId, isNew, function (err, result) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
message.content = result;
|
||||
message.cleanedContent = S(result).stripTags().decodeHTMLEntities().s;
|
||||
next(null, message);
|
||||
});
|
||||
}, next);
|
||||
},
|
||||
function (messages, next) {
|
||||
if (messages.length > 1) {
|
||||
// Add a spacer in between messages with time gaps between them
|
||||
messages = messages.map(function (message, index) {
|
||||
// Compare timestamps with the previous message, and check if a spacer needs to be added
|
||||
if (index > 0 && parseInt(message.timestamp, 10) > parseInt(messages[index - 1].timestamp, 10) + (1000 * 60 * 5)) {
|
||||
// If it's been 5 minutes, this is a new set of messages
|
||||
message.newSet = true;
|
||||
} else if (index > 0 && message.fromuid !== messages[index - 1].fromuid) {
|
||||
// If the previous message was from the other person, this is also a new set
|
||||
message.newSet = true;
|
||||
}
|
||||
|
||||
return message;
|
||||
});
|
||||
|
||||
next(undefined, messages);
|
||||
} else if (messages.length === 1) {
|
||||
// For single messages, we don't know the context, so look up the previous message and compare
|
||||
var key = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
|
||||
async.waterfall([
|
||||
async.apply(db.sortedSetRank, key, messages[0].messageId),
|
||||
function (index, next) {
|
||||
// Continue only if this isn't the first message in sorted set
|
||||
if (index > 0) {
|
||||
db.getSortedSetRange(key, index - 1, index - 1, next);
|
||||
} else {
|
||||
messages[0].newSet = true;
|
||||
return next(undefined, messages);
|
||||
}
|
||||
},
|
||||
function (mid, next) {
|
||||
Messaging.getMessageFields(mid, ['fromuid', 'timestamp'], next);
|
||||
}
|
||||
], function (err, fields) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
if (
|
||||
(parseInt(messages[0].timestamp, 10) > parseInt(fields.timestamp, 10) + (1000 * 60 * 5)) ||
|
||||
(parseInt(messages[0].fromuid, 10) !== parseInt(fields.fromuid, 10))
|
||||
) {
|
||||
// If it's been 5 minutes, this is a new set of messages
|
||||
messages[0].newSet = true;
|
||||
}
|
||||
|
||||
next(undefined, messages);
|
||||
});
|
||||
} else {
|
||||
next(null, []);
|
||||
}
|
||||
}
|
||||
], callback);
|
||||
};
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user