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,24 +87,26 @@ 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);
if(data !== "" && data !== undefined && data !== null && Functions.check_inlist(coll, guid, socket, offline, function() {
data.length < 151 && data.replace(/\s/g, '').length){ if(data !== "" && data !== undefined && data !== null &&
db.collection("user_names").find({"guid": guid}, function(err, docs) { data.length < 151 && data.replace(/\s/g, '').length){
if(docs.length == 1) { db.collection("user_names").find({"guid": guid}, function(err, docs) {
db.collection("registered_users").find({"_id": docs[0].name}, function(err, n) { if(docs.length == 1) {
var icon = false; db.collection("registered_users").find({"_id": docs[0].name}, function(err, n) {
if(n.length > 0 && n[0].icon) { var icon = false;
icon = n[0].icon; if(n.length > 0 && n[0].icon) {
} icon = n[0].icon;
db.collection("chat_logs").insert({ "createdAt": new Date(), all: false, channel: coll, from: docs[0].name, msg: ": " + data, icon: icon }); }
io.to(coll).emit('chat', {from: docs[0].name, msg: ": " + data, icon: icon}); db.collection("chat_logs").insert({ "createdAt": new Date(), all: false, channel: coll, from: docs[0].name, msg: ": " + data, icon: icon });
}); io.to(coll).emit('chat', {from: docs[0].name, msg: ": " + data, icon: icon});
} else if(docs.length == 0){ });
get_name(guid, {announce: false, channel: coll, message: data, all: false}); } else if(docs.length == 0){
} get_name(guid, {announce: false, channel: coll, message: data, all: false});
}); }
} });
}
});
} else { } else {
socket.emit('auth_required'); socket.emit('auth_required');
} }
@@ -133,27 +135,28 @@ 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) {
if(docs.length == 1) { if(docs.length == 1) {
db.collection("registered_users").find({"_id": docs[0].name}, function(err, n) { db.collection("registered_users").find({"_id": docs[0].name}, function(err, n) {
var icon = false; var icon = false;
if(n.length > 0 && n[0].icon) { if(n.length > 0 && n[0].icon) {
icon = n[0].icon; icon = n[0].icon;
} }
db.collection("chat_logs").insert({ "createdAt": new Date(), all: true, channel: coll, from: docs[0].name, msg: ": " + data, icon: icon }, function(err, docs) {}); db.collection("chat_logs").insert({ "createdAt": new Date(), all: true, channel: coll, from: docs[0].name, msg: ": " + data, icon: icon }, function(err, docs) {});
io.sockets.emit('chat.all', {from: docs[0].name, msg: ": " + data, channel: coll, icon: icon}); io.sockets.emit('chat.all', {from: docs[0].name, msg: ": " + data, channel: coll, icon: icon});
}); });
} else if(docs.length == 0) { } else if(docs.length == 0) {
get_name(guid, {announce: false, channel: coll, message: data, all: true}); get_name(guid, {announce: false, channel: coll, message: data, all: true});
} }
}); });
} }
});
} }
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 old_name = names[0].name; var no_name = false;
db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: old_name}}, function() {}); if(names.length == 0) no_name = true;
db.collection("user_names").update({"guid": guid}, {$set: {name: name, icon: icon}}, function(err, docs) { if(!no_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({"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,22 +14,24 @@ 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);
var toReturn = _fn(str); var toReturn = _fn(str);
toReturn = toReturn.replace(/_/g, "%5F"); toReturn = toReturn.replace(/_/g, "%5F");
toReturn = toReturn.replace(/%26amp%3B/g, "%26").replace(/%26amp%3b/g, "%26"); toReturn = toReturn.replace(/%26amp%3B/g, "%26").replace(/%26amp%3b/g, "%26");
toReturn = toReturn.toLowerCase(); toReturn = toReturn.toLowerCase();
return toReturn; return toReturn;
} }
function decodeChannelName(str) { function decodeChannelName(str) {
var _fn = decodeURIComponent; var _fn = decodeURIComponent;
str = str.toUpperCase(); str = str.toUpperCase();
var toReturn = _fn(str.replace(/%5F/g, "_")); var toReturn = _fn(str.replace(/%5F/g, "_"));
toReturn = filter.clean(toReturn); toReturn = filter.clean(toReturn);
return toReturn.toLowerCase(); return toReturn.toLowerCase();
} }
function remove_unique_id(short_id) { function remove_unique_id(short_id) {
@@ -41,31 +43,31 @@ function remove_name_from_db(guid, name) {
// //
// Find a way of indexing users in lists in a clever way, to avoid the search here // Find a way of indexing users in lists in a clever way, to avoid the search here
db.collection("connected_users").find({"_id": "total_users"}, function(err, all_users) { db.collection("connected_users").find({"_id": "total_users"}, function(err, all_users) {
var hasOne = all_users[0].total_users.some(function(v){ return v.indexOf(guid)>=0 }); var hasOne = all_users[0].total_users.some(function(v){ return v.indexOf(guid)>=0 });
if(!hasOne) { if(!hasOne) {
db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: name}}, function(err, updated) { db.collection("user_names").update({"_id": "all_names"}, {$pull: {names: name}}, function(err, updated) {
db.collection("user_names").remove({"guid": guid}, function(err, removed) { }); db.collection("user_names").remove({"guid": guid}, function(err, removed) { });
}); });
} }
}); });
} }
function isUrl(str) { function isUrl(str) {
var pattern = new RegExp("\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + var pattern = new RegExp("\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
"(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" + "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" +
"|mil|biz|info|mobi|name|aero|jobs|museum" + "|mil|biz|info|mobi|name|aero|jobs|museum" +
"|travel|[a-z]{2}))(:[\\d]{1,5})?" + "|travel|[a-z]{2}))(:[\\d]{1,5})?" +
"(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" + "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
"((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" + "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
"(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" + "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
"([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" + "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
"(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b"); "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
if(!pattern.test(str)) { if(!pattern.test(str)) {
return false; return false;
} else { } else {
return true; return true;
} }
} }
function getSession(socket) { function getSession(socket) {
@@ -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);
} }
db.collection("user_names").find({"guid": guid}, function(err, docs) { Chat.namechange({initial: true, first:true, channel: coll}, guid, socket, false, function() {
if(docs.length == 1) { db.collection("user_names").find({"guid": guid}, function(err, docs) {
socket.broadcast.to(coll).emit('chat', {from: docs[0].name, msg: " joined"}); if(docs.length == 1) {
} 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){
if(callback != undefined && typeof(callback) == "function") callback();
});
}); });
db.collection("connected_users").update({"_id": "total_users"}, {$addToSet: {total_users: guid + coll}}, function(err, docs){});
}); });
}); });
} 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();
} }
} }