From d8810ceae90c050eb9770a3e0d5eeff90cac3628 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 16 Dec 2015 15:09:14 +0200 Subject: [PATCH] send messages --- public/src/client/chats.js | 41 +--------------------------------- public/src/modules/chat.js | 13 +++++------ src/messaging/create.js | 3 +++ src/messaging/notifications.js | 5 ++--- src/messaging/unread.js | 3 ++- src/socket.io/modules.js | 14 ++++++++++-- 6 files changed, 25 insertions(+), 54 deletions(-) diff --git a/public/src/client/chats.js b/public/src/client/chats.js index 9064b2ef17..7f0f319f9f 100644 --- a/public/src/client/chats.js +++ b/public/src/client/chats.js @@ -34,14 +34,6 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', } }; - Chats.getRecipientUid = function() { - return parseInt($('.expanded-chat').attr('data-uid'), 10); - }; - - Chats.isCurrentChat = function(uid) { - return Chats.getRecipientUid() === parseInt(uid, 10); - }; - Chats.addEventListeners = function() { $('[component="chat/recent"]').on('click', '[component="chat/recent/room"]', function() { Chats.switchChat($(this).attr('data-roomid')); @@ -280,42 +272,11 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', Chats.addSocketListeners = function() { socket.on('event:chats.receive', function(data) { - if (Chats.isCurrentChat(data.withUid)) { + if (parseInt(data.roomId, 10) === parseInt(ajaxify.data.roomId, 10)) { newMessage = data.self === 0; data.message.self = data.self; Chats.appendChatMessage($('.expanded-chat .chat-content'), data.message); - } else { - var contactEl = $('[component="chat/recent"] li[data-uid="' + data.withUid + '"]'), - userKey = parseInt(data.withUid, 10) === parseInt(data.message.fromuid, 10) ? 'fromUser' : 'toUser'; - - // Spawn a new contact if required - templates.parse('partials/chat_contacts', { - contacts: [{ - uid: data.message[userKey].uid, - username: data.message[userKey].username, - status: data.message[userKey].status, - picture: data.message[userKey].picture, - 'icon:text': data.message[userKey]['icon:text'], - 'icon:bgColor': data.message[userKey]['icon:bgColor'], - teaser: { - content: data.message.cleanedContent, - timestampISO: new Date(Date.now()).toISOString() - } - }] - }, function(html) { - translator.translate(html, function(translatedHTML) { - if (contactEl.length) { - contactEl.replaceWith(translatedHTML); - } else { - $('[component="chat/recent"]').prepend(translatedHTML); - } - - // Mark that contact list entry unread - $('.chats-list li[data-uid="' + data.withUid + '"]').addClass('unread').find('.timeago').timeago(); - app.alternatingTitle('[[modules:chat.user_has_messaged_you, ' + data.message.fromUser.username + ']]'); - }); - }); } }); diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index a419583ced..949537aae4 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -20,14 +20,12 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra socket.on('event:chats.receive', function(data) { var username = data.message.fromUser.username; - var isSelf = parseInt(data.message.fromUser.uid, 10) === parseInt(app.user.uid, 10); + var isSelf = data.self === 1; data.message.self = data.self; - if (isSelf) { - username = data.message.toUser.username; - } + newMessage = data.self === 0; - if (module.modalExists(data.withUid)) { - var modal = module.getModal(data.withUid); + if (module.modalExists(data.roomId)) { + var modal = module.getModal(data.roomId); Chats.appendChatMessage(modal.find('.chat-content'), data.message); @@ -49,8 +47,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra } } else { module.createModal({ - username: username, - touid: data.withUid, + roomId: data.roomId, silent: true }, function(modal) { module.toggleNew(modal.attr('UUID'), true, true); diff --git a/src/messaging/create.js b/src/messaging/create.js index da9be535a8..f9ccae958b 100644 --- a/src/messaging/create.js +++ b/src/messaging/create.js @@ -21,6 +21,7 @@ module.exports = function(Messaging) { if (!inRoom) { return next(new Error('[[error:not-allowed]]')); } + Messaging.addMessage(uid, roomId, content, timestamp, next); } ], callback); @@ -34,11 +35,13 @@ module.exports = function(Messaging) { if (content.length > (meta.config.maximumChatMessageLength || 1000)) { return callback(new Error('[[error:chat-message-too-long]]')); } + callback(); }; Messaging.addMessage = function(fromuid, roomId, content, timestamp, callback) { var mid; var message; + async.waterfall([ function (next) { Messaging.checkContent(content, next); diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index 046c07c88e..c9aa10eb33 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -5,7 +5,7 @@ var nconf = require('nconf'); var user = require('../user'); var emailer = require('../emailer'); -var notifications = require('./notifications'); +var notifications = require('../notifications'); var meta = require('../meta'); var utils = require('../../public/src/utils'); var sockets = require('../socket.io'); @@ -26,7 +26,7 @@ module.exports = function(Messaging) { message: messageObj }; uids.forEach(function(uid) { - data.self = parseInt(uid, 10) === parseInt(fromUid) ? 1 : 0; + data.self = parseInt(uid, 10) === parseInt(fromUid) ? 1 : 0; Messaging.pushUnreadCount(uid); sockets.in('uid_' + uid).emit('event:chats.receive', data); }); @@ -99,7 +99,6 @@ module.exports = function(Messaging) { fromUserslug: utils.slugify(messageObj.fromUser.username) }, next); }, callback); - }); }); } diff --git a/src/messaging/unread.js b/src/messaging/unread.js index 6933d9c1fb..4bceaab3ce 100644 --- a/src/messaging/unread.js +++ b/src/messaging/unread.js @@ -36,7 +36,8 @@ module.exports = function(Messaging) { var keys = uids.map(function(uid) { return 'uid:' + uid + ':chat:rooms:unread'; }); - db.sortedSetAdd(keys, Date.now(), roomId, next); + + db.sortedSetsAdd(keys, Date.now(), roomId, next); } ], callback); }; diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index 575012955e..cfebbfb848 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -197,10 +197,20 @@ SocketModules.chats.userStopTyping = function(socket, data, callback) { }; function sendTypingNotification(event, socket, data, callback) { - if (!socket.uid || !data) { + if (!socket.uid || !data || !data.roomId) { return; } - server.in('uid_' + data.touid).emit(event, data.fromUid); + + Messaging.getUidsInRoom(data.roomId, 0, -1, function(err, uids) { + if (err) { + return callback(err); + } + uids.forEach(function(uid) { + if (socket.uid !== parseInt(uid, 10)) { + server.in('uid_' + uid).emit(event, data.fromUid); + } + }); + }); } SocketModules.chats.getRecentChats = function(socket, data, callback) {