stations.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. const cache = require('./cache');
  3. const db = require('./db');
  4. const io = require('./io');
  5. const utils = require('./utils');
  6. const notifications = require('./notifications');
  7. const async = require('async');
  8. let skipTimeout = null;
  9. //TEMP
  10. cache.sub('station.pause', (stationId) => {
  11. clearTimeout(skipTimeout);
  12. skipTimeout = null;
  13. });
  14. cache.sub('station.resume', (stationId) => {
  15. module.exports.initializeAndReturnStation(stationId, (err, station) => {})
  16. });
  17. module.exports = {
  18. init: function(cb) {
  19. let _this = this;
  20. db.models.station.find({}, (err, stations) => {
  21. if (!err) {
  22. stations.forEach((station) => {
  23. console.info("Initializing Station: " + station._id);
  24. _this.initializeAndReturnStation(station._id, (err, station) => {
  25. //TODO Emit to homepage and admin station list
  26. });
  27. });
  28. cb();
  29. }
  30. });
  31. },
  32. calculateSongForStation: (station, cb) => {
  33. let songList = [];
  34. async.waterfall([
  35. (next) => {
  36. let genresDone = [];
  37. station.genres.forEach((genre) => {
  38. db.models.song.find({genres: genre}, (err, songs) => {
  39. if (!err) {
  40. songs.forEach((song) => {
  41. if (songList.indexOf(song._id) === -1) songList.push(song._id);
  42. });
  43. }
  44. genresDone.push(genre);
  45. if (genresDone.length === station.genres.length) {
  46. next();
  47. }
  48. });
  49. });
  50. },
  51. (next) => {
  52. let playlist = [];
  53. songList.forEach(function(songId) {
  54. if(station.playlist.indexOf(songId) === -1) playlist.push(songId);
  55. });
  56. station.playlist.filter((songId) => {
  57. if (songList.indexOf(songId) !== -1) playlist.push(songId);
  58. });
  59. db.models.station.update({_id: station._id}, {$set: {playlist: playlist}}, (err, result) => {
  60. next(err, playlist);
  61. });
  62. }
  63. ], (err, newPlaylist) => {
  64. cb(err, newPlaylist);
  65. });
  66. },
  67. initializeAndReturnStation: function(stationId, cb) {
  68. let _this = this;
  69. async.waterfall([
  70. // first check the cache for the station
  71. (next) => cache.hget('stations', stationId, next),
  72. // if the cached version exist
  73. (station, next) => {
  74. if (station) return next(true, station);
  75. db.models.station.findOne({ _id: stationId }, next);
  76. },
  77. // if the station exists in the DB, add it to the cache
  78. (station, next) => {
  79. if (!station) return cb('Station by that id does not exist');
  80. station = cache.schemas.station(station);
  81. cache.hset('stations', station._id, station, (err) => next(err, station));
  82. }
  83. ], (err, station) => {
  84. if (err && err !== true) return cb(err);
  85. // get notified when the next song for this station should play, so that we can notify our sockets
  86. /*let notification = notifications.subscribe(`stations.nextSong?id=${station._id}`, () => {*/
  87. function skipSongTemp() {
  88. // get the station from the cache
  89. //TODO Recalculate songs if the last song of the station playlist is getting played
  90. cache.hget('stations', station._id, (err, station) => {
  91. if (station) {
  92. // notify all the sockets on this station to go to the next song
  93. async.waterfall([
  94. (next) => {
  95. if (station.currentSongIndex < station.playlist.length - 1) {
  96. station.currentSongIndex++;
  97. db.models.song.findOne({ _id: station.playlist[station.currentSongIndex] }, (err, song) => {
  98. if (!err) {
  99. station.currentSong = {
  100. _id: song._id,
  101. title: song.title,
  102. artists: song.artists,
  103. duration: song.duration,
  104. likes: song.likes,
  105. dislikes: song.dislikes,
  106. skipDuration: song.skipDuration,
  107. thumbnail: song.thumbnail
  108. };
  109. station.startedAt = Date.now();
  110. station.timePaused = 0;
  111. next(null, station);
  112. }
  113. });
  114. } else {
  115. station.currentSongIndex = 0;
  116. _this.calculateSongForStation(station, (err, newPlaylist) => {
  117. console.log('New playlist: ', newPlaylist);
  118. if (!err) {
  119. db.models.song.findOne({ _id: newPlaylist[0] }, (err, song) => {
  120. if (song) {
  121. station.currentSong = {
  122. _id: song._id,
  123. title: song.title,
  124. artists: song.artists,
  125. duration: song.duration,
  126. likes: song.likes,
  127. dislikes: song.dislikes,
  128. skipDuration: song.skipDuration,
  129. thumbnail: song.thumbnail
  130. };
  131. station.startedAt = Date.now();
  132. station.timePaused = 0;
  133. station.playlist = newPlaylist;
  134. next(null, station);
  135. }
  136. });
  137. }
  138. })
  139. }
  140. },
  141. (station, next) => {
  142. cache.hset('stations', station._id, station, (err) => next(err, station));
  143. //TODO Also save to DB
  144. },
  145. ], (err, station) => {
  146. io.io.to(`station.${stationId}`).emit("event:songs.next", {
  147. currentSong: station.currentSong,
  148. startedAt: station.startedAt,
  149. paused: station.paused,
  150. timePaused: 0
  151. });
  152. // schedule a notification to be dispatched when the next song ends
  153. notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
  154. skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
  155. });
  156. }
  157. // the station doesn't exist anymore, unsubscribe from it
  158. else {
  159. notifications.remove(notification);
  160. }
  161. });
  162. }//, true);
  163. if (!station.paused) {
  164. if (!station.startedAt) {
  165. station.startedAt = Date.now();
  166. station.timePaused = 0;
  167. cache.hset('stations', stationId, station);
  168. }
  169. let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
  170. console.log(timeLeft, 1234);
  171. console.log((station.currentSong.duration * 1000), Date.now(), station.startedAt, station.timePaused);
  172. //setTimeout(skipSongTemp, station.currentSong.duration * 1000);
  173. if (skipTimeout === null) {
  174. skipTimeout = setTimeout(skipSongTemp, timeLeft);
  175. }
  176. if (station.currentSong.duration * 1000 < timeLeft) {
  177. clearTimeout(skipTimeout);
  178. skipSongTemp();
  179. }
  180. notifications.schedule(`stations.nextSong?id=${station.id}`, ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused)));
  181. }
  182. return cb(null, station);
  183. });
  184. }
  185. };