diff --git a/lib/Session.js b/lib/Session.js index 6ac4062..16d3e89 100644 --- a/lib/Session.js +++ b/lib/Session.js @@ -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; diff --git a/src/session.cc b/src/session.cc index 3bb7da1..c0eaa17 100644 --- a/src/session.cc +++ b/src/session.cc @@ -474,17 +474,40 @@ static Handle Session_PlaylistContainer(const Arguments& args) { ObjectHandle* session = ObjectHandle::Unwrap(args[0]); sp_playlistcontainer* spplaylistcontainer = sp_session_playlistcontainer(session->pointer); - + ObjectHandle* playlistcontainer = new ObjectHandle("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 Session_Starred_Create(const Arguments& args) { + HandleScope scope; + + assert(args.Length() == 1); + assert(args[0]->IsObject()); + + ObjectHandle* session = ObjectHandle::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* playlist = new ObjectHandle("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 target) { NODE_SET_METHOD(target, "session_config", Session_Config); NODE_SET_METHOD(target, "session_create", Session_Create); @@ -493,4 +516,5 @@ void nsp::init_session(Handle 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); } diff --git a/test/test-080-playlist.js b/test/test-080-playlist.js index fc3c077..2cb4118 100644 --- a/test/test-080-playlist.js +++ b/test/test-080-playlist.js @@ -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() { @@ -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) { @@ -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) };