|
@@ -29,6 +29,22 @@ cache.sub('playlist.delete', res => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
+cache.sub('playlist.moveSongToTop', res => {
|
|
|
+ utils.socketsFromUser(res.userId, (sockets) => {
|
|
|
+ sockets.forEach((socket) => {
|
|
|
+ socket.emit('event:playlist.moveSongToTop', {playlistId: res.playlistId, songId: res.songId});
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
+cache.sub('playlist.moveSongToBottom', res => {
|
|
|
+ utils.socketsFromUser(res.userId, (sockets) => {
|
|
|
+ sockets.forEach((socket) => {
|
|
|
+ socket.emit('event:playlist.moveSongToBottom', {playlistId: res.playlistId, songId: res.songId});
|
|
|
+ });
|
|
|
+ });
|
|
|
+});
|
|
|
+
|
|
|
cache.sub('playlist.addSong', res => {
|
|
|
utils.socketsFromUser(res.userId, (sockets) => {
|
|
|
sockets.forEach((socket) => {
|
|
@@ -230,7 +246,82 @@ let lib = {
|
|
|
return cb({ status: 'success', message: 'Playlist has been successfully updated' });
|
|
|
})
|
|
|
});
|
|
|
- }),/*
|
|
|
+ }),
|
|
|
+
|
|
|
+ moveSongToTop: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
|
|
|
+ playlists.getPlaylist(playlistId, (err, playlist) => {
|
|
|
+ if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
|
|
|
+ let found = false;
|
|
|
+ let foundSong;
|
|
|
+ playlist.songs.forEach((song) => {
|
|
|
+ if (song._id === songId) {
|
|
|
+ foundSong = song;
|
|
|
+ found = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (found) {
|
|
|
+ db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
|
|
|
+ console.log(err);
|
|
|
+ if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
|
|
|
+ db.models.playlist.update({_id: playlistId}, {
|
|
|
+ $push: {
|
|
|
+ songs: {
|
|
|
+ $each: [foundSong],
|
|
|
+ $position: 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, (err) => {
|
|
|
+ console.log(err);
|
|
|
+ if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
|
|
|
+ playlists.updatePlaylist(playlistId, (err) => {
|
|
|
+ if (err) return cb({ status: 'failure', message: err});
|
|
|
+ cache.pub('playlist.moveSongToTop', {playlistId, songId, userId: userId});
|
|
|
+ return cb({ status: 'success', message: 'Playlist has been successfully updated' });
|
|
|
+ })
|
|
|
+ });
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ return cb({status: 'failure', message: 'Song not found.'});
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }),
|
|
|
+
|
|
|
+ moveSongToBottom: hooks.loginRequired((session, playlistId, songId, cb, userId) => {
|
|
|
+ playlists.getPlaylist(playlistId, (err, playlist) => {
|
|
|
+ if (err || !playlist || playlist.createdBy !== userId) return cb({ status: 'failure', message: 'Something went wrong when getting the playlist.'});
|
|
|
+ let found = false;
|
|
|
+ let foundSong;
|
|
|
+ playlist.songs.forEach((song) => {
|
|
|
+ if (song._id === songId) {
|
|
|
+ foundSong = song;
|
|
|
+ found = true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (found) {
|
|
|
+ db.models.playlist.update({_id: playlistId}, {$pull: {songs: {_id: songId}}}, (err) => {
|
|
|
+ console.log(err);
|
|
|
+ if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
|
|
|
+ db.models.playlist.update({_id: playlistId}, {
|
|
|
+ $push: { songs: foundSong }
|
|
|
+ }, (err) => {
|
|
|
+ console.log(err);
|
|
|
+ if (err) return cb({status: 'failure', message: 'Something went wrong when moving the song.'});
|
|
|
+ playlists.updatePlaylist(playlistId, (err) => {
|
|
|
+ if (err) return cb({ status: 'failure', message: err});
|
|
|
+ cache.pub('playlist.moveSongToBottom', {playlistId, songId, userId: userId});
|
|
|
+ return cb({ status: 'success', message: 'Playlist has been successfully updated' });
|
|
|
+ })
|
|
|
+ });
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ return cb({status: 'failure', message: 'Song not found.'});
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }),
|
|
|
+
|
|
|
+ /*
|
|
|
|
|
|
promoteSong: hooks.loginRequired((session, playlistId, fromIndex, cb, userId) => {
|
|
|
db.models.playlist.findOne({ _id: playlistId }, (err, playlist) => {
|