diff --git a/server/handlers/dbFunctions/advancedFunctions/end.js b/server/handlers/dbFunctions/advancedFunctions/end.js index bdda261d..95e51bbd 100644 --- a/server/handlers/dbFunctions/advancedFunctions/end.js +++ b/server/handlers/dbFunctions/advancedFunctions/end.js @@ -1,4 +1,4 @@ -function end(obj, coll, guid, offline, socket) { +async function end(obj, coll, guid, offline, socket) { var socketid = socket.zoff_id; if (typeof obj !== "object") { return; @@ -38,67 +38,69 @@ function end(obj, coll, guid, offline, socket) { } } }; - db.collection(coll + "_settings").find(function(err, docs) { - var authentication_needed = false; - if ( - docs.length > 0 && - (docs[0].userpass != undefined && docs[0].userpass != "") + var docs = await find(coll + "_settings"); + var authentication_needed = false; + + if ( + docs.length > 0 && + (docs[0].userpass != undefined && docs[0].userpass != "") + ) { + callback_function = Functions.getSessionAdminUser; + authentication_needed = true; + var sessionAdminUser = await Functions.getSessionAdminUser(Functions.getSession(socket), coll); + obj.userpass = sessionAdminUser.userpass; + } + + if (userpass != "" || obj.pass == undefined) { + obj.pass = userpass; + } else { + obj.pass = crypto + .createHash("sha256") + .update(Functions.decrypt_string(obj.pass)) + .digest("base64"); + } + if ( + !authentication_needed || + (authentication_needed && + obj.hasOwnProperty("pass") && + docs[0].userpass == obj.pass) ) { - callback_function = Functions.getSessionAdminUser; - authentication_needed = true; - } - callback_function(Functions.getSession(socket), coll, function(userpass) { - if (userpass != "" || obj.pass == undefined) { - obj.pass = userpass; - } else { - obj.pass = crypto - .createHash("sha256") - .update(Functions.decrypt_string(obj.pass)) - .digest("base64"); - } + Functions.check_inlist( + coll, + guid, + socket, + offline, + undefined, + "place 13" + ); + var np = await find(coll, { now_playing: true }); + if (err !== null) console.log(err); if ( - !authentication_needed || - (authentication_needed && - obj.hasOwnProperty("pass") && - docs[0].userpass == obj.pass) + np !== null && + np !== undefined && + np.length == 1 && + np[0].id == id ) { - Functions.check_inlist( - coll, - guid, - socket, - offline, - undefined, - "place 13" - ); - db.collection(coll).find({ now_playing: true }, function(err, np) { - if (err !== null) console.log(err); - if ( - np !== null && - np !== undefined && - np.length == 1 && - np[0].id == id - ) { - var startTime = docs[0].startTime; - if ( - startTime + parseInt(np[0].duration) <= - Functions.get_time() + 5 - ) { - change_song(coll, false, id, docs); - } - } - }); - } else { - socket.emit("auth_required"); + var startTime = docs[0].startTime; + if ( + startTime + parseInt(np[0].duration) <= + Functions.get_time() + 5 + ) { + changeSong(coll, false, id, docs); + } } - }); - }); - } else { - var result = { - msg: { - expected: "object", - got: typeof obj + } else { + socket.emit("auth_required"); } - }; - socket.emit("update_required", result); + } else { + var result = { + msg: { + expected: "object", + got: typeof obj + } + }; + socket.emit("update_required", result); + } } -} + + module.exports.end = end; diff --git a/server/handlers/dbFunctions/advancedFunctions/frontpageUpdates.js b/server/handlers/dbFunctions/advancedFunctions/frontpageUpdates.js index 49a1efde..bd0eb7b3 100644 --- a/server/handlers/dbFunctions/advancedFunctions/frontpageUpdates.js +++ b/server/handlers/dbFunctions/advancedFunctions/frontpageUpdates.js @@ -3,7 +3,7 @@ var mongojs = require("mongojs"); var db = require(pathThumbnails + "/handlers/db.js"); var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); -function incrementList(collection, way) { +async function incrementList(collection, way) { return update( collection, { _id: coll, count: { $gt: 0 } }, diff --git a/server/handlers/dbFunctions/advancedFunctions/play.js b/server/handlers/dbFunctions/advancedFunctions/play.js index 96b0a33e..b1e3d1a8 100644 --- a/server/handlers/dbFunctions/advancedFunctions/play.js +++ b/server/handlers/dbFunctions/advancedFunctions/play.js @@ -4,126 +4,125 @@ var db = require(pathThumbnails + "/handlers/db.js"); var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); async function getNowPlaying(list, socket) { - return new Promise((resolve, reject) => { - if (typeof list !== "string" || typeof fn !== "function") { - socket.emit("update_required"); - return; - } - var docs = await find(list, { now_playing: true }); - if (docs.length === 0) { - resolve("No song currently playing"); - return; - } - var title = docs[0].title; - if (title === undefined) resolve("No song currently playing"); - else resolve(title); - }); -} - -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) { - if (err !== null) console.log(err); - try { - if (Functions.get_time() - conf[0].startTime > np[0].duration) { - change_song(coll, false, np[0].id, conf); - } else if (conf !== null && conf !== undefined && conf.length !== 0) { - if (conf[0].adminpass !== "") conf[0].adminpass = true; - if (conf[0].hasOwnProperty("userpass") && conf[0].userpass != "") - conf[0].userpass = true; - 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() }; - if (socket === undefined) { - io.to(coll).emit("np", toSend); - // - getNextSong(coll, undefined); - var url = - "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; - if (np[0].source == "soundcloud") url = np[0].thumbnail; - sendColor(coll, false, url); - } else { - var url = - "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; - if (np[0].source == "soundcloud") url = np[0].thumbnail; - sendColor(coll, socket, url); - if (broadcast) { - socket.to(coll).emit("np", toSend); - return; - } - socket.emit("np", toSend); - } - } - } catch (e) { - if (socket) { - if (broadcast) { - socket.to(coll).emit("np", {}); + return new Promise((resolve, reject) => { + if (typeof list !== "string" || typeof fn !== "function") { + socket.emit("update_required"); return; - } - socket.emit("np", {}); - } else { - io.to(coll).emit("np", {}); } - } + var docs = await find(list, { now_playing: true }); + if (docs.length === 0) { + resolve("No song currently playing"); + return; + } + var title = docs[0].title; + if (title === undefined) resolve("No song currently playing"); + else resolve(title); }); - }); } -function getNextSong(coll, socket, callback) { - //coll = coll.replace(/ /g,''); - db.collection(coll).aggregate( - [ - { - $match: { - views: { - $exists: false - }, - type: { - $ne: "suggested" - } +async function sendPlay(coll, socket, broadcast) { + //coll = coll.replace(/ /g,''); + var np = await find(coll, { now_playing: true }); + var conf = await find(coll + "_settings"); + if (err !== null) console.log(err); + try { + if (Functions.get_time() - conf[0].startTime > np[0].duration) { + changeSong(coll, false, np[0].id, conf); + } else if (conf !== null && conf !== undefined && conf.length !== 0) { + if (conf[0].adminpass !== "") conf[0].adminpass = true; + if (conf[0].hasOwnProperty("userpass") && conf[0].userpass != "") + conf[0].userpass = true; + 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() }; + if (socket === undefined) { + io.to(coll).emit("np", toSend); + // + getNextSong(coll, undefined); + var url = + "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; + if (np[0].source == "soundcloud") url = np[0].thumbnail; + sendColor(coll, false, url); + } else { + var url = + "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; + if (np[0].source == "soundcloud") url = np[0].thumbnail; + sendColor(coll, socket, url); + if (broadcast) { + socket.to(coll).emit("np", toSend); + return; + } + socket.emit("np", toSend); + } } - }, - { - $sort: { - now_playing: 1, - votes: -1, - added: 1, - title: 1 - } - }, - { - $limit: 1 - } - ], - function(err, doc) { - if (doc.length == 1) { - var thumbnail = ""; - var source = "youtube"; - if (doc[0].source && doc[0].source == "soundcloud") { - source = "soundcloud"; - thumbnail = doc[0].thumbnail; - } - if (socket != undefined) { - socket.emit("next_song", { - videoId: doc[0].id, - title: doc[0].title, - source: source, - thumbnail: thumbnail - }); + } catch (e) { + if (socket) { + if (broadcast) { + socket.to(coll).emit("np", {}); + return; + } + socket.emit("np", {}); } else { - io.to(coll).emit("next_song", { - videoId: doc[0].id, - title: doc[0].title, - source: source, - thumbnail: thumbnail - }); + io.to(coll).emit("np", {}); } - } - if (typeof callback == "function") callback(); } - ); } -module.exports.getNowPlaying = getNowPlaying; +async function getNextSong(coll, socket) { + //coll = coll.replace(/ /g,''); + return new Promise((resolve, reject) => { + var docs = await aggregate(coll, + [ + { + $match: { + views: { + $exists: false + }, + type: { + $ne: "suggested" + } + } + }, + { + $sort: { + now_playing: 1, + votes: -1, + added: 1, + title: 1 + } + }, + { + $limit: 1 + } + ]); + if (doc.length == 1) { + var thumbnail = ""; + var source = "youtube"; + if (doc[0].source && doc[0].source == "soundcloud") { + source = "soundcloud"; + thumbnail = doc[0].thumbnail; + } + if (socket != undefined) { + socket.emit("next_song", { + videoId: doc[0].id, + title: doc[0].title, + source: source, + thumbnail: thumbnail + }); + } else { + io.to(coll).emit("next_song", { + videoId: doc[0].id, + title: doc[0].title, + source: source, + thumbnail: thumbnail + }); + } + } + resolve(); + }); + } + + module.exports.getNowPlaying = getNowPlaying; + module.exports.getNextSong = getNextSong; + module.exports.sendPlay = sendPlay; diff --git a/server/handlers/dbFunctions/advancedFunctions/sendList.js b/server/handlers/dbFunctions/advancedFunctions/sendList.js index 215dc69d..f09edfed 100644 --- a/server/handlers/dbFunctions/advancedFunctions/sendList.js +++ b/server/handlers/dbFunctions/advancedFunctions/sendList.js @@ -42,7 +42,7 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { userpass: "" }; await update(coll + "_settings", { id: "config" }, conf, { upsert: true }); - send_list(coll, socket, send, list_send, configs, shuffled); + sendList(coll, socket, send, list_send, configs, shuffled); } else { var docs = await aggregate(coll, [ { @@ -134,11 +134,11 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { { $set: { now_playing: false } }, { multi: true } ); - send_list(coll, socket, send, list_send, configs, shuffled); + sendList(coll, socket, send, list_send, configs, shuffled); } else { if (Functions.get_time() - conf[0].startTime > np_docs[0].duration) { await changeSong(coll, false, np_docs[0].id, conf, socket); - send_list(coll, socket, send, list_send, configs, shuffled); + sendList(coll, socket, send, list_send, configs, shuffled); } else { if (list_send) { io.to(coll).emit("channel", { @@ -154,9 +154,9 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { }); } if (socket === undefined && send) { - send_play(coll); + sendPlay(coll); } else if (send) { - send_play(coll, socket); + sendPlay(coll, socket); } } } @@ -175,9 +175,9 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { }); } if (socket === undefined && send) { - send_play(coll); + sendPlay(coll); } else if (send) { - send_play(coll, socket); + sendPlay(coll, socket); } } if (configs) { diff --git a/server/handlers/dbFunctions/advancedFunctions/timeout.js b/server/handlers/dbFunctions/advancedFunctions/timeout.js new file mode 100644 index 00000000..b69d7c65 --- /dev/null +++ b/server/handlers/dbFunctions/advancedFunctions/timeout.js @@ -0,0 +1,66 @@ +var path = require("path"); +var mongojs = require("mongojs"); +var db = require(pathThumbnails + "/handlers/db.js"); +var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); +var create = require(pathThumbnails + "/handlers/dbFunctions/create.js"); +var insert = require(pathThumbnails + "/handlers/dbFunctions/insert.js"); + +function checkTimeout( + type, + timeout, + channel, + guid, + conf_pass, + this_pass, + socket, + error_message +) { + return new Promise((resolve, reject) => { + if (conf_pass != "" && conf_pass == this_pass) { + resolve(); + return; + } + var docs = await find("timeout_api", + { + type: type, + guid: guid + }); + if (docs.length > 0) { + var date = new Date(docs[0].createdAt); + date.setSeconds(date.getSeconds() + timeout); + var now = new Date(); + + var retry_in = (date.getTime() - now.getTime()) / 1000; + if (retry_in > 0) { + if (typeof error_callback == "function") { + reject(); + return; + } else if (error_message) { + var sOrNot = + Math.ceil(retry_in) > 1 || Math.ceil(retry_in) == 0 ? "s" : ""; + socket.emit( + "toast", + error_message + Math.ceil(retry_in) + " second" + sOrNot + "." + ); + } else { + socket.emit("toast", "wait_longer"); + } + return; + } + } + var now_date = new Date(); + await update("timeout_api", + { type: type, guid: guid }, + { + $set: { + createdAt: now_date, + type: type, + guid: guid + } + }, + { upsert: true }); + resolve(); + }); +} + +module.exports.checkTimeout = checkTimeout; diff --git a/server/handlers/list.js b/server/handlers/list.js index 6f02f1d6..5cdf6284 100644 --- a/server/handlers/list.js +++ b/server/handlers/list.js @@ -15,6 +15,7 @@ var filter = new Filter({ placeHolder: "x" }); var request = require("request"); var db = require(pathThumbnails + "/handlers/db.js"); +// done function now_playing(list, fn, socket) { if (typeof list !== "string" || typeof fn !== "function") { socket.emit("update_required");