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

121 lines
3.0 KiB
JavaScript

11 years ago
var db = require('./database'),
async = require('async'),
user = require('./user'),
plugins = require('./plugins');
meta = require('./meta');
12 years ago
(function(Messaging) {
function sortUids(fromuid, touid) {
var uids = [fromuid, touid];
uids.sort();
return uids;
}
Messaging.addMessage = function(fromuid, touid, content, callback) {
var uids = sortUids(fromuid, touid);
11 years ago
db.incrObjectField('global', 'nextMid', function(err, mid) {
if (err) {
12 years ago
return callback(err, null);
11 years ago
}
12 years ago
var message = {
content: content,
timestamp: Date.now(),
fromuid: fromuid,
touid: touid
};
11 years ago
db.setObject('message:' + mid, message);
db.listAppend('messages:' + uids[0] + ':' + uids[1], mid);
12 years ago
Messaging.updateChatTime(fromuid, touid);
Messaging.updateChatTime(touid, fromuid);
12 years ago
callback(null, message);
});
}
Messaging.getMessages = function(fromuid, touid, callback) {
var uids = sortUids(fromuid, touid);
db.getListRange('messages:' + uids[0] + ':' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) {
11 years ago
if (err) {
12 years ago
return callback(err, null);
11 years ago
}
12 years ago
if (!mids || !mids.length) {
12 years ago
return callback(null, []);
}
user.getUserField(touid, 'username', function(err, tousername) {
if(err) {
return callback(err, null);
}
12 years ago
var messages = [];
function getMessage(mid, next) {
11 years ago
db.getObject('message:' + mid, function(err, message) {
if (err) {
12 years ago
return next(err);
11 years ago
}
12 years ago
Messaging.parse(message.content, message.fromuid, fromuid, tousername, function(result) {
message.content = result;
messages.push(message);
next(null);
});
12 years ago
});
}
async.eachSeries(mids, getMessage, function(err) {
11 years ago
if (err) {
12 years ago
return callback(err, null);
11 years ago
}
12 years ago
callback(null, messages);
});
});
});
};
12 years ago
Messaging.parse = function (message, fromuid, myuid, tousername, callback) {
plugins.fireHook('filter:post.parse', message, function(err, parsed) {
if (err) {
return callback(message);
}
var username;
if (fromuid === myuid) {
username = "<span class='chat-user chat-user-you'>You</span>: ";
} else {
username = "<span class='chat-user'>" + tousername + "</span>: ";
}
var result = username + parsed;
callback(result);
});
};
Messaging.updateChatTime = function(uid, toUid, callback) {
db.sortedSetAdd('uid:' + uid + ':chats', Date.now(), toUid, function(err) {
if (callback) {
callback(err);
}
});
};
Messaging.getRecentChats = function(uid, callback) {
db.getSortedSetRevRange('uid:' + uid + ':chats', 0, 9, function(err, uids) {
if(err) {
return callback(err);
}
user.getMultipleUserFields(uids, ['username', 'picture', 'uid'], callback);
});
};
12 years ago
}(exports));