|
@@ -75,6 +75,7 @@ module.exports = {
|
|
|
},
|
|
|
|
|
|
initializeAndReturnStation: function(stationId, cb) {
|
|
|
+ console.log("INITIALIZE", stationId);
|
|
|
let _this = this;
|
|
|
async.waterfall([
|
|
|
|
|
@@ -105,130 +106,135 @@ module.exports = {
|
|
|
//TODO Recalculate songs if the last song of the station playlist is getting played
|
|
|
cache.hget('stations', station._id, (err, station) => {
|
|
|
if (station) {
|
|
|
- // notify all the sockets on this station to go to the next song
|
|
|
- async.waterfall([
|
|
|
+ if (!station.paused) {
|
|
|
+ // notify all the sockets on this station to go to the next song
|
|
|
+ async.waterfall([
|
|
|
|
|
|
- (next) => {
|
|
|
- if (station.playlist.length > 0) {
|
|
|
- function func() {
|
|
|
- if (station.currentSongIndex < station.playlist.length - 1) {
|
|
|
- station.currentSongIndex++;
|
|
|
- songs.getSong(station.playlist[station.currentSongIndex], (err, song) => {
|
|
|
- if (!err) {
|
|
|
- station.currentSong = {
|
|
|
- _id: song._id,
|
|
|
- title: song.title,
|
|
|
- artists: song.artists,
|
|
|
- duration: song.duration,
|
|
|
- likes: song.likes,
|
|
|
- dislikes: song.dislikes,
|
|
|
- skipDuration: song.skipDuration,
|
|
|
- thumbnail: song.thumbnail
|
|
|
- };
|
|
|
- station.startedAt = Date.now();
|
|
|
- station.timePaused = 0;
|
|
|
- next(null, station);
|
|
|
- } else {
|
|
|
- station.currentSongIndex++;
|
|
|
- func();
|
|
|
- }
|
|
|
- });
|
|
|
- } else {
|
|
|
- station.currentSongIndex = 0;
|
|
|
- _this.calculateSongForStation(station, (err, newPlaylist) => {
|
|
|
- console.log('New playlist: ', newPlaylist);
|
|
|
- if (!err) {
|
|
|
- songs.getSong(newPlaylist[0], (err, song) => {
|
|
|
- if (song) {
|
|
|
- station.currentSong = {
|
|
|
- _id: song._id,
|
|
|
- title: song.title,
|
|
|
- artists: song.artists,
|
|
|
- duration: song.duration,
|
|
|
- likes: song.likes,
|
|
|
- dislikes: song.dislikes,
|
|
|
- skipDuration: song.skipDuration,
|
|
|
- thumbnail: song.thumbnail
|
|
|
- };
|
|
|
- station.playlist = newPlaylist;
|
|
|
- } else {
|
|
|
- station.currentSong = _this.defaultSong;
|
|
|
- }
|
|
|
+ (next) => {
|
|
|
+ if (station.playlist.length > 0) {
|
|
|
+ function func() {
|
|
|
+ if (station.currentSongIndex < station.playlist.length - 1) {
|
|
|
+ station.currentSongIndex++;
|
|
|
+ songs.getSong(station.playlist[station.currentSongIndex], (err, song) => {
|
|
|
+ if (!err) {
|
|
|
+ station.currentSong = {
|
|
|
+ _id: song._id,
|
|
|
+ title: song.title,
|
|
|
+ artists: song.artists,
|
|
|
+ duration: song.duration,
|
|
|
+ likes: song.likes,
|
|
|
+ dislikes: song.dislikes,
|
|
|
+ skipDuration: song.skipDuration,
|
|
|
+ thumbnail: song.thumbnail
|
|
|
+ };
|
|
|
station.startedAt = Date.now();
|
|
|
station.timePaused = 0;
|
|
|
next(null, station);
|
|
|
- });
|
|
|
- } else {
|
|
|
- station.currentSong = _this.defaultSong;
|
|
|
- station.startedAt = Date.now();
|
|
|
- station.timePaused = 0;
|
|
|
- next(null, station);
|
|
|
- }
|
|
|
- })
|
|
|
+ } else {
|
|
|
+ station.currentSongIndex++;
|
|
|
+ func();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ station.currentSongIndex = 0;
|
|
|
+ _this.calculateSongForStation(station, (err, newPlaylist) => {
|
|
|
+ console.log('New playlist: ', newPlaylist);
|
|
|
+ if (!err) {
|
|
|
+ songs.getSong(newPlaylist[0], (err, song) => {
|
|
|
+ if (song) {
|
|
|
+ station.currentSong = {
|
|
|
+ _id: song._id,
|
|
|
+ title: song.title,
|
|
|
+ artists: song.artists,
|
|
|
+ duration: song.duration,
|
|
|
+ likes: song.likes,
|
|
|
+ dislikes: song.dislikes,
|
|
|
+ skipDuration: song.skipDuration,
|
|
|
+ thumbnail: song.thumbnail
|
|
|
+ };
|
|
|
+ station.playlist = newPlaylist;
|
|
|
+ } else {
|
|
|
+ station.currentSong = _this.defaultSong;
|
|
|
+ }
|
|
|
+ station.startedAt = Date.now();
|
|
|
+ station.timePaused = 0;
|
|
|
+ next(null, station);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ station.currentSong = _this.defaultSong;
|
|
|
+ station.startedAt = Date.now();
|
|
|
+ station.timePaused = 0;
|
|
|
+ next(null, station);
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
- func();
|
|
|
- } else {
|
|
|
- _this.calculateSongForStation(station, (err, playlist) => {
|
|
|
- if (!err && playlist.length === 0) {
|
|
|
- station.currentSongIndex = 0;
|
|
|
- station.currentSong = _this.defaultSong;
|
|
|
- station.startedAt = Date.now();
|
|
|
- station.timePaused = 0;
|
|
|
- next(null, station);
|
|
|
- } else {
|
|
|
- songs.getSong(playlist[0], (err, song) => {
|
|
|
- if (!err) {
|
|
|
- station.currentSong = {
|
|
|
- _id: song._id,
|
|
|
- title: song.title,
|
|
|
- artists: song.artists,
|
|
|
- duration: song.duration,
|
|
|
- likes: song.likes,
|
|
|
- dislikes: song.dislikes,
|
|
|
- skipDuration: song.skipDuration,
|
|
|
- thumbnail: song.thumbnail
|
|
|
- };
|
|
|
- } else {
|
|
|
- station.currentSong = _this.defaultSong;
|
|
|
- }
|
|
|
+
|
|
|
+ func();
|
|
|
+ } else {
|
|
|
+ _this.calculateSongForStation(station, (err, playlist) => {
|
|
|
+ if (!err && playlist.length === 0) {
|
|
|
station.currentSongIndex = 0;
|
|
|
+ station.currentSong = _this.defaultSong;
|
|
|
station.startedAt = Date.now();
|
|
|
station.timePaused = 0;
|
|
|
- station.playlist = playlist;
|
|
|
next(null, station);
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- },
|
|
|
+ } else {
|
|
|
+ songs.getSong(playlist[0], (err, song) => {
|
|
|
+ if (!err) {
|
|
|
+ station.currentSong = {
|
|
|
+ _id: song._id,
|
|
|
+ title: song.title,
|
|
|
+ artists: song.artists,
|
|
|
+ duration: song.duration,
|
|
|
+ likes: song.likes,
|
|
|
+ dislikes: song.dislikes,
|
|
|
+ skipDuration: song.skipDuration,
|
|
|
+ thumbnail: song.thumbnail
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ station.currentSong = _this.defaultSong;
|
|
|
+ }
|
|
|
+ station.currentSongIndex = 0;
|
|
|
+ station.startedAt = Date.now();
|
|
|
+ station.timePaused = 0;
|
|
|
+ station.playlist = playlist;
|
|
|
+ next(null, station);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ },
|
|
|
|
|
|
- (station, next) => {
|
|
|
- cache.hset('stations', station._id, station, (err) => next(err, station));
|
|
|
- //TODO Also save to DB
|
|
|
- },
|
|
|
+ (station, next) => {
|
|
|
+ cache.hset('stations', station._id, station, (err) => next(err, station));
|
|
|
+ //TODO Also save to DB
|
|
|
+ },
|
|
|
|
|
|
|
|
|
- ], (err, station) => {
|
|
|
- io.io.to(`station.${stationId}`).emit("event:songs.next", {
|
|
|
- currentSong: station.currentSong,
|
|
|
- startedAt: station.startedAt,
|
|
|
- paused: station.paused,
|
|
|
- timePaused: 0
|
|
|
+ ], (err, station) => {
|
|
|
+ io.io.to(`station.${stationId}`).emit("event:songs.next", {
|
|
|
+ currentSong: station.currentSong,
|
|
|
+ startedAt: station.startedAt,
|
|
|
+ paused: station.paused,
|
|
|
+ timePaused: 0
|
|
|
+ });
|
|
|
+ utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
|
|
|
+ // schedule a notification to be dispatched when the next song ends
|
|
|
+ console.log("NEXT SONG!!!");
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
|
|
|
+ //skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
|
|
|
});
|
|
|
- utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
|
|
|
- // schedule a notification to be dispatched when the next song ends
|
|
|
- notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
|
|
|
- //skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
- // the station doesn't exist anymore, unsubscribe from it
|
|
|
+ // the station doesn't exist anymore or is paused, unsubscribe from it
|
|
|
else {
|
|
|
notifications.remove(notification);
|
|
|
}
|
|
|
});
|
|
|
}, true);
|
|
|
|
|
|
+ console.log(station.paused);
|
|
|
if (!station.paused) {
|
|
|
if (!station.startedAt) {
|
|
|
station.startedAt = Date.now();
|
|
@@ -239,10 +245,12 @@ module.exports = {
|
|
|
console.log(timeLeft);
|
|
|
if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
|
|
|
console.log("Test");
|
|
|
- notifications.schedule(`stations.nextSong?id=${station.id}`, 1);
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, 1);
|
|
|
} else {
|
|
|
- notifications.schedule(`stations.nextSong?id=${station.id}`, timeLeft);
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ notifications.unschedule(`stations.nextSong?id${station._id}`);
|
|
|
}
|
|
|
|
|
|
return cb(null, station);
|