v1.18.x
barisusakli 11 years ago
parent 131b71f361
commit 3a69a191ff

@ -51,9 +51,9 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
}); });
socket.on('event:chats.receive', function(data) { socket.on('event:chats.receive', function(data) {
if (module.modalExists(data.uid)) { if (module.modalExists(data.withUid)) {
var modal = module.getModal(data.uid); var modal = module.getModal(data.withUid);
module.appendChatMessage(modal, data.message, data.timestamp); module.appendChatMessage(modal, data.message);
if (modal.is(":visible")) { if (modal.is(":visible")) {
module.bringModalToTop(modal); module.bringModalToTop(modal);
@ -65,16 +65,16 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
} }
if (!modal.is(":visible") || !app.isFocused) { if (!modal.is(":visible") || !app.isFocused) {
app.alternatingTitle(data.username + ' has messaged you'); app.alternatingTitle(data.message.user.username + ' has messaged you');
} }
} else { } 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); 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'); sounds.play('chat-incoming');
} }
}); });
@ -203,7 +203,7 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
function getChatMessages(chatModal, callback) { function getChatMessages(chatModal, callback) {
socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) { socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) {
for(var i = 0; i<messages.length; ++i) { for(var i = 0; i<messages.length; ++i) {
module.appendChatMessage(chatModal, messages[i].content, messages[i].timestamp); module.appendChatMessage(chatModal, messages[i]);
} }
callback(); callback();
}); });
@ -226,19 +226,34 @@ define(['taskbar', 'string', 'sounds'], function(taskbar, S, sounds) {
var msg = S(chatModal.find('#chat-message-input').val()).stripTags().s; var msg = S(chatModal.find('#chat-message-input').val()).stripTags().s;
if(msg.length) { if(msg.length) {
msg = msg +'\n'; 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(''); chatModal.find('#chat-message-input').val('');
sounds.play('chat-outgoing'); sounds.play('chat-outgoing');
} }
} }
module.appendChatMessage = function(chatModal, message, timestamp) { module.appendChatMessage = function(chatModal, data) {
var chatContent = chatModal.find('#chat-content'); var chatContent = chatModal.find('#chat-content');
var time = '<span class="chat-timestamp pull-right timeago" title="' + utils.toISOString(timestamp) + '"></span> '; var isYou = parseInt(app.uid, 10) === parseInt(data.fromuid, 10);
message = $('<li class="chat-message">' + S(message + time).stripTags('p').s + '</li>');
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('img:not(".chat-user-image")').addClass('img-responsive');
message.find('span.timeago').timeago(); message.find('span.timeago').timeago();

@ -18,7 +18,7 @@ var db = require('./database'),
db.incrObjectField('global', 'nextMid', function(err, mid) { db.incrObjectField('global', 'nextMid', function(err, mid) {
if (err) { if (err) {
return callback(err, null); return callback(err);
} }
var message = { var message = {
@ -28,68 +28,71 @@ var db = require('./database'),
touid: touid touid: touid
}; };
db.setObject('message:' + mid, message); db.setObject('message:' + mid, message, function(err) {
db.listAppend('messages:' + uids[0] + ':' + uids[1], mid); if (err) {
return callback(err);
}
Messaging.updateChatTime(fromuid, touid); db.listAppend('messages:' + uids[0] + ':' + uids[1], mid);
Messaging.updateChatTime(touid, fromuid);
callback(null, message); 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); var uids = sortUids(fromuid, touid);
db.getListRange('messages:' + uids[0] + ':' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) { db.getListRange('messages:' + uids[0] + ':' + uids[1], -((meta.config.chatMessagesToDisplay || 50) - 1), -1, function(err, mids) {
if (err) { if (err) {
return callback(err, null); return callback(err);
} }
if (!mids || !mids.length) { if (!mids || !mids.length) {
return callback(null, []); return callback(null, []);
} }
user.getMultipleUserFields([fromuid, touid], ['username', 'userslug', 'picture'], function(err, userData) { getMessages(mids, fromuid, touid, isNew, callback);
if(err) { });
return callback(err, null); };
}
userData[0].uid = touid; function getMessages(mids, fromuid, touid, isNew, callback) {
userData[1].uid = fromuid; user.getMultipleUserFields([fromuid, touid], ['uid', 'username', 'userslug', 'picture'], function(err, userData) {
if(err) {
return callback(err);
}
function getMessage(mid, next) { var keys = mids.map(function(mid) {
db.getObject('message:' + mid, function(err, message) { return 'message:' + mid;
if (err) { });
return next(err);
}
Messaging.parse(message.content, message.fromuid, fromuid, userData[1], userData[0], false, function(result) { db.getObjects(keys, function(err, messages) {
message.content = result; if (err) {
next(null, message); return callback(err);
});
});
} }
async.map(mids, getMessage, callback); 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) { Messaging.parse = function (message, fromuid, myuid, toUserData, myUserData, isNew, callback) {
plugins.fireHook('filter:post.parse', message, function(err, parsed) { plugins.fireHook('filter:post.parse', message, function(err, parsed) {
if (err) { if (err) {
return callback(message); 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 = { var messageData = {
message: message, message: message,
@ -99,7 +102,7 @@ var db = require('./database'),
toUserData: toUserData, toUserData: toUserData,
myUserData: myUserData, myUserData: myUserData,
isNew: isNew, isNew: isNew,
parsedMessage: picture + '<strong>' + username + '</strong><br/>' + parsed parsedMessage: parsed
}; };
plugins.fireHook('filter:messaging.parse', messageData, function(err, messageData) { 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) { callback(null, _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) {
return parseInt(replyObj.tid, 10) === parseInt(tid, 10); return parseInt(replyObj.tid, 10) === parseInt(tid, 10);
}).map(function(replyObj) { }).map(function(replyObj) {
return replyObj.uid return replyObj.uid;
})); }));
} };
/* Chat */ /* Chat */
@ -153,10 +153,10 @@ SocketModules.chats.get = function(socket, data, callback) {
return callback(new Error('invalid data')); 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) { if(!data) {
return callback(new Error('invalid data')); return callback(new Error('invalid data'));
} }
@ -168,60 +168,45 @@ SocketModules.chats.send = function(socket, data) {
var msg = S(data.message).stripTags().s; var msg = S(data.message).stripTags().s;
user.getMultipleUserFields([socket.uid, touid], ['username', 'userslug', 'picture'], function(err, usersData) { Messaging.addMessage(socket.uid, touid, msg, function(err, message) {
if(err) { if (err) {
return; return callback(err);
} }
var username = usersData[0].username, sendChatNotification(socket.uid, touid, message.user.username);
toUsername = usersData[1].username,
finalMessage = username + ' : ' + msg,
notifText = 'New message from <strong>' + username + '</strong>';
if (!module.parent.exports.isUserOnline(touid)) {
notifications.create({
text: notifText,
path: 'javascript:app.openChat(&apos;' + username + '&apos;, ' + 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) { server.getUserSockets(socket.uid).forEach(function(s) {
s.emit('event:chats.receive', { s.emit('event:chats.receive', {
uid: touid, withUid: touid,
fromUid: socket.uid, message: message
username: toUsername,
message: parsed,
timestamp: Date.now()
});
});
}); });
}); });
}); });
}; };
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(&apos;' + username + '&apos;, ' + fromuid + ');',
uniqueId: 'notification_' + fromuid + '_' + touid,
from: fromuid
}, function(nid) {
notifications.push(nid, [touid], function(success) {
});
});
}
}
SocketModules.chats.list = function(socket, data, callback) { SocketModules.chats.list = function(socket, data, callback) {
Messaging.getRecentChats(socket.uid, callback); Messaging.getRecentChats(socket.uid, callback);
}; };

Loading…
Cancel
Save