v1.18.x
Baris Usakli 12 years ago
parent 25550e18d0
commit 0a485a7ff6

@ -287,12 +287,14 @@
var username = $(this).parents('li').attr('data-username');
var touid = $(this).parents('li').attr('data-uid');
require(['chat'], function(chat){
if(username === app.username || !app.username)
return;
require(['chat'], function(chat) {
var chatModal = chat.createModalIfDoesntExist(username, touid);
chatModal.show();
chat.bringModalToTop(chatModal);
});
});
ajaxify.register_events([

@ -1,8 +1,7 @@
define(['taskbar'], function(taskbar) {
var module = {};
module.bringModalToTop = function(chatModal) {
var topZ = 0;
$('.modal').each(function() {
@ -11,7 +10,7 @@ define(['taskbar'], function(taskbar) {
topZ = thisZ;
}
});
chatModal.css('zIndex', topZ+1);
chatModal.css('zIndex', topZ + 1);
}
module.createModalIfDoesntExist = function(username, touid) {
@ -39,7 +38,9 @@ define(['taskbar'], function(taskbar) {
module.bringModalToTop(chatModal);
});
addSendHandler(chatModal, touid);
addSendHandler(chatModal, touid);
getChatMessages(chatModal, touid);
}
taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username});
@ -58,6 +59,15 @@ define(['taskbar'], function(taskbar) {
taskbar.minimize('chat', uuid);
}
function getChatMessages(chatModal, touid) {
socket.emit('getChatMessages', {touid:touid}, function(messages) {
console.log(messages);
for(var i = 0; i<messages.length; ++i) {
module.appendChatMessage(chatModal, messages[i].content);
}
});
}
function addSendHandler(chatModal, touid) {
chatModal.find('#chat-message-input').off('keypress');
chatModal.find('#chat-message-input').on('keypress', function(e) {

@ -0,0 +1,77 @@
var RDB = require('./redis'),
async = require('async');
(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);
RDB.incr('global:next_message_id', function(err, mid) {
if(err)
return callback(err, null);
console.log('creating message', mid);
var message = {
content: content,
timestamp: Date.now(),
fromuid: fromuid,
touid: touid
};
RDB.hmset('message:' + mid, message);
RDB.rpush('messages:' + uids[0] + ':' + uids[1], mid);
callback(null, message);
});
}
Messaging.getMessages = function(fromuid, touid, callback) {
var uids = sortUids(fromuid, touid);
RDB.lrange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) {
if(err)
return callback(err, null);
if(!mids || !mids.length) {
return callback(null, []);
}
user.getUserField(touid, 'username', function(err, tousername) {
var messages = [];
function getMessage(mid, next) {
RDB.hgetall('message:' + mid, function(err, message) {
if(err)
return next(err);
if(message.fromuid === fromuid)
message.content = 'You : ' + message.content;
else
message.content = tousername + ' : ' + message.content;
messages.push(message);
next(null);
});
}
async.eachSeries(mids, getMessage, function(err) {
if(err)
return callback(err, null);
callback(null, messages);
});
});
});
}
}(exports));

@ -506,9 +506,22 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
});
});
socket.on('getChatMessages', function(data, callback) {
var touid = data.touid;
require('./messaging').getMessages(uid, touid, function(err, messages) {
if(err)
return callback(null);
callback(messages);
});
});
socket.on('sendChatMessage', function(data) {
var touid = data.touid;
if(touid === uid || uid === 0) {
return;
}
if(userSockets[touid]) {
var msg = utils.strip_tags(data.message),
@ -521,11 +534,15 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }),
userSockets[touid][x].emit('chatMessage', {fromuid:uid, username:username, message:finalMessage});
}
notifications.create(finalMessage, 5, '#', 'notification_'+uid+'_'+touid, function(nid) {
notifications.create(finalMessage, 5, '#', 'notification_' + uid + '_' + touid, function(nid) {
notifications.push(nid, [touid], function(success) {
});
});
require('./messaging').addMessage(uid, touid, msg, function(err, message) {
});
});
}
});

Loading…
Cancel
Save