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,8 +59,8 @@ 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([
{ {
@@ -77,10 +77,11 @@ function addFromOtherList(arr, guid, offline, socket) {
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) {
db.collection(channel + "_settings").update({id: "config"}, {$set: {startTime: Functions.get_time()}}, function(e,d) {
if(to_set_np) { if(to_set_np) {
var to_change = { var to_change = {
_id: channel, _id: channel,
count: docs.length, count: res.nInserted != undefined ? res.nInserted : res.insertedCount,
frontpage: true, frontpage: true,
accessed: Functions.get_time(), accessed: Functions.get_time(),
} }
@@ -104,7 +105,12 @@ function addFromOtherList(arr, guid, offline, socket) {
} }
}); });
}); });
}) });
});
} 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,8 +166,8 @@ 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'));
@@ -171,13 +177,19 @@ function addPlaylist(arr, guid, socket) {
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 to_set_np = now_playing;
var bulk = dbo.collection(channel).initializeUnorderedBulkOp({useLegacyOps: true}); var bulk = dbo.collection(channel).initializeUnorderedBulkOp({useLegacyOps: true});
for(var i = 0; i < arr.songs.length; i++) { for(var i = 0; i < arr.songs.length; i++) {
var this_element = arr.songs[i]; var this_element = arr.songs[i];
if(!this_element.hasOwnProperty("duration") || !this_element.hasOwnProperty("id") || !this_element.hasOwnProperty("title")) {
continue;
}
this_element.added = time; this_element.added = time;
this_element.now_playing = now_playing; this_element.now_playing = now_playing;
this_element.votes = 0; this_element.votes = 0;
this_element.guids = []; this_element.guids = [];
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.start = parseInt(this_element.start);
this_element.end = parseInt(this_element.end); this_element.end = parseInt(this_element.end);
this_element.duration = parseInt(this_element.duration); this_element.duration = parseInt(this_element.duration);
@@ -190,9 +202,39 @@ function addPlaylist(arr, guid, socket) {
bulk.insert(this_element); bulk.insert(this_element);
} }
bulk.execute(function(err, results) { bulk.execute(function(err, results) {
console.log(err, results); db.collection(channel + "_settings").update({id: "config"}, {$set: {startTime: Functions.get_time()}}, function(e,d) {
if(to_set_np) {
var to_change = {
_id: channel,
count: results.nInserted,
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);
}); });
*/
} }
}); });
}, },