Skip to content

Commit

Permalink
Fixed DubTrack.fm importing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
DerpgonCz authored and explodingcamera committed Jul 3, 2016
1 parent 6946222 commit 9a5984f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 39 deletions.
2 changes: 1 addition & 1 deletion socketserver/playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
68 changes: 31 additions & 37 deletions socketserver/socketserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -1940,18 +1942,24 @@ 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'
};
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'
Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion webserver/public/lib/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9a5984f

Please sign in to comment.