diff --git a/server/handlers/chat.js b/server/handlers/chat.js index e09ae18c..9e56e4a5 100644 --- a/server/handlers/chat.js +++ b/server/handlers/chat.js @@ -301,13 +301,41 @@ function generate_name(guid, announce_payload, second, round) { } function get_name(guid, announce_payload, first) { + if(!announce_payload.announce && announce_payload.hasOwnProperty("socket")) { + Functions.getSessionChatPass(Functions.getSession(announce_payload.socket), function(name, pass) { + if(name == "" || pass == "") { + get_name_generate(guid, announce_payload, first); + return; + } + db.collection("registered_users").find({"_id": name.toLowerCase()}, function(err, docs) { + if(docs[0].password == Functions.hash_pass(Functions.decrypt_string(pass))) { + var icon = false; + if(docs[0].icon) { + icon = docs[0].icon; + } + Functions.setSessionChatPass(Functions.getSession(announce_payload.socket), name.toLowerCase(), pass, function() { + }); + db.collection("user_names").update({"guid": guid}, {$set: {name: name, icon: icon}}, {upsert: true}, function(err, docs) { + db.collection("user_names").update({"_id": "all_names"}, {$addToSet: {names: name}}, function(err, docs) { + name = name; + }); + }); + } + }); + }); + } else { + get_name_generate(guid, announce_payload, first); + } +} + +function get_name_generate(guid, announce_payload, first) { db.collection("user_names").find({"guid": guid}, function(err, docs) { if(docs.length == 0) { generate_name(guid, announce_payload); } else { name = docs[0].name; } - }) + }); } module.exports.get_history = get_history; diff --git a/server/handlers/functions.js b/server/handlers/functions.js index 36b9b415..5c752a48 100644 --- a/server/handlers/functions.js +++ b/server/handlers/functions.js @@ -38,7 +38,7 @@ function remove_unique_id(short_id) { db.collection("unique_ids").update({"_id": "unique_ids"}, {$pull: {unique_ids: short_id}}, function(err, docs) {}); } -function remove_name_from_db(guid, name) { +function remove_name_from_db(guid) { // Use temporary, with caution. Can bottleneck in large quantity of users. // // Find a way of indexing users in lists in a clever way, to avoid the search here diff --git a/server/handlers/io.js b/server/handlers/io.js index d1377b0b..e86e0141 100644 --- a/server/handlers/io.js +++ b/server/handlers/io.js @@ -48,7 +48,7 @@ module.exports = function() { var in_list = false; var name = ""; var short_id; - Chat.get_name(guid, {announce: false}); + Chat.get_name(guid, {announce: false, socket: socket}); var offline = false; var chromecast_object = false; @@ -273,7 +273,7 @@ module.exports = function() { }); }); } - Functions.remove_name_from_db(guid, name); + Functions.remove_name_from_db(guid); }); } diff --git a/server/handlers/list.js b/server/handlers/list.js index fcf9ccde..70075e32 100644 --- a/server/handlers/list.js +++ b/server/handlers/list.js @@ -754,7 +754,7 @@ function getNextSong(coll, socket, callback) { function left_channel(coll, guid, short_id, in_list, socket, change) { if(!coll) { if(!change) { - Functions.remove_name_from_db(guid, name); + Functions.remove_name_from_db(guid); } return; } @@ -772,7 +772,7 @@ function left_channel(coll, guid, short_id, in_list, socket, change) { io.to(coll).emit("viewers", new_doc[0].users.length); socket.leave(coll); if(!change) { - Functions.remove_name_from_db(guid, name); + Functions.remove_name_from_db(guid); } }); }); @@ -782,7 +782,7 @@ function left_channel(coll, guid, short_id, in_list, socket, change) { //if(updated.nModified > 0) { db.collection("connected_users").update({"_id": "total_users"}, {$pull: {total_users: guid + coll}}, function(err, updated){}); if(!change) { - Functions.remove_name_from_db(guid, name); + Functions.remove_name_from_db(guid); } //} });