Changed functionality for adding playlists

dquote> - Sending event for addPlaylist, and sending the whole list instead of one-by one
This commit is contained in:
Kasper Rynning-Tønnesen
2018-03-21 21:10:08 +01:00
parent 4e87a6309f
commit f518a06ed2
5 changed files with 146 additions and 81 deletions

View File

@@ -35,6 +35,19 @@
pass: Base64(channel_pass) pass: Base64(channel_pass)
} }
'addPlaylist', {
channel: CHANNEL_NAME,
userpass: Base64(CHANNEL_PASSWORD),
adminpass: Base64(PASSWORD),
songs: [
{
id: song_id,
title: song_title,
duration: song_duration
}, ... { ... }
]
}
// Imports songs from another zoff-channel // Imports songs from another zoff-channel
'import_zoff', { 'import_zoff', {
channel: CHANNELNAME, channel: CHANNELNAME,

View File

@@ -285,6 +285,10 @@ module.exports = function() {
List.end(obj, coll, guid, offline, socket); List.end(obj, coll, guid, offline, socket);
}); });
socket.on('addPlaylist', function(arr) {
ListChange.addPlaylist(arr, guid, offline, socket);
})
socket.on('add', function(arr) socket.on('add', function(arr)
{ {
if(coll !== undefined) { if(coll !== undefined) {

View File

@@ -59,52 +59,58 @@ function addFromOtherList(arr, guid, offline, socket) {
if(new_conf.length > 0 && (new_conf[0].userpass == "" || !new_conf[0].userpass || new_conf[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(socketid, otheruser)).digest("base64"))) { if(new_conf.length > 0 && (new_conf[0].userpass == "" || !new_conf[0].userpass || new_conf[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(socketid, otheruser)).digest("base64"))) {
db.collection(channel + "_settings").find({id: "config"}, function(e, this_conf) { db.collection(channel + "_settings").find({id: "config"}, function(e, this_conf) {
var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true)); var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true));
if((this_conf[0].userpass == "" || !this_conf[0].userpass || this_conf[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(socketid, arr.userpass)).digest("base64")) && if((this_conf[0].userpass == "" || !this_conf[0].userpass || this_conf[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(socketid, arr.userpass)).digest("base64"))) {
((this_conf[0].addsongs === true && (hash == this_conf[0].adminpass || this_conf[0].adminpass === "")) || if(((this_conf[0].addsongs === true && (hash == this_conf[0].adminpass || this_conf[0].adminpass === "")) ||
this_conf[0].addsongs === false)) { this_conf[0].addsongs === false)) {
db.collection(new_channel).aggregate([ db.collection(new_channel).aggregate([
{ {
"$match": { type: "video" } "$match": { type: "video" }
}, },
{ {
"$project": project_object "$project": project_object
} }
], function(e, docs) { ], function(e, docs) {
var path = require('path'); var path = require('path');
var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js')); var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js'));
var MongoClient = require('mongodb').MongoClient; var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://" + mongo_config.host + ":" + mongo_config.port + "/"; var url = "mongodb://" + mongo_config.host + ":" + mongo_config.port + "/";
MongoClient.connect(url, function(err, _db) { MongoClient.connect(url, function(err, _db) {
var dbo = _db.db(mongo_config.config); var dbo = _db.db(mongo_config.config);
dbo.collection(channel).insertMany(docs, {ordered: false}, function(err, res) { dbo.collection(channel).insertMany(docs, {ordered: false}, function(err, res) {
if(to_set_np) { db.collection(channel + "_settings").update({id: "config"}, {$set: {startTime: Functions.get_time()}}, function(e,d) {
var to_change = { if(to_set_np) {
_id: channel, var to_change = {
count: docs.length, _id: channel,
frontpage: true, count: res.nInserted != undefined ? res.nInserted : res.insertedCount,
accessed: Functions.get_time(), frontpage: true,
} accessed: Functions.get_time(),
db.collection(channel).find({now_playing: true}, function(e, np_docs) { }
to_change.id = np_docs[0].id; db.collection(channel).find({now_playing: true}, function(e, np_docs) {
to_change.title = np_docs[0].title; to_change.id = np_docs[0].id;
db.collection("frontpage_lists").update({_id: channel}, {$set: to_change}, function(e, d) { to_change.title = np_docs[0].title;
List.send_list(channel, undefined, false, true, false); db.collection("frontpage_lists").update({_id: channel}, {$set: to_change}, function(e, d) {
List.send_play(channel, undefined); List.send_list(channel, undefined, false, true, false);
socket.emit("toast", "addedplaylist"); List.send_play(channel, undefined);
_db.close(); socket.emit("toast", "addedplaylist");
}); _db.close();
});
});
} else {
db.collection("frontpage_lists").update({_id: channel}, {$inc: {count: res.nInserted != undefined ? res.nInserted : res.insertedCount}}, function(e, d) {
List.send_list(channel, undefined, false, true, false);
List.send_play(channel, undefined);
socket.emit("toast", "addedplaylist");
_db.close();
})
}
}); });
} else { });
db.collection("frontpage_lists").update({_id: channel}, {$inc: {count: res.nInserted != undefined ? res.nInserted : res.insertedCount}}, function(e, d) {
List.send_list(channel, undefined, false, true, false);
List.send_play(channel, undefined);
socket.emit("toast", "addedplaylist");
_db.close();
})
}
}); });
}); });
}) } else {
socket.emit("toast", "listhaspass");
return;
}
} else { } else {
socket.emit("auth_required"); socket.emit("auth_required");
return; return;
@@ -122,11 +128,11 @@ function addFromOtherList(arr, guid, offline, socket) {
} }
} }
function addPlaylist(arr, guid, socket) { function addPlaylist(arr, guid, offline, socket) {
var socketid = socket.zoff_id; var socketid = socket.zoff_id;
if(typeof(arr) == "object") { if(typeof(arr) == "object") {
if(!arr.hasOwnProperty("channel") || !arr.hasOwnProperty("new_channel") if(!arr.hasOwnProperty("channel") || !arr.hasOwnProperty("songs")
|| typeof(arr.channel) != "string" || typeof(arr.new_channel) != "string") { || typeof(arr.channel) != "string" || typeof(arr.songs) != "object") {
var result = { var result = {
channel: { channel: {
expected: "string", expected: "string",
@@ -160,39 +166,75 @@ function addPlaylist(arr, guid, socket) {
db.collection(channel + "_settings").find({id: "config"}, function(e, conf) { db.collection(channel + "_settings").find({id: "config"}, function(e, conf) {
if(conf.length > 0) { if(conf.length > 0) {
var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true)); var hash = Functions.hash_pass(Functions.hash_pass(Functions.decrypt_string(socketid, arr.adminpass), true));
if((conf[0].userpass == "" || !conf[0].userpass || conf[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(socketid, arr.userpass)).digest("base64")) && if((conf[0].userpass == "" || !conf[0].userpass || conf[0].userpass == crypto.createHash('sha256').update(Functions.decrypt_string(socketid, arr.userpass)).digest("base64"))) {
((conf[0].addsongs === true && (hash == conf[0].adminpass || conf[0].adminpass === "")) || if(((conf[0].addsongs === true && (hash == conf[0].adminpass || conf[0].adminpass === "")) ||
conf[0].addsongs === false)) { conf[0].addsongs === false)) {
var path = require('path'); var path = require('path');
var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js')); var mongo_config = require(path.join(path.join(__dirname, '../config/'), 'mongo_config.js'));
var MongoClient = require('mongodb').MongoClient; var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://" + mongo_config.host + ":" + mongo_config.port + "/"; var url = "mongodb://" + mongo_config.host + ":" + mongo_config.port + "/";
MongoClient.connect(url, function(err, _db) { MongoClient.connect(url, function(err, _db) {
var dbo = _db.db(mongo_config.config); var dbo = _db.db(mongo_config.config);
var number_elements = arr.songs.length + 1; var number_elements = arr.songs.length + 1;
var time = Functions.get_time() - number_elements; var time = Functions.get_time() - number_elements;
var bulk = dbo.collection(channel).initializeUnorderedBulkOp({useLegacyOps: true}); var to_set_np = now_playing;
for(var i = 0; i < arr.songs.length; i++) { var bulk = dbo.collection(channel).initializeUnorderedBulkOp({useLegacyOps: true});
var this_element = arr.songs[i]; for(var i = 0; i < arr.songs.length; i++) {
this_element.added = time; var this_element = arr.songs[i];
this_element.now_playing = now_playing; if(!this_element.hasOwnProperty("duration") || !this_element.hasOwnProperty("id") || !this_element.hasOwnProperty("title")) {
this_element.votes = 0; continue;
this_element.guids = []; }
this_element.start = parseInt(this_element.start); this_element.added = time;
this_element.end = parseInt(this_element.end); this_element.now_playing = now_playing;
this_element.duration = parseInt(this_element.duration); this_element.votes = 0;
if(this_element.start > this_element.end) { this_element.guids = [];
this_element.start = 0; if(!this_element.hasOwnProperty("start")) this_element.start = 0;
if(!this_element.hasOwnProperty("end")) this_element.end = this_element.duration;
this_element.start = parseInt(this_element.start);
this_element.end = parseInt(this_element.end);
this_element.duration = parseInt(this_element.duration);
if(this_element.start > this_element.end) {
this_element.start = 0;
}
if(now_playing) {
now_playing = false;
}
bulk.insert(this_element);
} }
if(now_playing) { bulk.execute(function(err, results) {
now_playing = false; db.collection(channel + "_settings").update({id: "config"}, {$set: {startTime: Functions.get_time()}}, function(e,d) {
} if(to_set_np) {
bulk.insert(this_element); var to_change = {
} _id: channel,
bulk.execute(function(err, results) { count: results.nInserted,
console.log(err, results); frontpage: true,
accessed: Functions.get_time(),
}
db.collection(channel).find({now_playing: true}, function(e, np_docs) {
to_change.id = np_docs[0].id;
to_change.title = np_docs[0].title;
db.collection("frontpage_lists").update({_id: channel}, {$set: to_change}, function(e, d) {
List.send_list(channel, undefined, false, true, false);
List.send_play(channel, undefined);
socket.emit("toast", "addedplaylist");
_db.close();
});
});
} else {
db.collection("frontpage_lists").update({_id: channel}, {$inc: {count: results.nInserted != undefined ? results.nInserted : results.insertedCount}}, function(e, d) {
List.send_list(channel, undefined, false, true, false);
List.send_play(channel, undefined);
socket.emit("toast", "addedplaylist");
_db.close();
})
}
});
});
}); });
}); } else {
socket.emit("toast", "listhaspass");
return;
}
} else { } else {
socket.emit("auth_required"); socket.emit("auth_required");
return; return;
@@ -731,6 +773,7 @@ function vote(coll, id, guid, socket, full_list, last) {
}); });
} }
module.exports.addPlaylist = addPlaylist;
module.exports.addFromOtherList = addFromOtherList; module.exports.addFromOtherList = addFromOtherList;
module.exports.add_function = add_function; module.exports.add_function = add_function;
module.exports.voteUndecided = voteUndecided; module.exports.voteUndecided = voteUndecided;

View File

@@ -689,6 +689,8 @@ function toast(msg) {
return; return;
} }
} }
document.getElementById("import_spotify").disabled = false;
document.getElementById("import").disabled = false;
tried_again = false; tried_again = false;
msg=Helper.rnd(["I'm sorry, but you have to be an admin to do that!", "Only admins can do that", "You're not allowed to do that, try logging in!", "I can't let you do that", "Please log in to do that"]); msg=Helper.rnd(["I'm sorry, but you have to be an admin to do that!", "Only admins can do that", "You're not allowed to do that, try logging in!", "I can't let you do that", "Please log in to do that"]);
//Crypt.remove_pass(chan.toLowerCase()); //Crypt.remove_pass(chan.toLowerCase());

View File

@@ -282,9 +282,10 @@ var Search = {
Search.submitArrayExpected -= 1; Search.submitArrayExpected -= 1;
} }
if((Search.submitArray.length - 1) == Search.submitArrayExpected) { if((Search.submitArray.length - 1) == Search.submitArrayExpected) {
$.each(Search.submitArray, function(i, data){ socket.emit("addPlaylist", {channel: chan.toLowerCase(), songs: Search.submitArray});
/*$.each(Search.submitArray, function(i, data){
Search.submit(data.id, data.title, data.duration, true, i, Search.submitArray.length - 1, 0, data.duration); Search.submit(data.id, data.title, data.duration, true, i, Search.submitArray.length - 1, 0, data.duration);
}); });*/
document.getElementById("import_spotify").disabled = false; document.getElementById("import_spotify").disabled = false;
$("#import_spotify").removeClass("hide"); $("#import_spotify").removeClass("hide");
$("#playlist_loader_spotify").addClass("hide"); $("#playlist_loader_spotify").addClass("hide");
@@ -424,13 +425,15 @@ var Search = {
enc_title= song.snippet.title;//encodeURIComponent(song.snippet.title); enc_title= song.snippet.title;//encodeURIComponent(song.snippet.title);
//Search.submit(song.id, enc_title, duration, playlist, i); //Search.submit(song.id, enc_title, duration, playlist, i);
x += 1; x += 1;
to_add.push({id: song.id, enc_title: enc_title, duration: duration, playlist: playlist}); to_add.push({id: song.id, title: enc_title, duration: duration, playlist: playlist});
} }
}); });
socket.emit("addPlaylist", {channel: chan.toLowerCase(), songs: to_add});
/*
$.each(to_add, function(i, item){ $.each(to_add, function(i, item){
Search.submit(item.id, item.enc_title, item.duration, item.playlist, i, x, 0, item.duration); Search.submit(item.id, item.enc_title, item.duration, item.playlist, i, x, 0, item.duration);
}); });
*/
} }
}); });
}, },