From 9a5984ff242376838937f864fdbfcc4530d50d19 Mon Sep 17 00:00:00 2001 From: DerpgonCz Date: Tue, 28 Jun 2016 16:29:18 +0200 Subject: [PATCH] Fixed DubTrack.fm importing No longer duplicates or removes all songs Fixed a bug with wrong number of songs in an active playlist on login Nicer message when importing a DT playlist Closes #9 and fixes #62 --- socketserver/playlist.js | 2 +- socketserver/socketserver.js | 68 ++++++++++++++++------------------ webserver/public/lib/js/app.js | 2 +- 3 files changed, 33 insertions(+), 39 deletions(-) diff --git a/socketserver/playlist.js b/socketserver/playlist.js index 07ee17b..f0c7c73 100644 --- a/socketserver/playlist.js +++ b/socketserver/playlist.js @@ -109,7 +109,7 @@ Playlist.prototype.getExpanded = function(callback){ if(videoData[id]) out.push(videoData[id]); else { - that.data.content.splice(i, 1); + that.data.content.splice(i--, 1); changed = true; } } diff --git a/socketserver/socketserver.js b/socketserver/socketserver.js index d45be65..d737e0c 100644 --- a/socketserver/socketserver.js +++ b/socketserver/socketserver.js @@ -7,6 +7,7 @@ var request = require('request'); var util = require('util'); var extend = require('extend'); var updateNotifier = require('update-notifier'); +var _ = require('underscore'); //Files var config = require('../serverconfig'); @@ -1363,11 +1364,12 @@ var SocketServer = function(server){ if (socket.user.activepl){ socket.user.playlistCache[ socket.user.activepl ].getExpanded(function(err, plData){ tempUser.playlists[ socket.user.activepl ].content = YT.removeThumbs(plData); - returnObj.data.user = tempUser, + tempUser.playlists[ socket.user.activepl ].num = plData.length; + returnObj.data.user = tempUser; socket.sendJSON(returnObj); }); }else{ - returnObj.data.user = tempUser, + returnObj.data.user = tempUser; socket.sendJSON(returnObj); } @@ -1940,7 +1942,7 @@ var SocketServer = function(server){ }; socket.sendJSON(returnObj); }); - } else if (data.type == 'playlistAddSong'){ + } else if(data.type == 'playlistAddSong') { if (!data.data.cid){ returnObj.data = { error: 'PropsMissing' @@ -1948,10 +1950,16 @@ var SocketServer = function(server){ socket.sendJSON(returnObj); break; } - if (!data.data.pos){ + + //Set correct playlist position + if (data.data.pos != 'top' && data.data.pos != 'bottom'){ data.data.pos = 'top'; } + + //If an array of CIDs was supplied if (Array.isArray(data.data.cid)) { + + //Check if any CIDs were supplied if (data.data.cid.length == 0) { returnObj.data = { error: 'emptyCidArray' @@ -1960,48 +1968,34 @@ var SocketServer = function(server){ break; } - var songsAdded = 0; - var videos = []; - + //Remove all duplicate CIds data.data.cid.filter(function(e, i, a){ - return a.indexOf(e) != i; + return a.indexOf(e) != i && pl.content.indexOf(e) == -1; }); + //Add data to the playlist for (var i = 0, len = data.data.cid.length; i < len; i++) { var cid = data.data.cid[i]; - if (pl.data.content.indexOf(cid) == -1) { - pl.addSong(cid, data.data.pos, function(err, vidData, pos){ - if (!err){ - for (var i in vidData) { - videos.push(vidData[i]); - } - } else { - console.log(err); - } + if(data.data.pos == 'top') + pl.data.content.unshift(cid); + else + pl.data.content.push(cid); + } - if (++songsAdded == data.data.cid.length) { - returnObj.data = { - video: videos, - pos: data.data.pos, - plid: pl.id - }; + //Save playlist + pl.save(); - socket.sendJSON(returnObj); - } - }); - } else { - if (++songsAdded == data.data.cid.length) { - returnObj.data = { - video: videos, - pos: data.data.pos, - plid: pl.id - }; + //Fetch all song data and return them to client + YT.getVideo(data.data.cid, function(err, videos) { + returnObj.data = { + video: _.values(videos), + pos: data.data.pos, + plid: pl.id + }; - socket.sendJSON(returnObj); - } - } - } + socket.sendJSON(returnObj); + }); } else { if (pl.data.content.indexOf(data.data.cid) > -1){ returnObj.data = { diff --git a/webserver/public/lib/js/app.js b/webserver/public/lib/js/app.js index 671fe3f..ff21cbc 100644 --- a/webserver/public/lib/js/app.js +++ b/webserver/public/lib/js/app.js @@ -6683,7 +6683,7 @@ MP.api.playlist.addSong(pldata.id, songs, function(err, data){ MP.makeAlertModal({ - content: 'Playlist imported successfully.', + content: 'Playlist ' + plName + ' imported successfully (imported ' + data.video.length + ').', }); }); } else {