Fix for offline-listeners overflooding total viewers

This commit is contained in:
Kasper Rynning-Tønnesen
2018-01-29 13:57:54 +01:00
parent a6e87696b8
commit 05980fe33c
7 changed files with 35 additions and 25 deletions

View File

@@ -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});
});
});
}

View File

@@ -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 {
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) {});
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"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {});
}
}

View File

@@ -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);
});

View File

@@ -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);

View File

@@ -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) {});

View File

@@ -153,20 +153,20 @@ $().ready(function(){
socket.on("connect", function(){
if(connect_error){
connect_error = false;
if(offline) {
socket.emit("offline", {status: true, channel: chan != undefined ? chan.toLowerCase() : ""});
}
if(chan != undefined && (Crypt.get_pass(chan.toLowerCase()) !== undefined && Crypt.get_pass(chan.toLowerCase()) !== "")){
socket.emit("password", {password: Crypt.crypt_pass(Crypt.get_pass(chan.toLowerCase())), channel: chan.toLowerCase()});
}
if(chan != undefined && conf_arr.name !== undefined && conf_arr.name !== "" && conf_arr.chat_pass !== undefined && conf_arr.chat_pass !== ""){
setTimeout(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");
if(offline) {
socket.emit("offline", {status: true, channel: chan != undefined ? chan.toLowerCase() : ""});
}
if(chan != undefined && (Crypt.get_pass(chan.toLowerCase()) !== undefined && Crypt.get_pass(chan.toLowerCase()) !== "")){
socket.emit("password", {password: Crypt.crypt_pass(Crypt.get_pass(chan.toLowerCase())), channel: chan.toLowerCase()});
}
if(chan != undefined && conf_arr.name !== undefined && conf_arr.name !== "" && conf_arr.chat_pass !== undefined && conf_arr.chat_pass !== ""){
setTimeout(function() {
Chat.namechange(conf_arr.name + " " + conf_arr.chat_pass, true);
}, 100); //to take height for delay on establishing connection
}
});
}
@@ -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()))});
});
}

View File

@@ -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}));
});
});
});