stations.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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.playlist.length > 0) {
  96. function func() {
  97. if (station.currentSongIndex < station.playlist.length - 1) {
  98. station.currentSongIndex++;
  99. db.models.song.findOne({_id: station.playlist[station.currentSongIndex]}, (err, song) => {
  100. if (!err && song) {
  101. station.currentSong = {
  102. _id: song._id,
  103. title: song.title,
  104. artists: song.artists,
  105. duration: song.duration,
  106. likes: song.likes,
  107. dislikes: song.dislikes,
  108. skipDuration: song.skipDuration,
  109. thumbnail: song.thumbnail
  110. };
  111. station.startedAt = Date.now();
  112. station.timePaused = 0;
  113. next(null, station);
  114. } else {
  115. station.currentSongIndex++;
  116. func();
  117. }
  118. });
  119. } else {
  120. station.currentSongIndex = 0;
  121. _this.calculateSongForStation(station, (err, newPlaylist) => {
  122. console.log('New playlist: ', newPlaylist);
  123. if (!err) {
  124. db.models.song.findOne({_id: newPlaylist[0]}, (err, song) => {
  125. if (song) {
  126. station.currentSong = {
  127. _id: song._id,
  128. title: song.title,
  129. artists: song.artists,
  130. duration: song.duration,
  131. likes: song.likes,
  132. dislikes: song.dislikes,
  133. skipDuration: song.skipDuration,
  134. thumbnail: song.thumbnail
  135. };
  136. station.playlist = newPlaylist;
  137. } else {
  138. station.currentSong = _this.defaultSong;
  139. }
  140. station.startedAt = Date.now();
  141. station.timePaused = 0;
  142. next(null, station);
  143. });
  144. } else {
  145. station.currentSong = _this.defaultSong;
  146. station.startedAt = Date.now();
  147. station.timePaused = 0;
  148. next(null, station);
  149. }
  150. })
  151. }
  152. }
  153. func();
  154. } else {
  155. _this.calculateSongForStation(station, (err, playlist) => {
  156. if (!err && playlist.length === 0) {
  157. station.currentSongIndex = 0;
  158. station.currentSong = _this.defaultSong;
  159. station.startedAt = Date.now();
  160. station.timePaused = 0;
  161. next(null, station);
  162. } else {
  163. station.currentSongIndex = 0;
  164. station.currentSong = playlist[0];
  165. station.startedAt = Date.now();
  166. station.timePaused = 0;
  167. station.playlist = playlist;
  168. next(null, station);
  169. }
  170. });
  171. }
  172. },
  173. (station, next) => {
  174. cache.hset('stations', station._id, station, (err) => next(err, station));
  175. //TODO Also save to DB
  176. },
  177. ], (err, station) => {
  178. io.io.to(`station.${stationId}`).emit("event:songs.next", {
  179. currentSong: station.currentSong,
  180. startedAt: station.startedAt,
  181. paused: station.paused,
  182. timePaused: 0
  183. });
  184. console.log(io.io.to(`station.${stationId}`).sockets);
  185. utils.socketsJoinSongRoom(io.io.to(`station.${stationId}`).sockets, `song.${station.currentSong._id}`);
  186. // schedule a notification to be dispatched when the next song ends
  187. notifications.schedule(`stations.nextSong?id=${station.id}`, station.currentSong.duration * 1000);
  188. skipTimeout = setTimeout(skipSongTemp, station.currentSong.duration * 1000);
  189. });
  190. }
  191. // the station doesn't exist anymore, unsubscribe from it
  192. else {
  193. notifications.remove(notification);
  194. }
  195. });
  196. }//, true);
  197. if (!station.paused) {
  198. if (!station.startedAt) {
  199. station.startedAt = Date.now();
  200. station.timePaused = 0;
  201. cache.hset('stations', stationId, station);
  202. }
  203. let timeLeft = ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused));
  204. console.log(timeLeft, 1234);
  205. console.log((station.currentSong.duration * 1000), Date.now(), station.startedAt, station.timePaused);
  206. //setTimeout(skipSongTemp, station.currentSong.duration * 1000);
  207. if (skipTimeout === null) {
  208. skipTimeout = setTimeout(skipSongTemp, timeLeft);
  209. }
  210. if (station.currentSong.duration * 1000 < timeLeft) {
  211. clearTimeout(skipTimeout);
  212. skipSongTemp();
  213. }
  214. notifications.schedule(`stations.nextSong?id=${station.id}`, ((station.currentSong.duration * 1000) - (Date.now() - station.startedAt - station.timePaused)));
  215. }
  216. return cb(null, station);
  217. });
  218. },
  219. defaultSong: {
  220. _id: '60ItHLz5WEA',
  221. title: 'Faded',
  222. artists: ['Alan Walker'],
  223. duration: 212,
  224. skipDuration: 0,
  225. likes: 0,
  226. dislikes: 0,
  227. thumbnail: 'https://i.scdn.co/image/2ddde58427f632037093857ebb71a67ddbdec34b'
  228. }
  229. };