From 693e30f27378b421cf422934988956d24725cffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Fri, 28 Sep 2018 11:13:49 +0200 Subject: [PATCH] Added a own aggregates file - Aggregate and project missing fields - Updated how channels are created, to add the new fields --- server/handlers/aggregates.js | 57 ++++++++++++++++ server/handlers/list.js | 36 ++++------- server/handlers/list_settings.js | 11 +++- server/routing/admin/api.js | 67 +++++++++++++++++-- server/routing/client/api.js | 107 +++---------------------------- 5 files changed, 151 insertions(+), 127 deletions(-) create mode 100644 server/handlers/aggregates.js diff --git a/server/handlers/aggregates.js b/server/handlers/aggregates.js new file mode 100644 index 00000000..bedbdae2 --- /dev/null +++ b/server/handlers/aggregates.js @@ -0,0 +1,57 @@ +var toShowConfig = { + "addsongs": true, + "adminpass": 1, + "allvideos": 1, + "frontpage": 1, + "longsongs": 1, + "removeplay": 1, + "shuffle": 1, + "skip": 1, + "startTime": 1, + "userpass": 1, + "vote": 1, + "toggleChat": { $ifNull: [ "$toggleChat", true ] }, + "description": { $ifNull: [ "$description", "" ] }, + "thumbnail": { $ifNull: [ "$thumbnail", "" ] }, + "rules": { $ifNull: [ "$rules", "" ] }, + "_id": 0 +}; + +var project_object = { + "_id": 0, + "id": 1, + "added": 1, + "now_playing": 1, + "title": 1, + "votes": 1, + "start": 1, + "duration": 1, + "end": 1, + "type": 1, + "source": { $ifNull: [ "$source", "youtube" ] }, + "thumbnail": { + $ifNull: [ "$thumbnail", { + $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] + } ] + } +}; + +var toShowChannel = { + start: 1, + end: 1, + added: 1, + id: 1, + title: 1, + votes: 1, + duration: 1, + type: 1, + _id: 0, + now_playing: 1, + type: 1, + source: 1, + thumbnail: 1, +}; + +module.exports.project_object = project_object; +module.exports.toShowConfig = toShowConfig; +module.exports.toShowChannel = toShowChannel; diff --git a/server/handlers/list.js b/server/handlers/list.js index f2548e66..4de1deb2 100644 --- a/server/handlers/list.js +++ b/server/handlers/list.js @@ -2,6 +2,7 @@ var ColorThief = require('color-thief-jimp'); var Jimp = require('jimp'); var Functions = require(pathThumbnails + '/handlers/functions.js'); var Frontpage = require(pathThumbnails + '/handlers/frontpage.js'); +var projects = require(pathThumbnails + "/handlers/aggregates.js"); var crypto = require('crypto'); var Filter = require('bad-words'); var filter = new Filter({ placeHolder: 'x'}); @@ -98,7 +99,7 @@ function list(msg, guid, coll, offline, socket) { } 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"}; + 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); @@ -421,38 +422,29 @@ function change_song_post(coll, next_song, conf, callback, socket) { function send_list(coll, socket, send, list_send, configs, shuffled) { //coll = coll.replace(/ /g,''); - db.collection(coll + "_settings").find({id: "config"}, function(err, _conf){ + db.collection(coll + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, _conf){ 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, "desc": "", userpass: ""}; + 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: ""}; db.collection(coll + "_settings").update({id: "config"}, conf, {upsert: true}, function(err, docs) { send_list(coll, socket, send, list_send, configs, shuffled); }); } else { - var project_object = { - "_id": 0, - "id": 1, - "added": 1, - "now_playing": 1, - "title": 1, - "votes": 1, - "start": 1, - "duration": 1, - "end": 1, - "type": 1, - "source": { $ifNull: [ "$source", "youtube" ] }, - "thumbnail": { - $ifNull: [ "$thumbnail", { - $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] - } ] - } - }; db.collection(coll).aggregate([ { "$match": {type: {$ne: "suggested"}} }, { - "$project": project_object + "$project": projects.project_object }, { "$sort" : { "now_playing" : -1, "votes": -1, "added": 1 } } ], function(err, docs) diff --git a/server/handlers/list_settings.js b/server/handlers/list_settings.js index c57a6403..93ba8456 100644 --- a/server/handlers/list_settings.js +++ b/server/handlers/list_settings.js @@ -230,7 +230,16 @@ function conf_function(params, coll, guid, offline, socket) { $set:obj }, function(err, docs){ Functions.setSessionUserPass(Functions.getSession(socket), obj["userpass"], coll, function() { - db.collection(coll + "_settings").find(function(err, docs){ + db.collection(coll + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs){ if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; diff --git a/server/routing/admin/api.js b/server/routing/admin/api.js index ff1638d6..c393134e 100644 --- a/server/routing/admin/api.js +++ b/server/routing/admin/api.js @@ -9,6 +9,7 @@ var uniqid = require('uniqid'); var crypto = require('crypto'); var ObjectId = mongojs.ObjectId; var sIO = require(path.join(__dirname, '../../apps/client.js')).socketIO; +var projects = require(pathThumbnails + "/handlers/aggregates.js"); router.use(function(req, res, next) { next(); // make sure we go to the next routes and don't stop here @@ -62,7 +63,16 @@ router.route('/api/approve_thumbnail').post(function(req, res){ db.collection("frontpage_lists").update({_id: channel}, {$set:{thumbnail: thumbnail}}, {upsert: true}, function(err, docs){ db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{thumbnail: thumbnail}}, {upsert: true}, function(err, docs){ db.collection("suggested_thumbnails").remove({channel: channel}, function(err, docs){ - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs) { if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; @@ -97,7 +107,16 @@ router.route('/api/approve_rules').post(function(req, res){ var rules = docs[0].rules; db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{rules: rules}}, {upsert: true}, function(err, docs){ db.collection("suggested_rules").remove({channel: channel}, function(err, docs){ - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs) { if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; @@ -127,7 +146,16 @@ router.route('/api/remove_rules').post(function(req, res){ if(req.isAuthenticated()){ var channel = req.body.channel; db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{rules: ""}}, function(err, docs){ - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs) { if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; @@ -148,7 +176,16 @@ router.route('/api/approve_description').post(function(req, res){ db.collection("frontpage_lists").update({_id: channel}, {$set:{description: description}}, {upsert: true}, function(err, docs){ db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{description: description}}, function(err, docs){ db.collection("suggested_descriptions").remove({channel: channel}, function(err, docs){ - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs) { if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; @@ -180,7 +217,16 @@ router.route('/api/remove_thumbnail').post(function(req, res){ var channel = req.body.channel; db.collection("frontpage_lists").update({_id: channel}, {$set:{thumbnail: ""}}, function(err, docs){ db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{thumbnail: ""}}, function(err, docs){ - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs) { if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; @@ -199,7 +245,16 @@ router.route('/api/remove_description').post(function(req, res){ var channel = req.body.channel; db.collection("frontpage_lists").update({_id: channel}, {$set:{description: ""}}, function(err, docs){ db.collection(channel + "_settings").update({views:{$exists:true}}, {$set:{description: ""}}, function(err, docs){ - db.collection(channel + "_settings").find(function(err, docs) { + db.collection(channel + "_settings").aggregate([ + { + "$match": { + id: "config" + } + }, + { + "$project": projects.toShowConfig + }, + ], function(err, docs) { if(docs[0].adminpass !== "") docs[0].adminpass = true; if(docs[0].hasOwnProperty("userpass") && docs[0].userpass != "") docs[0].userpass = true; else docs[0].userpass = false; diff --git a/server/routing/client/api.js b/server/routing/client/api.js index ba0f0776..f96b0de8 100644 --- a/server/routing/client/api.js +++ b/server/routing/client/api.js @@ -14,60 +14,7 @@ var Search = require(pathThumbnails + '/handlers/search.js'); var uniqid = require('uniqid'); var Filter = require('bad-words'); var filter = new Filter({ placeHolder: 'x'}); - -var toShowChannel = { - start: 1, - end: 1, - added: 1, - id: 1, - title: 1, - votes: 1, - duration: 1, - type: 1, - _id: 0, - now_playing: 1, - type: 1, - source: 1, - thumbnail: 1, -}; - -var project_object = { - "_id": 0, - "id": 1, - "added": 1, - "now_playing": 1, - "title": 1, - "votes": 1, - "start": 1, - "duration": 1, - "end": 1, - "type": 1, - "source": { $ifNull: [ "$source", "youtube" ] }, - "thumbnail": { - $ifNull: [ "$thumbnail", { - $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] - } ] - } -}; - -var toShowConfig = { - addsongs: 1, - adminpass: 1, - allvideos: 1, - frontpage: 1, - longsongs: 1, - removeplay: 1, - shuffle: 1, - skip: 1, - startTime: 1, - userpass: 1, - vote: 1, - toggleChat: 1, - description: { $ifNull: [ "$description", "" ] }, - thumbnail: { $ifNull: [ "$thumbnail", "" ] }, - rules: { $ifNull: [ "$rules", "" ] }, - _id: 0 -}; +var projects = require(pathThumbnails + "/handlers/aggregates.js"); var error = { not_found: { @@ -610,7 +557,7 @@ router.route('/api/list/:channel_name/__np__').post(function(req, res) { return; }); } else { - db.collection(channel_name).find({now_playing: true}, toShowChannel, function(err, list) { + db.collection(channel_name).find({now_playing: true}, projects.toShowChannel, function(err, list) { if(list.length > 0) { db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) { if(authorized) { @@ -847,35 +794,17 @@ 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 project_object = { - "_id": 0, - "id": 1, - "added": 1, - "now_playing": 1, - "title": 1, - "votes": 1, - "start": 1, - "duration": 1, - "end": 1, - "type": 1, - "source": { $ifNull: [ "$source", "youtube" ] }, - "thumbnail": { - $ifNull: [ "$thumbnail", { - $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] - } ] - } - }; var channel_name = cleanChannelName(req.params.channel_name); db.collection(channel_name).aggregate([ { "$match": { } }, { - "$project": project_object + "$project": projects.project_object }, { "$sort" : { "now_playing" : -1, "votes": -1, "added": 1 } } ], function(err, docs) { - //db.collection(channel_name).find({views: {$exists: false}}, toShowChannel, function(err, docs) { + //db.collection(channel_name).find({views: {$exists: false}}, projects.toShowChannel, function(err, docs) { if(docs.length > 0) { db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) { if(conf.length == 0) { @@ -906,7 +835,7 @@ router.route('/api/list/:channel_name/:video_id').get(function(req, res) { if(video_id == "__np__") { searchQuery = {now_playing: true}; } - db.collection(channel_name).find(searchQuery, toShowChannel, function(err, docs) { + db.collection(channel_name).find(searchQuery, projects.toShowChannel, function(err, docs) { db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) { if(conf.length == 0) { res.status(404).send(error.not_found.list); @@ -946,7 +875,7 @@ router.route('/api/conf/:channel_name').get(function(req, res) { } }, { - "$project": toShowConfig + "$project": projects.toShowConfig }, ], function(err, docs) { if(docs.length > 0 && (docs[0].userpass == "" || docs[0].userpass == undefined)) { @@ -1038,7 +967,7 @@ router.route('/api/conf/:channel_name').post(function(req, res) { } }, { - "$project": toShowConfig + "$project": projects.toShowConfig }, ], function(err, docs) { if(docs.length > 0 && docs[0].userpass == userpass) { @@ -1164,32 +1093,14 @@ router.route('/api/list/:channel_name').post(function(req, res) { return; }); } else { - var project_object = { - "_id": 0, - "id": 1, - "added": 1, - "now_playing": 1, - "title": 1, - "votes": 1, - "start": 1, - "duration": 1, - "end": 1, - "type": 1, - "source": { $ifNull: [ "$source", "youtube" ] }, - "thumbnail": { - $ifNull: [ "$thumbnail", { - $concat : [ "https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] - } ] - } - }; db.collection(channel_name).aggregate([ { "$match": { } }, - { "$project": project_object }, + { "$project": projects.project_object }, { "$sort" : { "now_playing" : -1, "votes": -1, "added": 1 } } ], function(err, list) { - //db.collection(channel_name).find({views: {$exists: false}}, toShowChannel, function(err, list) { + //db.collection(channel_name).find({views: {$exists: false}}, projects.toShowChannel, function(err, list) { if(list.length > 0) { db.collection(channel_name + "_settings").find({ id: "config" }, function(err, conf) { if(conf.length == 0) {