From cb91587567984474f7168f319d4e8269264b8321 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 18 Jan 2016 15:35:24 +0200 Subject: [PATCH] closes #4063 --- public/src/client/account/header.js | 15 ++++++++--- src/messaging.js | 39 +++++++++++++++++++++++++++++ src/socket.io/modules.js | 6 +++++ 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/public/src/client/account/header.js b/public/src/client/account/header.js index 89ae4d5f2b..6d8166a819 100644 --- a/public/src/client/account/header.js +++ b/public/src/client/account/header.js @@ -1,5 +1,5 @@ 'use strict'; -/* globals define, app, ajaxify, socket, RELATIVE_PATH */ +/* globals define, app, config, ajaxify, socket, bootbox, translator */ define('forum/account/header', [ 'coverPhoto', @@ -30,7 +30,16 @@ define('forum/account/header', [ }); components.get('account/chat').on('click', function() { - app.newChat(theirid); + socket.emit('modules.chats.hasPrivateChat', theirid, function(err, roomId) { + if (err) { + return app.alertError(err.message); + } + if (roomId) { + app.openChat(roomId); + } else { + app.newChat(theirid); + } + }); }); components.get('account/ban').on('click', banAccount); @@ -65,7 +74,7 @@ define('forum/account/header', [ }, callback); }, function() { - uploader.open(RELATIVE_PATH + '/api/user/' + ajaxify.data.userslug + '/uploadcover', { uid: yourid }, 0, function(imageUrlOnServer) { + uploader.open(config.RELATIVE_PATH + '/api/user/' + ajaxify.data.userslug + '/uploadcover', { uid: yourid }, 0, function(imageUrlOnServer) { components.get('account/cover').css('background-image', 'url(' + imageUrlOnServer + '?v=' + Date.now() + ')'); }); }, diff --git a/src/messaging.js b/src/messaging.js index 444dfb2496..cd5d72e8ee 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -384,5 +384,44 @@ var async = require('async'), ], callback); }; + Messaging.hasPrivateChat = function(uid, withUid, callback) { + async.waterfall([ + function (next) { + async.parallel({ + myRooms: async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':chat:rooms', 0, -1), + theirRooms: async.apply(db.getSortedSetRevRange, 'uid:' + withUid + ':chat:rooms', 0, -1) + }, next); + }, + function (results, next) { + var roomIds = results.myRooms.filter(function(roomId) { + return roomId && results.theirRooms.indexOf(roomId) !== -1; + }); + + if (!roomIds.length) { + return callback(); + } + + var index = 0; + var roomId = 0; + async.whilst(function() { + return index < roomIds.length && !roomId; + }, function(next) { + Messaging.getUserCountInRoom(roomIds[index], function(err, count) { + if (err) { + return next(err); + } + if (count === 2) { + roomId = roomIds[index]; + next(null, roomId); + } else { + ++ index; + next(); + } + }); + }, next); + } + ], callback); + }; + }(exports)); diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js index 3d12f66ad0..4c20559823 100644 --- a/src/socket.io/modules.js +++ b/src/socket.io/modules.js @@ -294,6 +294,12 @@ SocketModules.chats.getRecentChats = function(socket, data, callback) { Messaging.getRecentChats(socket.uid, start, stop, callback); }; +SocketModules.chats.hasPrivateChat = function(socket, uid, callback) { + if (!socket.uid || !uid) { + return callback(null, new Error('[[error:invalid-data]]')); + } + Messaging.hasPrivateChat(socket.uid, uid, callback); +}; /* Sounds */ SocketModules.sounds.getSounds = function(socket, data, callback) {