mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 20:16:04 +01:00
closes #1285
This commit is contained in:
@@ -51,9 +51,9 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
|
||||
});
|
||||
|
||||
socket.on('event:chats.receive', function(data) {
|
||||
if (module.modalExists(data.uid)) {
|
||||
var modal = module.getModal(data.uid);
|
||||
module.appendChatMessage(modal, data.message, data.timestamp);
|
||||
if (module.modalExists(data.withUid)) {
|
||||
var modal = module.getModal(data.withUid);
|
||||
module.appendChatMessage(modal, data.message);
|
||||
|
||||
if (modal.is(":visible")) {
|
||||
module.bringModalToTop(modal);
|
||||
@@ -65,16 +65,16 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
|
||||
}
|
||||
|
||||
if (!modal.is(":visible") || !app.isFocused) {
|
||||
app.alternatingTitle(data.username + ' has messaged you');
|
||||
app.alternatingTitle(data.message.user.username + ' has messaged you');
|
||||
}
|
||||
} else {
|
||||
module.createModal(data.username, data.uid, function(modal) {
|
||||
module.createModal(data.message.user.username, data.withUid, function(modal) {
|
||||
module.toggleNew(modal.attr('UUID'), true);
|
||||
app.alternatingTitle(data.username + ' has messaged you');
|
||||
app.alternatingTitle(data.message.user.username + ' has messaged you');
|
||||
});
|
||||
}
|
||||
|
||||
if (parseInt(app.uid, 10) !== parseInt(data.fromUid, 10)) {
|
||||
if (parseInt(app.uid, 10) !== parseInt(data.message.fromuid, 10)) {
|
||||
sounds.play('chat-incoming');
|
||||
}
|
||||
});
|
||||
@@ -203,7 +203,7 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
|
||||
function getChatMessages(chatModal, callback) {
|
||||
socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) {
|
||||
for(var i = 0; i<messages.length; ++i) {
|
||||
module.appendChatMessage(chatModal, messages[i].content, messages[i].timestamp);
|
||||
module.appendChatMessage(chatModal, messages[i]);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
@@ -226,19 +226,34 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
|
||||
var msg = S(chatModal.find('#chat-message-input').val()).stripTags().s;
|
||||
if(msg.length) {
|
||||
msg = msg +'\n';
|
||||
socket.emit('modules.chats.send', { touid:chatModal.touid, message:msg});
|
||||
socket.emit('modules.chats.send', {touid:chatModal.touid, message:msg});
|
||||
chatModal.find('#chat-message-input').val('');
|
||||
sounds.play('chat-outgoing');
|
||||
}
|
||||
}
|
||||
|
||||
module.appendChatMessage = function(chatModal, message, timestamp) {
|
||||
module.appendChatMessage = function(chatModal, data) {
|
||||
var chatContent = chatModal.find('#chat-content');
|
||||
|
||||
var time = '<span class="chat-timestamp pull-right timeago" title="' + utils.toISOString(timestamp) + '"></span> ';
|
||||
message = $('<li class="chat-message">' + S(message + time).stripTags('p').s + '</li>');
|
||||
var isYou = parseInt(app.uid, 10) === parseInt(data.fromuid, 10);
|
||||
|
||||
message.toggleClass('chat-message-them', !message.find('.chat-user-you').length);
|
||||
var message = $('<li class="chat-message" data-uid="' + data.fromuid + '"></li>');
|
||||
var time = '<span class="chat-timestamp pull-right timeago" title="' + utils.toISOString(data.timestamp) + '"></span> ';
|
||||
|
||||
|
||||
if (data.fromuid !== chatContent.children().last().attr('data-uid')) {
|
||||
var userPicture = $('<a href="/user/' + data.user.userslug + '"><img class="chat-user-image" src="' + data.user.picture + '"></a>');
|
||||
var userName = $('<strong><span class="chat-user"> '+ data.user.username + '</span></strong>');
|
||||
userName.toggleClass('chat-user-you', isYou);
|
||||
|
||||
message.append(userPicture)
|
||||
.append(userName)
|
||||
.append('<br/>');
|
||||
}
|
||||
|
||||
message.append(S(data.content + time).stripTags('p').s);
|
||||
|
||||
message.toggleClass('chat-message-them', !isYou);
|
||||
message.find('img:not(".chat-user-image")').addClass('img-responsive');
|
||||
message.find('span.timeago').timeago();
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ var db = require('./database'),
|
||||
|
||||
db.incrObjectField('global', 'nextMid', function(err, mid) {
|
||||
if (err) {
|
||||
return callback(err, null);
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var message = {
|
||||
@@ -28,68 +28,71 @@ var db = require('./database'),
|
||||
touid: touid
|
||||
};
|
||||
|
||||
db.setObject('message:' + mid, message);
|
||||
db.listAppend('messages:' + uids[0] + ':' + uids[1], mid);
|
||||
db.setObject('message:' + mid, message, function(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
Messaging.updateChatTime(fromuid, touid);
|
||||
Messaging.updateChatTime(touid, fromuid);
|
||||
callback(null, message);
|
||||
db.listAppend('messages:' + uids[0] + ':' + uids[1], mid);
|
||||
|
||||
Messaging.updateChatTime(fromuid, touid);
|
||||
Messaging.updateChatTime(touid, fromuid);
|
||||
|
||||
getMessages([mid], fromuid, touid, true, function(err, messages) {
|
||||
callback(err, messages ? messages[0] : null);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Messaging.getMessages = function(fromuid, touid, callback) {
|
||||
Messaging.getMessages = function(fromuid, touid, isNew, callback) {
|
||||
var uids = sortUids(fromuid, touid);
|
||||
|
||||
db.getListRange('messages:' + uids[0] + ':' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) {
|
||||
if (err) {
|
||||
return callback(err, null);
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if (!mids || !mids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
user.getMultipleUserFields([fromuid, touid], ['username', 'userslug', 'picture'], function(err, userData) {
|
||||
if(err) {
|
||||
return callback(err, null);
|
||||
}
|
||||
|
||||
userData[0].uid = touid;
|
||||
userData[1].uid = fromuid;
|
||||
|
||||
function getMessage(mid, next) {
|
||||
db.getObject('message:' + mid, function(err, message) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
Messaging.parse(message.content, message.fromuid, fromuid, userData[1], userData[0], false, function(result) {
|
||||
message.content = result;
|
||||
next(null, message);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async.map(mids, getMessage, callback);
|
||||
});
|
||||
getMessages(mids, fromuid, touid, isNew, callback);
|
||||
});
|
||||
};
|
||||
|
||||
function getMessages(mids, fromuid, touid, isNew, callback) {
|
||||
user.getMultipleUserFields([fromuid, touid], ['uid', 'username', 'userslug', 'picture'], function(err, userData) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var keys = mids.map(function(mid) {
|
||||
return 'message:' + mid;
|
||||
});
|
||||
|
||||
db.getObjects(keys, function(err, messages) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
async.map(messages, function(message, next) {
|
||||
message.user = parseInt(message.fromuid, 10) === parseInt(fromuid, 10) ? userData[0] : userData[1];
|
||||
|
||||
Messaging.parse(message.content, message.fromuid, fromuid, userData[1], userData[0], isNew, function(result) {
|
||||
message.content = result;
|
||||
next(null, message);
|
||||
});
|
||||
}, callback);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Messaging.parse = function (message, fromuid, myuid, toUserData, myUserData, isNew, callback) {
|
||||
plugins.fireHook('filter:post.parse', message, function(err, parsed) {
|
||||
if (err) {
|
||||
return callback(message);
|
||||
}
|
||||
var username,
|
||||
picture;
|
||||
|
||||
if (parseInt(fromuid, 10) === parseInt(myuid, 10)) {
|
||||
picture = '<a href="/user/' + myUserData.userslug + '"><img class="chat-user-image" src="' + myUserData.picture + '"></a>';
|
||||
username = '<span class="chat-user chat-user-you"> '+ myUserData.username + '</span> ';
|
||||
} else {
|
||||
picture = '<a href="/user/' + toUserData.userslug + '"><img class="chat-user-image" src="' + toUserData.picture + '"></a>';
|
||||
username = '<span class="chat-user"> ' + toUserData.username + '</span> ';
|
||||
}
|
||||
|
||||
var messageData = {
|
||||
message: message,
|
||||
@@ -99,7 +102,7 @@ var db = require('./database'),
|
||||
toUserData: toUserData,
|
||||
myUserData: myUserData,
|
||||
isNew: isNew,
|
||||
parsedMessage: picture + '<strong>' + username + '</strong><br/>' + parsed
|
||||
parsedMessage: parsed
|
||||
};
|
||||
|
||||
plugins.fireHook('filter:messaging.parse', messageData, function(err, messageData) {
|
||||
|
||||
@@ -142,9 +142,9 @@ SocketModules.composer.getUsersByTid = function(socket, tid, callback) {
|
||||
callback(null, _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) {
|
||||
return parseInt(replyObj.tid, 10) === parseInt(tid, 10);
|
||||
}).map(function(replyObj) {
|
||||
return replyObj.uid
|
||||
return replyObj.uid;
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
||||
/* Chat */
|
||||
|
||||
@@ -153,10 +153,10 @@ SocketModules.chats.get = function(socket, data, callback) {
|
||||
return callback(new Error('invalid data'));
|
||||
}
|
||||
|
||||
Messaging.getMessages(socket.uid, data.touid, callback);
|
||||
Messaging.getMessages(socket.uid, data.touid, false, callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.send = function(socket, data) {
|
||||
SocketModules.chats.send = function(socket, data, callback) {
|
||||
if(!data) {
|
||||
return callback(new Error('invalid data'));
|
||||
}
|
||||
@@ -168,60 +168,45 @@ SocketModules.chats.send = function(socket, data) {
|
||||
|
||||
var msg = S(data.message).stripTags().s;
|
||||
|
||||
user.getMultipleUserFields([socket.uid, touid], ['username', 'userslug', 'picture'], function(err, usersData) {
|
||||
if(err) {
|
||||
return;
|
||||
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var username = usersData[0].username,
|
||||
toUsername = usersData[1].username,
|
||||
finalMessage = username + ' : ' + msg,
|
||||
notifText = 'New message from <strong>' + username + '</strong>';
|
||||
sendChatNotification(socket.uid, touid, message.user.username);
|
||||
|
||||
if (!module.parent.exports.isUserOnline(touid)) {
|
||||
notifications.create({
|
||||
text: notifText,
|
||||
path: 'javascript:app.openChat('' + username + '', ' + socket.uid + ');',
|
||||
uniqueId: 'notification_' + socket.uid + '_' + touid,
|
||||
from: socket.uid
|
||||
}, function(nid) {
|
||||
notifications.push(nid, [touid], function(success) {
|
||||
|
||||
});
|
||||
server.getUserSockets(touid).forEach(function(s) {
|
||||
s.emit('event:chats.receive', {
|
||||
withUid: socket.uid,
|
||||
message: message
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
usersData[0].uid = socket.uid;
|
||||
usersData[1].uid = touid;
|
||||
|
||||
Messaging.parse(msg, socket.uid, socket.uid, usersData[1], usersData[0], true, function(parsed) {
|
||||
Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
|
||||
|
||||
|
||||
server.getUserSockets(touid).forEach(function(s) {
|
||||
s.emit('event:chats.receive', {
|
||||
uid: socket.uid,
|
||||
fromUid: socket.uid,
|
||||
username: username,
|
||||
message: parsed,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
});
|
||||
|
||||
server.getUserSockets(socket.uid).forEach(function(s) {
|
||||
s.emit('event:chats.receive', {
|
||||
uid: touid,
|
||||
fromUid: socket.uid,
|
||||
username: toUsername,
|
||||
message: parsed,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
});
|
||||
server.getUserSockets(socket.uid).forEach(function(s) {
|
||||
s.emit('event:chats.receive', {
|
||||
withUid: touid,
|
||||
message: message
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function sendChatNotification(fromuid, touid, username) {
|
||||
if (!module.parent.exports.isUserOnline(touid)) {
|
||||
var notifText = 'New message from <strong>' + username + '</strong>';
|
||||
notifications.create({
|
||||
text: notifText,
|
||||
path: 'javascript:app.openChat('' + username + '', ' + fromuid + ');',
|
||||
uniqueId: 'notification_' + fromuid + '_' + touid,
|
||||
from: fromuid
|
||||
}, function(nid) {
|
||||
notifications.push(nid, [touid], function(success) {
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
SocketModules.chats.list = function(socket, data, callback) {
|
||||
Messaging.getRecentChats(socket.uid, callback);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user