Prettier JSON and less globally exposed variables

This commit is contained in:
Kasper Rynning-Tønnesen
2018-05-30 14:19:10 +02:00
parent 65f7f50c18
commit 3c40e8960c
11 changed files with 168 additions and 129 deletions

View File

@@ -1,3 +1,9 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
function get_history(channel, all, socket) {
var query = {};
if(all) {
@@ -67,7 +73,7 @@ function chat(msg, guid, offline, socket) {
return;
}
var coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass) {
if(userpass != "" || msg.pass == undefined) {
@@ -120,7 +126,7 @@ function all_chat(msg, guid, offline, socket) {
}
var coll = msg.channel.toLowerCase().replace(/ /g,'');
var data = msg.data;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.check_inlist(coll, guid, socket, offline);
if(data !== "" && data !== undefined && data !== null &&
@@ -268,11 +274,11 @@ function generate_name(guid, announce_payload, second) {
}
});
} else {
Chat.generate_name(guid, announce_payload, tmp_name);
generate_name(guid, announce_payload, tmp_name);
}
})
} else {
Chat.generate_name(guid, announce_payload, tmp_name);
generate_name(guid, announce_payload, tmp_name);
}
})
}
@@ -280,7 +286,7 @@ function generate_name(guid, announce_payload, second) {
function get_name(guid, announce_payload, first) {
db.collection("user_names").find({"guid": guid}, function(err, docs) {
if(docs.length == 0) {
Chat.generate_name(guid, announce_payload);
generate_name(guid, announce_payload);
} else {
name = docs[0].name;
}

View File

@@ -1,3 +1,5 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var db = require(pathThumbnails + '/handlers/db.js');
function frontpage_lists(msg, socket) {
if(msg == undefined || !msg.hasOwnProperty('version') || msg.version != VERSION || msg.version == undefined) {
var result = {

View File

@@ -8,6 +8,9 @@ try {
}
var mongojs = require('mongojs');
var connected_db = mongojs('mongodb://' + mongo_config.host + '/user_credentials');
var crypto = require('crypto');
var db = require(pathThumbnails + '/handlers/db.js');
var uniqid = require('uniqid');
function remove_unique_id(short_id) {
db.collection("unique_ids").update({"_id": "unique_ids"}, {$pull: {unique_ids: short_id}}, function(err, docs) {});
@@ -50,7 +53,7 @@ function getSession(socket) {
}
function remove_from_array(array, element){
if(Functions.contains(array, element)){
if(contains(array, element)){
var index = array.indexOf(element);
if(index != -1)
array.splice(index, 1);

View File

@@ -1,5 +1,19 @@
var cookieParser = require("cookie-parser");
var cookie = require("cookie");
var Functions = require(pathThumbnails + '/handlers/functions.js');
var ListChange = require(pathThumbnails + '/handlers/list_change.js');
var Chat = require(pathThumbnails + '/handlers/chat.js');
var List = require(pathThumbnails + '/handlers/list.js');
var Suggestions = require(pathThumbnails + '/handlers/suggestions.js');
var ListSettings = require(pathThumbnails + '/handlers/list_settings.js');
var Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
var Search = require(pathThumbnails + '/handlers/search.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
module.exports = function() {
io.on('connection', function(socket){
try {
@@ -68,7 +82,7 @@ module.exports = function() {
socketid = msg.socket_id;
socket.zoff_id = socketid;
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
if(coll.indexOf("?") > -1){
coll = coll.substring(0, coll.indexOf("?"));
@@ -96,7 +110,7 @@ module.exports = function() {
_list = _list.substring(0, _list.indexOf("?"));
msg.channel = _list;
}
coll = emojiStrip(_list).toLowerCase();
coll = Functions.removeEmojis(_list).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -189,7 +203,7 @@ module.exports = function() {
offline = true;
if(channel != "") coll = channel;
if(coll !== undefined) {
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
db.collection("connected_users").findAndModify({
@@ -314,7 +328,7 @@ module.exports = function() {
_list = _list.substring(0, _list.indexOf("?"));
msg.channel = _list;
}
coll = emojiStrip(_list).toLowerCase();
coll = Functions.removeEmojis(_list).toLowerCase();
coll = coll.replace(/_/g, "");
//
coll = filter.clean(coll);
@@ -339,7 +353,7 @@ module.exports = function() {
try {
coll = obj.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -368,7 +382,7 @@ module.exports = function() {
try {
coll = arr.list.replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -387,7 +401,7 @@ module.exports = function() {
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -408,7 +422,7 @@ module.exports = function() {
try {
coll = msg.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -461,7 +475,7 @@ module.exports = function() {
try {
coll = msg.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -485,7 +499,7 @@ module.exports = function() {
try {
coll = obj.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -514,7 +528,7 @@ module.exports = function() {
}
if(msg.hasOwnProperty("channel") && msg.channel != "" && typeof(msg.channel) == "string") {
coll = msg.channel.replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
List.left_channel(coll, guid, short_id, in_list, socket, false);
}
@@ -546,7 +560,7 @@ module.exports = function() {
try {
coll = obj.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);

View File

@@ -1,5 +1,12 @@
var ColorThief = require('color-thief-jimp');
var Jimp = require('jimp');
var Functions = require(pathThumbnails + '/handlers/functions.js');
var Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var request = require('request');
var db = require(pathThumbnails + '/handlers/db.js');
function now_playing(list, fn, socket) {
if(typeof(list) !== 'string' || typeof(fn) !== 'function') {
@@ -47,7 +54,7 @@ function list(msg, guid, coll, offline, socket) {
return;
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
var pass = crypto.createHash('sha256').update(Functions.decrypt_string(msg.pass)).digest("base64");
db.collection('frontpage_lists').find({"_id": coll}, function(err, frontpage_lists){
@@ -71,7 +78,7 @@ function list(msg, guid, coll, offline, socket) {
io.to(coll).emit("viewers", 1);
}
List.send_list(coll, socket, true, false, true);
send_list(coll, socket, true, false, true);
} else {
socket.emit("auth_required");
@@ -83,7 +90,7 @@ function list(msg, guid, coll, offline, socket) {
var configs = {"addsongs":false, "adminpass":"", "allvideos":true, "frontpage":true, "longsongs":false, "removeplay": false, "shuffle": true, "skip": false, "skips": [], "startTime":Functions.get_time(), "views": [], "vote": false, "desc": "", userpass: "", id: "config"};
db.collection(coll + "_settings").insert(configs, function(err, docs){
socket.join(coll);
List.send_list(coll, socket, true, false, true);
send_list(coll, socket, true, false, true);
db.collection("frontpage_lists").insert({"_id": coll, "count" : 0, "frontpage": true, "accessed": Functions.get_time(), "viewers": 1}, function(e,d){
});
Functions.check_inlist(coll, guid, socket, offline);
@@ -114,7 +121,7 @@ function skip(list, guid, coll, offline, socket) {
try {
coll = list.channel.toLowerCase().replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
@@ -187,7 +194,7 @@ function skip(list, guid, coll, offline, socket) {
(frontpage_viewers[0].viewers == 2 && docs[0].skips.length+1 == 2 && !Functions.contains(docs[0].skips, guid)) ||
(docs[0].adminpass == hash && docs[0].adminpass !== "" && docs[0].skip))
{
List.change_song(coll, error, video_id);
change_song(coll, error, video_id);
socket.emit("toast", "skip");
db.collection("user_names").find({"guid": guid}, function(err, docs) {
if(docs.length == 1) {
@@ -266,7 +273,7 @@ function change_song(coll, error, id, callback, socket) {
db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
List.change_song_post(coll, next_song, callback, socket);
change_song_post(coll, next_song, callback, socket);
if(!callback) {
io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true});
}
@@ -284,7 +291,7 @@ function change_song(coll, error, id, callback, socket) {
},{multi:true}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
if(docs.n >= 1) List.change_song_post(coll, next_song, callback, socket);
if(docs.n >= 1) change_song_post(coll, next_song, callback, socket);
});
});
}
@@ -295,7 +302,7 @@ function change_song(coll, error, id, callback, socket) {
db.collection(coll).remove({now_playing:true, id:id}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
List.change_song_post(coll, next_song, callback, socket);
change_song_post(coll, next_song, callback, socket);
if(!callback) {
io.to(coll).emit("channel", {type: "deleted", value: now_playing_doc[0].id, removed: true});
}
@@ -313,7 +320,7 @@ function change_song(coll, error, id, callback, socket) {
},{multi:true}, function(err, docs){
var next_song;
if(now_playing_doc.length == 2) next_song = now_playing_doc[1].id;
List.change_song_post(coll, next_song, callback, socket);
change_song_post(coll, next_song, callback, socket);
});
}
}
@@ -375,10 +382,10 @@ function change_song_post(coll, next_song, callback, socket) {
db.collection(coll + "_settings").find({id: "config"}, function(err, conf){
if(!callback) {
io.to(coll).emit("channel", {type: "song_change", time: Functions.get_time(), remove: conf[0].removeplay});
List.send_play(coll);
send_play(coll);
} else {
socket.to(coll).emit("channel", {type: "song_change", time: Functions.get_time(), remove: conf[0].removeplay});
List.send_play(coll, socket, true);
send_play(coll, socket, true);
callback();
}
Frontpage.update_frontpage(coll, docs[0].id, docs[0].title, docs[0].thumbnail, docs[0].source);
@@ -467,7 +474,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
}
}, function(err, returnDocs){
Frontpage.update_frontpage(coll, now_playing_doc[0].id, now_playing_doc[0].title, now_playing_doc[0].thumbnail, now_playing_doc[0].source);
List.send_list(coll, socket, send, list_send, configs, shuffled);
send_list(coll, socket, send, list_send, configs, shuffled);
});
});
}
@@ -492,8 +499,8 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
})
} else {
if(Functions.get_time()-conf[0].startTime > np_docs[0].duration){
List.change_song(coll, false, np_docs[0].id, function() {
List.send_list(coll, socket, send, list_send, configs, shuffled);
change_song(coll, false, np_docs[0].id, function() {
send_list(coll, socket, send, list_send, configs, shuffled);
}, socket);
} else {
if(list_send) {
@@ -502,9 +509,9 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
}
if(socket === undefined && send) {
List.send_play(coll);
send_play(coll);
} else if(send) {
List.send_play(coll, socket);
send_play(coll, socket);
}
}
}
@@ -516,9 +523,9 @@ function send_list(coll, socket, send, list_send, configs, shuffled)
socket.emit("channel", {type: "list", playlist: docs, shuffled: shuffled});
}
if(socket === undefined && send) {
List.send_play(coll);
send_play(coll);
} else if(send) {
List.send_play(coll, socket);
send_play(coll, socket);
}
}
});
@@ -591,13 +598,13 @@ function end(obj, coll, guid, offline, socket) {
if(docs[0].removeplay === true && startTime+parseInt(np[0].duration)<=Functions.get_time()+5)
{
db.collection(coll).remove({now_playing:true}, function(err, docs){
List.change_song_post(coll);
change_song_post(coll);
db.collection("frontpage_lists").update({_id:coll, count: {$gt: 0}}, {$inc:{count:-1}, $set:{accessed: Functions.get_time()}}, {upsert:true}, function(err, docs){});
});
}else{
if(startTime+parseInt(np[0].duration)<=Functions.get_time()+5)
{
List.change_song(coll, false, id);
change_song(coll, false, id);
}
}
});
@@ -626,7 +633,7 @@ function send_play(coll, socket, broadcast) {
if(err !== null) console.log(err);
try{
if(Functions.get_time()-conf[0].startTime > np[0].duration){
List.change_song(coll, false, np[0].id);
change_song(coll, false, np[0].id);
} else if(conf !== null && conf !== undefined && conf.length !== 0)
{
if(conf[0].adminpass !== "") conf[0].adminpass = true;
@@ -638,7 +645,7 @@ function send_play(coll, socket, broadcast) {
if(socket === undefined) {
io.to(coll).emit("np", toSend);
//
List.getNextSong(coll)
getNextSong(coll)
var url = 'https://img.youtube.com/vi/'+np[0].id+'/mqdefault.jpg';
if(np[0].source == "soundcloud") url = np[0].thumbnail;
sendColor(coll, false, url);

View File

@@ -1,3 +1,12 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var List = require(pathThumbnails + '/handlers/list.js');
var Frontpage = require(pathThumbnails + '/handlers/frontpage.js');
var Search = require(pathThumbnails + '/handlers/search.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
function addFromOtherList(arr, guid, offline, socket) {
var socketid = socket.zoff_id;
if(typeof(arr) == "object") {
@@ -425,7 +434,7 @@ function add_function(arr, coll, guid, offline, socket) {
socket.emit("toast", "addedsong");
});
} else {
ListChange.vote(coll, id, guid, socket);
vote(coll, id, guid, socket);
}
});
} else {
@@ -460,7 +469,7 @@ function add_function(arr, coll, guid, offline, socket) {
} else if(docs[0].now_playing === true){
socket.emit("toast", "alreadyplay");
} else{
if(conf[0].vote === false) ListChange.vote(coll, id, guid, socket);
if(conf[0].vote === false) vote(coll, id, guid, socket);
else socket.emit("toast", "listhaspass");
}
});
@@ -519,7 +528,7 @@ function voteUndecided(msg, coll, guid, offline, socket) {
return;
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass) {
if(adminpass != "" || msg.adminpass == undefined) {
@@ -535,13 +544,13 @@ function voteUndecided(msg, coll, guid, offline, socket) {
Functions.check_inlist(coll, guid, socket, offline);
if(msg.type == "del") {
ListChange.del(msg, socket, socketid);
del(msg, socket, socketid);
} else {
var id = msg.id;
var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(msg.adminpass), true));
if(docs !== null && docs.length !== 0 && ((docs[0].vote === true && (hash == docs[0].adminpass || docs[0].adminpass === "")) ||
docs[0].vote === false)) {
ListChange.vote(coll, id, guid, socket);
vote(coll, id, guid, socket);
} else {
socket.emit("toast", "listhaspass");
}
@@ -583,7 +592,7 @@ function shuffle(msg, coll, guid, offline, socket) {
return;
}
coll = msg.channel.toLowerCase().replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass) {
if(adminpass != "" || msg.adminpass == undefined) {
@@ -654,7 +663,7 @@ function shuffle(msg, coll, guid, offline, socket) {
function del(params, socket, socketid) {
if(params.id){
var coll = emojiStrip(params.channel).toLowerCase();
var coll = Functions.removeEmojis(params.channel).toLowerCase();
coll = coll.replace(/_/g, "").replace(/ /g,'');
coll = filter.clean(coll);
@@ -705,7 +714,7 @@ function delete_all(msg, coll, guid, offline, socket) {
coll = msg.channel;
}
coll = coll.replace(/ /g,'');
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = filter.clean(coll);
Functions.getSessionAdminUser(Functions.getSession(socket), coll, function(userpass, adminpass, gotten) {
if(adminpass != "" || msg.adminpass == undefined) {

View File

@@ -1,3 +1,9 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var crypto = require('crypto');
var Filter = require('bad-words');
var filter = new Filter({ placeHolder: 'x'});
var db = require(pathThumbnails + '/handlers/db.js');
function password(inp, coll, guid, offline, socket) {
var sessionId = Functions.getSession(socket);
if(sessionId == "") sessionId = "empty";
@@ -22,9 +28,9 @@ function password(inp, coll, guid, offline, socket) {
try {
coll = inp.channel;
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
} catch(e) {
return;
@@ -86,9 +92,9 @@ function conf_function(params, coll, guid, offline, socket) {
try {
coll = params.channel.replace(/ /g,'');
if(coll.length == 0) return;
coll = emojiStrip(coll).toLowerCase();
coll = Functions.removeEmojis(coll).toLowerCase();
coll = coll.replace(/_/g, "");
coll = filter.clean(coll);
} catch(e) {
return;

View File

@@ -7,6 +7,8 @@ try {
console.log("Seems you forgot to create the file api_key.js in /server/config/. Have a look at api_key.example.js.");
process.exit();
}
var request = require('request');
var db = require(pathThumbnails + '/handlers/db.js');
function get_correct_info(song_generated, channel, broadcast, callback) {
channel = channel.replace(/ /g,'');

View File

@@ -1,4 +1,7 @@
var Functions = require(pathThumbnails + '/handlers/functions.js');
var Notifications = require(pathThumbnails + '/handlers/notifications.js');
var crypto = require('crypto');
var db = require(pathThumbnails + '/handlers/db.js');
function thumbnail(msg, coll, guid, offline, socket) {
if(msg.thumbnail != undefined && msg.channel && msg.channel != undefined && Functions.isUrl(msg.thumbnail)){