Added a own aggregates file

- Aggregate and project missing fields
- Updated how channels are created, to add the new fields
This commit is contained in:
Kasper Rynning-Tønnesen
2018-09-28 11:13:49 +02:00
parent 3102893e15
commit 693e30f273
5 changed files with 151 additions and 127 deletions

View File

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

View File

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

View File

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