From 120553eac47355e0f7666029e563b76e87ac8de8 Mon Sep 17 00:00:00 2001 From: Alf Hammerseth Date: Sat, 16 Nov 2019 15:16:34 +0100 Subject: [PATCH] Add linting and unused cleanup --- server/handlers/aggregates.js | 38 ++- server/handlers/chat.js | 415 ++++++++++++++------------ server/handlers/db.js | 135 ++++++--- server/handlers/frontpage.js | 83 ++++-- server/handlers/functions.js | 379 ++++++++++++++++-------- server/handlers/io.js | 279 +++++++++++------- server/handlers/list.js | 455 +++++++++++++++++------------ server/handlers/list_change.js | 482 ++++++++++++++++++------------- server/handlers/list_settings.js | 118 ++++---- server/handlers/notifications.js | 4 +- server/handlers/search.js | 143 +++++---- server/handlers/suggestions.js | 95 +++--- 12 files changed, 1560 insertions(+), 1066 deletions(-) diff --git a/server/handlers/aggregates.js b/server/handlers/aggregates.js index c7b28dad..4be51eab 100644 --- a/server/handlers/aggregates.js +++ b/server/handlers/aggregates.js @@ -10,12 +10,24 @@ var toShowConfig = { startTime: 1, userpass: 1, vote: 1, - toggleChat: { $ifNull: ["$toggleChat", true] }, - strictSkip: { $ifNull: ["$strictSkip", false] }, - strictSkipNumber: { $ifNull: ["$strictSkipNumber", 10] }, - description: { $ifNull: ["$description", ""] }, - thumbnail: { $ifNull: ["$thumbnail", ""] }, - rules: { $ifNull: ["$rules", ""] }, + toggleChat: { + $ifNull: ["$toggleChat", true] + }, + strictSkip: { + $ifNull: ["$strictSkip", false] + }, + strictSkipNumber: { + $ifNull: ["$strictSkipNumber", 10] + }, + description: { + $ifNull: ["$description", ""] + }, + thumbnail: { + $ifNull: ["$thumbnail", ""] + }, + rules: { + $ifNull: ["$rules", ""] + }, _id: 0 }; @@ -30,8 +42,12 @@ var project_object = { duration: 1, end: 1, type: 1, - added_by: { $ifNull: ["$added_by", "Anonymous"] }, - source: { $ifNull: ["$source", "youtube"] }, + added_by: { + $ifNull: ["$added_by", "Anonymous"] + }, + source: { + $ifNull: ["$source", "youtube"] + }, thumbnail: { $ifNull: [ "$thumbnail", @@ -40,7 +56,9 @@ var project_object = { } ] }, - tags: { $ifNull: ["$tags", []] } + tags: { + $ifNull: ["$tags", []] + } }; var toShowChannel = { @@ -62,4 +80,4 @@ var toShowChannel = { module.exports.project_object = project_object; module.exports.toShowConfig = toShowConfig; -module.exports.toShowChannel = toShowChannel; +module.exports.toShowChannel = toShowChannel; \ No newline at end of file diff --git a/server/handlers/chat.js b/server/handlers/chat.js index 849b78fd..b3a65d46 100644 --- a/server/handlers/chat.js +++ b/server/handlers/chat.js @@ -1,12 +1,5 @@ var Functions = require(pathThumbnails + "/handlers/functions.js"); var crypto = require("crypto"); -var Filter = require("bad-words"); -var filter = new Filter({ placeHolder: "x" }); -/*var filter = { -clean: function(str) { -return str; -} -}*/ var db = require(pathThumbnails + "/handlers/db.js"); function get_history(channel, all, socket) { @@ -27,7 +20,7 @@ function get_history(channel, all, socket) { Functions.getSessionAdminUser( Functions.getSession(socket), channel, - function(userpass) { + function (userpass) { if (userpass != "" || pass == undefined) { pass = userpass; } else { @@ -36,7 +29,9 @@ function get_history(channel, all, socket) { .update(Functions.decrypt_string(pass)) .digest("base64"); } - db.collection(channel + "_settings").find({ id: "config" }, function( + db.collection(channel + "_settings").find({ + id: "config" + }, function ( err, conf ) { @@ -65,14 +60,21 @@ function getAndSendLogs(channel, all, socket, pass, query) { icon: 1, _id: 0 }) - .sort({ createdAt: 1 }) - .limit(20, function(err, docs) { - socket.emit("chat_history", { all: all, data: docs }); + .sort({ + createdAt: 1 + }) + .limit(20, function (err, docs) { + socket.emit("chat_history", { + all: all, + data: docs + }); }); } function getUserNameByGuid(guid, callback) { - db.collection("user_names").find({ guid: guid }, function(err, usernames) { + db.collection("user_names").find({ + guid: guid + }, function (err, usernames) { if (usernames.length == 1) { callback(usernames[0].name); return; @@ -110,8 +112,8 @@ function chat(msg, guid, offline, socket) { coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); - checkIfUserIsBanned(coll, socket, guid, function() { - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + checkIfUserIsBanned(coll, socket, guid, function () { + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass ) { @@ -123,7 +125,7 @@ function chat(msg, guid, offline, socket) { .update(Functions.decrypt_string(msg.pass)) .digest("base64"); } - db.collection(coll + "_settings").find(function(err, conf) { + db.collection(coll + "_settings").find(function (err, conf) { if ( conf.length > 0 && (conf[0].hasOwnProperty("toggleChat") && !conf[0].toggleChat) @@ -147,18 +149,18 @@ function chat(msg, guid, offline, socket) { guid, socket, offline, - function() { + function () { if (data == "/who") { db.collection("user_names").distinct( - "name", - { channels: coll }, - function(err, docs) { + "name", { + channels: coll + }, + function (err, docs) { var userAdd = "s"; if (docs.length == 1) userAdd = ""; socket.emit("chat", { from: "System", - msg: - ": User" + + msg: ": User" + userAdd + " in channel are: " + docs.join(", "), @@ -173,7 +175,9 @@ function chat(msg, guid, offline, socket) { data.length < 151 && data.replace(/\s/g, "").length ) { - db.collection("user_names").find({ guid: guid }, function( + db.collection("user_names").find({ + guid: guid + }, function ( err, docs ) { @@ -186,9 +190,10 @@ function chat(msg, guid, offline, socket) { if (splitData[1].length > 0) { var passToCompare = Functions.hash_pass(adminpass); if (passToCompare == conf[0].adminpass) { - db.collection("user_names").find( - { name: splitData[1] }, - function(err, name) { + db.collection("user_names").find({ + name: splitData[1] + }, + function (err, name) { if (name.length == 1) { if ( data.startsWith("/ban") && @@ -200,36 +205,31 @@ function chat(msg, guid, offline, socket) { var connection_id = name[0].connection_id; var yourSelf = Functions.hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers[ - "accept-language" - ] + socket.handshake.address + + socket.handshake.headers[ + "accept-language" + ] ); if (connection_id != yourSelf) { - db.collection(coll + "_banned_chat").update( - { + db.collection(coll + "_banned_chat").update({ connection_id: connection_id - }, - { + }, { connection_id: connection_id, by: docs[0].name, reason: reason - }, - { + }, { upsert: true }, - function(err, results) { + function (err, results) { io.to(coll).emit("chat", { from: "System", - msg: - ": " + + msg: ": " + docs[0].name + " has banned " + splitData[1] + " for: " + reason, - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } @@ -237,17 +237,16 @@ function chat(msg, guid, offline, socket) { } else { socket.emit("chat", { from: "System", - msg: - ": I'm sorry but you can't ban yourself..", - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + msg: ": I'm sorry but you can't ban yourself..", + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } } else if (data.startsWith("/unban")) { - db.collection(coll + "_banned_chat").remove( - { connection_id: name[0].connection_id }, - function(err, results) { + db.collection(coll + "_banned_chat").remove({ + connection_id: name[0].connection_id + }, + function (err, results) { if ( results.hasOwnProperty("n") && results.n == 1 && @@ -258,22 +257,18 @@ function chat(msg, guid, offline, socket) { ) { io.to(coll).emit("chat", { from: "System", - msg: - ": " + + msg: ": " + docs[0].name + " has unbanned " + splitData[1], - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } else { socket.emit("chat", { from: "System", - msg: - ": Cannot find anyone with that username in this chat.", - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + msg: ": Cannot find anyone with that username in this chat.", + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } @@ -285,10 +280,8 @@ function chat(msg, guid, offline, socket) { ) { socket.emit("chat", { from: "System", - msg: - ": You are doing that command wrong. its /ban USERNAME", - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + msg: ": You are doing that command wrong. its /ban USERNAME", + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } @@ -296,8 +289,7 @@ function chat(msg, guid, offline, socket) { socket.emit("chat", { from: "System", msg: ": No user by that name.", - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } @@ -306,27 +298,24 @@ function chat(msg, guid, offline, socket) { } else { socket.emit("chat", { from: "System", - msg: - ": You are not logged in as an admin to the channel, don't try any funnybusiness.", - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + msg: ": You are not logged in as an admin to the channel, don't try any funnybusiness.", + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } } else { socket.emit("chat", { from: "System", - msg: - ": You are doing that command wrong. its /ban USERNAME REASON or /unban USERNAME", - icon: - "https://zoff.me/assets/images/favicon-32x32.png" + msg: ": You are doing that command wrong. its /ban USERNAME REASON or /unban USERNAME", + icon: "https://zoff.me/assets/images/favicon-32x32.png" }); return; } } else { - 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; if (n.length > 0 && n[0].icon) { icon = n[0].icon; @@ -399,7 +388,7 @@ function all_chat(msg, guid, offline, socket) { guid, socket, offline, - function() { + function () { if ( data !== "" && data !== undefined && @@ -407,17 +396,19 @@ function all_chat(msg, guid, offline, socket) { 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) { - 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; if (n.length > 0 && n[0].icon) { icon = n[0].icon; } - db.collection("chat_logs").insert( - { + db.collection("chat_logs").insert({ createdAt: new Date(), all: true, channel: coll, @@ -425,7 +416,7 @@ function all_chat(msg, guid, offline, socket) { msg: ": " + data, icon: icon }, - function(err, docs) {} + function (err, docs) {} ); io.sockets.emit("chat.all", { from: docs[0].name, @@ -454,7 +445,7 @@ function all_chat(msg, guid, offline, socket) { function checkIfChatEnabled(channel, socket, callback) { if (channel == "" || channel == undefined) callback(); else { - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").find(function (err, docs) { if ( docs.length > 0 && (docs[0].hasOwnProperty("toggleChat") && !docs[0].toggleChat) @@ -475,24 +466,34 @@ function checkIfChatEnabled(channel, socket, callback) { function checkIfUserIsBanned(channel, socket, guid, callback, callback_error) { var connection_id = Functions.hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers["accept-language"] + socket.handshake.address + + socket.handshake.headers["accept-language"] ); - db.collection(channel + "_banned_chat").find( - { $or: [{ connection_id: connection_id }, { connection_id: guid }] }, - function(err, docs) { + db.collection(channel + "_banned_chat").find({ + $or: [{ + connection_id: connection_id + }, { + connection_id: guid + }] + }, + function (err, docs) { if (docs.length == 0) callback(); else { - db.collection("user_names").findAndModify( - { - query: { guid, guid }, - update: { $addToSet: { channels: channel } } + db.collection("user_names").findAndModify({ + query: { + guid, + guid + }, + update: { + $addToSet: { + channels: channel + } + } }, - function(e, d) { + function (e, d) { socket.emit("chat", { from: "System", - msg: - ": You can't chat in this channel, you are banned. The reason is: " + + msg: ": You can't chat in this channel, you are banned. The reason is: " + docs[0].reason, icon: "https://zoff.me/assets/images/favicon-32x32.png" }); @@ -506,7 +507,7 @@ function checkIfUserIsBanned(channel, socket, guid, callback, callback_error) { } function namechange(data, guid, socket, tried, callback) { - checkIfChatEnabled(data.channel, socket, function(enabled) { + checkIfChatEnabled(data.channel, socket, function (enabled) { if (!enabled) { callback(false); return; @@ -515,11 +516,11 @@ function namechange(data, guid, socket, tried, callback) { data.channel, socket, guid, - function() { + function () { var pw = ""; var new_password; var first = false; - Functions.getSessionChatPass(Functions.getSession(socket), function( + Functions.getSessionChatPass(Functions.getSession(socket), function ( name, pass ) { @@ -561,9 +562,10 @@ function namechange(data, guid, socket, tried, callback) { return; } - db.collection("registered_users").find( - { _id: name.toLowerCase() }, - function(err, docs) { + db.collection("registered_users").find({ + _id: name.toLowerCase() + }, + function (err, docs) { var accepted_password = false; var icon = false; if (docs.length == 0) { @@ -576,12 +578,17 @@ function namechange(data, guid, socket, tried, callback) { Functions.getSession(socket), name.toLowerCase(), data.password, - function() { - db.collection("registered_users").update( - { _id: name.toLowerCase() }, - { $set: { password: password } }, - { upsert: true }, - function() {} + function () { + db.collection("registered_users").update({ + _id: name.toLowerCase() + }, { + $set: { + password: password + } + }, { + upsert: true + }, + function () {} ); } ); @@ -595,13 +602,16 @@ function namechange(data, guid, socket, tried, callback) { Functions.getSession(socket), name.toLowerCase(), data.new_password, - function() { - db.collection("registered_users").update( - { _id: name.toLowerCase(), password: password }, - { - $set: { password: Functions.hash_pass(new_password) } + function () { + db.collection("registered_users").update({ + _id: name.toLowerCase(), + password: password + }, { + $set: { + password: Functions.hash_pass(new_password) + } }, - function() {} + function () {} ); } ); @@ -609,17 +619,19 @@ function namechange(data, guid, socket, tried, callback) { Functions.setSessionChatPass( Functions.getSession(socket), name.toLowerCase(), - fetched - ? data.password - : Functions.hash_pass( - Functions.decrypt_string(data.password) - ), - function() {} + fetched ? + data.password : + Functions.hash_pass( + Functions.decrypt_string(data.password) + ), + function () {} ); } } if (accepted_password) { - db.collection("user_names").find({ guid: guid }, function( + db.collection("user_names").find({ + guid: guid + }, function ( err, names ) { @@ -631,16 +643,20 @@ function namechange(data, guid, socket, tried, callback) { if (names.length == 0) no_name = true; 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({ + _id: "all_names" + }, { + $pull: { + names: old_name + } + }, + function () {} ); } var connection_id = Functions.hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers["accept-language"] + socket.handshake.address + + socket.handshake.headers["accept-language"] ); var updateElement = { $set: { @@ -650,17 +666,25 @@ function namechange(data, guid, socket, tried, callback) { } }; if (data.hasOwnProperty("channel") && data.channel != "") { - updateElement["$addToSet"] = { channels: data.channel }; + updateElement["$addToSet"] = { + channels: data.channel + }; } - db.collection("user_names").update( - { guid: guid }, - updateElement, - { upsert: true }, - function(err, docs) { - db.collection("user_names").update( - { _id: "all_names" }, - { $addToSet: { names: name } }, - function(err, docs) { + db.collection("user_names").update({ + guid: guid + }, + updateElement, { + upsert: true + }, + function (err, docs) { + db.collection("user_names").update({ + _id: "all_names" + }, { + $addToSet: { + names: name + } + }, + function (err, docs) { //socket.emit('name', {type: "name", accepted: true}); if (old_name != name && !first && !no_name) { if ( @@ -699,8 +723,11 @@ function namechange(data, guid, socket, tried, callback) { } else { Functions.removeSessionChatPass( Functions.getSession(socket), - function() { - socket.emit("name", { type: "name", accepted: false }); + function () { + socket.emit("name", { + type: "name", + accepted: false + }); } ); } @@ -715,19 +742,27 @@ function namechange(data, guid, socket, tried, callback) { function removename(guid, coll, socket) { //coll = coll.replace(/ /g,''); - checkIfChatEnabled(coll, socket, function(enabled) { + checkIfChatEnabled(coll, socket, function (enabled) { if (!enabled) return; - db.collection("user_names").find({ guid: guid }, function(err, docs) { + db.collection("user_names").find({ + guid: guid + }, function (err, docs) { if (docs.length == 1) { var old_name = docs[0].name; Functions.removeSessionChatPass( Functions.getSession(socket), - function() { - db.collection("user_names").update( - { _id: "all_names" }, - { $pull: { names: old_name } }, - function(err, updated) { - db.collection("user_names").remove({ guid: guid }, function( + function () { + db.collection("user_names").update({ + _id: "all_names" + }, { + $pull: { + names: old_name + } + }, + function (err, updated) { + db.collection("user_names").remove({ + guid: guid + }, function ( err, removed ) { @@ -753,16 +788,23 @@ function generate_name(guid, announce_payload, second, round, channel) { second ? second : guid, Math.floor(8 + round) ); - db.collection("registered_users").find({ _id: tmp_name }, function( + db.collection("registered_users").find({ + _id: tmp_name + }, function ( err, docs ) { if (docs.length == 0) { - db.collection("user_names").update( - { _id: "all_names" }, - { $addToSet: { names: tmp_name } }, - { upsert: true }, - function(err, updated) { + db.collection("user_names").update({ + _id: "all_names" + }, { + $addToSet: { + names: tmp_name + } + }, { + upsert: true + }, + function (err, updated) { if ( updated.nModified == 1 || (updated.hasOwnProperty("upserted") && @@ -771,8 +813,8 @@ function generate_name(guid, announce_payload, second, round, channel) { ) { var connection_id = Functions.hash_pass( announce_payload.socket.handshake.headers["user-agent"] + - announce_payload.socket.handshake.address + - announce_payload.socket.handshake.headers["accept-language"] + announce_payload.socket.handshake.address + + announce_payload.socket.handshake.headers["accept-language"] ); var updateElement = { $set: { @@ -782,7 +824,9 @@ function generate_name(guid, announce_payload, second, round, channel) { } }; if (channel != undefined && channel != "") { - updateElement["$addToSet"] = { channels: channel }; + updateElement["$addToSet"] = { + channels: channel + }; } if ( announce_payload.hasOwnProperty("channel") && @@ -792,11 +836,13 @@ function generate_name(guid, announce_payload, second, round, channel) { channels: announce_payload.channel }; } - db.collection("user_names").update( - { guid: guid }, - updateElement, - { upsert: true }, - function(err, update) { + db.collection("user_names").update({ + guid: guid + }, + updateElement, { + upsert: true + }, + function (err, update) { name = tmp_name; if (announce_payload.announce) { io.to(announce_payload.channel).emit("chat", { @@ -843,7 +889,7 @@ function get_name(guid, announce_payload, first) { if (!announce_payload.announce && announce_payload.hasOwnProperty("socket")) { Functions.getSessionChatPass( Functions.getSession(announce_payload.socket), - function(name, pass) { + function (name, pass) { if (name == "" || pass == "") { get_name_generate( guid, @@ -853,9 +899,10 @@ function get_name(guid, announce_payload, first) { ); return; } - db.collection("registered_users").find( - { _id: name.toLowerCase() }, - function(err, docs) { + db.collection("registered_users").find({ + _id: name.toLowerCase() + }, + function (err, docs) { if ( docs[0].password == Functions.hash_pass(Functions.decrypt_string(pass)) @@ -868,15 +915,19 @@ function get_name(guid, announce_payload, first) { Functions.getSession(announce_payload.socket), name.toLowerCase(), pass, - function() {} + function () {} ); var connection_id = Functions.hash_pass( announce_payload.socket.handshake.headers["user-agent"] + - announce_payload.socket.handshake.address + - announce_payload.socket.handshake.headers["accept-language"] + announce_payload.socket.handshake.address + + announce_payload.socket.handshake.headers["accept-language"] ); var updateElement = { - $set: { name: name, icon: icon, connection_id: connection_id } + $set: { + name: name, + icon: icon, + connection_id: connection_id + } }; if ( announce_payload.hasOwnProperty("channel") && @@ -885,15 +936,21 @@ function get_name(guid, announce_payload, first) { updateElement["$addToSet"] = { channel: announce_payload.channel }; - db.collection("user_names").update( - { guid: guid }, - updateElement, - { upsert: true }, - function(err, docs) { - db.collection("user_names").update( - { _id: "all_names" }, - { $addToSet: { names: name } }, - function(err, docs) { + db.collection("user_names").update({ + guid: guid + }, + updateElement, { + upsert: true + }, + function (err, docs) { + db.collection("user_names").update({ + _id: "all_names" + }, { + $addToSet: { + names: name + } + }, + function (err, docs) { name = name; } ); @@ -910,7 +967,9 @@ function get_name(guid, announce_payload, first) { } function get_name_generate(guid, announce_payload, first, channel) { - db.collection("user_names").find({ guid: guid }, function(err, docs) { + db.collection("user_names").find({ + guid: guid + }, function (err, docs) { if (docs.length == 0) { generate_name(guid, announce_payload, undefined); } else { @@ -926,4 +985,4 @@ module.exports.all_chat = all_chat; module.exports.namechange = namechange; module.exports.removename = removename; module.exports.generate_name = generate_name; -module.exports.get_name = get_name; +module.exports.get_name = get_name; \ No newline at end of file diff --git a/server/handlers/db.js b/server/handlers/db.js index d66d68c2..32538f39 100644 --- a/server/handlers/db.js +++ b/server/handlers/db.js @@ -17,68 +17,113 @@ var connected_db = mongojs( ); var ObjectId = mongojs.ObjectId; -db.collection("chat_logs").createIndex( - { createdAt: 1 }, - { expireAfterSeconds: 600 }, - function() {} +db.collection("chat_logs").createIndex({ + createdAt: 1 + }, { + expireAfterSeconds: 600 + }, + function () {} ); -db.collection("timeout_api").createIndex( - { createdAt: 1 }, - { expireAfterSeconds: 120 }, - function() {} +db.collection("timeout_api").createIndex({ + createdAt: 1 + }, { + expireAfterSeconds: 120 + }, + function () {} ); -db.collection("api_links").createIndex( - { createdAt: 1 }, - { expireAfterSeconds: 86400 }, - function() {} +db.collection("api_links").createIndex({ + createdAt: 1 + }, { + expireAfterSeconds: 86400 + }, + function () {} ); -db.on("connected", function(err) { +db.on("connected", function (err) { console.log("connected"); }); -db.on("error", function(err) { +db.on("error", function (err) { console.log("\n" + new Date().toString() + "\n Database error: ", err); }); -db.on("error", function(err) { +db.on("error", function (err) { console.log("\n" + new Date().toString() + "\n Database error: ", err); }); /* Resetting usernames, and connected users */ -db.collection("unique_ids").update( - { _id: "unique_ids" }, - { $set: { unique_ids: [] } }, - { multi: true, upsert: true }, - function(err, docs) {} +db.collection("unique_ids").update({ + _id: "unique_ids" + }, { + $set: { + unique_ids: [] + } + }, { + multi: true, + upsert: true + }, + function (err, docs) {} ); -db.collection("user_names").remove( - { guid: { $exists: true } }, - { multi: true, upsert: true }, - function(err, docs) {} +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("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({ + users: { + $exists: true + } + }, { + $set: { + users: [] + } + }, { + 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("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) {} +db.collection("frontpage_lists").update({ + viewers: { + $ne: 0 + } + }, { + $set: { + viewers: 0 + } + }, { + multi: true, + upsert: true + }, + function (err, docs) {} ); -module.exports = db; +module.exports = db; \ No newline at end of file diff --git a/server/handlers/frontpage.js b/server/handlers/frontpage.js index 2818a272..9e81906a 100644 --- a/server/handlers/frontpage.js +++ b/server/handlers/frontpage.js @@ -1,5 +1,6 @@ var Functions = require(pathThumbnails + "/handlers/functions.js"); var db = require(pathThumbnails + "/handlers/db.js"); + function frontpage_lists(msg, socket) { if ( msg == undefined || @@ -17,11 +18,15 @@ function frontpage_lists(msg, socket) { return; } - db.collection("frontpage_lists").find({ frontpage: true }, function( + db.collection("frontpage_lists").find({ + frontpage: true + }, function ( err, docs ) { - db.collection("connected_users").find({ _id: "total_users" }, function( + db.collection("connected_users").find({ + _id: "total_users" + }, function ( err, tot ) { @@ -44,16 +49,22 @@ function get_frontpage_lists(callback) { title: 1, viewers: 1, accessed: 1, - pinned: { $ifNull: ["$pinned", 0] }, + pinned: { + $ifNull: ["$pinned", 0] + }, description: { - $ifNull: [ - { + $ifNull: [{ $cond: { if: { - $or: [ - { $eq: ["$description", ""] }, - { $eq: ["$description", null] }, - { $eq: ["$description", undefined] } + $or: [{ + $eq: ["$description", ""] + }, + { + $eq: ["$description", null] + }, + { + $eq: ["$description", undefined] + } ] }, then: "This list has no description", @@ -64,14 +75,18 @@ function get_frontpage_lists(callback) { ] }, thumbnail: { - $ifNull: [ - { + $ifNull: [{ $cond: { if: { - $or: [ - { $eq: ["$thumbnail", ""] }, - { $eq: ["$thumbnail", null] }, - { $eq: ["$thumbnail", undefined] } + $or: [{ + $eq: ["$thumbnail", ""] + }, + { + $eq: ["$thumbnail", null] + }, + { + $eq: ["$thumbnail", undefined] + } ] }, then: { @@ -80,16 +95,19 @@ function get_frontpage_lists(callback) { else: "$thumbnail" } }, - { $concat: ["https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] } + { + $concat: ["https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] + } ] } }; db.collection("frontpage_lists").aggregate( - [ - { + [{ $match: { frontpage: true, - count: { $gt: 3 } + count: { + $gt: 3 + } } }, { @@ -111,7 +129,9 @@ function get_frontpage_lists(callback) { function update_frontpage(coll, id, title, thumbnail, source, callback) { //coll = coll.replace(/ /g,''); - db.collection("frontpage_lists").find({ _id: coll }, function(e, doc) { + db.collection("frontpage_lists").find({ + _id: coll + }, function (e, doc) { var updateObject = { id: id, title: title, @@ -120,20 +140,23 @@ function update_frontpage(coll, id, title, thumbnail, source, callback) { if ( doc.length > 0 && ((doc[0].thumbnail != "" && - doc[0].thumbnail != undefined && - (doc[0].thumbnail.indexOf("https://i1.sndcdn.com") > -1 || - doc[0].thumbnail.indexOf("https://w1.sndcdn.com") > -1 || - doc[0].thumbnail.indexOf("https://img.youtube.com") > -1)) || + doc[0].thumbnail != undefined && + (doc[0].thumbnail.indexOf("https://i1.sndcdn.com") > -1 || + doc[0].thumbnail.indexOf("https://w1.sndcdn.com") > -1 || + doc[0].thumbnail.indexOf("https://img.youtube.com") > -1)) || (doc[0].thumbnail == "" || doc[0].thumbnail == undefined)) ) { updateObject.thumbnail = thumbnail; if (thumbnail == undefined) updateObject.thumbnail = ""; } - db.collection("frontpage_lists").update( - { _id: coll }, - { $set: updateObject }, - { upsert: true }, - function(err, returnDocs) { + db.collection("frontpage_lists").update({ + _id: coll + }, { + $set: updateObject + }, { + upsert: true + }, + function (err, returnDocs) { if (typeof callback == "function") callback(); } ); @@ -142,4 +165,4 @@ function update_frontpage(coll, id, title, thumbnail, source, callback) { module.exports.get_frontpage_lists = get_frontpage_lists; module.exports.frontpage_lists = frontpage_lists; -module.exports.update_frontpage = update_frontpage; +module.exports.update_frontpage = update_frontpage; \ No newline at end of file diff --git a/server/handlers/functions.js b/server/handlers/functions.js index b0664447..d858d8a4 100644 --- a/server/handlers/functions.js +++ b/server/handlers/functions.js @@ -18,7 +18,9 @@ var crypto = require("crypto"); var db = require(pathThumbnails + "/handlers/db.js"); var uniqid = require("uniqid"); var Filter = require("bad-words"); -var filter = new Filter({ placeHolder: "x" }); +var filter = new Filter({ + placeHolder: "x" +}); var Chat = require(pathThumbnails + "/handlers/chat.js"); @@ -42,10 +44,14 @@ function decodeChannelName(str) { } function remove_unique_id(short_id) { - db.collection("unique_ids").update( - { _id: "unique_ids" }, - { $pull: { unique_ids: short_id } }, - function(err, docs) {} + db.collection("unique_ids").update({ + _id: "unique_ids" + }, { + $pull: { + unique_ids: short_id + } + }, + function (err, docs) {} ); } @@ -53,21 +59,31 @@ function remove_name_from_db(guid, channel) { // Use temporary, with caution. Can bottleneck in large quantity of users. // // 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( + db.collection("connected_users").find({ + _id: "total_users" + }, function ( err, all_users ) { - var hasOne = all_users[0].total_users.some(function(v) { + var hasOne = all_users[0].total_users.some(function (v) { return v.indexOf(guid) >= 0; }); if (!hasOne) { - db.collection("user_names").find({ guid: guid }, function(err, user) { + db.collection("user_names").find({ + guid: guid + }, function (err, user) { if (user.length == 1) { - db.collection("user_names").update( - { _id: "all_names" }, - { $pull: { names: user[0].name } }, - function(err, updated) { - db.collection("user_names").remove({ guid: guid }, function( + db.collection("user_names").update({ + _id: "all_names" + }, { + $pull: { + names: user[0].name + } + }, + function (err, updated) { + db.collection("user_names").remove({ + guid: guid + }, function ( err, removed ) {}); @@ -77,10 +93,14 @@ function remove_name_from_db(guid, channel) { }); } else { if (channel == undefined || channel == "") return; - db.collection("user_names").update( - { guid: guid }, - { $pull: { channels: channel } }, - function(err, docs) { + db.collection("user_names").update({ + guid: guid + }, { + $pull: { + channels: channel + } + }, + function (err, docs) { //console.log("Pulled user from current channel"); } ); @@ -91,15 +111,15 @@ function remove_name_from_db(guid, channel) { function isUrl(str) { var pattern = new RegExp( "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" + - "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" + - "|mil|biz|info|mobi|name|aero|jobs|museum" + - "|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})*)?\\b" + "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" + + "|mil|biz|info|mobi|name|aero|jobs|museum" + + "|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})*)?\\b" ); if (!pattern.test(str)) { return false; @@ -120,8 +140,8 @@ function getSession(socket) { // Returning "sessiong"-based on place of connection return hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers["accept-language"] + socket.handshake.address + + socket.handshake.headers["accept-language"] ); //return "empty"; } @@ -136,10 +156,15 @@ function remove_from_array(array, element) { function generate_channel_name(res) { var trying_id = uniqid.time().toLowerCase(); - db.collection("frontpage_lists").find( - { frontpage: { $exists: true }, _id: trying_id }, - { _id: 1 }, - function(err, docs) { + db.collection("frontpage_lists").find({ + frontpage: { + $exists: true + }, + _id: trying_id + }, { + _id: 1 + }, + function (err, docs) { if (docs.length == 0) { res.send(trying_id); return; @@ -163,20 +188,31 @@ function check_inlist(coll, guid, socket, offline, callback, double_check) { } //coll = coll.replace(/ /g,''); if (!offline && coll != undefined) { - db.collection("connected_users").update( - { _id: coll }, - { $addToSet: { users: guid } }, - { upsert: true }, - function(err, updated) { + db.collection("connected_users").update({ + _id: coll + }, { + $addToSet: { + users: guid + } + }, { + upsert: true + }, + function (err, updated) { if (updated.nModified > 0 || updated.upserted != undefined) { - db.collection("connected_users").find({ _id: coll }, function( + db.collection("connected_users").find({ + _id: coll + }, function ( err, new_doc ) { - db.collection("frontpage_lists").update( - { _id: coll }, - { $set: { viewers: new_doc[0].users.length } }, - function() { + db.collection("frontpage_lists").update({ + _id: coll + }, { + $set: { + viewers: new_doc[0].users.length + } + }, + function () { if ( new_doc[0].users == undefined || new_doc[0].users.length == undefined @@ -185,23 +221,32 @@ function check_inlist(coll, guid, socket, offline, callback, double_check) { } else { io.to(coll).emit("viewers", new_doc[0].users.length); } - Chat.namechange( - { initial: true, first: true, channel: coll }, + Chat.namechange({ + initial: true, + first: true, + channel: coll + }, guid, socket, false, - function(enabled) { - db.collection("user_names").find({ guid: guid }, function( + function (enabled) { + 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; - db.collection("user_names").update( - { guid: guid }, - { $addToSet: { channels: coll } }, - function(err, doc) {} + db.collection("user_names").update({ + guid: guid + }, { + $addToSet: { + channels: coll + } + }, + function (err, doc) {} ); if (enabled) { socket.broadcast.to(coll).emit("chat", { @@ -222,10 +267,14 @@ function check_inlist(coll, guid, socket, offline, callback, double_check) { }); } }); - 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" @@ -239,7 +288,9 @@ function check_inlist(coll, guid, socket, offline, callback, double_check) { ); }); } else { - db.collection("connected_users").find({ _id: coll }, function( + db.collection("connected_users").find({ + _id: coll + }, function ( err, new_doc ) { @@ -252,24 +303,36 @@ function check_inlist(coll, guid, socket, offline, callback, double_check) { ); } else { if (offline) { - db.collection("connected_users").update( - { _id: "offline_users" }, - { $addToSet: { users: guid } }, - function(err, docs) {} + 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: coll + }, { + $addToSet: { + users: guid + } + }, + function (err, docs) {} ); } // 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(); @@ -372,11 +435,16 @@ function setSessionAdminPass(id, adminpass, list, callback) { connected_db .collection(id) - .update( - { _id: list }, - { $set: { adminpass: hash_pass(decrypt_string(adminpass), true) } }, - { upsert: true }, - function(e, d) { + .update({ + _id: list + }, { + $set: { + adminpass: hash_pass(decrypt_string(adminpass), true) + } + }, { + upsert: true + }, + function (e, d) { callback(); return; } @@ -392,11 +460,17 @@ function setSessionChatPass(id, name, pass, callback) { } connected_db .collection(id) - .update( - { _id: "_chat_" }, - { $set: { password: pass, name: name } }, - { upsert: true }, - function(e) { + .update({ + _id: "_chat_" + }, { + $set: { + password: pass, + name: name + } + }, { + upsert: true + }, + function (e) { callback(); return; } @@ -414,7 +488,9 @@ function getSessionChatPass(id, callback) { return; } - connected_db.collection(id).find({ _id: "_chat_" }, function(e, d) { + connected_db.collection(id).find({ + _id: "_chat_" + }, function (e, d) { if (d.length > 0) { var name = ""; var pass = ""; @@ -446,11 +522,15 @@ function setChromecastHost(id, other_id, list, callback) { } connected_db .collection(id) - .update( - { _id: list }, - { chromecast: true, id: other_id }, - { upsert: true }, - function(e, docs) { + .update({ + _id: list + }, { + chromecast: true, + id: other_id + }, { + upsert: true + }, + function (e, docs) { callback(true); return; } @@ -469,11 +549,16 @@ function setSessionUserPass(id, userpass, list, callback) { connected_db .collection(id) - .update( - { _id: list }, - { $set: { userpass: userpass } }, - { upsert: true }, - function(e, d) { + .update({ + _id: list + }, { + $set: { + userpass: userpass + } + }, { + upsert: true + }, + function (e, d) { callback(); return; } @@ -489,7 +574,9 @@ function getSessionAdminUser(id, list, callback) { callback("", "", false); return; } - connected_db.collection(id).find({ _id: list }, function(e, d) { + connected_db.collection(id).find({ + _id: list + }, function (e, d) { var userpass = ""; var adminpass = ""; if (d.length > 0) { @@ -514,7 +601,9 @@ function removeSessionChatPass(id, callback) { callback(); return; } - connected_db.collection(id).remove({ _id: "_chat_" }, function() { + connected_db.collection(id).remove({ + _id: "_chat_" + }, function () { callback(); return; }); @@ -527,17 +616,27 @@ function removeSessionAdminPass(id, channel, callback) { } connected_db .collection(id) - .update({ _id: channel }, { $set: { adminpass: "" } }, function() { + .update({ + _id: channel + }, { + $set: { + adminpass: "" + } + }, function () { callback(); return; }); } function remove_from_chat_channel(coll, guid) { - db.collection("user_names").update( - { guid: guid }, - { $pull: { channels: coll } }, - function(err, docs) {} + db.collection("user_names").update({ + guid: guid + }, { + $pull: { + channels: coll + } + }, + function (err, docs) {} ); } @@ -551,25 +650,44 @@ function left_channel(coll, guid, short_id, in_list, socket, change, caller) { return; } //coll = coll.replace(/ /g,''); - db.collection("connected_users").update( - { _id: coll }, - { $pull: { users: guid } }, - function(err, updated) { + 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").update({ + _id: "total_users" + }, { + $pull: { + total_users: guid + coll + } + }, + function (err, updated) {} ); - db.collection("connected_users").find({ _id: coll }, function( + 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( + 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 ) { @@ -594,15 +712,23 @@ function left_channel(coll, guid, short_id, in_list, socket, change, caller) { ); }); } else { - db.collection("connected_users").update( - { _id: "offline_users" }, - { $pull: { users: guid } }, - function(err, updated) { + 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) {} + db.collection("connected_users").update({ + _id: "total_users" + }, { + $pull: { + total_users: guid + coll + } + }, + function (err, updated) {} ); if (!change) { remove_name_from_db(guid, coll); @@ -634,12 +760,11 @@ function checkTimeout( callback(); return; } - db.collection("timeout_api").find( - { + db.collection("timeout_api").find({ type: type, guid: guid }, - function(err, docs) { + function (err, docs) { if (docs.length > 0) { var date = new Date(docs[0].createdAt); date.setSeconds(date.getSeconds() + timeout); @@ -663,17 +788,19 @@ function checkTimeout( } } var now_date = new Date(); - db.collection("timeout_api").update( - { type: type, guid: guid }, - { + db.collection("timeout_api").update({ + type: type, + guid: guid + }, { $set: { createdAt: now_date, type: type, guid: guid } + }, { + upsert: true }, - { upsert: true }, - function(err, docs) { + function (err, docs) { callback(); return; } @@ -707,4 +834,4 @@ module.exports.rndName = rndName; module.exports.decrypt_string = decrypt_string; module.exports.get_time = get_time; module.exports.contains = contains; -module.exports.hash_pass = hash_pass; +module.exports.hash_pass = hash_pass; \ No newline at end of file diff --git a/server/handlers/io.js b/server/handlers/io.js index b73ea7b6..22e1fbcb 100644 --- a/server/handlers/io.js +++ b/server/handlers/io.js @@ -11,7 +11,9 @@ 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 filter = new Filter({ + placeHolder: "x" +}); /*var filter = { clean: function(str) { return str; @@ -19,8 +21,8 @@ var filter = new Filter({ placeHolder: "x" }); }*/ var db = require(pathThumbnails + "/handlers/db.js"); -module.exports = function() { - io.on("connection", function(socket) { +module.exports = function () { + io.on("connection", function (socket) { try { var parsedCookies = cookie.parse(socket.handshake.headers.cookie); socket.cookie_id = parsedCookies["_uI"]; @@ -34,14 +36,14 @@ module.exports = function() { if (guid == "empty" || guid == null || guid == undefined) guid = Functions.hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers["accept-language"] + socket.handshake.address + + socket.handshake.headers["accept-language"] ); socket.guid = guid; - socket.on("close", function() {}); + socket.on("close", function () {}); - socket.on("pinging", function() { + socket.on("pinging", function () { socket.emit("ok"); }); @@ -51,13 +53,16 @@ module.exports = function() { var in_list = false; var name = ""; var short_id; - Chat.get_name(guid, { announce: false, socket: socket }); + Chat.get_name(guid, { + announce: false, + socket: socket + }); var offline = false; var chromecast_object = false; socket.emit("guid", guid); - socket.on("self_ping", function(msg) { + socket.on("self_ping", function (msg) { var channel = msg.channel; if (channel.indexOf("?") > -1) { channel = channel.substring(0, channel.indexOf("?")); @@ -67,48 +72,68 @@ module.exports = function() { } //channel = channel.replace(/ /g,''); if (offline) { - db.collection("connected_users").update( - { _id: "offline_users" }, - { $addToSet: { users: guid } }, - { upsert: true }, - function(err, docs) {} + db.collection("connected_users").update({ + _id: "offline_users" + }, { + $addToSet: { + users: guid + } + }, { + upsert: true + }, + function (err, docs) {} ); } else { - db.collection("connected_users").update( - { _id: channel }, - { $addToSet: { users: guid } }, - { upsert: true }, - function(err, docs) { - db.collection("frontpage_lists").update( - { _id: channel }, - { $inc: { viewers: 1 } }, - { upsert: true }, - function() {} + db.collection("connected_users").update({ + _id: channel + }, { + $addToSet: { + users: guid + } + }, { + upsert: true + }, + function (err, docs) { + db.collection("frontpage_lists").update({ + _id: channel + }, { + $inc: { + viewers: 1 + } + }, { + upsert: true + }, + function () {} ); } ); } if (channel != "" && channel != undefined) { - db.collection("connected_users").update( - { _id: "total_users" }, - { $addToSet: { total_users: guid + channel } }, - { 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("logout", function() { + socket.on("logout", function () { Functions.removeSessionAdminPass( Functions.getSession(socket), coll, - function() {} + function () {} ); }); - socket.on("next_song", function(obj) { + socket.on("next_song", function (obj) { if (obj == undefined || !obj.hasOwnProperty("channel")) return; - db.collection(obj.channel + "_settings").find(function(e, docs) { + db.collection(obj.channel + "_settings").find(function (e, docs) { if (docs.length == 0) return; var pass = ""; if (obj.hasOwnProperty("pass")) { @@ -128,7 +153,7 @@ module.exports = function() { }); }); - socket.on("chromecast", function(msg) { + socket.on("chromecast", function (msg) { try { if ( typeof msg == "object" && @@ -143,7 +168,9 @@ module.exports = function() { if (msg.hasOwnProperty("channel")) { msg.channel = Functions.encodeChannelName(msg.channel); } - db.collection("connected_users").find({ _id: msg.channel }, function( + db.collection("connected_users").find({ + _id: msg.channel + }, function ( err, connected_users_channel ) { @@ -161,7 +188,7 @@ module.exports = function() { socket.cookie_id, msg.socket_id, msg.channel, - function(results) {} + function (results) {} ); //socket.cookie_id = msg.guid; guid = msg.guid; @@ -179,14 +206,14 @@ module.exports = function() { } }); - socket.on("get_id", function() { + socket.on("get_id", function () { socket.emit("id_chromecast", { cookie_id: Functions.getSession(socket), guid: guid }); }); - socket.on("error_video", function(msg) { + socket.on("error_video", function (msg) { try { msg.channel = Functions.encodeChannelName(msg.channel); var _list = msg.channel; //.replace(/ /g,''); @@ -208,17 +235,26 @@ module.exports = function() { Search.check_error_video(msg, coll); }); - socket.on("get_spread", function() { - db.collection("connected_users").find({ _id: "total_users" }, function( + socket.on("get_spread", function () { + db.collection("connected_users").find({ + _id: "total_users" + }, function ( err, tot ) { - 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) { + 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, @@ -239,7 +275,7 @@ module.exports = function() { }); }); - socket.on("suggest_thumbnail", function(msg) { + socket.on("suggest_thumbnail", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -250,7 +286,7 @@ module.exports = function() { Suggestions.thumbnail(msg, coll, guid, offline, socket); }); - socket.on("suggest_description", function(msg) { + socket.on("suggest_description", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -261,7 +297,7 @@ module.exports = function() { Suggestions.description(msg, coll, guid, offline, socket); }); - socket.on("suggest_rules", function(msg) { + socket.on("suggest_rules", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -272,7 +308,7 @@ module.exports = function() { Suggestions.rules(msg, coll, guid, offline, socket); }); - socket.on("namechange", function(msg) { + socket.on("namechange", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -283,7 +319,7 @@ module.exports = function() { Chat.namechange(msg, guid, socket); }); - socket.on("removename", function(msg) { + socket.on("removename", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -304,7 +340,7 @@ module.exports = function() { Chat.removename(guid, msg.channel, socket); }); - socket.on("offline", function(msg) { + socket.on("offline", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -340,41 +376,66 @@ module.exports = function() { if (coll !== undefined) { coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); - db.collection("connected_users").findAndModify( - { - query: { _id: coll }, - update: { $pull: { users: guid } }, + db.collection("connected_users").findAndModify({ + query: { + _id: coll + }, + update: { + $pull: { + users: guid + } + }, upsert: true }, - function(err, updated, d) { + function (err, updated, d) { if (d.n == 1) { var num = 0; if (updated && updated.users) { num = updated.users.length; } io.to(coll).emit("viewers", num); - db.collection("frontpage_lists").update( - { _id: coll, viewers: { $gt: 0 } }, - { $inc: { viewers: -1 } }, - function(err, docs) {} + db.collection("frontpage_lists").update({ + _id: coll, + viewers: { + $gt: 0 + } + }, { + $inc: { + viewers: -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 } }, - { upsert: true }, - 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 + } + }, { + upsert: true + }, + function (err, docs) { if ( docs.nModified == 1 && (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) {} ); } } @@ -390,10 +451,14 @@ module.exports = function() { Functions.remove_unique_id(short_id); } else { offline = false; - db.collection("connected_users").update( - { _id: "offline_users" }, - { $pull: { users: guid } }, - function(err, docs) { + db.collection("connected_users").update({ + _id: "offline_users" + }, { + $pull: { + users: guid + } + }, + function (err, docs) { Functions.check_inlist( coll, guid, @@ -407,7 +472,7 @@ module.exports = function() { } }); - socket.on("get_history", function(msg) { + socket.on("get_history", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -441,7 +506,7 @@ module.exports = function() { Chat.get_history(msg.channel, msg.all, socket); }); - socket.on("chat", function(msg) { + socket.on("chat", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -452,7 +517,7 @@ module.exports = function() { Chat.chat(msg, guid, offline, socket); }); - socket.on("all,chat", function(data) { + socket.on("all,chat", function (data) { if (data.hasOwnProperty("channel") && data.channel.indexOf("?") > -1) { var _list = data.channel.substring(0, data.channel.indexOf("?")); data.channel = _list; @@ -463,7 +528,7 @@ module.exports = function() { Chat.all_chat(data, guid, offline, socket); }); - socket.on("frontpage_lists", function(msg) { + socket.on("frontpage_lists", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -474,7 +539,7 @@ module.exports = function() { Frontpage.frontpage_lists(msg, socket); }); - socket.on("import_zoff", function(msg) { + socket.on("import_zoff", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -485,11 +550,11 @@ module.exports = function() { ListChange.addFromOtherList(msg, guid, offline, socket); }); - socket.on("now_playing", function(list, fn) { + socket.on("now_playing", function (list, fn) { List.now_playing(list, fn, socket); }); - socket.on("id", function(arr) { + socket.on("id", function (arr) { if (arr.hasOwnProperty("channel") && arr.channel.indexOf("?") > -1) { var _list = arr.channel.substring(0, arr.channel.indexOf("?")); arr.channel = _list; @@ -504,7 +569,7 @@ module.exports = function() { }); }); - socket.on("join_silent", function(msg) { + socket.on("join_silent", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -517,7 +582,7 @@ module.exports = function() { List.join_silent(msg, socket); }); - socket.on("list", function(msg) { + socket.on("list", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -548,7 +613,7 @@ module.exports = function() { Functions.get_short_id(socket); }); - socket.on("end", function(obj) { + socket.on("end", function (obj) { if (obj.hasOwnProperty("channel") && obj.channel.indexOf("?") > -1) { var _list = obj.channel.substring(0, obj.channel.indexOf("?")); obj.channel = _list; @@ -570,7 +635,7 @@ module.exports = function() { List.end(obj, coll, guid, offline, socket); }); - socket.on("addPlaylist", function(arr) { + socket.on("addPlaylist", function (arr) { if (arr.hasOwnProperty("channel") && arr.channel.indexOf("?") > -1) { var _list = arr.channel.substring(0, arr.channel.indexOf("?")); arr.channel = _list; @@ -581,7 +646,7 @@ module.exports = function() { ListChange.addPlaylist(arr, guid, offline, socket); }); - socket.on("add", function(arr) { + socket.on("add", function (arr) { if (arr.hasOwnProperty("list") && arr.list.indexOf("?") > -1) { var _list = arr.list.substring(0, arr.list.indexOf("?")); arr.list = _list; @@ -610,7 +675,7 @@ module.exports = function() { ListChange.add_function(arr, coll, guid, offline, socket); }); - socket.on("delete_all", function(msg) { + socket.on("delete_all", function (msg) { try { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); @@ -632,7 +697,7 @@ module.exports = function() { ListChange.delete_all(msg, coll, guid, offline, socket); }); - socket.on("vote", function(msg) { + socket.on("vote", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -655,7 +720,7 @@ module.exports = function() { ListChange.voteUndecided(msg, coll, guid, offline, socket); }); - socket.on("password", function(inp) { + socket.on("password", function (inp) { if (inp.hasOwnProperty("channel") && inp.channel.indexOf("?") > -1) { var _list = inp.channel.substring(0, inp.channel.indexOf("?")); inp.channel = _list; @@ -667,7 +732,7 @@ module.exports = function() { ListSettings.password(inp, coll, guid, offline, socket); }); - socket.on("skip", function(list) { + socket.on("skip", function (list) { if (list.hasOwnProperty("channel") && list.channel.indexOf("?") > -1) { var _list = list.channel.substring(0, list.channel.indexOf("?")); list.channel = _list; @@ -680,7 +745,7 @@ module.exports = function() { List.skip(list, guid, coll, offline, socket); }); - socket.on("conf", function(conf) { + socket.on("conf", function (conf) { if (conf.hasOwnProperty("channel") && conf.channel.indexOf("?") > -1) { var _list = conf.channel.substring(0, conf.channel.indexOf("?")); conf.channel = _list; @@ -694,7 +759,7 @@ module.exports = function() { ListSettings.conf_function(conf, coll, guid, offline, socket); }); - socket.on("shuffle", function(msg) { + socket.on("shuffle", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -716,7 +781,7 @@ module.exports = function() { ListChange.shuffle(msg, coll, guid, offline, socket); }); - socket.on("change_channel", function(obj) { + socket.on("change_channel", function (obj) { if (obj == undefined && coll != undefined) { obj = {}; obj.channel = coll; @@ -762,7 +827,7 @@ module.exports = function() { in_list = false; }); - socket.on("disconnect", function() { + socket.on("disconnect", function () { Functions.left_channel( coll, guid, @@ -774,7 +839,7 @@ module.exports = function() { ); }); - socket.on("disconnected", function() { + socket.on("disconnected", function () { Functions.left_channel( coll, guid, @@ -786,7 +851,7 @@ module.exports = function() { ); }); - socket.on("left_channel", function(msg) { + socket.on("left_channel", function (msg) { if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { var _list = msg.channel.substring(0, msg.channel.indexOf("?")); msg.channel = _list; @@ -814,7 +879,7 @@ module.exports = function() { } }); - socket.on("reconnect_failed", function() { + socket.on("reconnect_failed", function () { Functions.left_channel( coll, guid, @@ -826,7 +891,7 @@ module.exports = function() { ); }); - socket.on("connect_timeout", function() { + socket.on("connect_timeout", function () { Functions.left_channel( coll, guid, @@ -838,7 +903,7 @@ module.exports = function() { ); }); - socket.on("error", function() { + socket.on("error", function () { Functions.left_channel( coll, guid, @@ -850,7 +915,7 @@ module.exports = function() { ); }); - socket.on("pos", function(obj) { + socket.on("pos", function (obj) { if ( obj != undefined && obj.hasOwnProperty("channel") && @@ -893,11 +958,11 @@ module.exports = function() { return; } if (coll == undefined) return; - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { Functions.getSessionAdminUser( Functions.getSession(socket), coll, - function(userpass, adminpass) { + function (userpass, adminpass) { if (userpass != "" || obj.pass == undefined) { obj.pass = userpass; } else { @@ -943,4 +1008,4 @@ setTimeout(send_ping, 25000); }); }); }); -}*/ +}*/ \ No newline at end of file diff --git a/server/handlers/list.js b/server/handlers/list.js index 6f02f1d6..2ba16944 100644 --- a/server/handlers/list.js +++ b/server/handlers/list.js @@ -4,15 +4,7 @@ var Functions = require(pathThumbnails + "/handlers/functions.js"); var Frontpage = require(pathThumbnails + "/handlers/frontpage.js"); var projects = require(pathThumbnails + "/handlers/aggregates.js"); var crypto = require("crypto"); -var Filter = require("bad-words"); var Search = require(pathThumbnails + "/handlers/search.js"); -var filter = new Filter({ placeHolder: "x" }); -/*var filter = { - clean: function(str) { - return str; - } -}*/ -var request = require("request"); var db = require(pathThumbnails + "/handlers/db.js"); function now_playing(list, fn, socket) { @@ -20,7 +12,9 @@ function now_playing(list, fn, socket) { socket.emit("update_required"); return; } - db.collection(list).find({ now_playing: true }, function(err, docs) { + db.collection(list).find({ + now_playing: true + }, function (err, docs) { if (docs.length === 0) { fn("No song currently playing"); return; @@ -32,44 +26,44 @@ function now_playing(list, fn, socket) { } function join_silent(msg, socket) { - if (typeof msg === "object" && msg !== undefined && msg !== null) { - var channelName = msg.channel; - var tryingPassword = false; - var password = ""; - if(msg.password != "") { - tryingPassword = true; - password = Functions.decrypt_string(msg.password); - password = crypto - .createHash("sha256") - .update(password) - .digest("base64"); - } - - channelName = channelName.toLowerCase(); //.replace(/ /g,''); - channelName = Functions.removeEmojis(channelName).toLowerCase(); - db.collection(channelName + "_settings").find(function(err, docs) { - if(docs.length == 0) { - socket.emit("join_silent_declined", ""); - return; - } - if(docs[0].userpass == "" || docs[0].userpass == undefined || docs[0].userpass == password) { - socket.join(channelName); - socket.emit("join_silent_accepted", ""); - - send_play(channelName, socket); - } else { - socket.emit("join_silent_declined", ""); - } - }); - } else { - return; + if (typeof msg === "object" && msg !== undefined && msg !== null) { + var channelName = msg.channel; + var tryingPassword = false; + var password = ""; + if (msg.password != "") { + tryingPassword = true; + password = Functions.decrypt_string(msg.password); + password = crypto + .createHash("sha256") + .update(password) + .digest("base64"); } + + channelName = channelName.toLowerCase(); //.replace(/ /g,''); + channelName = Functions.removeEmojis(channelName).toLowerCase(); + db.collection(channelName + "_settings").find(function (err, docs) { + if (docs.length == 0) { + socket.emit("join_silent_declined", ""); + return; + } + if (docs[0].userpass == "" || docs[0].userpass == undefined || docs[0].userpass == password) { + socket.join(channelName); + socket.emit("join_silent_accepted", ""); + + send_play(channelName, socket); + } else { + socket.emit("join_silent_declined", ""); + } + }); + } else { + return; + } } function list(msg, guid, coll, offline, socket) { var socketid = socket.zoff_id; if (typeof msg === "object" && msg !== undefined && msg !== null) { - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass, gotten @@ -111,12 +105,14 @@ function list(msg, guid, coll, offline, socket) { coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); var pass = msg.pass; - db.collection("frontpage_lists").find({ _id: coll }, function( + db.collection("frontpage_lists").find({ + _id: coll + }, function ( err, frontpage_lists ) { if (frontpage_lists.length == 1) { - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { if ( docs.length == 0 || (docs.length > 0 && @@ -134,16 +130,18 @@ function list(msg, guid, coll, offline, socket) { Functions.getSession(socket), msg.pass, coll, - function() {} + function () {} ); - socket.emit("auth_accepted", { value: true }); + socket.emit("auth_accepted", { + value: true + }); } if (docs.length > 0 && docs[0].userpass != pass) { Functions.setSessionUserPass( Functions.getSession(socket), "", coll, - function() {} + function () {} ); } if ( @@ -177,11 +175,13 @@ function list(msg, guid, coll, offline, socket) { } }); } else { - db.createCollection(coll, function(err, docs) { - db.collection(coll).createIndex( - { id: 1 }, - { unique: true }, - function(e, d) { + db.createCollection(coll, function (err, docs) { + db.collection(coll).createIndex({ + id: 1 + }, { + unique: true + }, + function (e, d) { var configs = { addsongs: false, adminpass: "", @@ -202,21 +202,20 @@ function list(msg, guid, coll, offline, socket) { id: "config", toggleChat: true }; - db.collection(coll + "_settings").insert(configs, function( + db.collection(coll + "_settings").insert(configs, function ( err, docs ) { socket.join(coll); send_list(coll, socket, true, false, true); - db.collection("frontpage_lists").insert( - { + db.collection("frontpage_lists").insert({ _id: coll, count: 0, frontpage: true, accessed: Functions.get_time(), viewers: 1 }, - function(e, d) {} + function (e, d) {} ); Functions.check_inlist( coll, @@ -283,9 +282,8 @@ function skip(list, guid, coll, offline, socket, callback) { }, userpass: { expected: "string", - got: list.hasOwnProperty("userpass") - ? typeof list.userpass - : undefined + got: list.hasOwnProperty("userpass") ? + typeof list.userpass : undefined }, id: { expected: "string", @@ -296,7 +294,7 @@ function skip(list, guid, coll, offline, socket, callback) { return; } list.id = list.id + ""; - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass ) { @@ -318,7 +316,7 @@ function skip(list, guid, coll, offline, socket, callback) { .digest("base64"); } - db.collection(coll + "_settings").find(function(_err, docs) { + db.collection(coll + "_settings").find(function (_err, docs) { if ( docs.length > 0 && (docs[0].userpass == undefined || @@ -343,14 +341,14 @@ function skip(list, guid, coll, offline, socket, callback) { video_id, coll, err == "5" || - err == "100" || - err == "101" || - err == "150" || - err == 5 || - err == 100 || - err == 101 || - err == 150, - function(trueError) { + err == "100" || + err == "101" || + err == "150" || + err == 5 || + err == 100 || + err == 101 || + err == 150, + function (trueError) { var error = false; if (!trueError) { adminpass = list.pass; @@ -370,21 +368,23 @@ function skip(list, guid, coll, offline, socket, callback) { (docs[0].adminpass == hash && docs[0].adminpass !== "") || error ) { - db.collection("frontpage_lists").find({ _id: coll }, function( + db.collection("frontpage_lists").find({ + _id: coll + }, function ( err, frontpage_viewers ) { if ( error || ((strictSkip && - ((docs[0].adminpass == hash && - docs[0].adminpass !== "") || - docs[0].skips.length + 1 >= strictSkipNumber)) || + ((docs[0].adminpass == hash && + docs[0].adminpass !== "") || + docs[0].skips.length + 1 >= strictSkipNumber)) || (!strictSkip && ((frontpage_viewers[0].viewers / 2 <= - docs[0].skips.length + 1 && - !Functions.contains(docs[0].skips, guid) && - frontpage_viewers[0].viewers != 2) || + docs[0].skips.length + 1 && + !Functions.contains(docs[0].skips, guid) && + frontpage_viewers[0].viewers != 2) || (frontpage_viewers[0].viewers == 2 && docs[0].skips.length + 1 == 2 && !Functions.contains(docs[0].skips, guid)) || @@ -400,16 +400,18 @@ function skip(list, guid, coll, offline, socket, callback) { error, true, socket, - function() { + function () { change_song(coll, error, video_id, docs); socket.emit("toast", "skip"); - db.collection("user_names").find( - { guid: guid }, - function(err, docs) { + db.collection("user_names").find({ + guid: guid + }, + function (err, docs) { 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; if (n.length > 0 && n[0].icon) { icon = n[0].icon; @@ -428,10 +430,14 @@ function skip(list, guid, coll, offline, socket, callback) { "The channel is skipping too often, please wait " ); } else if (!Functions.contains(docs[0].skips, guid)) { - db.collection(coll + "_settings").update( - { id: "config" }, - { $push: { skips: guid } }, - function(err, d) { + db.collection(coll + "_settings").update({ + id: "config" + }, { + $push: { + skips: guid + } + }, + function (err, d) { if ( frontpage_viewers[0].viewers == 2 && !strictSkip @@ -450,13 +456,15 @@ function skip(list, guid, coll, offline, socket, callback) { "toast", to_skip + " more are needed to skip!" ); - db.collection("user_names").find( - { guid: guid }, - function(err, docs) { + db.collection("user_names").find({ + guid: guid + }, + function (err, docs) { 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; if (n.length > 0 && n[0].icon) { icon = n[0].icon; @@ -503,8 +511,7 @@ function change_song(coll, error, id, conf, callback, socket) { var startTime = conf[0].startTime; if (conf !== null && conf.length !== 0) { db.collection(coll).aggregate( - [ - { + [{ $match: { views: { $exists: false @@ -526,7 +533,7 @@ function change_song(coll, error, id, conf, callback, socket) { $limit: 2 } ], - function(err, now_playing_doc) { + function (err, now_playing_doc) { if (now_playing_doc.length == 0) { console.log("empty list", coll, callback, id, conf); } @@ -535,7 +542,10 @@ function change_song(coll, error, id, conf, callback, socket) { ((id && id == now_playing_doc[0].id) || !id) ) { if (error) { - db.collection(coll).remove({ now_playing: true, id: id }, function( + db.collection(coll).remove({ + now_playing: true, + id: id + }, function ( err, docs ) { @@ -551,19 +561,30 @@ function change_song(coll, error, id, conf, callback, socket) { }); } if (docs.deletedCount == 1) { - db.collection("frontpage_lists").update( - { _id: coll, count: { $gt: 0 } }, - { - $inc: { count: -1 }, - $set: { accessed: Functions.get_time() } + db.collection("frontpage_lists").update({ + _id: coll, + count: { + $gt: 0 + } + }, { + $inc: { + count: -1 + }, + $set: { + accessed: Functions.get_time() + } + }, { + upsert: true }, - { upsert: true }, - function(err, docs) {} + function (err, docs) {} ); } }); } else if (conf[0].removeplay === true) { - db.collection(coll).remove({ now_playing: true, id: id }, function( + db.collection(coll).remove({ + now_playing: true, + id: id + }, function ( err, docs ) { @@ -579,14 +600,22 @@ function change_song(coll, error, id, conf, callback, socket) { }); } if (docs.deletedCount == 1) { - db.collection("frontpage_lists").update( - { _id: coll, count: { $gt: 0 } }, - { - $inc: { count: -1 }, - $set: { accessed: Functions.get_time() } + db.collection("frontpage_lists").update({ + _id: coll, + count: { + $gt: 0 + } + }, { + $inc: { + count: -1 + }, + $set: { + accessed: Functions.get_time() + } + }, { + upsert: true }, - { upsert: true }, - function(err, docs) {} + function (err, docs) {} ); } }); @@ -596,17 +625,19 @@ function change_song(coll, error, id, conf, callback, socket) { conf[0].skipped_time != Functions.get_time()) || conf[0].skipped_time == undefined ) { - db.collection(coll).update( - { now_playing: true, id: id }, - { + db.collection(coll).update({ + now_playing: true, + id: id + }, { $set: { now_playing: false, votes: 0, guids: [] } + }, { + multi: true }, - { multi: true }, - function(err, docs) { + function (err, docs) { var next_song; if (now_playing_doc.length == 2) next_song = now_playing_doc[1].id; @@ -629,10 +660,14 @@ function change_song(coll, error, id, conf, callback, socket) { now_playing_doc.length > 1 && now_playing_doc[1].id == id ) { - db.collection(coll).update( - { id: now_playing_doc[0].id }, - { $set: { now_playing: false } }, - function(e, d) { + db.collection(coll).update({ + id: now_playing_doc[0].id + }, { + $set: { + now_playing: false + } + }, + function (e, d) { change_song(coll, error, id, conf, callback, socket, error); } ); @@ -649,8 +684,7 @@ function change_song(coll, error, id, conf, callback, socket) { function change_song_post(coll, next_song, conf, callback, socket, removed) { //coll = coll.replace(/ /g,''); db.collection(coll).aggregate( - [ - { + [{ $match: { now_playing: false, type: { @@ -669,7 +703,7 @@ function change_song_post(coll, next_song, conf, callback, socket, removed) { $limit: 2 } ], - function(err, docs) { + function (err, docs) { if (docs !== null && docs.length > 0) { var id = docs[0].id; if (next_song && next_song != id) { @@ -679,9 +713,10 @@ function change_song_post(coll, next_song, conf, callback, socket, removed) { return; } } - db.collection(coll).update( - { id: id, now_playing: false }, - { + db.collection(coll).update({ + id: id, + now_playing: false + }, { $set: { now_playing: true, votes: 0, @@ -689,7 +724,7 @@ function change_song_post(coll, next_song, conf, callback, socket, removed) { added: Functions.get_time() } }, - function(err, returnDocs) { + function (err, returnDocs) { if ( (returnDocs.hasOwnProperty("nModified") && returnDocs.nModified == 0) || @@ -699,15 +734,15 @@ function change_song_post(coll, next_song, conf, callback, socket, removed) { callback(); return; } - db.collection(coll + "_settings").update( - { id: "config" }, - { + db.collection(coll + "_settings").update({ + id: "config" + }, { $set: { startTime: Functions.get_time(), skips: [] } }, - function(err, returnDocs) { + function (err, returnDocs) { //db.collection(coll + "_settings").find({id: "config"}, function(err, conf){ if (!callback) { io.to(coll).emit("channel", { @@ -756,8 +791,7 @@ function change_song_post(coll, next_song, conf, callback, socket, removed) { function send_list(coll, socket, send, list_send, configs, shuffled) { //coll = coll.replace(/ /g,''); db.collection(coll + "_settings").aggregate( - [ - { + [{ $match: { id: "config" } @@ -766,7 +800,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { $project: projects.toShowConfig } ], - function(err, _conf) { + function (err, _conf) { var conf = _conf; if (conf.length == 0) { var conf = { @@ -789,38 +823,50 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { toggleChat: true, userpass: "" }; - db.collection(coll + "_settings").update( - { id: "config" }, - conf, - { upsert: true }, - function(err, docs) { + db.collection(coll + "_settings").update({ + id: "config" + }, + conf, { + upsert: true + }, + function (err, docs) { send_list(coll, socket, send, list_send, configs, shuffled); } ); } else { db.collection(coll).aggregate( - [ - { - $match: { type: { $ne: "suggested" } } + [{ + $match: { + type: { + $ne: "suggested" + } + } }, { $project: projects.project_object }, - { $sort: { now_playing: -1, votes: -1, added: 1 } } + { + $sort: { + now_playing: -1, + votes: -1, + added: 1 + } + } ], - function( + function ( err, docs //db.collection(coll).find({type: {$ne: "suggested"}}, function(err, docs) ) { if (docs.length > 0) { - db.collection(coll).find({ now_playing: true }, function( + db.collection(coll).find({ + now_playing: true + }, function ( err, np_docs ) { if (np_docs.length == 0) { db.collection(coll).aggregate( - [ - { + [{ $match: { views: { $exists: false @@ -842,11 +888,12 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { $limit: 1 } ], - function(err, now_playing_doc) { + function (err, now_playing_doc) { if (now_playing_doc[0].now_playing == false) { - db.collection(coll).update( - { id: now_playing_doc[0].id, now_playing: false }, - { + db.collection(coll).update({ + id: now_playing_doc[0].id, + now_playing: false + }, { $set: { now_playing: true, votes: 0, @@ -854,16 +901,16 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { added: Functions.get_time() } }, - function(err, returnDocs) { - db.collection(coll + "_settings").update( - { id: "config" }, - { + function (err, returnDocs) { + db.collection(coll + "_settings").update({ + id: "config" + }, { $set: { startTime: Functions.get_time(), skips: [] } }, - function(err, returnDocs) { + function (err, returnDocs) { Frontpage.update_frontpage( coll, now_playing_doc[0].id, @@ -888,8 +935,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { ); } else if (np_docs.length > 1) { db.collection(coll).aggregate( - [ - { + [{ $match: { now_playing: true } @@ -903,13 +949,21 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { } } ], - function(e, docs) { + function (e, docs) { var real_now_playing = docs[docs.length - 1]; - db.collection(coll).update( - { now_playing: true, id: { $ne: real_now_playing.id } }, - { $set: { now_playing: false } }, - { multi: true }, - function(e, d) { + db.collection(coll).update({ + now_playing: true, + id: { + $ne: real_now_playing.id + } + }, { + $set: { + now_playing: false + } + }, { + multi: true + }, + function (e, d) { send_list( coll, socket, @@ -932,7 +986,7 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { false, np_docs[0].id, conf, - function() { + function () { send_list( coll, socket, @@ -1013,11 +1067,13 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { desc: "", userpass: "" }; - db.collection(coll + "_settings").update( - { id: "config" }, - conf, - { upsert: true }, - function(err, docs) { + db.collection(coll + "_settings").update({ + id: "config" + }, + conf, { + upsert: true + }, + function (err, docs) { io.to(coll).emit("conf", conf); } ); @@ -1028,8 +1084,12 @@ function send_list(coll, socket, send, list_send, configs, shuffled) { ); if (socket) { db.collection(coll) - .find({ type: "suggested" }) - .sort({ added: 1 }, function(err, sugg) { + .find({ + type: "suggested" + }) + .sort({ + added: 1 + }, function (err, sugg) { socket.emit("suggested", sugg); }); } @@ -1068,14 +1128,14 @@ function end(obj, coll, guid, offline, socket) { } obj.id = obj.id + ""; id = id + ""; - var callback_function = function() { + var callback_function = function () { for (var i = 0; i < arguments.length; i++) { if (typeof arguments[i] == "function") { arguments[i](); } } }; - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { var authentication_needed = false; if ( docs.length > 0 && @@ -1084,7 +1144,7 @@ function end(obj, coll, guid, offline, socket) { callback_function = Functions.getSessionAdminUser; authentication_needed = true; } - callback_function(Functions.getSession(socket), coll, function(userpass) { + callback_function(Functions.getSession(socket), coll, function (userpass) { if (userpass != "" || obj.pass == undefined) { obj.pass = userpass; } else { @@ -1107,7 +1167,9 @@ function end(obj, coll, guid, offline, socket) { undefined, "place 13" ); - db.collection(coll).find({ now_playing: true }, function(err, np) { + db.collection(coll).find({ + now_playing: true + }, function (err, np) { if (err !== null) console.log(err); if ( np !== null && @@ -1142,8 +1204,10 @@ function end(obj, coll, guid, offline, socket) { function send_play(coll, socket, broadcast) { //coll = coll.replace(/ /g,''); - db.collection(coll).find({ now_playing: true }, function(err, np) { - db.collection(coll + "_settings").find(function(err, conf) { + db.collection(coll).find({ + now_playing: true + }, function (err, np) { + db.collection(coll + "_settings").find(function (err, conf) { if (err !== null) console.log(err); try { if (Functions.get_time() - conf[0].startTime > np[0].duration) { @@ -1155,7 +1219,11 @@ function send_play(coll, socket, broadcast) { else conf[0].userpass = false; if (!np.hasOwnProperty("start")) np.start = 0; if (!np.hasOwnProperty("end")) np.end = np.duration; - toSend = { np: np, conf: conf, time: Functions.get_time() }; + toSend = { + np: np, + conf: conf, + time: Functions.get_time() + }; if (socket === undefined) { io.to(coll).emit("np", toSend); // @@ -1200,25 +1268,35 @@ function sendColor(coll, socket, url, ajax, res) { //var url = 'https://img.youtube.com/vi/'+id+'/mqdefault.jpg'; Jimp.read(url) - .then(function(image) { + .then(function (image) { var c = ColorThief.getColor(image); if (ajax) { - res.header({ "Content-Type": "application/json" }); + res.header({ + "Content-Type": "application/json" + }); res.status(200).send(c); return; } else { if (socket) { - socket.emit("color", { color: c, only: true }); + socket.emit("color", { + color: c, + only: true + }); } else { - io.to(coll).emit("color", { color: c, only: false }); + io.to(coll).emit("color", { + color: c, + only: false + }); } } }) - .catch(function(err) { + .catch(function (err) { console.log("Crashed on fetching image, url is " + url); console.log("Is ajax: " + ajax); if (ajax) { - res.header({ "Content-Type": "application/json" }); + res.header({ + "Content-Type": "application/json" + }); res.status(404); return; } @@ -1228,8 +1306,7 @@ function sendColor(coll, socket, url, ajax, res) { function getNextSong(coll, socket, callback) { //coll = coll.replace(/ /g,''); db.collection(coll).aggregate( - [ - { + [{ $match: { views: { $exists: false @@ -1251,7 +1328,7 @@ function getNextSong(coll, socket, callback) { $limit: 1 } ], - function(err, doc) { + function (err, doc) { if (doc.length == 1) { var thumbnail = ""; var source = "youtube"; @@ -1290,4 +1367,4 @@ module.exports.change_song_post = change_song_post; module.exports.send_list = send_list; module.exports.end = end; module.exports.send_play = send_play; -module.exports.getNextSong = getNextSong; +module.exports.getNextSong = getNextSong; \ No newline at end of file diff --git a/server/handlers/list_change.js b/server/handlers/list_change.js index 8e7a4bf6..35355a8b 100644 --- a/server/handlers/list_change.js +++ b/server/handlers/list_change.js @@ -4,13 +4,6 @@ var Frontpage = require(pathThumbnails + "/handlers/frontpage.js"); var Search = require(pathThumbnails + "/handlers/search.js"); var Chat = require(pathThumbnails + "/handlers/chat.js"); var crypto = require("crypto"); -var Filter = require("bad-words"); -var filter = new Filter({ placeHolder: "x" }); -/*var filter = { -clean: function(str) { -return str; -} -}*/ var db = require(pathThumbnails + "/handlers/db.js"); function addFromOtherList(arr, guid, offline, socket) { @@ -29,9 +22,8 @@ function addFromOtherList(arr, guid, offline, socket) { }, new_channel: { expected: "string", - got: arr.hasOwnProperty("new_channel") - ? typeof arr.new_channel - : undefined + got: arr.hasOwnProperty("new_channel") ? + typeof arr.new_channel : undefined } }; socket.emit("update_required", result); @@ -39,7 +31,9 @@ function addFromOtherList(arr, guid, offline, socket) { } var channel = arr.channel; //.replace(/ /g,'').toLowerCase(); var new_channel = Functions.encodeChannelName(arr.new_channel); //.replace(/ /g, '').toLowerCase(); - db.collection("frontpage_lists").find({ _id: new_channel }, function( + db.collection("frontpage_lists").find({ + _id: new_channel + }, function ( err, fp ) { @@ -50,7 +44,7 @@ function addFromOtherList(arr, guid, offline, socket) { Functions.getSessionAdminUser( Functions.getSession(socket), channel, - function(userpass, adminpass) { + function (userpass, adminpass) { if (userpass != "" || arr.userpass == undefined) { arr.userpass = userpass; } else { @@ -69,7 +63,7 @@ function addFromOtherList(arr, guid, offline, socket) { Functions.getSessionAdminUser( Functions.getSession(socket), new_channel, - function(userpass) { + function (userpass) { var otheruser = ""; if (userpass != "") { otheruser = userpass; @@ -80,17 +74,23 @@ function addFromOtherList(arr, guid, offline, socket) { .digest("base64"); } - db.collection(channel).find({ now_playing: true }, function( + db.collection(channel).find({ + now_playing: true + }, function ( e, np ) { var project_object = { id: 1, added: 1, - guids: { $literal: [] }, + guids: { + $literal: [] + }, now_playing: 1, title: 1, - votes: { $literal: 0 }, + votes: { + $literal: 0 + }, start: 1, duration: 1, end: 1, @@ -101,21 +101,25 @@ function addFromOtherList(arr, guid, offline, socket) { }; var to_set_np = true; if (np.length > 0) { - project_object.now_playing = { $literal: false }; + project_object.now_playing = { + $literal: false + }; to_set_np = false; } - db.collection(new_channel + "_settings").find( - { id: "config" }, - function(e, this_conf) { + db.collection(new_channel + "_settings").find({ + id: "config" + }, + function (e, this_conf) { if ( this_conf.length > 0 && (this_conf[0].userpass == "" || !this_conf[0].userpass || this_conf[0].userpass == otheruser) ) { - db.collection(channel + "_settings").find( - { id: "config" }, - function(e, this_conf) { + db.collection(channel + "_settings").find({ + id: "config" + }, + function (e, this_conf) { var hash = arr.adminpass; if (this_conf.length == 0) { socket.emit("toast", "nolist"); @@ -128,8 +132,8 @@ function addFromOtherList(arr, guid, offline, socket) { ) { var connection_id = Functions.hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers["accept-language"] + socket.handshake.address + + socket.handshake.headers["accept-language"] ); Functions.checkTimeout( "add_playlist", @@ -139,7 +143,7 @@ function addFromOtherList(arr, guid, offline, socket) { this_conf[0].adminpass, hash, socket, - function() { + function () { if ( (this_conf[0].addsongs === true && (hash == this_conf[0].adminpass || @@ -147,15 +151,16 @@ function addFromOtherList(arr, guid, offline, socket) { this_conf[0].addsongs === false ) { db.collection(new_channel).aggregate( - [ - { - $match: { type: "video" } + [{ + $match: { + type: "video" + } }, { $project: project_object } ], - function(e, docs) { + function (e, docs) { var path = require("path"); var mongo_config = require(path.join( path.join(__dirname, "../config/"), @@ -171,29 +176,31 @@ function addFromOtherList(arr, guid, offline, socket) { "/"; MongoClient.connect( url, - function(err, _db) { + function (err, _db) { var dbo = _db.db(mongo_config.config); dbo .collection(channel) .insertMany( - docs, - { ordered: false }, - function(err, res) { - db.collection(channel).count( - { - type: { $ne: "suggested" } + docs, { + ordered: false + }, + function (err, res) { + db.collection(channel).count({ + type: { + $ne: "suggested" + } }, - function(err, count) { + function (err, count) { db.collection( channel + "_settings" - ).update( - { id: "config" }, - { + ).update({ + id: "config" + }, { $set: { startTime: Functions.get_time() } }, - function(e, d) { + function (e, d) { if (to_set_np) { var to_change = { _id: channel, @@ -203,11 +210,10 @@ function addFromOtherList(arr, guid, offline, socket) { }; db.collection( new_channel - ).find( - { + ).find({ now_playing: true }, - function( + function ( e, np_docs ) { @@ -217,36 +223,35 @@ function addFromOtherList(arr, guid, offline, socket) { np_docs[0].title; db.collection( "frontpage_lists" - ).find( - { + ).find({ _id: new_channel }, - function( + function ( e, doc ) { if ( doc.length > - 0 && + 0 && ((doc[0] - .thumbnail != - "" && - doc[0] + .thumbnail != + "" && + doc[0] .thumbnail != undefined && - (doc[0].thumbnail.indexOf( - "https://i1.sndcdn.com" - ) > -1 || - doc[0].thumbnail.indexOf( - "https://w1.sndcdn.com" - ) > + (doc[0].thumbnail.indexOf( + "https://i1.sndcdn.com" + ) > -1 || + doc[0].thumbnail.indexOf( + "https://w1.sndcdn.com" + ) > -1)) || (doc[0] .thumbnail == "" || doc[0] - .thumbnail == - undefined)) + .thumbnail == + undefined)) ) { to_change.thumbnail = np_docs[0].thumbnail; @@ -254,14 +259,12 @@ function addFromOtherList(arr, guid, offline, socket) { db.collection( "frontpage_lists" - ).update( - { + ).update({ _id: channel - }, - { + }, { $set: to_change }, - function( + function ( e, d ) { @@ -290,14 +293,14 @@ function addFromOtherList(arr, guid, offline, socket) { } else { db.collection( "frontpage_lists" - ).update( - { _id: channel }, - { + ).update({ + _id: channel + }, { $set: { count: count } }, - function(e, d) { + function (e, d) { List.send_list( channel, undefined, @@ -382,7 +385,9 @@ function addPlaylist(arr, guid, offline, socket) { socket.emit("toast", "Empty list.."); return; } - db.collection("frontpage_lists").find({ _id: channel }, function(err, fp) { + db.collection("frontpage_lists").find({ + _id: channel + }, function (err, fp) { if (fp.length == 0) { socket.emit("toast", "nolist"); return; @@ -391,7 +396,7 @@ function addPlaylist(arr, guid, offline, socket) { Functions.getSessionAdminUser( Functions.getSession(socket), channel, - function(userpass, adminpass) { + function (userpass, adminpass) { if (userpass != "" || arr.userpass == undefined) { arr.userpass = userpass; } else { @@ -407,12 +412,15 @@ function addPlaylist(arr, guid, offline, socket) { Functions.hash_pass(Functions.decrypt_string(arr.adminpass), true) ); } - db.collection(channel).find({ now_playing: true }, function(e, np) { + db.collection(channel).find({ + now_playing: true + }, function (e, np) { var now_playing = false; if (np.length == 0) now_playing = true; - db.collection(channel + "_settings").find( - { id: "config" }, - function(e, conf) { + db.collection(channel + "_settings").find({ + id: "config" + }, + function (e, conf) { if (arr.length == 0 || arr.songs.length == 0) { socket.emit("toast", "Empty list.."); return; @@ -432,8 +440,8 @@ function addPlaylist(arr, guid, offline, socket) { ) { var connection_id = Functions.hash_pass( socket.handshake.headers["user-agent"] + - socket.handshake.address + - socket.handshake.headers["accept-language"] + socket.handshake.address + + socket.handshake.headers["accept-language"] ); Functions.checkTimeout( "add_playlist", @@ -443,7 +451,7 @@ function addPlaylist(arr, guid, offline, socket) { conf[0].adminpass, hash, socket, - function() { + function () { var path = require("path"); var mongo_config = require(path.join( path.join(__dirname, "../config/"), @@ -458,7 +466,7 @@ function addPlaylist(arr, guid, offline, socket) { "/"; MongoClient.connect( url, - function(err, _db) { + function (err, _db) { var dbo = _db.db(mongo_config.config); var number_elements = arr.songs.length + 1; var time = Functions.get_time() - number_elements; @@ -516,26 +524,29 @@ function addPlaylist(arr, guid, offline, socket) { } } else if (this_element.source == "youtube") this_element.thumbnail = - "https://img.youtube.com/vi/" + - this_element.id + - "/mqdefault.jpg"; + "https://img.youtube.com/vi/" + + this_element.id + + "/mqdefault.jpg"; if (now_playing) { now_playing = false; } bulk.insert(this_element); } - bulk.execute(function(err, results) { - db.collection(channel).count( - { type: { $ne: "suggested" } }, - function(err, count) { - db.collection(channel + "_settings").update( - { id: "config" }, - { + bulk.execute(function (err, results) { + db.collection(channel).count({ + type: { + $ne: "suggested" + } + }, + function (err, count) { + db.collection(channel + "_settings").update({ + id: "config" + }, { $set: { startTime: Functions.get_time() } }, - function(e, d) { + function (e, d) { if (to_set_np) { var to_change = { _id: channel, @@ -543,32 +554,35 @@ function addPlaylist(arr, guid, offline, socket) { frontpage: true, accessed: Functions.get_time() }; - db.collection(channel).find( - { now_playing: true }, - function(e, np_docs) { + db.collection(channel).find({ + now_playing: true + }, + function (e, np_docs) { to_change.id = np_docs[0].id; to_change.title = np_docs[0].title; db.collection( "frontpage_lists" - ).find({ _id: channel }, function( + ).find({ + _id: channel + }, function ( e, doc ) { if ( doc.length > 0 && ((doc[0].thumbnail != "" && - doc[0].thumbnail != + doc[0].thumbnail != undefined && - (doc[0].thumbnail.indexOf( - "https://i1.sndcdn.com" - ) > -1 || - doc[0].thumbnail.indexOf( - "https://w1.sndcdn.com" - ) > -1)) || + (doc[0].thumbnail.indexOf( + "https://i1.sndcdn.com" + ) > -1 || + doc[0].thumbnail.indexOf( + "https://w1.sndcdn.com" + ) > -1)) || (doc[0].thumbnail == "" || doc[0].thumbnail == - undefined)) + undefined)) ) { to_change.thumbnail = np_docs[0].thumbnail; @@ -576,10 +590,12 @@ function addPlaylist(arr, guid, offline, socket) { db.collection( "frontpage_lists" - ).update( - { _id: channel }, - { $set: to_change }, - function(e, d) { + ).update({ + _id: channel + }, { + $set: to_change + }, + function (e, d) { List.send_list( channel, undefined, @@ -604,10 +620,14 @@ function addPlaylist(arr, guid, offline, socket) { } else { db.collection( "frontpage_lists" - ).update( - { _id: channel }, - { $set: { count: count } }, - function(e, d) { + ).update({ + _id: channel + }, { + $set: { + count: count + } + }, + function (e, d) { List.send_list( channel, undefined, @@ -753,9 +773,8 @@ function add_function(arr, coll, guid, offline, socket) { }, adminpass: { expected: "string", - got: arr.hasOwnProperty("adminpass") - ? typeof arr.adminpass - : undefined + got: arr.hasOwnProperty("adminpass") ? + typeof arr.adminpass : undefined }, source: { expected: "string (youtube or soundcloud)", @@ -763,9 +782,8 @@ function add_function(arr, coll, guid, offline, socket) { }, thumbnail: { expected: "url if source == soundcloud", - got: arr.hasOwnProperty("thumbnail") - ? typeof arr.thumbnail - : undefined + got: arr.hasOwnProperty("thumbnail") ? + typeof arr.thumbnail : undefined } }; socket.emit("update_required", result); @@ -774,7 +792,7 @@ function add_function(arr, coll, guid, offline, socket) { if (arr.hasOwnProperty("offsiteAdd") && arr.offsiteAdd) { coll = arr.list; } - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass ) { @@ -793,7 +811,7 @@ function add_function(arr, coll, guid, offline, socket) { .update(Functions.decrypt_string(arr.pass)) .digest("base64"); } - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { if ( docs.length > 0 && (docs[0].userpass == undefined || @@ -820,22 +838,28 @@ function add_function(arr, coll, guid, offline, socket) { var source = arr.source; var tags = arr.tags; conf = docs; - Chat.getUserNameByGuid(guid, function(username) { + Chat.getUserNameByGuid(guid, function (username) { if ( docs !== null && docs.length !== 0 && ((docs[0].addsongs === true && - (hash == docs[0].adminpass || docs[0].adminpass === "")) || + (hash == docs[0].adminpass || docs[0].adminpass === "")) || docs[0].addsongs === false) ) { - db.collection(coll).find( - { id: id, type: { $ne: "suggested" } }, - function(err, docs) { + db.collection(coll).find({ + id: id, + type: { + $ne: "suggested" + } + }, + function (err, docs) { if (docs !== null && docs.length === 0) { var guids = [guid]; var added = Functions.get_time(); var votes = 1; - db.collection(coll).find({ now_playing: true }, function( + db.collection(coll).find({ + now_playing: true + }, function ( err, docs ) { @@ -871,26 +895,31 @@ function add_function(arr, coll, guid, offline, socket) { } } else if (source == "youtube") new_song.thumbnail = - "https://img.youtube.com/vi/" + - new_song.id + - "/mqdefault.jpg"; - db.collection(coll).update( - { id: id }, - new_song, - { upsert: true }, - function(err, docs) { + "https://img.youtube.com/vi/" + + new_song.id + + "/mqdefault.jpg"; + db.collection(coll).update({ + id: id + }, + new_song, { + upsert: true + }, + function (err, docs) { new_song._id = "asd"; if (np) { List.send_list(coll, undefined, false, true, false); - db.collection(coll + "_settings").update( - { id: "config" }, - { $set: { startTime: Functions.get_time() } } - ); + db.collection(coll + "_settings").update({ + id: "config" + }, { + $set: { + startTime: Functions.get_time() + } + }); List.send_play(coll, undefined); var thumbnail = - arr.thumbnail != undefined - ? arr.thumbnail - : undefined; + arr.thumbnail != undefined ? + arr.thumbnail : + undefined; Frontpage.update_frontpage( coll, id, @@ -912,14 +941,19 @@ function add_function(arr, coll, guid, offline, socket) { else if (source == "soundcloud") Search.get_genres_soundcloud(new_song, coll); } - db.collection("frontpage_lists").update( - { _id: coll }, - { - $inc: { count: 1 }, - $set: { accessed: Functions.get_time() } + db.collection("frontpage_lists").update({ + _id: coll + }, { + $inc: { + count: 1 + }, + $set: { + accessed: Functions.get_time() + } + }, { + upsert: true }, - { upsert: true }, - function(err, docs) {} + function (err, docs) {} ); List.getNextSong(coll, undefined); } @@ -932,7 +966,9 @@ function add_function(arr, coll, guid, offline, socket) { } ); } else { - db.collection(coll).find({ id: id }, function(err, docs) { + db.collection(coll).find({ + id: id + }, function (err, docs) { if (docs.length === 0) { var suggestedAdd = { added: Functions.get_time(), @@ -955,11 +991,14 @@ function add_function(arr, coll, guid, offline, socket) { } else { suggestedAdd.source = "youtube"; } - db.collection(coll).update( - { id: id }, - { $set: suggestedAdd }, - { upsert: true }, - function(err, docs) { + db.collection(coll).update({ + id: id + }, { + $set: suggestedAdd + }, { + upsert: true + }, + function (err, docs) { socket.emit("toast", "suggested"); var toSend = suggestedAdd; toSend.guids = []; @@ -1035,9 +1074,8 @@ function voteUndecided(msg, coll, guid, offline, socket) { }, adminpass: { expected: "adminpass", - got: msg.hasOwnProperty("adminpass") - ? typeof msg.adminpass - : undefined + got: msg.hasOwnProperty("adminpass") ? + typeof msg.adminpass : undefined }, pass: { expected: "string", @@ -1050,7 +1088,7 @@ function voteUndecided(msg, coll, guid, offline, socket) { coll = msg.channel.toLowerCase(); //.replace(/ /g,''); coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass ) { @@ -1070,7 +1108,9 @@ function voteUndecided(msg, coll, guid, offline, socket) { .digest("base64"); } - db.collection(coll + "_settings").find({ id: "config" }, function( + db.collection(coll + "_settings").find({ + id: "config" + }, function ( err, docs ) { @@ -1098,7 +1138,7 @@ function voteUndecided(msg, coll, guid, offline, socket) { docs !== null && docs.length !== 0 && ((docs[0].vote === true && - (hash == docs[0].adminpass || docs[0].adminpass === "")) || + (hash == docs[0].adminpass || docs[0].adminpass === "")) || docs[0].vote === false) ) { vote(coll, id, guid, socket); @@ -1145,7 +1185,7 @@ function shuffle(msg, coll, guid, offline, socket) { coll = msg.channel.toLowerCase(); //.replace(/ /g,''); coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass ) { @@ -1174,7 +1214,7 @@ function shuffle(msg, coll, guid, offline, socket) { "foo", "bar", socket, - function() { + function () { Functions.check_inlist( coll, guid, @@ -1184,7 +1224,7 @@ function shuffle(msg, coll, guid, offline, socket) { "place 7" ); var hash = msg.adminpass; - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { if ( docs.length > 0 && (docs[0].userpass == undefined || @@ -1198,8 +1238,10 @@ function shuffle(msg, coll, guid, offline, socket) { docs[0].shuffle === false) ) { db.collection(coll) - .find({ now_playing: false }) - .forEach(function(err, docs) { + .find({ + now_playing: false + }) + .forEach(function (err, docs) { if (!docs) { List.send_list(coll, undefined, false, true, false, true); socket.emit("toast", "shuffled"); @@ -1207,10 +1249,13 @@ function shuffle(msg, coll, guid, offline, socket) { return; } else { num = Math.floor(Math.random() * 1000000); - db.collection(coll).update( - { id: docs.id }, - { $set: { added: num } } - ); + db.collection(coll).update({ + id: docs.id + }, { + $set: { + added: num + } + }); } }); } else socket.emit("toast", "wrongpass"); @@ -1219,7 +1264,7 @@ function shuffle(msg, coll, guid, offline, socket) { } }); - var complete = function(tot, curr) { + var complete = function (tot, curr) { if (tot == curr) { List.send_list(coll, undefined, false, true, false); List.getNextSong(coll, undefined); @@ -1236,33 +1281,45 @@ function del(params, socket, socketid) { //coll = coll.replace(/_/g, "").replace(/ /g,''); //coll = filter.clean(coll); - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { if ( docs !== null && docs.length !== 0 && docs[0].adminpass == params.adminpass ) { - db.collection(coll).find({ id: params.id }, function(err, docs) { + db.collection(coll).find({ + id: params.id + }, function (err, docs) { var dont_increment = false; if (docs[0]) { if (docs[0].type == "suggested") { dont_increment = true; } - db.collection(coll).remove({ id: params.id }, function(err, docs) { + db.collection(coll).remove({ + id: params.id + }, function (err, docs) { socket.emit("toast", "deletesong"); io.to(coll).emit("channel", { type: "deleted", value: params.id }); if (!dont_increment) - db.collection("frontpage_lists").update( - { _id: coll, count: { $gt: 0 } }, - { - $inc: { count: -1 }, - $set: { accessed: Functions.get_time() } + db.collection("frontpage_lists").update({ + _id: coll, + count: { + $gt: 0 + } + }, { + $inc: { + count: -1 + }, + $set: { + accessed: Functions.get_time() + } + }, { + upsert: true }, - { upsert: true }, - function(err, docs) {} + function (err, docs) {} ); }); } @@ -1283,9 +1340,8 @@ function delete_all(msg, coll, guid, offline, socket) { }, adminpass: { expected: "adminpass", - got: msg.hasOwnProperty("adminpass") - ? typeof msg.adminpass - : undefined + got: msg.hasOwnProperty("adminpass") ? + typeof msg.adminpass : undefined }, pass: { expected: "string", @@ -1301,7 +1357,7 @@ function delete_all(msg, coll, guid, offline, socket) { //coll = coll.replace(/ /g,''); coll = Functions.removeEmojis(coll).toLowerCase(); //coll = filter.clean(coll); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass, gotten @@ -1323,7 +1379,7 @@ function delete_all(msg, coll, guid, offline, socket) { } var hash = msg.adminpass; var hash_userpass = msg.pass; - db.collection(coll + "_settings").find(function(err, conf) { + db.collection(coll + "_settings").find(function (err, conf) { if (conf.length == 1 && conf) { conf = conf[0]; if ( @@ -1335,16 +1391,27 @@ function delete_all(msg, coll, guid, offline, socket) { conf.userpass != undefined && conf.pass == hash_userpass)) ) { - db.collection(coll).remove( - { views: { $exists: false }, type: "video" }, - { multi: true }, - function(err, succ) { + db.collection(coll).remove({ + views: { + $exists: false + }, + type: "video" + }, { + multi: true + }, + function (err, succ) { List.send_list(coll, false, true, true, true); - db.collection("frontpage_lists").update( - { _id: coll }, - { $set: { count: 0, accessed: Functions.get_time() } }, - { upsert: true }, - function(err, docs) {} + db.collection("frontpage_lists").update({ + _id: coll + }, { + $set: { + count: 0, + accessed: Functions.get_time() + } + }, { + upsert: true + }, + function (err, docs) {} ); socket.emit("toast", "deleted_songs"); } @@ -1369,22 +1436,31 @@ function delete_all(msg, coll, guid, offline, socket) { function vote(coll, id, guid, socket) { //coll = coll.replace(/ /g,''); - db.collection(coll).find( - { id: id, now_playing: false, type: "video" }, - function(err, docs) { + db.collection(coll).find({ + id: id, + now_playing: false, + type: "video" + }, + function (err, docs) { if ( docs !== null && docs.length > 0 && !Functions.contains(docs[0].guids, guid) ) { - db.collection(coll).update( - { id: id }, - { - $inc: { votes: 1 }, - $set: { added: Functions.get_time() }, - $push: { guids: guid } + db.collection(coll).update({ + id: id + }, { + $inc: { + votes: 1 + }, + $set: { + added: Functions.get_time() + }, + $push: { + guids: guid + } }, - function(err, docs) { + function (err, docs) { socket.emit("toast", "voted"); io.to(coll).emit("channel", { type: "vote", @@ -1409,4 +1485,4 @@ module.exports.voteUndecided = voteUndecided; module.exports.shuffle = shuffle; module.exports.del = del; module.exports.delete_all = delete_all; -module.exports.vote = vote; +module.exports.vote = vote; \ No newline at end of file diff --git a/server/handlers/list_settings.js b/server/handlers/list_settings.js index c6df7f5f..e139c6d3 100644 --- a/server/handlers/list_settings.js +++ b/server/handlers/list_settings.js @@ -1,12 +1,5 @@ var Functions = require(pathThumbnails + "/handlers/functions.js"); var crypto = require("crypto"); -var Filter = require("bad-words"); -var filter = new Filter({ placeHolder: "x" }); -/*var filter = { - clean: function(str) { - return str; - } -}*/ var projects = require(pathThumbnails + "/handlers/aggregates.js"); var db = require(pathThumbnails + "/handlers/db.js"); @@ -48,12 +41,12 @@ function password(inp, coll, guid, offline, socket) { uncrypted = pw; pw = Functions.hash_pass(Functions.decrypt_string(pw), true); Functions.check_inlist(coll, guid, socket, offline, undefined, "place 8"); - Functions.getSessionAdminUser(sessionId, coll, function( + Functions.getSessionAdminUser(sessionId, coll, function ( userpass, adminpass ) { adminpass = Functions.hash_pass(adminpass); - db.collection(coll + "_settings").find(function(err, docs) { + db.collection(coll + "_settings").find(function (err, docs) { if (docs !== null && docs.length !== 0) { if ( docs[0].adminpass === "" || @@ -63,11 +56,15 @@ function password(inp, coll, guid, offline, socket) { sessionId, inp.password, coll, - function() { - db.collection(coll + "_settings").update( - { id: "config" }, - { $set: { adminpass: Functions.hash_pass(pw) } }, - function(err, docs) { + function () { + db.collection(coll + "_settings").update({ + id: "config" + }, { + $set: { + adminpass: Functions.hash_pass(pw) + } + }, + function (err, docs) { if (adminpass != pw && adminpass != "") { socket.emit("toast", "changedpass"); } else { @@ -86,11 +83,15 @@ function password(inp, coll, guid, offline, socket) { sessionId, inp.password, coll, - function() { - db.collection(coll + "_settings").update( - { id: "config" }, - { $set: { adminpass: Functions.hash_pass(pw) } }, - function(err, docs) { + function () { + db.collection(coll + "_settings").update({ + id: "config" + }, { + $set: { + adminpass: Functions.hash_pass(pw) + } + }, + function (err, docs) { if (adminpass != pw) { socket.emit("toast", "changedpass"); } @@ -104,7 +105,7 @@ function password(inp, coll, guid, offline, socket) { Functions.getSession(socket), "", coll, - function() { + function () { socket.emit("toast", "wrongpass"); socket.emit("pw", false); } @@ -146,7 +147,7 @@ function conf_function(params, coll, guid, offline, socket) { Functions.check_inlist(coll, guid, socket, offline, undefined, "place 9"); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass, gotten @@ -181,15 +182,13 @@ function conf_function(params, coll, guid, offline, socket) { var result = { adminpass: { expected: "string", - got: params.hasOwnProperty("adminpass") - ? typeof params.adminpass - : undefined + got: params.hasOwnProperty("adminpass") ? + typeof params.adminpass : undefined }, userpass: { expected: "string", - got: params.hasOwnProperty("userpass") - ? typeof params.userpass - : undefined + got: params.hasOwnProperty("userpass") ? + typeof params.userpass : undefined }, vote: { expected: "boolean", @@ -197,39 +196,33 @@ function conf_function(params, coll, guid, offline, socket) { }, addsongs: { expected: "boolean", - got: params.hasOwnProperty("addsongs") - ? typeof params.addsongs - : undefined + got: params.hasOwnProperty("addsongs") ? + typeof params.addsongs : undefined }, longsongs: { expected: "boolean", - got: params.hasOwnProperty("longsongs") - ? typeof params.longsongs - : undefined + got: params.hasOwnProperty("longsongs") ? + typeof params.longsongs : undefined }, frontpage: { expected: "boolean", - got: params.hasOwnProperty("frontpage") - ? typeof params.frontpage - : undefined + got: params.hasOwnProperty("frontpage") ? + typeof params.frontpage : undefined }, skipping: { expected: "boolean", - got: params.hasOwnProperty("skipping") - ? typeof params.skipping - : undefined + got: params.hasOwnProperty("skipping") ? + typeof params.skipping : undefined }, shuffling: { expected: "boolean", - got: params.hasOwnProperty("shuffling") - ? typeof params.shuffling - : undefined + got: params.hasOwnProperty("shuffling") ? + typeof params.shuffling : undefined }, userpass_changed: { expected: "boolean", - got: params.hasOwnProperty("userpass_changed") - ? typeof params.userpass_changed - : undefined + got: params.hasOwnProperty("userpass_changed") ? + typeof params.userpass_changed : undefined } }; socket.emit("update_required", result); @@ -266,15 +259,16 @@ function conf_function(params, coll, guid, offline, socket) { hash = adminpass; } if (userpass != "") { - if (!params.userpass_changed && gotten) { - } else { + if (!params.userpass_changed && gotten) {} else { userpass = crypto .createHash("sha256") .update(userpass) .digest("base64"); } } - db.collection(coll + "_settings").find({ id: "config" }, function( + db.collection(coll + "_settings").find({ + id: "config" + }, function ( err, docs ) { @@ -326,20 +320,19 @@ function conf_function(params, coll, guid, offline, socket) { } else if (frontpage) { obj["userpass"] = ""; } - db.collection(coll + "_settings").update( - { id: "config" }, - { + db.collection(coll + "_settings").update({ + id: "config" + }, { $set: obj }, - function(err, docs) { + function (err, docs) { Functions.setSessionUserPass( Functions.getSession(socket), obj["userpass"], coll, - function() { + function () { db.collection(coll + "_settings").aggregate( - [ - { + [{ $match: { id: "config" } @@ -348,7 +341,7 @@ function conf_function(params, coll, guid, offline, socket) { $project: projects.toShowConfig } ], - function(err, docs) { + function (err, docs) { if (docs[0].adminpass !== "") docs[0].adminpass = true; if ( docs[0].hasOwnProperty("userpass") && @@ -359,16 +352,17 @@ function conf_function(params, coll, guid, offline, socket) { io.to(coll).emit("conf", docs); socket.emit("toast", "savedsettings"); - db.collection("frontpage_lists").update( - { _id: coll }, - { + db.collection("frontpage_lists").update({ + _id: coll + }, { $set: { frontpage: frontpage, accessed: Functions.get_time() } + }, { + upsert: true }, - { upsert: true }, - function(err, docs) {} + function (err, docs) {} ); } ); @@ -393,4 +387,4 @@ function conf_function(params, coll, guid, offline, socket) { } module.exports.password = password; -module.exports.conf_function = conf_function; +module.exports.conf_function = conf_function; \ No newline at end of file diff --git a/server/handlers/notifications.js b/server/handlers/notifications.js index 851e468b..e262c77f 100644 --- a/server/handlers/notifications.js +++ b/server/handlers/notifications.js @@ -8,7 +8,7 @@ function requested_change(type, string, channel) { let transporter = nodemailer.createTransport(mailconfig); - transporter.verify(function(error, success) { + transporter.verify(function (error, success) { if (error) { return; } else { @@ -44,4 +44,4 @@ function requested_change(type, string, channel) { } } -module.exports.requested_change = requested_change; +module.exports.requested_change = requested_change; \ No newline at end of file diff --git a/server/handlers/search.js b/server/handlers/search.js index 27958f95..d43b6a74 100644 --- a/server/handlers/search.js +++ b/server/handlers/search.js @@ -19,7 +19,10 @@ function check_if_error_or_blocked(id, channel, errored, callback) { callback(false); return; } - db.collection(channel).find({ id: id, now_playing: true }, function( + db.collection(channel).find({ + id: id, + now_playing: true + }, function ( err, song ) { @@ -29,16 +32,14 @@ function check_if_error_or_blocked(id, channel, errored, callback) { } var song_info = song[0]; if (song_info.source != "soundcloud") { - request( - { + request({ type: "GET", - url: - "https://www.googleapis.com/youtube/v3/videos?part=id,status,contentDetails&key=" + + url: "https://www.googleapis.com/youtube/v3/videos?part=id,status,contentDetails&key=" + key + "&id=" + song_info.id }, - function(error, response, body) { + function (error, response, body) { try { var resp = JSON.parse(body); if (resp.pageInfo.totalResults == 0) { @@ -62,7 +63,7 @@ function check_if_error_or_blocked(id, channel, errored, callback) { ) && resp.items[0].contentDetails.regionRestriction.blocked.length > 0 ) { - var any = resp.items[0].contentDetails.blocked.some(function( + var any = resp.items[0].contentDetails.blocked.some(function ( element ) { return countryCodes.indexOf(element) > -1; @@ -81,16 +82,14 @@ function check_if_error_or_blocked(id, channel, errored, callback) { } ); } else { - request( - { + request({ type: "GET", - url: - "http://api.soundcloud.com/tracks/" + + url: "http://api.soundcloud.com/tracks/" + song_info.id + "?client_id=" + soundcloudKey }, - function(error, response, body) { + function (error, response, body) { try { var resp = JSON.parse(body); if (resp.sharing != "public" || resp.embeddable_by != "all") { @@ -116,10 +115,10 @@ function filterFunction(el) { function get_genres_soundcloud(song, channel) { request( "http://api.soundcloud.com/tracks/" + - song.id + - "?client_id=" + - soundcloudKey, - function(err, response, body) { + song.id + + "?client_id=" + + soundcloudKey, + function (err, response, body) { if (err) { console.log("error start", err, song, "error end"); return; @@ -138,14 +137,14 @@ function get_genres_soundcloud(song, channel) { .concat(object.tag_list.toLowerCase().split('"')); genre = genre.filter(filterFunction); - db.collection(channel).update( - { id: song.id }, - { + db.collection(channel).update({ + id: song.id + }, { $set: { tags: genre } }, - function(e, d) {} + function (e, d) {} ); } catch (e) { console.log("errored 2", e); @@ -203,10 +202,10 @@ function get_genres_soundcloud_recursive(arr, channel, i, callback) { var song = arr[i]; request( "http://api.soundcloud.com/tracks/" + - song.id + - "?client_id=" + - soundcloudKey, - function(err, response, body) { + song.id + + "?client_id=" + + soundcloudKey, + function (err, response, body) { if (err) { console.log("error start", err, song, "error end"); get_genres_soundcloud_recursive(arr, channel, i + 1, callback); @@ -228,14 +227,14 @@ function get_genres_soundcloud_recursive(arr, channel, i, callback) { .concat(object.tag_list.toLowerCase().split('"')); genre = genre.filter(filterFunction); - db.collection(channel).update( - { id: song.id }, - { + db.collection(channel).update({ + id: song.id + }, { $set: { tags: genre } }, - function(e, d) { + function (e, d) { get_genres_soundcloud_recursive(arr, channel, i + 1, callback); } ); @@ -258,8 +257,8 @@ function get_genres_list_recursive(list, channel, callback) { soundcloud_array.push(list[i]); } } - start_youtube_get(youtube_array, channel, function() { - start_soundcloud_get(soundcloud_array, channel, function() { + start_youtube_get(youtube_array, channel, function () { + start_soundcloud_get(soundcloud_array, channel, function () { if (typeof callback == "function") callback(); }); }); @@ -281,16 +280,14 @@ function get_genres_youtube_recursive(arr, channel, i, callback) { } ids.push(arr[y].id); } - request( - { + request({ type: "GET", - url: - "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + + url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + key + "&id=" + ids.join(",") }, - function(error, response, body) { + function (error, response, body) { if (error) { get_genres_youtube_recursive(arr, channel, i + ids.length, callback); return; @@ -315,14 +312,14 @@ function get_genres_youtube_recursive(arr, channel, i, callback) { .split(","); genre = genre.filter(filterFunction); //console.log(resp.items[i].id + " - ", genre); - db.collection(channel).update( - { id: resp.items[z].id }, - { + db.collection(channel).update({ + id: resp.items[z].id + }, { $set: { tags: genre } }, - function(e, d) {} + function (e, d) {} ); } get_genres_youtube_recursive(arr, channel, i + ids.length, callback); @@ -334,16 +331,14 @@ function get_genres_youtube_recursive(arr, channel, i, callback) { } function get_genres_youtube(ids, channel) { - request( - { + request({ type: "GET", - url: - "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + + url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + key + "&id=" + ids }, - function(error, response, body) { + function (error, response, body) { if (error) { return; } @@ -366,14 +361,14 @@ function get_genres_youtube(ids, channel) { .split(","); genre = genre.filter(filterFunction); //console.log(resp.items[i].id + " - ", genre); - db.collection(channel).update( - { id: resp.items[i].id }, - { + db.collection(channel).update({ + id: resp.items[i].id + }, { $set: { tags: genre } }, - function(e, d) {} + function (e, d) {} ); } } @@ -383,16 +378,14 @@ function get_genres_youtube(ids, channel) { function get_correct_info(song_generated, channel, broadcast, callback) { //channel = channel.replace(/ /g,''); - request( - { + request({ type: "GET", - url: - "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + + url: "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id,topicDetails&key=" + key + "&id=" + song_generated.id }, - function(error, response, body) { + function (error, response, body) { try { var resp = JSON.parse(body); if (resp.items.length == 1) { @@ -424,9 +417,9 @@ function get_correct_info(song_generated, channel, broadcast, callback) { song_generated.start = 0; song_generated.end = duration; } - db.collection(channel).update( - { id: song_generated.id }, - { + db.collection(channel).update({ + id: song_generated.id + }, { $set: { duration: song_generated.duration, start: song_generated.start, @@ -435,7 +428,7 @@ function get_correct_info(song_generated, channel, broadcast, callback) { tags: genre } }, - function(err, docs) { + function (err, docs) { if (broadcast && docs.nModified == 1) { song_generated.new_id = song_generated.id; //if(song_generated.type == "video") @@ -455,14 +448,14 @@ function get_correct_info(song_generated, channel, broadcast, callback) { } ); } else { - db.collection(channel).update( - { id: song_generated.id }, - { + db.collection(channel).update({ + id: song_generated.id + }, { $set: { tags: genre } }, - function(e, d) { + function (e, d) { if (typeof callback == "function") { callback(song_generated, true); } @@ -502,16 +495,14 @@ function check_error_video(msg, channel) { } if (msg.source == "soundcloud") return; //channel = channel.replace(/ /g,''); - request( - { + request({ type: "GET", - url: - "https://www.googleapis.com/youtube/v3/videos?part=id&key=" + + url: "https://www.googleapis.com/youtube/v3/videos?part=id&key=" + key + "&id=" + msg.id }, - function(error, response, body) { + function (error, response, body) { try { var resp = JSON.parse(body); if (resp.pageInfo.totalResults == 0) { @@ -531,12 +522,11 @@ function findSimilar(msg, channel, broadcast, callback) { key + "&videoEmbeddable=true&part=id&type=video&order=viewCount&safeSearch=none&maxResults=5&q=" + encodeURIComponent(msg.title); - request( - { + request({ method: "GET", url: yt_url }, - function(error, response, body) { + function (error, response, body) { var resp = JSON.parse(body); if (resp.items.length > 0) { var vid_url = @@ -546,12 +536,11 @@ function findSimilar(msg, channel, broadcast, callback) { for (var i = 0; i < resp.items.length; i++) { vid_url += resp.items[i].id.videoId + ","; } - request( - { + request({ type: "GET", url: vid_url }, - function(error, response, body) { + function (error, response, body) { var resp = JSON.parse(body); var found = false; var element = {}; @@ -576,12 +565,12 @@ function findSimilar(msg, channel, broadcast, callback) { } } if (found) { - db.collection(channel).update( - { id: msg.id }, - { + db.collection(channel).update({ + id: msg.id + }, { $set: element }, - function(err, docs) { + function (err, docs) { if ( docs && docs.hasOwnProperty("nModified") && @@ -671,4 +660,4 @@ module.exports.get_genres_soundcloud = get_genres_soundcloud; module.exports.get_genres_youtube = get_genres_youtube; module.exports.get_genres_list = get_genres_list; module.exports.check_error_video = check_error_video; -module.exports.get_correct_info = get_correct_info; +module.exports.get_correct_info = get_correct_info; \ No newline at end of file diff --git a/server/handlers/suggestions.js b/server/handlers/suggestions.js index 7896e88e..1d4e3eec 100644 --- a/server/handlers/suggestions.js +++ b/server/handlers/suggestions.js @@ -22,22 +22,22 @@ function thumbnail(msg, coll, guid, offline, socket) { }, thumbnail: { expected: "string", - got: msg.hasOwnProperty("thumbnail") - ? typeof msg.thumbnail - : undefined + got: msg.hasOwnProperty("thumbnail") ? + typeof msg.thumbnail : + undefined }, adminpass: { expected: "string", - got: msg.hasOwnProperty("adminpass") - ? typeof msg.adminpass - : undefined + got: msg.hasOwnProperty("adminpass") ? + typeof msg.adminpass : + undefined } }; socket.emit("update_required", result); return; } //coll = coll.replace(/ /g,''); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass ) { @@ -63,7 +63,9 @@ function thumbnail(msg, coll, guid, offline, socket) { } var channel = msg.channel.toLowerCase(); var hash = msg.adminpass; - db.collection(channel + "_settings").find({ id: "config" }, function( + db.collection(channel + "_settings").find({ + id: "config" + }, function ( err, docs ) { @@ -79,11 +81,16 @@ function thumbnail(msg, coll, guid, offline, socket) { docs[0].adminpass !== "" && docs[0].adminpass == hash ) { - db.collection("suggested_thumbnails").update( - { channel: channel }, - { $set: { thumbnail: msg.thumbnail } }, - { upsert: true }, - function(err, docs) { + db.collection("suggested_thumbnails").update({ + channel: channel + }, { + $set: { + thumbnail: msg.thumbnail + } + }, { + upsert: true + }, + function (err, docs) { Notifications.requested_change( "thumbnail", msg.thumbnail, @@ -117,22 +124,22 @@ function description(msg, coll, guid, offline, socket) { }, description: { expected: "string", - got: msg.hasOwnProperty("description") - ? typeof msg.description - : undefined + got: msg.hasOwnProperty("description") ? + typeof msg.description : + undefined }, adminpass: { expected: "string", - got: msg.hasOwnProperty("adminpass") - ? typeof msg.adminpass - : undefined + got: msg.hasOwnProperty("adminpass") ? + typeof msg.adminpass : + undefined } }; socket.emit("update_required", result); return; } //coll = coll.replace(/ /g,''); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass, gotten @@ -154,7 +161,9 @@ function description(msg, coll, guid, offline, socket) { } var channel = msg.channel.toLowerCase(); var hash = msg.adminpass; - db.collection(channel + "_settings").find({ id: "config" }, function( + db.collection(channel + "_settings").find({ + id: "config" + }, function ( err, docs ) { @@ -170,11 +179,16 @@ function description(msg, coll, guid, offline, socket) { docs[0].adminpass !== "" && docs[0].adminpass == hash ) { - db.collection("suggested_descriptions").update( - { channel: channel }, - { $set: { description: msg.description } }, - { upsert: true }, - function(err, docs) { + db.collection("suggested_descriptions").update({ + channel: channel + }, { + $set: { + description: msg.description + } + }, { + upsert: true + }, + function (err, docs) { Notifications.requested_change( "description", msg.description, @@ -212,16 +226,16 @@ function rules(msg, coll, guid, offline, socket) { }, adminpass: { expected: "string", - got: msg.hasOwnProperty("adminpass") - ? typeof msg.adminpass - : undefined + got: msg.hasOwnProperty("adminpass") ? + typeof msg.adminpass : + undefined } }; socket.emit("update_required", result); return; } //coll = coll.replace(/ /g,''); - Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( + Functions.getSessionAdminUser(Functions.getSession(socket), coll, function ( userpass, adminpass, gotten @@ -243,7 +257,9 @@ function rules(msg, coll, guid, offline, socket) { } var channel = msg.channel.toLowerCase(); var hash = msg.adminpass; - db.collection(channel + "_settings").find({ id: "config" }, function( + db.collection(channel + "_settings").find({ + id: "config" + }, function ( err, docs ) { @@ -259,11 +275,16 @@ function rules(msg, coll, guid, offline, socket) { docs[0].adminpass !== "" && docs[0].adminpass == hash ) { - db.collection("suggested_rules").update( - { channel: channel }, - { $set: { rules: msg.rules } }, - { upsert: true }, - function(err, docs) { + db.collection("suggested_rules").update({ + channel: channel + }, { + $set: { + rules: msg.rules + } + }, { + upsert: true + }, + function (err, docs) { Notifications.requested_change("rules", msg.rules, channel); socket.emit("toast", "suggested_rules"); } @@ -281,4 +302,4 @@ function rules(msg, coll, guid, offline, socket) { module.exports.thumbnail = thumbnail; module.exports.description = description; -module.exports.rules = rules; +module.exports.rules = rules; \ No newline at end of file