mirror of
				https://github.com/KevinMidboe/zoff.git
				synced 2025-10-29 18:00:23 +00:00 
			
		
		
		
	Merge pull request #278 from zoff-music/fix/offline-listeners-fix
Fix for offline-listeners overflooding total viewers
This commit is contained in:
		| @@ -5,7 +5,7 @@ function frontpage_lists(msg, socket) { | ||||
|  | ||||
|     db.collection("frontpage_lists").find({frontpage:true}, function(err, docs){ | ||||
|         db.collection("connected_users").find({"_id": "total_users"}, function(err, tot){ | ||||
|             socket.compress(true).emit("playlists", {channels: docs, viewers: tot[0].total_users}); | ||||
|             socket.compress(true).emit("playlists", {channels: docs, viewers: tot[0].total_users.length}); | ||||
|         }); | ||||
|     }); | ||||
| } | ||||
|   | ||||
| @@ -41,15 +41,20 @@ function check_inlist(coll, guid, socket, offline) | ||||
|                                 socket.broadcast.to(coll).emit('chat', {from: docs[0].name, msg: " joined"}); | ||||
|                             } | ||||
|                         }); | ||||
|                         db.collection("connected_users").update({"_id": "total_users"}, {$inc: {total_users: 1}}, function(err, docs){}); | ||||
|                         db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){}); | ||||
|                     }); | ||||
|                 }); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|     } else { | ||||
|         if(offline) { | ||||
|             db.collection("connected_users").update({"_id": "offline_users"}, {$addToSet: {users: guid}}, function(err, docs){}); | ||||
|         } else { | ||||
|             db.collection("connected_users").update({"_id": coll}, {$addToSet: {users: guid}}, function(err, docs){}); | ||||
|         db.collection("connected_users").update({"_id": "total_users"}, {$inc: {total_users: 1}}, function(err, docs) {}); | ||||
|         } | ||||
|         // | ||||
|         db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {}); | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -34,7 +34,7 @@ module.exports = function() { | ||||
|                 }); | ||||
|             } | ||||
|  | ||||
|             db.collection("connected_users").update({"_id": "total_users"}, {$inc: {total_users: 1}}, {upsert: true}, function(err, docs){}); | ||||
|             db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + channel}}, {upsert: true}, function(err, docs){}); | ||||
|         }); | ||||
|  | ||||
|         socket.on('chromecast', function(msg) { | ||||
| @@ -76,9 +76,9 @@ module.exports = function() { | ||||
|                 db.collection("connected_users").find({"_id": "offline_users"}, function(err, off) { | ||||
|                     db.collection("connected_users").find({"_id": {$ne: "total_users"}, "_id": {$ne: "offline_users"}}, function(err, users_list) { | ||||
|                         if(tot.length > 0 && off.length == 0) { | ||||
|                             socket.emit("spread_listeners", {offline: 0, total: tot[0].total_users, online_users: users_list}); | ||||
|                             socket.emit("spread_listeners", {offline: 0, total: tot[0].total_users.length, online_users: users_list}); | ||||
|                         } else if(tot.length > 0 && off.length > 0){ | ||||
|                             socket.emit("spread_listeners", {offline: off[0].users.length, total: tot[0].total_users, online_users: users_list}); | ||||
|                             socket.emit("spread_listeners", {offline: off[0].users.length, total: tot[0].total_users.length, online_users: users_list}); | ||||
|                         } | ||||
|                     }); | ||||
|                 }); | ||||
| @@ -117,6 +117,7 @@ module.exports = function() { | ||||
|                 offline = true; | ||||
|                 if(channel != "") coll = channel; | ||||
|                 if(coll !== undefined) { | ||||
|  | ||||
|                     db.collection("connected_users").findAndModify({ | ||||
|                         query: {"_id": coll}, | ||||
|                         update: {$pull: {users: guid}}, | ||||
| @@ -129,10 +130,10 @@ module.exports = function() { | ||||
|                             } | ||||
|                             io.to(coll).emit("viewers", num); | ||||
|                             db.collection("frontpage_lists").update({"_id": coll, "viewers": {$gt: 0}}, {$inc: {viewers: -1}}, function(err, docs) { }); | ||||
|                             db.collection("connected_users").update({"_id": "total_users", total_users: {$gt: 0}}, {$inc: {total_users: -1}}, function(err, docs){ | ||||
|                             db.collection("connected_users").update({"_id": "total_users"}, {$pull: {total_users: guid + coll}}, function(err, docs){ | ||||
|                                 db.collection("connected_users").update({"_id": "offline_users"}, {$addToSet: {users: guid}}, function(err, docs) { | ||||
|                                     if(docs.nModified == 1) { | ||||
|                                         db.collection("connected_users").update({"_id": "total_users"}, {$inc: {total_users: 1}}, function(err, docs) {}); | ||||
|                                         db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {}); | ||||
|                                     } | ||||
|                                 }); | ||||
|                             }); | ||||
| @@ -146,7 +147,7 @@ module.exports = function() { | ||||
|                 offline = false; | ||||
|                 db.collection("connected_users").update({"_id": "offline_users"}, {$pull: {users: guid}}, function(err, docs) { | ||||
|                     if(docs.n && docs.n == 1) { | ||||
|                         db.collection("connected_users").update({"_id": "total_users", "total_users": {$gt: 0}}, {$inc: {total_users: -1}}, function(err, docs){}); | ||||
|                         db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + channel}}, function(err, docs){}); | ||||
|                     } | ||||
|                     Functions.check_inlist(coll, guid, socket, offline); | ||||
|                 }); | ||||
| @@ -194,6 +195,10 @@ module.exports = function() { | ||||
|             } catch(e) { | ||||
|                 return; | ||||
|             } | ||||
|  | ||||
|             if(msg.hasOwnProperty("offline") && msg.offline) { | ||||
|                 offline = true; | ||||
|             } | ||||
|             List.list(msg, guid, coll, offline, socket); | ||||
|             Functions.get_short_id(socket); | ||||
|         }); | ||||
|   | ||||
| @@ -500,7 +500,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); | ||||
|                 }); | ||||
|                 db.collection("connected_users").update({"_id": "total_users", total_users: {$gt: 0}}, {$inc: {total_users: -1}}, function(err, updated){}); | ||||
|                 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); | ||||
|   | ||||
| @@ -145,5 +145,5 @@ db.collection("unique_ids").update({"_id": "unique_ids"}, {$set: {unique_ids: [] | ||||
| db.collection("user_names").remove({"guid": {$exists: true}}, {multi: true, upsert: true}, function(err, docs){}); | ||||
| db.collection("user_names").update({"_id": "all_names"}, {$set: {names: []}}, {multi: true, upsert: true}, function(err, docs){}); | ||||
| db.collection("connected_users").update({users: {$exists: true}}, {$set: {users: []}}, {multi: true, upsert: true}, function(err, docs){}); | ||||
| db.collection("connected_users").update({"_id": "total_users"}, {$set: {total_users: 0}}, {multi: true, upsert: true}, function(err, docs) {}); | ||||
| db.collection("connected_users").update({"_id": "total_users"}, {$set: {total_users: []}}, {multi: true, upsert: true}, function(err, docs) {}); | ||||
| db.collection("frontpage_lists").update({viewers: {$ne: 0}}, {$set: {"viewers": 0}}, {multi: true, upsert: true}, function(err, docs) {}); | ||||
|   | ||||
| @@ -153,9 +153,6 @@ $().ready(function(){ | ||||
|     socket.on("connect", function(){ | ||||
|         if(connect_error){ | ||||
|             connect_error = false; | ||||
|             $(".connect_error").fadeOut(function(){ | ||||
|                 $(".connect_error").remove(); | ||||
|                 Materialize.toast("Connected!", 2000, "green lighten"); | ||||
|             if(offline) { | ||||
|                 socket.emit("offline", {status: true, channel: chan != undefined ? chan.toLowerCase() : ""}); | ||||
|             } | ||||
| @@ -167,6 +164,9 @@ $().ready(function(){ | ||||
|                     Chat.namechange(conf_arr.name + " " + conf_arr.chat_pass, true); | ||||
|                 }, 100); //to take height for delay on establishing connection | ||||
|             } | ||||
|             $(".connect_error").fadeOut(function(){ | ||||
|                 $(".connect_error").remove(); | ||||
|                 Materialize.toast("Connected!", 2000, "green lighten"); | ||||
|             }); | ||||
|         } | ||||
|  | ||||
| @@ -746,7 +746,7 @@ function get_list_listener(){ | ||||
|     socket.on("get_list", function(){ | ||||
|         var add = ""; | ||||
|         if(private_channel) add = Crypt.getCookie("_uI") + "_"; | ||||
|         socket.emit("list", {version: parseInt(localStorage.getItem("VERSION")), channel: add + chan.toLowerCase(), pass: embed ? '' : Crypt.crypt_pass(Crypt.get_userpass(chan.toLowerCase()))}); | ||||
|         socket.emit("list", { offline: offline, version: parseInt(localStorage.getItem("VERSION")), channel: add + chan.toLowerCase(), pass: embed ? '' : Crypt.crypt_pass(Crypt.get_userpass(chan.toLowerCase()))}); | ||||
|     }); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -10,7 +10,7 @@ router.route('/api/frontpages').get(function(req, res) { | ||||
|     db.collection("frontpage_lists").find({frontpage: true, count: {$gt: 0}}, function(err, docs) { | ||||
|         db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) { | ||||
|             res.setHeader('Content-Type', 'application/json'); | ||||
|             res.send(JSON.stringify({channels: docs, viewers: tot[0].total_users})); | ||||
|             res.send(JSON.stringify({channels: docs, viewers: tot[0].total_users.length})); | ||||
|         }); | ||||
|     }); | ||||
| }); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user