|
@@ -15,7 +15,15 @@ cache.sub('station.pause', (stationId) => {
|
|
});
|
|
});
|
|
|
|
|
|
cache.sub('station.resume', (stationId) => {
|
|
cache.sub('station.resume', (stationId) => {
|
|
- module.exports.getStation(stationId, (err, station) => {})
|
|
|
|
|
|
+ module.exports.initializeStation(stationId)
|
|
|
|
+});
|
|
|
|
+
|
|
|
|
+cache.sub('station.queueUpdate', (stationId) => {
|
|
|
|
+ module.exports.getStation(stationId, (err, station) => {
|
|
|
|
+ if (!station.currentSong) {
|
|
|
|
+ module.exports.initializeStation(stationId);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
});
|
|
});
|
|
|
|
|
|
module.exports = {
|
|
module.exports = {
|
|
@@ -27,7 +35,7 @@ module.exports = {
|
|
if (!err) {
|
|
if (!err) {
|
|
stations.forEach((station) => {
|
|
stations.forEach((station) => {
|
|
console.info("Initializing Station: " + station._id);
|
|
console.info("Initializing Station: " + station._id);
|
|
- _this.initializeStation(station);
|
|
|
|
|
|
+ _this.initializeStation(station._id);
|
|
});
|
|
});
|
|
cb();
|
|
cb();
|
|
}
|
|
}
|
|
@@ -35,6 +43,7 @@ module.exports = {
|
|
},
|
|
},
|
|
|
|
|
|
initializeStation: function(stationId, cb) {
|
|
initializeStation: function(stationId, cb) {
|
|
|
|
+ console.log(112233, stationId);
|
|
if (typeof cb !== 'function') cb = ()=>{};
|
|
if (typeof cb !== 'function') cb = ()=>{};
|
|
let _this = this;
|
|
let _this = this;
|
|
_this.getStation(stationId, (err, station) => {
|
|
_this.getStation(stationId, (err, station) => {
|
|
@@ -48,116 +57,137 @@ module.exports = {
|
|
async.waterfall([
|
|
async.waterfall([
|
|
|
|
|
|
(next) => {
|
|
(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) {
|
|
|
|
- let $set = {};
|
|
|
|
|
|
+ if (station.type === "official") {
|
|
|
|
+ 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) {
|
|
|
|
+ let $set = {};
|
|
|
|
|
|
- $set.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
|
|
|
|
- };
|
|
|
|
- $set.startedAt = Date.now();
|
|
|
|
- $set.timePaused = 0;
|
|
|
|
- next(null, $set);
|
|
|
|
- } else {
|
|
|
|
- db.models.station.update({_id: station._id}, {$inc: {currentSongIndex: 1}}, (err) => {
|
|
|
|
- _this.updateStation(station._id, () => {
|
|
|
|
- func();
|
|
|
|
|
|
+ $set.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
|
|
|
|
+ };
|
|
|
|
+ $set.startedAt = Date.now();
|
|
|
|
+ $set.timePaused = 0;
|
|
|
|
+ next(null, $set);
|
|
|
|
+ } else {
|
|
|
|
+ db.models.station.update({_id: station._id}, {$inc: {currentSongIndex: 1}}, (err) => {
|
|
|
|
+ _this.updateStation(station._id, () => {
|
|
|
|
+ func();
|
|
|
|
+ });
|
|
});
|
|
});
|
|
- });
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- db.models.station.update({_id: station._id}, {$set: {currentSongIndex: 0}}, (err) => {
|
|
|
|
- _this.updateStation(station._id, (err, station) => {
|
|
|
|
- console.log(12345678, err, station);
|
|
|
|
- _this.calculateSongForStation(station, (err, newPlaylist) => {
|
|
|
|
- console.log('New playlist: ', newPlaylist);
|
|
|
|
- if (!err) {
|
|
|
|
- songs.getSong(newPlaylist[0], (err, song) => {
|
|
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ db.models.station.update({_id: station._id}, {$set: {currentSongIndex: 0}}, (err) => {
|
|
|
|
+ _this.updateStation(station._id, (err, station) => {
|
|
|
|
+ console.log(12345678, err, station);
|
|
|
|
+ _this.calculateSongForStation(station, (err, newPlaylist) => {
|
|
|
|
+ console.log('New playlist: ', newPlaylist);
|
|
|
|
+ if (!err) {
|
|
|
|
+ songs.getSong(newPlaylist[0], (err, song) => {
|
|
|
|
+ let $set = {};
|
|
|
|
+ if (song) {
|
|
|
|
+ $set.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 {
|
|
|
|
+ $set.currentSong = _this.defaultSong;
|
|
|
|
+ }
|
|
|
|
+ $set.startedAt = Date.now();
|
|
|
|
+ $set.timePaused = 0;
|
|
|
|
+ next(null, $set);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
let $set = {};
|
|
let $set = {};
|
|
- if (song) {
|
|
|
|
- $set.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 {
|
|
|
|
- $set.currentSong = _this.defaultSong;
|
|
|
|
- }
|
|
|
|
|
|
+ $set.currentSong = _this.defaultSong;
|
|
$set.startedAt = Date.now();
|
|
$set.startedAt = Date.now();
|
|
$set.timePaused = 0;
|
|
$set.timePaused = 0;
|
|
next(null, $set);
|
|
next(null, $set);
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- let $set = {};
|
|
|
|
- $set.currentSong = _this.defaultSong;
|
|
|
|
- $set.startedAt = Date.now();
|
|
|
|
- $set.timePaused = 0;
|
|
|
|
- next(null, $set);
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ });
|
|
});
|
|
});
|
|
- });
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ func();
|
|
|
|
+ } else {
|
|
|
|
+ _this.calculateSongForStation(station, (err, playlist) => {
|
|
|
|
+ if (!err && playlist.length === 0) {
|
|
|
|
+ let $set = {};
|
|
|
|
+ $set.currentSongIndex = 0;
|
|
|
|
+ $set.currentSong = _this.defaultSong;
|
|
|
|
+ $set.startedAt = Date.now();
|
|
|
|
+ $set.timePaused = 0;
|
|
|
|
+ next(null, $set);
|
|
|
|
+ } else {
|
|
|
|
+ songs.getSong(playlist[0], (err, song) => {
|
|
|
|
+ let $set = {};
|
|
|
|
+ if (!err) {
|
|
|
|
+ $set.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 {
|
|
|
|
+ $set.currentSong = _this.defaultSong;
|
|
|
|
+ }
|
|
|
|
+ $set.currentSongIndex = 0;
|
|
|
|
+ $set.startedAt = Date.now();
|
|
|
|
+ $set.timePaused = 0;
|
|
|
|
+ next(null, $set);
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ });
|
|
}
|
|
}
|
|
- func();
|
|
|
|
} else {
|
|
} else {
|
|
- _this.calculateSongForStation(station, (err, playlist) => {
|
|
|
|
- if (!err && playlist.length === 0) {
|
|
|
|
|
|
+ if (station.queue.length > 0) {
|
|
|
|
+ db.models.station.update({_id: stationId}, {$pull: {_id: station.queue[0]._id}}, (err) => {
|
|
|
|
+ if (err) return next(err);
|
|
let $set = {};
|
|
let $set = {};
|
|
- $set.currentSongIndex = 0;
|
|
|
|
- $set.currentSong = _this.defaultSong;
|
|
|
|
|
|
+ $set.currentSong = {
|
|
|
|
+ _id: station.queue[0]._id,
|
|
|
|
+ title: station.queue[0].title,
|
|
|
|
+ duration: station.queue[0].duration
|
|
|
|
+ };
|
|
$set.startedAt = Date.now();
|
|
$set.startedAt = Date.now();
|
|
$set.timePaused = 0;
|
|
$set.timePaused = 0;
|
|
next(null, $set);
|
|
next(null, $set);
|
|
- } else {
|
|
|
|
- songs.getSong(playlist[0], (err, song) => {
|
|
|
|
- let $set = {};
|
|
|
|
- if (!err) {
|
|
|
|
- $set.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 {
|
|
|
|
- $set.currentSong = _this.defaultSong;
|
|
|
|
- }
|
|
|
|
- $set.currentSongIndex = 0;
|
|
|
|
- $set.startedAt = Date.now();
|
|
|
|
- $set.timePaused = 0;
|
|
|
|
- next(null, $set);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ func();
|
|
|
|
+ } else {
|
|
|
|
+ next(null, {currentSong: null});
|
|
|
|
+ }
|
|
}
|
|
}
|
|
},
|
|
},
|
|
|
|
|
|
($set, next) => {
|
|
($set, next) => {
|
|
db.models.station.update({_id: station._id}, {$set}, (err) => {
|
|
db.models.station.update({_id: station._id}, {$set}, (err) => {
|
|
_this.updateStation(station._id, (err, station) => {
|
|
_this.updateStation(station._id, (err, station) => {
|
|
- console.log(err, station);
|
|
|
|
next(null, station);
|
|
next(null, station);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
@@ -165,41 +195,51 @@ module.exports = {
|
|
|
|
|
|
|
|
|
|
], (err, station) => {
|
|
], (err, station) => {
|
|
- console.log(err, station);
|
|
|
|
- io.io.to(`station.${station._id}`).emit("event:songs.next", {
|
|
|
|
- currentSong: station.currentSong,
|
|
|
|
- startedAt: station.startedAt,
|
|
|
|
- paused: station.paused,
|
|
|
|
- timePaused: 0
|
|
|
|
- });
|
|
|
|
- utils.socketsJoinSongRoom(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
|
|
|
|
- // schedule a notification to be dispatched when the next song ends
|
|
|
|
- console.log("NEXT SONG!!!");
|
|
|
|
- if (!station.paused) {
|
|
|
|
- notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
|
|
|
|
|
|
+ console.log(err);
|
|
|
|
+ if (!err) {
|
|
|
|
+ io.io.to(`station.${station._id}`).emit("event:songs.next", {
|
|
|
|
+ currentSong: station.currentSong,
|
|
|
|
+ startedAt: station.startedAt,
|
|
|
|
+ paused: station.paused,
|
|
|
|
+ timePaused: 0
|
|
|
|
+ });
|
|
|
|
+ if (station.currentSong !== null && station.currentSong._id !== undefined) {
|
|
|
|
+ utils.socketsJoinSongRoom(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
|
|
|
|
+ console.log("NEXT SONG!!!", station.currentSong);
|
|
|
|
+ if (!station.paused) {
|
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, station.currentSong.duration * 1000);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ console.log("22", !!(station.currentSong));
|
|
|
|
+ utils.socketsLeaveSongRooms(io.io.to(`station.${station._id}`).sockets, `song.${station.currentSong._id}`);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- cb(err, station);
|
|
|
|
});
|
|
});
|
|
}
|
|
}
|
|
// the station doesn't exist anymore, unsubscribe from it
|
|
// the station doesn't exist anymore, unsubscribe from it
|
|
else {
|
|
else {
|
|
|
|
+ console.log(112233445566, "REMOVE NOTIFICATION");
|
|
notifications.remove(notification);
|
|
notifications.remove(notification);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}, true);
|
|
}, true);
|
|
- if (!station.paused) {
|
|
|
|
|
|
+ if (!station.paused ) {
|
|
/*if (!station.startedAt) {
|
|
/*if (!station.startedAt) {
|
|
station.startedAt = Date.now();
|
|
station.startedAt = Date.now();
|
|
station.timePaused = 0;
|
|
station.timePaused = 0;
|
|
cache.hset('stations', stationId, station);
|
|
cache.hset('stations', stationId, station);
|
|
}*/
|
|
}*/
|
|
- let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
|
|
|
|
- if (isNaN(timeLeft)) timeLeft = -1;
|
|
|
|
- if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
|
|
|
|
- console.log("Test");
|
|
|
|
- notifications.schedule(`stations.nextSong?id=${station._id}`, 1);
|
|
|
|
|
|
+ if (station.currentSong) {
|
|
|
|
+ let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
|
|
|
|
+ if (isNaN(timeLeft)) timeLeft = -1;
|
|
|
|
+ if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
|
|
|
|
+ console.log("Test");
|
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, 1);
|
|
|
|
+ } else {
|
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
|
|
|
|
+ }
|
|
} else {
|
|
} else {
|
|
- notifications.schedule(`stations.nextSong?id=${station._id}`, timeLeft);
|
|
|
|
|
|
+ notifications.schedule(`stations.nextSong?id=${station._id}`, 1);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
notifications.unschedule(`stations.nextSong?id${station._id}`);
|
|
notifications.unschedule(`stations.nextSong?id${station._id}`);
|
|
@@ -268,6 +308,7 @@ module.exports = {
|
|
(station, next) => {
|
|
(station, next) => {
|
|
if (station) {
|
|
if (station) {
|
|
station = cache.schemas.station(station);
|
|
station = cache.schemas.station(station);
|
|
|
|
+ console.log(1234321, stationId);
|
|
cache.hset('stations', stationId, station);
|
|
cache.hset('stations', stationId, station);
|
|
next(true, station);
|
|
next(true, station);
|
|
} else next('Station not found.');
|
|
} else next('Station not found.');
|
|
@@ -289,7 +330,7 @@ module.exports = {
|
|
|
|
|
|
(station, next) => {
|
|
(station, next) => {
|
|
if (!station) return next('Station not found.');
|
|
if (!station) return next('Station not found.');
|
|
-
|
|
|
|
|
|
+ console.log(123444321, stationId);
|
|
cache.hset('stations', stationId, station, (err) => {
|
|
cache.hset('stations', stationId, station, (err) => {
|
|
if (err) return next(err);
|
|
if (err) return next(err);
|
|
next(null, station);
|
|
next(null, station);
|