Moved left_channel function to functions file, and sending icon for leaving/joining channel also

This commit is contained in:
Kasper Rynning-Tønnesen
2018-09-20 15:31:37 +02:00
parent ef369f6ca6
commit 7466ef7f61
3 changed files with 54 additions and 50 deletions

View File

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

View File

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

View File

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