diff --git a/server/handlers/dbFunctions/advancedFunctions/end.js b/server/handlers/dbFunctions/advancedFunctions/end.js new file mode 100644 index 00000000..bdda261d --- /dev/null +++ b/server/handlers/dbFunctions/advancedFunctions/end.js @@ -0,0 +1,104 @@ +function end(obj, coll, guid, offline, socket) { + var socketid = socket.zoff_id; + if (typeof obj !== "object") { + return; + } + id = obj.id; + + if (id !== undefined && id !== null && id !== "") { + if ( + !obj.hasOwnProperty("id") || + !obj.hasOwnProperty("channel") || + (typeof obj.id != "string" && typeof obj.id != "number") || + typeof obj.channel != "string" + ) { + var result = { + channel: { + expected: "string", + got: obj.hasOwnProperty("channel") ? typeof obj.channel : undefined + }, + pass: { + expected: "string", + got: obj.hasOwnProperty("pass") ? typeof obj.pass : undefined + }, + id: { + expected: "string || number", + got: obj.hasOwnProperty("id") ? typeof obj.id : undefined + } + }; + socket.emit("update_required", result); + return; + } + obj.id = obj.id + ""; + id = id + ""; + 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) { + var authentication_needed = false; + if ( + docs.length > 0 && + (docs[0].userpass != undefined && docs[0].userpass != "") + ) { + 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"); + } + if ( + !authentication_needed || + (authentication_needed && + obj.hasOwnProperty("pass") && + docs[0].userpass == obj.pass) + ) { + 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"); + } + }); + }); + } else { + var result = { + msg: { + expected: "object", + got: typeof obj + } + }; + socket.emit("update_required", result); + } +} diff --git a/server/handlers/dbFunctions/advancedFunctions/functions.js b/server/handlers/dbFunctions/advancedFunctions/functions.js index 39ae060b..7ffb54c6 100644 --- a/server/handlers/dbFunctions/advancedFunctions/functions.js +++ b/server/handlers/dbFunctions/advancedFunctions/functions.js @@ -5,11 +5,11 @@ var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); var remove = require(pathThumbnails + "/handlers/dbFunctions/remove.js"); var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); -async function setSessionAdminPass(id, adminpass, list, callback) { +async function setSessionAdminPass(id, adminpass, list) { return new Promise((resolve, reject) => { try { if (id == "empty" || id == undefined) { - callback(); + resolve(); return; } @@ -19,14 +19,14 @@ async function setSessionAdminPass(id, adminpass, list, callback) { { $set: { adminpass: hash_pass(decrypt_string(adminpass), true) } }, { upsert: true } ); - callback(); + resolve(); } catch (e) { reject(); } }); } -async function setSessionChatPass(id, name, pass, callback) { +async function setSessionChatPass(id, name, pass) { return new Promise((resolve, reject) => { try { if (id == "empty" || id == undefined) { @@ -47,7 +47,7 @@ async function setSessionChatPass(id, name, pass, callback) { }); } -async function getSessionChatPass(id, callback) { +async function getSessionChatPass(id) { return new Promise((resolve, reject) => { try { if (id == "empty" || id == undefined) { @@ -74,7 +74,7 @@ async function getSessionChatPass(id, callback) { }); } -async function setChromecastHost(id, other_id, list, callback) { +async function setChromecastHost(id, other_id, list) { return new Promise((resolve, reject) => { try { if ( @@ -100,7 +100,7 @@ async function setChromecastHost(id, other_id, list, callback) { }); } -async function setSessionUserPass(id, userpass, list, callback) { +async function setSessionUserPass(id, userpass, list) { return new Promise((resolve, reject) => { try { if (id == "empty" || id == undefined || userpass == undefined) { @@ -122,7 +122,7 @@ async function setSessionUserPass(id, userpass, list, callback) { }); } -async function getSessionAdminUser(id, list, callback) { +async function getSessionAdminUser(id, list) { return new Promise((resolve, reject) => { try { if (id == "empty" || id == undefined) { @@ -134,7 +134,7 @@ async function getSessionAdminUser(id, list, callback) { var adminpass = ""; if (d.length > 0) { if (d[0].hasOwnProperty("chromecast") && d[0].chromecast) { - return await getSessionAdminUser(d[0].id, list, callback); + return await getSessionAdminUser(d[0].id, list); } else { if (d[0].userpass != undefined) userpass = d[0].userpass; if (d[0].adminpass != undefined) adminpass = d[0].adminpass; @@ -149,7 +149,7 @@ async function getSessionAdminUser(id, list, callback) { }); } -async function removeSessionChatPass(id, callback) { +async function removeSessionChatPass(id) { return new Promise((resolve, reject) => { if (id == "empty" || id == undefined) { resolve(); @@ -161,7 +161,7 @@ async function removeSessionChatPass(id, callback) { }); } -async function removeSessionAdminPass(id, channel, callback) { +async function removeSessionAdminPass(id, channel) { return new Promise((resolve, reject) => { if (id == "empty" || id == undefined) { resolve(); @@ -171,3 +171,47 @@ async function removeSessionAdminPass(id, channel, callback) { resolve(); }); } + +function sendColor(coll, socket, url, ajax, res) { + if (coll != undefined && typeof coll == "string") { + //coll = coll.replace(/ /g,''); + } + if (url.indexOf("://") == -1) + url = "https://img.youtube.com/vi/" + url + "/mqdefault.jpg"; + //var url = 'https://img.youtube.com/vi/'+id+'/mqdefault.jpg'; + + Jimp.read(url) + .then(function(image) { + var c = ColorThief.getColor(image); + if (ajax) { + res.header({ "Content-Type": "application/json" }); + res.status(200).send(c); + return; + } else { + if (socket) { + socket.emit("color", { color: c, only: true }); + } else { + io.to(coll).emit("color", { color: c, only: false }); + } + } + }) + .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.status(404); + return; + } + }); +} + +module.exports.setSessionAdminPass = setSessionAdminPass; +module.exports.setSessionChatPass = setSessionChatPass; +module.exports.getSessionChatPass = getSessionChatPass; +module.exports.setChromecastHost = setChromecastHost; +module.exports.setSessionUserPass = setSessionUserPass; +module.exports.getSessionAdminUser = getSessionAdminUser; +module.exports.removeSessionChatPass = removeSessionChatPass; +module.exports.removeSessionAdminPass = removeSessionAdminPass; +module.exports.sendColor = sendColor; diff --git a/server/handlers/dbFunctions/advancedFunctions/joinList.js b/server/handlers/dbFunctions/advancedFunctions/joinList.js index bc42e2c8..af7438e1 100644 --- a/server/handlers/dbFunctions/advancedFunctions/joinList.js +++ b/server/handlers/dbFunctions/advancedFunctions/joinList.js @@ -2,6 +2,8 @@ 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"); async function joinSilent(msg, socket) { if (typeof msg === "object" && msg !== undefined && msg !== null) { @@ -41,7 +43,7 @@ async function joinSilent(msg, socket) { } } -async function list(msg, guid, coll, offline, socket) { +async function joinList(msg, guid, coll, offline, socket) { var socketid = socket.zoff_id; if (typeof msg === "object" && msg !== undefined && msg !== null) { var sessionAdminUser = await Functions.getSessionAdminUser( @@ -144,58 +146,47 @@ async function list(msg, guid, coll, offline, socket) { socket.emit("auth_required"); } } else { - db.createCollection(coll, function(err, docs) { - db.collection(coll).createIndex({ id: 1 }, { unique: true }, function( - e, - d - ) { - var configs = { - addsongs: false, - adminpass: "", - allvideos: true, - frontpage: true, - longsongs: false, - removeplay: false, - shuffle: true, - skip: false, - skips: [], - startTime: Functions.get_time(), - views: [], - vote: false, - description: "", - thumbnail: "", - rules: "", - userpass: "", - id: "config", - toggleChat: true - }; - db.collection(coll + "_settings").insert(configs, function( - err, - docs - ) { - socket.join(coll); - send_list(coll, socket, true, false, true); - db.collection("frontpage_lists").insert( - { - _id: coll, - count: 0, - frontpage: true, - accessed: Functions.get_time(), - viewers: 1 - }, - function(e, d) {} - ); - Functions.check_inlist( - coll, - guid, - socket, - offline, - undefined, - "place 11" - ); - }); - }); + var docs = await create.collection(coll); + var index = await create.index(coll, { id: 1 }, { unique: true }); + + var configs = { + addsongs: false, + adminpass: "", + allvideos: true, + frontpage: true, + longsongs: false, + removeplay: false, + shuffle: true, + skip: false, + skips: [], + startTime: Functions.get_time(), + views: [], + vote: false, + description: "", + thumbnail: "", + rules: "", + userpass: "", + id: "config", + toggleChat: true + }; + await insert(coll + "_settings", configs); + socket.join(coll); + send_list(coll, socket, true, false, true); + insert("frontpage_lists", { + _id: coll, + count: 0, + frontpage: true, + accessed: Functions.get_time(), + viewers: 1 }); + Functions.check_inlist( + coll, + guid, + socket, + offline, + undefined, + "place 11" + ); } } else { var result = { @@ -207,3 +198,6 @@ async function list(msg, guid, coll, offline, socket) { socket.emit("update_required", result); } } + +module.exports.joinSilent = joinSilent; +module.exports.joinList = joinList; diff --git a/server/handlers/dbFunctions/advancedFunctions/play.js b/server/handlers/dbFunctions/advancedFunctions/play.js index e95f008f..96b0a33e 100644 --- a/server/handlers/dbFunctions/advancedFunctions/play.js +++ b/server/handlers/dbFunctions/advancedFunctions/play.js @@ -20,4 +20,110 @@ async function getNowPlaying(list, 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) { + 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; + } + socket.emit("np", {}); + } else { + io.to(coll).emit("np", {}); + } + } + }); + }); +} + +function getNextSong(coll, socket, callback) { + //coll = coll.replace(/ /g,''); + db.collection(coll).aggregate( + [ + { + $match: { + views: { + $exists: false + }, + type: { + $ne: "suggested" + } + } + }, + { + $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 + }); + } else { + io.to(coll).emit("next_song", { + videoId: doc[0].id, + title: doc[0].title, + source: source, + thumbnail: thumbnail + }); + } + } + if (typeof callback == "function") callback(); + } + ); +} + module.exports.getNowPlaying = getNowPlaying; diff --git a/server/handlers/dbFunctions/advancedFunctions/search.js b/server/handlers/dbFunctions/advancedFunctions/search.js new file mode 100644 index 00000000..e69de29b diff --git a/server/handlers/dbFunctions/advancedFunctions/sendList.js b/server/handlers/dbFunctions/advancedFunctions/sendList.js new file mode 100644 index 00000000..215dc69d --- /dev/null +++ b/server/handlers/dbFunctions/advancedFunctions/sendList.js @@ -0,0 +1,221 @@ +var path = require("path"); +var mongojs = require("mongojs"); +var db = require(pathThumbnails + "/handlers/db.js"); +var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); +var remove = require(pathThumbnails + "/handlers/dbFunctions/remove.js"); +var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); +var aggregate = require(pathThumbnails + "/handlers/dbFunctions/aggregate.js"); +var sort = require(pathThumbnails + "/handlers/dbFunctions/sort.js"); + +async function sendList(coll, socket, send, list_send, configs, shuffled) { + //coll = coll.replace(/ /g,''); + var conf = await aggregate(coll + "_settings", [ + { + $match: { + id: "config" + } + }, + { + $project: projects.toShowConfig + } + ]); + var conf = _conf; + if (conf.length == 0) { + var conf = { + id: "config", + addsongs: false, + adminpass: "", + allvideos: true, + frontpage: true, + longsongs: false, + removeplay: false, + shuffle: true, + skip: false, + skips: [], + startTime: Functions.get_time(), + views: [], + vote: false, + description: "", + thumbnail: "", + rules: "", + toggleChat: true, + userpass: "" + }; + await update(coll + "_settings", { id: "config" }, conf, { upsert: true }); + send_list(coll, socket, send, list_send, configs, shuffled); + } else { + var docs = await aggregate(coll, [ + { + $match: { type: { $ne: "suggested" } } + }, + { + $project: projects.project_object + }, + { $sort: { now_playing: -1, votes: -1, added: 1 } } + ]); + if (docs.length > 0) { + var np_docs = await find(coll, { now_playing: true }); + if (np_docs.length == 0) { + var now_playing_doc = await aggregate(coll, [ + { + $match: { + views: { + $exists: false + }, + type: { + $ne: "suggested" + } + } + }, + { + $sort: { + now_playing: -1, + votes: -1, + added: 1, + title: 1 + } + }, + { + $limit: 1 + } + ]); + if (now_playing_doc[0].now_playing == false) { + await update( + coll, + { id: now_playing_doc[0].id, now_playing: false }, + { + $set: { + now_playing: true, + votes: 0, + guids: [], + added: Functions.get_time() + } + } + ); + await update( + coll + "_settings", + { id: "config" }, + { + $set: { + startTime: Functions.get_time(), + skips: [] + } + } + ); + Frontpage.update_frontpage( + coll, + now_playing_doc[0].id, + now_playing_doc[0].title, + now_playing_doc[0].thumbnail, + now_playing_doc[0].source + ); + sendList(coll, socket, send, list_send, configs, shuffled); + } + } else if (np_docs.length > 1) { + var docs = await aggregate(coll, [ + { + $match: { + now_playing: true + } + }, + { + $sort: { + now_playing: -1, + votes: -1, + added: 1, + title: 1 + } + } + ]); + var real_now_playing = docs[docs.length - 1]; + await update( + coll, + { now_playing: true, id: { $ne: real_now_playing.id } }, + { $set: { now_playing: false } }, + { multi: true } + ); + send_list(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); + } else { + if (list_send) { + io.to(coll).emit("channel", { + type: "list", + playlist: docs, + shuffled: shuffled + }); + } else if (!list_send) { + socket.emit("channel", { + type: "list", + playlist: docs, + shuffled: shuffled + }); + } + if (socket === undefined && send) { + send_play(coll); + } else if (send) { + send_play(coll, socket); + } + } + } + } else { + if (list_send) { + io.to(coll).emit("channel", { + type: "list", + playlist: docs, + shuffled: shuffled + }); + } else if (!list_send) { + socket.emit("channel", { + type: "list", + playlist: docs, + shuffled: shuffled + }); + } + if (socket === undefined && send) { + send_play(coll); + } else if (send) { + send_play(coll, socket); + } + } + if (configs) { + if (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; + io.to(coll).emit("conf", conf); + } else if (conf.length == 0 && docs.length > 0) { + var conf = { + id: "config", + addsongs: false, + adminpass: "", + allvideos: true, + frontpage: true, + longsongs: false, + removeplay: false, + shuffle: true, + skip: false, + skips: [], + startTime: Functions.get_time(), + views: [], + vote: false, + desc: "", + userpass: "" + }; + await update(coll + "_settings", { id: "config" }, conf, { + upsert: true + }); + io.to(coll).emit("conf", conf); + } + } + } + if (socket) { + var sugg = await sort(coll, { type: "suggested" }, { added: 1 }); + socket.emit("suggested", sugg); + } +} + +module.exports.sendList = sendList; diff --git a/server/handlers/dbFunctions/advancedFunctions/skip.js b/server/handlers/dbFunctions/advancedFunctions/skip.js new file mode 100644 index 00000000..b95e0d70 --- /dev/null +++ b/server/handlers/dbFunctions/advancedFunctions/skip.js @@ -0,0 +1,220 @@ +var path = require("path"); +var mongojs = require("mongojs"); +var db = require(pathThumbnails + "/handlers/db.js"); +var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); +var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); + +async function skip(list, guid, coll, offline, socket, callback) { + var socketid = socket.zoff_id; + + if (list === undefined || list === null || list === "") { + var result = { + msg: { + expected: "object", + got: typeof list + } + }; + socket.emit("update_required", result); + return; + } + if (coll == undefined && list.hasOwnProperty("channel")) + coll = list.channel.toLowerCase(); + if (coll !== undefined) { + try { + coll = list.channel.toLowerCase(); //.replace(/ /g,''); + if (coll.length == 0) return; + coll = Functions.removeEmojis(coll).toLowerCase(); + //coll = coll.replace(/_/g, ""); + + //coll = filter.clean(coll); + } catch (e) { + return; + } + } + if (!list.hasOwnProperty("id") || list.id == undefined) { + socket.emit("toast", "The list is empty."); + return; + } + if ( + !list.hasOwnProperty("id") || + !list.hasOwnProperty("channel") || + (typeof list.id != "string" && typeof list.id != "number") || + typeof list.channel != "string" + ) { + var result = { + channel: { + expected: "string", + got: list.hasOwnProperty("channel") ? typeof list.channel : undefined + }, + pass: { + expected: "string", + got: list.hasOwnProperty("pass") ? typeof list.pass : undefined + }, + userpass: { + expected: "string", + got: list.hasOwnProperty("userpass") ? typeof list.userpass : undefined + }, + id: { + expected: "string", + got: list.hasOwnProperty("id") ? typeof list.id : undefined + } + }; + socket.emit("update_required", result); + return; + } + list.id = list.id + ""; + var sessionAdminUser = await Functions.getSessionAdminUser( + Functions.getSession(socket), + coll + ); + var userpass = sessionAdminUser.userpass; + var adminpass = sessionAdminUser.adminpass; + var gotten = sessionAdminUser.gotten; + if (adminpass != "" || list.pass == undefined) { + list.pass = Functions.hash_pass(adminpass); + } else if (list.pass != "") { + list.pass = Functions.hash_pass( + Functions.hash_pass(Functions.decrypt_string(list.pass), true) + ); + } else { + list.pass = ""; + } + if (userpass != "" || list.userpass == undefined) { + list.userpass = userpass; + } else { + list.userpass = crypto + .createHash("sha256") + .update(Functions.decrypt_string(list.userpass)) + .digest("base64"); + } + + var docs = await find(coll + "_settings"); + if ( + docs.length > 0 && + (docs[0].userpass == undefined || + docs[0].userpass == "" || + (list.hasOwnProperty("userpass") && docs[0].userpass == list.userpass)) + ) { + Functions.check_inlist(coll, guid, socket, offline, undefined, "place 12"); + + var video_id; + adminpass = ""; + video_id = list.id; + var err = list.error; + var trueError = await Search.check_if_error_or_blocked( + video_id, + coll, + err == "5" || + err == "100" || + err == "101" || + err == "150" || + err == 5 || + err == 100 || + err == 101 || + err == 150 + ); + var error = false; + if (!trueError) { + adminpass = list.pass; + } else if (trueError) { + error = true; + } + hash = adminpass; + //db.collection(coll + "_settings").find(function(err, docs){ + var strictSkip = false; + var strictSkipNumber = 10; + if (docs[0].strictSkip) strictSkip = docs[0].strictSkip; + if (docs[0].strictSkipNumber) strictSkipNumber = docs[0].strictSkipNumber; + if (docs !== null && docs.length !== 0) { + if ( + !docs[0].skip || + (docs[0].adminpass == hash && docs[0].adminpass !== "") || + error + ) { + var frontpage_viewers = await find("frontpage_lists", { _id: coll }); + if ( + error || + ((strictSkip && + ((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) || + (frontpage_viewers[0].viewers == 2 && + docs[0].skips.length + 1 == 2 && + !Functions.contains(docs[0].skips, guid)) || + (docs[0].adminpass == hash && + docs[0].adminpass !== "" && + docs[0].skip)))) + ) { + var canContinue = await Functions.checkTimeout( + "skip", + 1, + coll, + coll, + error, + true, + socket, + "The channel is skipping too often, please wait " + ); + if (!canContinue) { + return; + } + change_song(coll, error, video_id, docs); + socket.emit("toast", "skip"); + var docs = await find("user_names", { guid: guid }); + if (docs.length == 1) { + var n = await find("registered_users", { _id: docs[0].name }); + var icon = false; + if (n.length > 0 && n[0].icon) { + icon = n[0].icon; + } + io.to(coll).emit("chat", { + from: docs[0].name, + icon: icon, + msg: " skipped" + }); + } + } else if (!Functions.contains(docs[0].skips, guid)) { + await update( + coll + "_settings", + { id: "config" }, + { $push: { skips: guid } } + ); + if (frontpage_viewers[0].viewers == 2 && !strictSkip) { + to_skip = 1; + } else if (strictSkip) { + to_skip = strictSkipNumber - docs[0].skips.length - 1; + } else { + to_skip = + Math.ceil(frontpage_viewers[0].viewers / 2) - + docs[0].skips.length - + 1; + } + socket.emit("toast", to_skip + " more are needed to skip!"); + var docs = await find("user_names", { guid: guid }); + if (docs.length == 1) { + var n = await find("registered_users", { _id: docs[0].name }); + var icon = false; + if (n.length > 0 && n[0].icon) { + icon = n[0].icon; + } + socket.to(coll).emit("chat", { + from: docs[0].name, + msg: " voted to skip" + }); + } + } else { + socket.emit("toast", "alreadyskip"); + } + } else socket.emit("toast", "noskip"); + } + + //}); + } else { + socket.emit("auth_required"); + } +} + +module.exports.skip = skip; diff --git a/server/handlers/dbFunctions/create.js b/server/handlers/dbFunctions/create.js new file mode 100644 index 00000000..f6c24f50 --- /dev/null +++ b/server/handlers/dbFunctions/create.js @@ -0,0 +1,33 @@ +var path = require("path"); +var mongojs = require("mongojs"); +var db = require(pathThumbnails + "/handlers/db.js"); + +async function collection(collection) { + return new Promise((resolve, reject) => { + db.createCollection(coll, function(err, docs) { + if (err) { + reject(err); + return; + } + resolve(docs); + }); + }); +} + +async function index(collection, indexObject, extraObject) { + return new Promise((resolve, reject) => { + db.collection(collection).createIndex(indexObject, extraObject, function( + err, + docs + ) { + if (err) { + reject(err); + return; + } + resolve(docs); + }); + }); +} + +module.exports.collection = collection; +module.exports.index = index; diff --git a/server/handlers/dbFunctions/find.js b/server/handlers/dbFunctions/find.js index ebc4650e..c707b024 100644 --- a/server/handlers/dbFunctions/find.js +++ b/server/handlers/dbFunctions/find.js @@ -2,7 +2,7 @@ var path = require("path"); var mongojs = require("mongojs"); var db = require(pathThumbnails + "/handlers/db.js"); -function find(collection, searchObject) { +async function find(collection, searchObject) { return new Promise((resolve, reject) => { db.collection(collection).find(searchObject, (error, result) => { if (error) { diff --git a/server/handlers/dbFunctions/insert.js b/server/handlers/dbFunctions/insert.js new file mode 100644 index 00000000..7090113b --- /dev/null +++ b/server/handlers/dbFunctions/insert.js @@ -0,0 +1,17 @@ +var path = require("path"); +var mongojs = require("mongojs"); +var db = require(pathThumbnails + "/handlers/db.js"); + +async function insert(collection, insertObject) { + return new Promise((resolve, reject) => { + db.collection(collection).insert(insertObject, function(err, docs) { + if (err) { + reject(err); + return; + } + resolve(docs); + }); + }); +} + +module.exports.insert = insert; diff --git a/server/handlers/dbFunctions/sort.js b/server/handlers/dbFunctions/sort.js new file mode 100644 index 00000000..b59aa135 --- /dev/null +++ b/server/handlers/dbFunctions/sort.js @@ -0,0 +1,19 @@ +var path = require("path"); +var mongojs = require("mongojs"); +var db = require(pathThumbnails + "/handlers/db.js"); + +async function find(collection, searchObject, sortObject) { + return new Promise((resolve, reject) => { + db.collection(collection) + .find(searchObject) + .sort(sortObject, (error, result) => { + if (error) { + reject(error); + return; + } + resolve(result); + }); + }); +} + +module.exports.find = find;