Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

starred playlist support #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,12 @@ Session.prototype.getPlaylistcontainer = function getPlaylistcontainer() {
return new sp.PlaylistContainer(b.session_playlistcontainer(this._sp_session));
};

/**
* get the starred playlist for the current session
*/
Session.prototype.getStarred = function getStarred() {
return new sp.Playlist(b.session_starred_create(this._sp_session));
}

// exports this Class
module.exports = Session;
30 changes: 27 additions & 3 deletions src/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,40 @@ static Handle<Value> Session_PlaylistContainer(const Arguments& args) {
ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);

sp_playlistcontainer* spplaylistcontainer = sp_session_playlistcontainer(session->pointer);

ObjectHandle<sp_playlistcontainer>* playlistcontainer = new ObjectHandle<sp_playlistcontainer>("sp_playlistcontainer");
playlistcontainer->pointer = spplaylistcontainer;

// actually call sp_playlistcontainer_add_callbacks
sp_error error = sp_playlistcontainer_add_callbacks(spplaylistcontainer, &nsp_playlistcontainer_callbacks, playlistcontainer);
NSP_THROW_IF_ERROR(error);

return scope.Close(playlistcontainer->object);
}

static Handle<Value> Session_Starred_Create(const Arguments& args) {
HandleScope scope;

assert(args.Length() == 1);
assert(args[0]->IsObject());

ObjectHandle<sp_session>* session = ObjectHandle<sp_session>::Unwrap(args[0]);

// actually call sp_session_starred_create
sp_playlist* spplaylist = sp_session_starred_create(session->pointer);

// Set the playlist in RAM
sp_playlist_set_in_ram(session->pointer, spplaylist, true);

ObjectHandle<sp_playlist>* playlist = new ObjectHandle<sp_playlist>("sp_playlist");
playlist->pointer = spplaylist;

sp_error error = sp_playlist_add_callbacks(spplaylist, &nsp_playlist_callbacks, playlist);
NSP_THROW_IF_ERROR(error);

return scope.Close(playlist->object);
}

void nsp::init_session(Handle<Object> target) {
NODE_SET_METHOD(target, "session_config", Session_Config);
NODE_SET_METHOD(target, "session_create", Session_Create);
Expand All @@ -493,4 +516,5 @@ void nsp::init_session(Handle<Object> target) {
NODE_SET_METHOD(target, "session_logout", Session_Logout);
NODE_SET_METHOD(target, "session_process_events", Session_Process_Events);
NODE_SET_METHOD(target, "session_playlistcontainer", Session_PlaylistContainer);
NODE_SET_METHOD(target, "session_starred_create", Session_Starred_Create);
}
26 changes: 24 additions & 2 deletions test/test-080-playlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ exports.playlist = {
test.done();
}, 'getting playlist from url should not throw');
},
'get playlist from Starred': function(test) {
var playlist;
test.doesNotThrow(function() {
playlist = session.getStarred()
test.ok(playlist instanceof sp.Playlist, 'We should get a playlist object');
test.done();
}, 'getting playlist from starred should not throw');
},
'attributes are mapped': function(test) {
var playlist = sp.Playlist.getFromUrl('spotify:user:flobyiv:playlist:5ZMnMnJWGXZ9qm4gacHpQF');
playlist.whenReady(function() {
Expand All @@ -30,7 +38,7 @@ exports.playlist = {
}, "getting attributes should not throw");
});
},
'get tracks': function(test) {
'get tracks from URI': function(test) {
var playlist = sp.Playlist.getFromUrl('spotify:user:flobyiv:playlist:5ZMnMnJWGXZ9qm4gacHpQF');
playlist.whenReady(function() {
playlist.getTracks(function(tracks) {
Expand All @@ -43,5 +51,19 @@ exports.playlist = {
test.done();
});
});
}.timed(10000)
}.timed(10000),
'get tracks from Starred': function(test) {
var playlist = session.getStarred();
playlist.whenReady(function() {
playlist.getTracks(function(tracks) {
test.ok(Array.isArray(tracks), 'tracks should be an array');
test.ok(tracks.length > 0, 'There should be tracks in the array');
test.equal(tracks.map(function(e) {return e instanceof sp.Track;}).indexOf(false), -1, 'It should only contain tracks');
test.equal(tracks.reduce(function(prev, current) {
return prev && current.isReady();
}, true), true, 'All tracks should be loaded');
test.done();
});
});
}.timed(40000)
};