From 0c738ffd127b5d4e8469108b8f1a004da36d47cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Thu, 30 Aug 2018 14:54:46 +0200 Subject: [PATCH] Correctly remove name from users online on leave --- server/handlers/functions.js | 8 ++++++-- server/handlers/list.js | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/server/handlers/functions.js b/server/handlers/functions.js index 53b08711..36b9b415 100644 --- a/server/handlers/functions.js +++ b/server/handlers/functions.js @@ -45,8 +45,12 @@ function remove_name_from_db(guid, name) { db.collection("connected_users").find({"_id": "total_users"}, function(err, all_users) { var hasOne = all_users[0].total_users.some(function(v){ return v.indexOf(guid)>=0 }); if(!hasOne) { - db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: name}}, function(err, updated) { - db.collection("user_names").remove({"guid": guid}, function(err, removed) { }); + db.collection("user_names").find({"guid": guid}, function(err, user){ + if(user.length == 1){ + db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: user[0].name}}, function(err, updated) { + db.collection("user_names").remove({"guid": guid}, function(err, removed) { }); + }); + } }); } }); diff --git a/server/handlers/list.js b/server/handlers/list.js index f401f05a..fcf9ccde 100644 --- a/server/handlers/list.js +++ b/server/handlers/list.js @@ -771,6 +771,9 @@ 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); + } }); }); @@ -778,13 +781,13 @@ function left_channel(coll, guid, short_id, in_list, socket, change) { db.collection("connected_users").update({"_id": "offline_users"}, {$pull: {users: guid}}, function(err, updated){ //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); + } //} }); } - if(!change) { - Functions.remove_name_from_db(guid, name); - } }); Functions.remove_unique_id(short_id); }