intermediary

This commit is contained in:
Kasper Rynning-Tønnesen
2019-10-08 07:47:23 +02:00
parent ee206c8d67
commit c6382c0af1
6 changed files with 247 additions and 179 deletions

View File

@@ -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,16 +38,19 @@ function end(obj, coll, guid, offline, socket) {
}
}
};
db.collection(coll + "_settings").find(function(err, docs) {
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;
}
callback_function(Functions.getSession(socket), coll, function(userpass) {
if (userpass != "" || obj.pass == undefined) {
obj.pass = userpass;
} else {
@@ -70,7 +73,7 @@ function end(obj, coll, guid, offline, socket) {
undefined,
"place 13"
);
db.collection(coll).find({ now_playing: true }, function(err, np) {
var np = await find(coll, { now_playing: true });
if (err !== null) console.log(err);
if (
np !== null &&
@@ -83,15 +86,12 @@ function end(obj, coll, guid, offline, socket) {
startTime + parseInt(np[0].duration) <=
Functions.get_time() + 5
) {
change_song(coll, false, id, docs);
changeSong(coll, false, id, docs);
}
}
});
} else {
socket.emit("auth_required");
}
});
});
} else {
var result = {
msg: {
@@ -101,4 +101,6 @@ function end(obj, coll, guid, offline, socket) {
};
socket.emit("update_required", result);
}
}
}
module.exports.end = end;

View File

@@ -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 } },

View File

@@ -20,14 +20,14 @@ async function getNowPlaying(list, socket) {
});
}
function send_play(coll, socket, broadcast) {
async function sendPlay(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) {
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) {
change_song(coll, false, np[0].id, conf);
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 != "")
@@ -67,13 +67,12 @@ function send_play(coll, socket, broadcast) {
io.to(coll).emit("np", {});
}
}
});
});
}
function getNextSong(coll, socket, callback) {
async function getNextSong(coll, socket) {
//coll = coll.replace(/ /g,'');
db.collection(coll).aggregate(
return new Promise((resolve, reject) => {
var docs = await aggregate(coll,
[
{
$match: {
@@ -96,8 +95,7 @@ function getNextSong(coll, socket, callback) {
{
$limit: 1
}
],
function(err, doc) {
]);
if (doc.length == 1) {
var thumbnail = "";
var source = "youtube";
@@ -121,9 +119,10 @@ function getNextSong(coll, socket, callback) {
});
}
}
if (typeof callback == "function") callback();
resolve();
});
}
);
}
module.exports.getNowPlaying = getNowPlaying;
module.exports.getNowPlaying = getNowPlaying;
module.exports.getNextSong = getNextSong;
module.exports.sendPlay = sendPlay;

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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");