From c70b6a0ea9a56f9a760285f618c16cda5caf33ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Fri, 2 Mar 2018 10:47:06 +0100 Subject: [PATCH 1/3] Prettier returing on API - Prettier returns on API - Upped to version 4 --- server/VERSION.js | 2 +- server/handlers/search.js | 117 ++++++----- server/public/assets/js/frontpage.js | 2 +- server/routing/client/api.js | 281 +++++++++++++++++---------- 4 files changed, 247 insertions(+), 155 deletions(-) diff --git a/server/VERSION.js b/server/VERSION.js index a28f81d4..8e327b54 100644 --- a/server/VERSION.js +++ b/server/VERSION.js @@ -1,4 +1,4 @@ -VERSION = 3; +VERSION = 4; try { module.exports = VERSION; diff --git a/server/handlers/search.js b/server/handlers/search.js index bee87004..3b1503e4 100644 --- a/server/handlers/search.js +++ b/server/handlers/search.js @@ -39,18 +39,21 @@ function get_correct_info(song_generated, channel, broadcast, callback) { if(broadcast && docs.nModified == 1) { song_generated.new_id = song_generated.id; //if(song_generated.type == "video") - io.to(channel).emit("channel", {type: "changed_values", value: song_generated}); if(typeof(callback) == "function") { - callback(); + callback(song_generated, true); + } else { + io.to(channel).emit("channel", {type: "changed_values", value: song_generated}); } } else { - callback(); + callback(song_generated, true); } }); } + } else { + findSimilar(song_generated, channel, broadcast, callback) } } catch(e){ - console.log(e); + callback({}, false); } }); } @@ -69,51 +72,7 @@ function check_error_video(msg, channel) { try { var resp = JSON.parse(body); if(resp.pageInfo.totalResults == 0) { - var yt_url = "https://www.googleapis.com/youtube/v3/search?key="+key+"&videoEmbeddable=true&part=id&type=video&order=viewCount&safeSearch=none&maxResults=5&q=" + encodeURIComponent(msg.title); - request({ - method: "GET", - url: yt_url, - }, function(error, response, body){ - var resp = JSON.parse(body); - if(resp.items.length > 0) { - var vid_url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key="+key+"&id="; - for(var i = 0; i < resp.items.length; i++) { - vid_url += resp.items[i].id.videoId + ","; - } - request({ - type: "GET", - url: vid_url - }, function(error, response, body) { - var resp = JSON.parse(body); - var found = false; - var element = {}; - for(var i = 0; i < resp.items.length; i++) { - if(similarity(resp.items[i].snippet.localized.title, msg.title) > 0.75) { - found = true; - element = { - title: resp.items[i].snippet.localized.title, - duration: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), - id: resp.items[i].id, - start: 0, - end: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), - } - break; - } - } - if(found) { - db.collection(channel).update({"id": msg.id}, { - $set: element - }, function(err, docs) { - if(docs.nModified == 1) { - element.new_id = element.id; - element.id = msg.id; - io.to(channel).emit("channel", {type: "changed_values", value: element}); - } - }); - } - }); - } - }); + findSimilar(msg, channel, true, undefined) } } catch(e){ console.log(e); @@ -121,6 +80,66 @@ function check_error_video(msg, channel) { }); } +function findSimilar(msg, channel, broadcast, callback) { + var yt_url = "https://www.googleapis.com/youtube/v3/search?key="+key+"&videoEmbeddable=true&part=id&type=video&order=viewCount&safeSearch=none&maxResults=5&q=" + encodeURIComponent(msg.title); + request({ + method: "GET", + url: yt_url, + }, function(error, response, body){ + var resp = JSON.parse(body); + if(resp.items.length > 0) { + var vid_url = "https://www.googleapis.com/youtube/v3/videos?part=contentDetails,snippet,id&key="+key+"&id="; + for(var i = 0; i < resp.items.length; i++) { + vid_url += resp.items[i].id.videoId + ","; + } + request({ + type: "GET", + url: vid_url + }, function(error, response, body) { + var resp = JSON.parse(body); + var found = false; + var element = {}; + for(var i = 0; i < resp.items.length; i++) { + if(similarity(resp.items[i].snippet.localized.title, msg.title) > 0.75) { + found = true; + element = { + title: resp.items[i].snippet.localized.title, + duration: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), + id: resp.items[i].id, + start: 0, + end: parseInt(durationToSeconds(resp.items[i].contentDetails.duration)), + } + break; + } + } + if(found) { + db.collection(channel).update({"id": msg.id}, { + $set: element + }, function(err, docs) { + if(docs.nModified == 1 && broadcast) { + element.new_id = element.id; + element.id = msg.id; + if(!callback) { + io.to(channel).emit("channel", {type: "changed_values", value: element}); + } + } + if(callback) { + msg.title = element.title; + msg.id = element.id; + msg.duration = element.duration; + msg.start = element.start; + msg.end = element.end; + callback(msg, true); + } + }); + } else if(callback) { + callback({}, false); + } + }); + } + }); +} + function similarity(s1, s2) { var longer = s1; var shorter = s2; diff --git a/server/public/assets/js/frontpage.js b/server/public/assets/js/frontpage.js index 1f749243..c7c726cc 100755 --- a/server/public/assets/js/frontpage.js +++ b/server/public/assets/js/frontpage.js @@ -254,7 +254,7 @@ var Frontpage = { url: add + "/api/frontpages", method: "get", success: function(response){ - Frontpage.frontpage_function(response); + Frontpage.frontpage_function(response.results); }, error: function() { Materialize.toast("Couldn't fetch lists, trying again in 3 seconds..", 3000, "red lighten connect_error"); diff --git a/server/routing/client/api.js b/server/routing/client/api.js index e3249540..ee4b008d 100644 --- a/server/routing/client/api.js +++ b/server/routing/client/api.js @@ -31,6 +31,51 @@ var toShowConfig = { _id: 0 }; +var error = { + not_found: { + youtube: { + status: 404, + error: "Couldn't find a song like that on YouTube.", + success: false, + }, + local: { + status: 404, + error: "Couldn't find a song like that in the channel", + success: false, + }, + list: { + status: 404, + error: "The list doesn't exist", + success: false, + } + }, + not_authenticated: { + status: 403, + error: "Wrong adminpassword or userpassword.", + success: false, + }, + formatting: { + status: 400, + error: "Malformed request parameters.", + success: false, + }, + conflicting: { + status: 409, + error: "That element already exists.", + success: false, + }, + tooMany: { + status: 429, + error: "You're doing too many requests, check header-field Retry-After for the wait-time left.", + success: false, + }, + no_error: { + status: 200, + error: false, + success: true, + } +} + router.use(function(req, res, next) { next(); // make sure we go to the next routes and don't stop here }); @@ -41,10 +86,14 @@ router.route('/api/help').get(function(req, res) { }) router.route('/api/frontpages').get(function(req, res) { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); db.collection("frontpage_lists").find({frontpage: true, count: {$gt: 0}}, function(err, docs) { db.collection("connected_users").find({"_id": "total_users"}, function(err, tot) { - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify({channels: docs, viewers: tot[0].total_users.length})); + var to_return = error.no_error; + to_return.results = {channels: docs, viewers: tot[0].total_users.length}; + res.status(200).send(JSON.stringify(to_return)); return; }); }); @@ -57,9 +106,10 @@ router.route('/api/generate_name').get(function(req, res) { router.route('/api/list/:channel_name/:video_id').delete(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); if(!req.body.hasOwnProperty('adminpass') || !req.body.hasOwnProperty('userpass') || !req.params.hasOwnProperty('channel_name') || !req.params.hasOwnProperty('video_id')) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } try { @@ -74,19 +124,19 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) { throw "Wrong format"; } } catch(e) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } checkTimeout(guid, res, "DELETE", function() { validateLogin(adminpass, userpass, channel_name, "delete", res, function(exists) { if(!exists) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } db.collection(channel_name).find({id:video_id, now_playing: false}, function(err, docs){ if(docs.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.local)); return; } var dont_increment = false; @@ -99,13 +149,13 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) { if(!dont_increment) { db.collection("frontpage_lists").update({_id: channel_name, count: {$gt: 0}}, {$inc: {count: -1}, $set:{accessed: Functions.get_time()}}, {upsert: true}, function(err, docs){ updateTimeout(guid, res, "DELETE", function(err, docs) { - res.sendStatus(200); + res.status(200).send(JSON.stringify(error.no_error)); return; }); }); } else { updateTimeout(guid, res, "DELETE", function(err, docs) { - res.sendStatus(200); + res.status(200).send(JSON.stringify(error.no_error)); return; }); } @@ -119,13 +169,14 @@ router.route('/api/list/:channel_name/:video_id').delete(function(req, res) { router.route('/api/conf/:channel_name').put(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); if(!req.body.hasOwnProperty('adminpass') || !req.body.hasOwnProperty('userpass') || !req.params.hasOwnProperty('channel_name') || !req.body.hasOwnProperty('vote') || !req.body.hasOwnProperty('addsongs') || !req.body.hasOwnProperty('longsongs') || !req.body.hasOwnProperty('frontpage') || !req.body.hasOwnProperty('allvideos') || !req.body.hasOwnProperty('skip') || !req.body.hasOwnProperty('shuffle') || !req.body.hasOwnProperty('userpass_changed')) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } try { @@ -153,13 +204,13 @@ router.route('/api/conf/:channel_name').put(function(req, res) { throw "Wrong format"; } } catch(e) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } checkTimeout(guid, res, "CONFIG", function() { validateLogin(adminpass, userpass, channel_name, "config", res, function(exists, conf) { if(!exists && conf.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } @@ -201,8 +252,9 @@ router.route('/api/conf/:channel_name').put(function(req, res) { }, {upsert:true}, function(err, docs){ updateTimeout(guid, res, "CONFIG", function(err, docs) { - res.header({'Content-Type': 'application/json'}); - res.status(200).send(JSON.stringify(obj)); + var to_return = error.no_error; + to_return.results = obj; + res.status(200).send(JSON.stringify(to_return)); return; }); }); @@ -214,9 +266,11 @@ router.route('/api/conf/:channel_name').put(function(req, res) { router.route('/api/list/:channel_name/:video_id').put(function(req,res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); + if(!req.body.hasOwnProperty('adminpass') || !req.body.hasOwnProperty('userpass') || !req.params.hasOwnProperty('channel_name') || !req.params.hasOwnProperty('video_id')) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -232,22 +286,22 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) { throw "Wrong format"; } } catch(e) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } checkTimeout(guid, res, "PUT", function() { validateLogin(adminpass, userpass, channel_name, "vote", res, function(exists) { if(!exists) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } db.collection(channel_name).find({id: video_id, now_playing: false, type:"video"}, function(err, song) { if(song.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.local)); return; } else if(song[0].guids.indexOf(guid) > -1) { - res.sendStatus(409); + res.status(409).send(JSON.stringify(error.conflicting)); return; } else { song[0].votes += 1; @@ -256,8 +310,9 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) { io.to(channel_name).emit("channel", {type: "vote", value: video_id, time: Functions.get_time()}); List.getNextSong(channel_name, function() { updateTimeout(guid, res, "PUT", function(err, docs) { - res.header({'Content-Type': 'application/json'}); - res.status(200).send(JSON.stringify(song[0])); + var to_return = error.no_error; + to_return.results = song[0]; + res.status(200).send(JSON.stringify(to_return)); return; }); }); @@ -271,9 +326,11 @@ router.route('/api/list/:channel_name/:video_id').put(function(req,res) { router.route('/api/list/:channel_name/__np__').post(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); + if(!req.body.hasOwnProperty('userpass')) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -284,7 +341,7 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) { var userpass = req.body.userpass; if(typeof(userpass) != "string") { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -293,19 +350,18 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) { if(list.length > 0) { db.collection(channel_name + "_settings").find({views: {$exists: true}}, function(err, conf) { if(conf.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } else if(conf[0].userpass != userpass && conf[0].userpass != "") { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } updateTimeout(guid, res, "POST", function(err, docs) { - res.setHeader('Content-Type', 'application/json'); res.status(200).send(JSON.stringify(list[0])); }); }); } else { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); } }); }); @@ -314,6 +370,7 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) { router.route('/api/list/:channel_name/:video_id').post(function(req,res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); var fetch_only = false; if(req.body.hasOwnProperty('fetch_song')) { @@ -323,7 +380,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { !req.params.hasOwnProperty('channel_name') || !req.params.hasOwnProperty('video_id') || !req.body.hasOwnProperty('duration') || !req.body.hasOwnProperty('start_time') || !req.body.hasOwnProperty('end_time') || !req.body.hasOwnProperty('title'))) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } try { @@ -346,7 +403,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { } } } catch(e) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -357,61 +414,68 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { if(result.length == 0 || result[0].type == "suggested") { var song_type = authenticated ? "video" : "suggested"; if(fetch_only && result.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.local)); return; } - db.collection(channel_name).find({now_playing: true}, function(err, now_playing) { var set_np = false; if(now_playing.length == 0 && authenticated) { set_np = true; } var new_song = {"added": Functions.get_time(),"guids":[guid],"id":video_id,"now_playing":set_np,"title":title,"votes":1, "duration":duration, "start": parseInt(start_time), "end": parseInt(end_time), "type": song_type}; - db.collection("frontpage_lists").find({"_id": channel_name}, function(err, count) { - var create_frontpage_lists = false; - if(count.length == 0) { - create_frontpage_lists = true; + Search.get_correct_info(new_song, channel_name, false, function(element, found) { + if(!found) { + res.status(404).send(JSON.stringify(error.not_found.youtube)); + return; } - if(!exists) { - var configs = {"addsongs":false, "adminpass":"", "allvideos":true, "frontpage":true, "longsongs":false, "removeplay": false, "shuffle": true, "skip": false, "skips": [], "startTime":Functions.get_time(), "views": [], "vote": false, "desc": ""}; - db.collection(channel_name + "_settings").insert(configs, function(err, docs){ - io.to(channel_name).emit("conf", configs); - }); - } - db.collection(channel_name).update({"id": new_song.id}, new_song, {upsert: true}, function(err, success) { - if(create_frontpage_lists) { - db.collection("frontpage_lists").update({"_id": channel_name, "count" : (authenticated ? 1 : 0), "frontpage": true, "accessed": Functions.get_time(), "viewers": 1}, {upsert: true}, function(err, docs) { - if(authenticated) { - io.to(channel_name).emit("channel", {type: "added", value: new_song}); - } else { - io.to(channel_name).emit("suggested", new_song); - } - postEnd(channel_name, configs, new_song, guid, res, authenticated); - }); - } else if(set_np) { - Frontpage.update_frontpage(channel_name, video_id, title, function() { - io.to(channel_name).emit("np", new_song); - postEnd(channel_name, configs, new_song, guid, res, authenticated); - }); - } else { - db.collection("frontpage_lists").update({"_id": channel_name}, {$inc: {count: (authenticated ? 1 : 0)}}, function(err, docs) { - if(authenticated) { - io.to(channel_name).emit("channel", {type: "added", value: new_song}); - } else { - io.to(channel_name).emit("suggested", new_song); - } - postEnd(channel_name, configs, new_song, guid, res, authenticated); + new_song = element; + db.collection("frontpage_lists").find({"_id": channel_name}, function(err, count) { + var create_frontpage_lists = false; + if(count.length == 0) { + create_frontpage_lists = true; + } + if(!exists) { + var configs = {"addsongs":false, "adminpass":"", "allvideos":true, "frontpage":true, "longsongs":false, "removeplay": false, "shuffle": true, "skip": false, "skips": [], "startTime":Functions.get_time(), "views": [], "vote": false, "desc": ""}; + db.collection(channel_name + "_settings").insert(configs, function(err, docs){ + io.to(channel_name).emit("conf", configs); }); } - }); - }) - }) + db.collection(channel_name).update({"id": new_song.id}, new_song, {upsert: true}, function(err, success) { + if(create_frontpage_lists) { + db.collection("frontpage_lists").update({"_id": channel_name, "count" : (authenticated ? 1 : 0), "frontpage": true, "accessed": Functions.get_time(), "viewers": 1}, {upsert: true}, function(err, docs) { + if(authenticated) { + io.to(channel_name).emit("channel", {type: "added", value: new_song}); + } else { + io.to(channel_name).emit("suggested", new_song); + } + postEnd(channel_name, configs, new_song, guid, res, authenticated); + }); + } else if(set_np) { + Frontpage.update_frontpage(channel_name, video_id, title, function() { + io.to(channel_name).emit("np", new_song); + postEnd(channel_name, configs, new_song, guid, res, authenticated); + }); + } else { + db.collection("frontpage_lists").update({"_id": channel_name}, {$inc: {count: (authenticated ? 1 : 0)}}, function(err, docs) { + if(authenticated) { + io.to(channel_name).emit("channel", {type: "added", value: new_song}); + } else { + io.to(channel_name).emit("suggested", new_song); + } + postEnd(channel_name, configs, new_song, guid, res, authenticated); + }); + } + }); + }) + }); + }); } else if(fetch_only) { - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(result[0])); + var to_return = error.no_error; + to_return.results = result[0]; + res.status(200).send(JSON.stringify(to_return)); return; } else { - res.sendStatus(409); + res.status(409).send(JSON.stringify(error.conflicting)); return; } }); @@ -422,23 +486,25 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { router.route('/api/list/:channel_name').get(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); var channel_name = req.params.channel_name; db.collection(channel_name).find({views: {$exists: false}}, toShowChannel, function(err, docs) { if(docs.length > 0) { db.collection(channel_name + "_settings").find({views: {$exists: true}}, function(err, conf) { if(conf.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } else if(conf[0].userpass != "" && conf[0].userpass != undefined) { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(docs)); + var to_return = error.no_error; + to_return.results = docs; + res.status(200).send(JSON.stringify(to_return)); }); } else { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); } }); }); @@ -446,6 +512,7 @@ router.route('/api/list/:channel_name').get(function(req, res) { router.route('/api/list/:channel_name/:video_id').get(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); var channel_name = req.params.channel_name; var video_id = req.params.video_id; @@ -457,18 +524,19 @@ router.route('/api/list/:channel_name/:video_id').get(function(req, res) { if(docs.length > 0) { db.collection(channel_name + "_settings").find({views: {$exists: true}}, function(err, conf) { if(conf.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } else if(conf[0].userpass != "" && conf[0].userpass != undefined) { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(docs[0])); + var to_return = error.no_error; + to_return.results = docs[0]; + res.status(200).send(JSON.stringify(to_return)); return; }); } else { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.local)); return; } }); @@ -477,6 +545,7 @@ router.route('/api/list/:channel_name/:video_id').get(function(req, res) { router.route('/api/conf/:channel_name').get(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); var channel_name = req.params.channel_name; db.collection(channel_name + "_settings").find({views: {$exists: true}}, toShowConfig, function(err, docs) { @@ -492,13 +561,14 @@ router.route('/api/conf/:channel_name').get(function(req, res) { } else { conf.userpass = false; } - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(conf)); + var to_return = error.no_error; + to_return.results = conf; + res.status(200).send(JSON.stringify(to_return)); } else if(docs.length > 0 && docs[0].userpass != "" && docs[0].userpass != undefined){ - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } else { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } }); @@ -507,9 +577,10 @@ router.route('/api/conf/:channel_name').get(function(req, res) { router.route('/api/conf/:channel_name').post(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); if(!req.body.hasOwnProperty('userpass')) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; @@ -519,7 +590,7 @@ router.route('/api/conf/:channel_name').post(function(req, res) { var userpass = req.body.userpass; if(typeof(userpass) != "string") { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -538,14 +609,15 @@ router.route('/api/conf/:channel_name').post(function(req, res) { conf.userpass = false; } updateTimeout(guid, res, "POST", function(err, docs) { - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(conf)); + var to_return = error.no_error; + to_return.results = conf; + res.status(200).send(JSON.stringify(to_return)); }); } else if(docs.length > 0 && docs[0].userpass != userpass) { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } else { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } }); @@ -555,9 +627,10 @@ router.route('/api/conf/:channel_name').post(function(req, res) { router.route('/api/list/:channel_name').post(function(req, res) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.header({"Content-Type": "application/json"}); if(!req.body.hasOwnProperty('userpass')) { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -568,7 +641,7 @@ router.route('/api/list/:channel_name').post(function(req, res) { var userpass = req.body.userpass; if(typeof(userpass) != "string") { - res.sendStatus(400); + res.status(400).send(JSON.stringify(error.formatting)); return; } @@ -577,19 +650,20 @@ router.route('/api/list/:channel_name').post(function(req, res) { if(list.length > 0) { db.collection(channel_name + "_settings").find({views: {$exists: true}}, function(err, conf) { if(conf.length == 0) { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); return; } else if(conf[0].userpass != userpass && conf[0].userpass != "") { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } updateTimeout(guid, res, "POST", function(err, docs) { - res.setHeader('Content-Type', 'application/json'); - res.status(200).send(JSON.stringify(list)); + var to_return = error.no_error; + to_return.results = list; + res.status(200).send(JSON.stringify(to_return)); }); }); } else { - res.sendStatus(404); + res.status(404).send(JSON.stringify(error.not_found.list)); } }); }); @@ -689,7 +763,7 @@ function checkTimeout(guid, res, type, callback) { var retry_in = (date.getTime() - now.getTime()) / 1000; if(retry_in > 0) { res.header({'Retry-After': retry_in}); - res.sendStatus(429); + res.status(429).send(JSON.stringify(error.tooMany)); return; } } @@ -711,7 +785,7 @@ function validateLogin(adminpass, userpass, channel_name, type, res, callback) { if(conf.length > 0 && ((conf[0].userpass == undefined || conf[0].userpass == "" || conf[0].userpass == userpass))) { exists = true; } else if(conf.length > 0 && type != "config") { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } @@ -726,7 +800,7 @@ function validateLogin(adminpass, userpass, channel_name, type, res, callback) { } else if(type == "add") { callback(exists, conf, false); } else { - res.sendStatus(403); + res.status(404).send(JSON.stringify(error.not_authenticated)); return; } }); @@ -738,11 +812,10 @@ function postEnd(channel_name, configs, new_song, guid, res, authenticated) { } List.getNextSong(channel_name, function() { updateTimeout(guid, res, "POST", function(err, docs) { - Search.get_correct_info(new_song, channel_name, !new_song.now_playing, function() { - res.header({'Content-Type': 'application/json'}); - res.status(authenticated ? 200 : 403).send(JSON.stringify(new_song)); - return; - }); + var to_return = error.no_error; + to_return.results = new_song; + res.status(authenticated ? 200 : 403).send(JSON.stringify(to_return)); + return; }); }); } From 7aad759c264a6d04d01e6b788f1c4347cee08307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Fri, 2 Mar 2018 10:48:15 +0100 Subject: [PATCH 2/3] Cleaned up admin.js some --- server/public/assets/js/admin.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/public/assets/js/admin.js b/server/public/assets/js/admin.js index 244c074a..f6bb094c 100755 --- a/server/public/assets/js/admin.js +++ b/server/public/assets/js/admin.js @@ -146,8 +146,8 @@ var Admin = { }, pw: function(msg) { - if(msg == false) return; - w_p = false; + if(!msg) return; + w_p = false; if(adminpass == undefined || adminpass == "") { adminpass = Crypt.get_pass(chan.toLowerCase()); } From ec9b9676fba91bf84d236187f23afefdcd59dfa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Fri, 2 Mar 2018 10:52:43 +0100 Subject: [PATCH 3/3] Added fix for ga not being present on localhost --- server/public/layouts/client/main.handlebars | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/public/layouts/client/main.handlebars b/server/public/layouts/client/main.handlebars index d22342cf..9839e27b 100644 --- a/server/public/layouts/client/main.handlebars +++ b/server/public/layouts/client/main.handlebars @@ -34,13 +34,17 @@