Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Fix Playlist Media management (#109)
Browse files Browse the repository at this point in the history
* Fix Playlist Media management

Change mid.length > 0 check to mid.length === 0 to fix playlistMoveMedia
Create function removeSongFromPlaylist
Don’t accept empty arrays in removeSongFromPlaylist
Allow non empty arrays in playlistMoveMedia
Change method of REST request from GET to POST in addSongToPlaylist
Change song ID of type Number to song data of type Object or Object
array
Allow multiple songs added at once if sdata is an Array
  • Loading branch information
au2001 authored and thedark1337 committed Jul 29, 2016
1 parent eac6abe commit dd1b9d5
Showing 1 changed file with 109 additions and 7 deletions.
116 changes: 109 additions & 7 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1830,11 +1830,11 @@ PlugAPI.prototype.activatePlaylist = function(pid, callback) {
/**
* Add a song to a playlist
* @param {Number} pid Playlist ID
* @param {Number} sid Song ID
* @param {Object|Object[]} sdata Songs data
* @param {RESTCallback} [callback] Callback function
* @returns {Boolean} If the REST request got queued
*/
PlugAPI.prototype.addSongToPlaylist = function(pid, sid, callback) {
PlugAPI.prototype.addSongToPlaylist = function(pid, sdata, callback) {
if (!room.getRoomMeta().slug) {
if (typeof callback === 'function') {
return callback(new Error('Not in a room'), null);
Expand All @@ -1849,13 +1849,61 @@ PlugAPI.prototype.addSongToPlaylist = function(pid, sid, callback) {
return;
}

if (!sid) {
if (!sdata || (Array.isArray(sdata) && sdata.length === 0)) {
if (typeof callback === 'function') {
return callback(new Error('Missing song ID'), null);
return callback(new Error('Missing song data'), null);
}
return;
}

if (!Array.isArray(sdata))
sdata = [sdata];

for (var i in sdata) {
if (!sdata.hasOwnProperty(i)) continue;
if (sdata[i].id == undefined) // On plug.dj, this ID is always 0, so it is not important apparently
sdata[i].id = 0;
if (typeof sdata[i].cid === 'number') // Known to cause "Error 400 - Bad Gateway" when adding SoundClound songs if the cid is a Number and not a String
sdata[i].cid = String(sdata[i].cid);

if (sdata[i].format == undefined) {
if (typeof callback === 'function') {
return callback(new Error('Missing song format'), null);
}
return;
}
if (sdata[i].cid == undefined) {
if (typeof callback === 'function') {
return callback(new Error('Missing song ID'), null);
}
return;
}
if (sdata[i].author == undefined) {
if (typeof callback === 'function') {
return callback(new Error('Missing song author'), null);
}
return;
}
if (sdata[i].title == undefined) {
if (typeof callback === 'function') {
return callback(new Error('Missing song title'), null);
}
return;
}
if (sdata[i].image == undefined) {
if (typeof callback === 'function') {
return callback(new Error('Missing song image'), null);
}
return;
}
if (sdata[i].duration == undefined) {
if (typeof callback === 'function') {
return callback(new Error('Missing song duration'), null);
}
return;
}
}

that.getPlaylist(pid, function(playlist) {
if (playlist == null) {
if (typeof callback === 'function') {
Expand All @@ -1872,13 +1920,67 @@ PlugAPI.prototype.addSongToPlaylist = function(pid, sid, callback) {
}
};

queueREST('GET', endpoints.PLAYLIST + '/' + pid + '/media/insert', {
media: sid,
queueREST('POST', endpoints.PLAYLIST + '/' + pid + '/media/insert', {
media: sdata,
append: true
}, callbackWrapper);
});
};

/**
* Removes a media from a playlist
* @param {Number} pid Playlist ID
* @param {Number|Number[]} mid Media ID(s)
* @param {RESTCallback} [callback] Callback function
* @returns {Boolean} If the REST request got queued
*/
PlugAPI.prototype.removeSongFromPlaylist = function(pid, mid, callback) {
if (!room.getRoomMeta().slug) {
if (typeof callback === 'function') {
return callback(new Error('Not in a room'), null);
}
return;
}

if (!pid) {
if (typeof callback === 'function') {
return callback(new Error('Missing playlist ID'), null);
}
return;
}

if (!mid || (Array.isArray(mid) && mid.length === 0)) {
if (typeof callback === 'function') {
return callback(new Error('Missing song ID'), null);
}
return;
}

that.getPlaylist(pid, function(playlist) {
if (playlist == null) {
if (typeof callback === 'function') {
return callback(new Error('Playlist not found'), null);
}
return;
}

var callbackWrapper = function(err, data) {
if (!err)
playlist.count++;
if (typeof callback === 'function') {
return callback(err, data);
}
};

if (!Array.isArray(mid))
mid = [mid];

queueREST('POST', endpoints.PLAYLIST + '/' + pid + '/media/delete', {
ids: mid
}, callbackWrapper);
});
};

/**
* Create a new playlist
* @param {String} name Name of new playlist
Expand Down Expand Up @@ -2030,7 +2132,7 @@ PlugAPI.prototype.getPlaylistMedias = function(pid, callback) {
* @returns {Boolean} If the REST request got queued
*/
PlugAPI.prototype.playlistMoveMedia = function(pid, mid, beforeMid, callback) {
if (!room.getRoomMeta().slug || !pid || !mid || (!Array.isArray(mid) || (Array.isArray(mid) && mid.length > 0)) || !beforeMid) return false;
if (!room.getRoomMeta().slug || !pid || !mid || (Array.isArray(mid) && mid.length === 0) || !beforeMid) return false;

this.getPlaylist(pid, function(playlist) {
if (playlist == null) {
Expand Down

0 comments on commit dd1b9d5

Please sign in to comment.