Merge pull request #367 from zoff-music/feature/chat-name-persist

More persistent chat-names, and server handling of chat-names instead…
This commit is contained in:
Kasper Rynning-Tønnesen
2018-08-28 14:34:11 +02:00
committed by GitHub
2 changed files with 102 additions and 83 deletions

View File

@@ -87,7 +87,8 @@ function chat(msg, guid, offline, socket) {
db.collection(coll + "_settings").find(function(err, docs){ db.collection(coll + "_settings").find(function(err, docs){
if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64")))) { if(docs.length > 0 && (docs[0].userpass == undefined || docs[0].userpass == "" || (msg.hasOwnProperty('pass') && docs[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64")))) {
var data = msg.data; var data = msg.data;
Functions.check_inlist(coll, guid, socket, offline);
Functions.check_inlist(coll, guid, socket, offline, function() {
if(data !== "" && data !== undefined && data !== null && if(data !== "" && data !== undefined && data !== null &&
data.length < 151 && data.replace(/\s/g, '').length){ data.length < 151 && data.replace(/\s/g, '').length){
db.collection("user_names").find({"guid": guid}, function(err, docs) { db.collection("user_names").find({"guid": guid}, function(err, docs) {
@@ -105,6 +106,7 @@ function chat(msg, guid, offline, socket) {
} }
}); });
} }
});
} else { } else {
socket.emit('auth_required'); socket.emit('auth_required');
} }
@@ -133,7 +135,7 @@ function all_chat(msg, guid, offline, socket) {
var data = msg.data; var data = msg.data;
coll = Functions.removeEmojis(coll).toLowerCase(); coll = Functions.removeEmojis(coll).toLowerCase();
//coll = filter.clean(coll); //coll = filter.clean(coll);
Functions.check_inlist(coll, guid, socket, offline); Functions.check_inlist(coll, guid, socket, offline, function() {
if(data !== "" && data !== undefined && data !== null && if(data !== "" && data !== undefined && data !== null &&
data.length < 151 && data.replace(/\s/g, '').length){ data.length < 151 && data.replace(/\s/g, '').length){
db.collection("user_names").find({"guid": guid}, function(err, docs) { db.collection("user_names").find({"guid": guid}, function(err, docs) {
@@ -151,9 +153,10 @@ function all_chat(msg, guid, offline, socket) {
} }
}); });
} }
});
} }
function namechange(data, guid, socket, tried) { function namechange(data, guid, socket, tried, callback) {
/*if(!data.hasOwnProperty("channel") || /*if(!data.hasOwnProperty("channel") ||
typeof(data.channel) != "string") return;*/ typeof(data.channel) != "string") return;*/
var pw = ""; var pw = "";
@@ -195,7 +198,8 @@ function namechange(data, guid, socket, tried) {
} }
accepted_password = true; accepted_password = true;
Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.password, function() { Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.password, function() {
db.collection("registered_users").update({"_id": name.toLowerCase()}, {$set: {password: Functions.hash_pass(password)}}, {upsert: true}, function() {}); db.collection("registered_users").update({"_id": name.toLowerCase()}, {$set: {password: Functions.hash_pass(password)}}, {upsert: true}, function() {
});
}); });
} else if(docs[0].password == Functions.hash_pass(password)) { } else if(docs[0].password == Functions.hash_pass(password)) {
if(docs[0].icon) { if(docs[0].icon) {
@@ -204,7 +208,9 @@ function namechange(data, guid, socket, tried) {
accepted_password = true; accepted_password = true;
if(new_password) { if(new_password) {
Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.new_password, function() { Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.new_password, function() {
db.collection("registered_users").update({"_id": name.toLowerCase(), password: Functions.hash_pass(password)}, {$set: {password: Functions.hash_pass(new_password)}}, function() {}); db.collection("registered_users").update({"_id": name.toLowerCase(), password: Functions.hash_pass(password)}, {$set: {password: Functions.hash_pass(new_password)}}, function() {
});
}); });
} else { } else {
Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.password, function() { Functions.setSessionChatPass(Functions.getSession(socket), name.toLowerCase(), data.password, function() {
@@ -213,18 +219,23 @@ function namechange(data, guid, socket, tried) {
} }
if(accepted_password) { if(accepted_password) {
db.collection("user_names").find({"guid": guid}, function(err, names) { db.collection("user_names").find({"guid": guid}, function(err, names) {
if(names.length > 0) { if(names.length > 0 || (docs.length != 0 && docs[0].password == Functions.hash_pass(password))) {
var no_name = false;
if(names.length == 0) no_name = true;
if(!no_name) {
var old_name = names[0].name; var old_name = names[0].name;
db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: old_name}}, function() {}); db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: old_name}}, function() {});
db.collection("user_names").update({"guid": guid}, {$set: {name: name, icon: icon}}, function(err, docs) { }
db.collection("user_names").update({"guid": guid}, {$set: {name: name, icon: icon}}, {upsert: true}, function(err, docs) {
db.collection("user_names").update({"_id": "all_names"}, {$addToSet: {names: name}}, function(err, docs) { db.collection("user_names").update({"_id": "all_names"}, {$addToSet: {names: name}}, function(err, docs) {
//socket.emit('name', {type: "name", accepted: true}); //socket.emit('name', {type: "name", accepted: true});
if(old_name != name && !first) { if(old_name != name && !first && !no_name) {
if(data.hasOwnProperty("channel") && typeof(data.channel) == "string") { if(data.hasOwnProperty("channel") && typeof(data.channel) == "string") {
io.to(data.channel).emit('chat', {from: old_name, msg: " changed name to " + name}); io.to(data.channel).emit('chat', {from: old_name, msg: " changed name to " + name});
io.sockets.emit('chat.all', {from: old_name , msg: " changed name to " + name, channel: data.channel}); io.sockets.emit('chat.all', {from: old_name , msg: " changed name to " + name, channel: data.channel});
} }
} }
if(callback != undefined && typeof(callback) == "function") callback();
}); });
}); });
} else { } else {

View File

@@ -14,6 +14,8 @@ var uniqid = require('uniqid');
var Filter = require('bad-words'); var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'}); var filter = new Filter({ placeHolder: 'x'});
var Chat = require(pathThumbnails + '/handlers/chat.js');
function encodeChannelName(str) { function encodeChannelName(str) {
var _fn = encodeURIComponent; var _fn = encodeURIComponent;
str = filter.clean(str); str = filter.clean(str);
@@ -107,7 +109,7 @@ function get_short_id(socket) {
socket.emit("id", new_short_id); socket.emit("id", new_short_id);
} }
function check_inlist(coll, guid, socket, offline) function check_inlist(coll, guid, socket, offline, callback)
{ {
if(coll == undefined) return; if(coll == undefined) return;
@@ -122,18 +124,23 @@ function check_inlist(coll, guid, socket, offline)
} else { } else {
io.to(coll).emit("viewers", new_doc[0].users.length); io.to(coll).emit("viewers", new_doc[0].users.length);
} }
Chat.namechange({initial: true, first:true, channel: coll}, guid, socket, false, function() {
db.collection("user_names").find({"guid": guid}, function(err, docs) { db.collection("user_names").find({"guid": guid}, function(err, docs) {
if(docs.length == 1) { if(docs.length == 1) {
socket.broadcast.to(coll).emit('chat', {from: docs[0].name, msg: " joined"}); socket.broadcast.to(coll).emit('chat', {from: docs[0].name, msg: " joined"});
} }
}); });
db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){}); db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){
if(callback != undefined && typeof(callback) == "function") callback();
});
});
}); });
}); });
} else { } else {
db.collection("connected_users").find({"_id": coll}, function(err, new_doc) { db.collection("connected_users").find({"_id": coll}, function(err, new_doc) {
io.to(coll).emit("viewers", new_doc[0].users.length); io.to(coll).emit("viewers", new_doc[0].users.length);
}); });
if(callback != undefined && typeof(callback) == "function") callback();
} }
}); });
@@ -147,6 +154,7 @@ function check_inlist(coll, guid, socket, offline)
if(coll != undefined && coll != "") { if(coll != undefined && coll != "") {
db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {}); db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs) {});
} }
if(callback != undefined && typeof(callback) == "function") callback();
} }
} }