From 6c0c110b68ab52d2d6d9051dd51f23d377797b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Sun, 24 Feb 2019 23:59:00 +0100 Subject: [PATCH] Added search-endpoint for fetching by category --- server/apps/client.js | 2 +- server/public/assets/js/list.js | 2 +- server/routing/client/api.js | 88 ++++++++++++++++++++++++++++++++- 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/server/apps/client.js b/server/apps/client.js index 8861f99f..4e448e43 100755 --- a/server/apps/client.js +++ b/server/apps/client.js @@ -72,7 +72,7 @@ app.use(bodyParser.urlencoded({ // to support URL-encoded bodies extended: true })); app.use(cookieParser()); -app.set('json spaces', 2); +//app.set('json spaces', 2); io = require('socket.io')({ pingTimeout: 25000, diff --git a/server/public/assets/js/list.js b/server/public/assets/js/list.js index 9448397c..9ed21f8f 100755 --- a/server/public/assets/js/list.js +++ b/server/public/assets/js/list.js @@ -1248,7 +1248,7 @@ var List = { var video_title = _song_info.title; var video_votes = _song_info.votes; var tags = ""; - if(_song_info.tags != undefined) _song_info.tags.toLowerCase(); + if(_song_info.tags != undefined) _song_info.tags.join(",").toLowerCase(); var video_thumb_url = "//img.youtube.com/vi/"+video_id+"/mqdefault.jpg"; if(_song_info.source == "soundcloud") { video_thumb_url = _song_info.thumbnail; diff --git a/server/routing/client/api.js b/server/routing/client/api.js index edf27440..95f8c8a1 100644 --- a/server/routing/client/api.js +++ b/server/routing/client/api.js @@ -732,6 +732,90 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) { }); }); + +router.route('/api/search/: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"}); + try { + var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress; + var guid = Functions.hash_pass(req.get('User-Agent') + ip + req.headers["accept-language"]); + var channel_name = cleanChannelName(req.params.channel_name); + var userpass; + if(req.body.hasOwnProperty("userpass")) { + req.body.userpass = req.body.userpass == "" ? "" : crypto.createHash('sha256').update(req.body.userpass, 'utf8').digest("base64"); + userpass = req.body.userpass; + } else { + userpass = ""; + } + var searchQuery = ""; + if(req.body.searchQuery == undefined || req.body.searchQuery == "") { + var to_send = error.formatting; + to_send.results = [result]; + res.status(400).send(to_send); + return; + } + searchQuery = req.body.searchQuery; + var token = ""; + if(req.body.hasOwnProperty("token")) { + token = req.body.token; + } + } catch(e) { + var result = { + userpass: { + expected: "string", + got: req.body.hasOwnProperty("userpass") ? typeof(req.body.userpass) : undefined + } + }; + var to_send = error.formatting; + to_send.results = [result]; + res.status(400).send(to_send); + return; + } + var cookie = req.cookies._uI; + Functions.getSessionAdminUser(cookie, channel_name, function(_u, _a) { + if(req.body.userpass == "") { + //userpass = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(_u))) + userpass = _u; + } + token_db.collection("api_token").find({token: token}, function(err, token_docs) { + var authorized = false; + var origin; + try { + origin = req.headers.referer.split("/")[2]; + } catch(e) { origin = ""; } + if(token_docs.length == 1 && token_docs[0].token == token && (token_docs[0].origin == "*" || origin.indexOf(token_docs[0].origin) > -1)) { + authorized = true; + } + checkOveruseApiToken(authorized, token_docs, res, function() { + checkTimeout(guid, res, authorized, "POST", function() { + db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) { + if(authorized) { + incrementToken(token); + } + if(conf.length == 0) { + res.status(404).send(error.not_found.list); + return; + } else if(conf[0].userpass != userpass && conf[0].userpass != "" && conf[0].userpass != undefined) { + res.status(403).send(error.not_authenticated); + return; + } + db.collection(channel_name).find({tags: {$regex : ".*" + searchQuery + ".*"}}, function(e, results) { + if(results.length == 0) { + res.status(404).send(error.not_found.list); + return; + } + var to_return = error.no_error; + to_return.results = results; + res.status(200).send(to_return); + }); + }); + }); + }); + }); + }); +}); + 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"); @@ -770,6 +854,8 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { var duration = parseInt(req.body.duration); var start_time = parseInt(req.body.start_time); var end_time = parseInt(req.body.end_time); + var tags = []; + if(req.body.tags != undefined) tags = req.body.tags.split(","); var source = req.body.source; if(source == "soundcloud" && !req.body.hasOwnProperty("thumbnail")) { throw "Wrong format"; @@ -852,7 +938,7 @@ router.route('/api/list/:channel_name/:video_id').post(function(req,res) { 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, "source": source}; + var new_song = {"tags": tags, "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, "source": source}; var runFunction = Search.get_correct_info; if(source == "soundcloud") { if(req.body.thumbnail.indexOf("https://i1.sndcdn.com") > -1 || req.body.thumbnail.indexOf("https://w1.sndcdn.com") > -1) {