stations.js 8.2 KB

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