diff --git a/server/handlers/functions.js b/server/handlers/functions.js index a586444b..cbd4a9e6 100644 --- a/server/handlers/functions.js +++ b/server/handlers/functions.js @@ -133,7 +133,9 @@ function check_inlist(coll, guid, socket, offline, callback) Chat.namechange({initial: true, first:true, channel: coll}, guid, socket, false, function() { db.collection("user_names").find({"guid": guid}, function(err, docs) { if(docs.length == 1) { - socket.broadcast.to(coll).emit('chat', {from: docs[0].name, msg: " joined"}); + var icon = ""; + if(docs[0].icon != undefined) icon = docs[0].icon; + socket.broadcast.to(coll).emit('chat', {from: docs[0].name, icon: icon, msg: " joined"}); } }); db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){ @@ -363,6 +365,50 @@ function removeSessionAdminPass(id, channel, callback) { }); } +function left_channel(coll, guid, short_id, in_list, socket, change) { + if(!coll) { + if(!change) { + remove_name_from_db(guid); + } + return; + } + //coll = coll.replace(/ /g,''); + db.collection("connected_users").update({"_id": coll}, {$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){}); + db.collection("connected_users").find({"_id": coll}, function(err, new_doc){ + db.collection("frontpage_lists").update({"_id": coll, viewers: {$gt: 0}}, {$inc: {viewers: -1}}, function(err, doc) { + db.collection("user_names").find({"guid": guid}, function(err, docs) { + if(docs.length == 1) { + var icon = ""; + if(docs[0].icon != undefined) icon = docs[0].icon; + io.to(coll).emit('chat', {from: docs[0].name, icon: icon, msg: " left"}); + } + }); + io.to(coll).emit("viewers", new_doc[0].users.length); + socket.leave(coll); + if(!change) { + remove_name_from_db(guid); + } + }); + }); + + } else { + 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) { + remove_name_from_db(guid); + } + //} + }); + + } + }); + remove_unique_id(short_id); +} + +module.exports.left_channel = left_channel; module.exports.setChromecastHost = setChromecastHost; module.exports.decodeChannelName = decodeChannelName; module.exports.encodeChannelName = encodeChannelName; diff --git a/server/handlers/io.js b/server/handlers/io.js index 497b2cac..02c4d275 100644 --- a/server/handlers/io.js +++ b/server/handlers/io.js @@ -609,18 +609,18 @@ module.exports = function() { return; } } - List.left_channel(coll, guid, short_id, in_list, socket, true); + Functions.left_channel(coll, guid, short_id, in_list, socket, true); in_list = false; }); socket.on('disconnect', function() { - List.left_channel(coll, guid, short_id, in_list, socket, false); + Functions.left_channel(coll, guid, short_id, in_list, socket, false); }); socket.on('disconnected', function() { - List.left_channel(coll, guid, short_id, in_list, socket, false); + Functions.left_channel(coll, guid, short_id, in_list, socket, false); }); socket.on("left_channel", function(msg) { @@ -635,23 +635,23 @@ module.exports = function() { coll = msg.channel;//.replace(/ /g,''); coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); - List.left_channel(coll, guid, short_id, in_list, socket, false); + Functions.left_channel(coll, guid, short_id, in_list, socket, false); } }) socket.on('reconnect_failed', function() { - List.left_channel(coll, guid, short_id, in_list, socket, false); + Functions.left_channel(coll, guid, short_id, in_list, socket, false); }); socket.on('connect_timeout', function() { - List.left_channel(coll, guid, short_id, in_list, socket, false); + Functions.left_channel(coll, guid, short_id, in_list, socket, false); }); socket.on('error', function() { - List.left_channel(coll, guid, short_id, in_list, socket, false); + Functions.left_channel(coll, guid, short_id, in_list, socket, false); }); socket.on('pos', function(obj) diff --git a/server/handlers/list.js b/server/handlers/list.js index a93eade5..6f09b0c3 100644 --- a/server/handlers/list.js +++ b/server/handlers/list.js @@ -755,47 +755,6 @@ 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); - } - return; - } - //coll = coll.replace(/ /g,''); - db.collection("connected_users").update({"_id": coll}, {$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){}); - db.collection("connected_users").find({"_id": coll}, function(err, new_doc){ - db.collection("frontpage_lists").update({"_id": coll, viewers: {$gt: 0}}, {$inc: {viewers: -1}}, function(err, doc) { - db.collection("user_names").find({"guid": guid}, function(err, docs) { - if(docs.length == 1) { - io.to(coll).emit('chat', {from: docs[0].name, msg: " left"}); - } - }); - io.to(coll).emit("viewers", new_doc[0].users.length); - socket.leave(coll); - if(!change) { - Functions.remove_name_from_db(guid); - } - }); - }); - - } else { - 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); - } - //} - }); - - } - }); - Functions.remove_unique_id(short_id); -} - module.exports.sendColor = sendColor; module.exports.now_playing = now_playing; module.exports.list = list; @@ -806,4 +765,3 @@ module.exports.send_list = send_list; module.exports.end = end; module.exports.send_play = send_play; module.exports.getNextSong = getNextSong; -module.exports.left_channel = left_channel;