stations.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. notifications.remove(`stations.nextSong?id=${stationId}`);
  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. console.log("NOTIFICATION!!!");
  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);
  220. if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
  221. console.log("Test");
  222. notifications.schedule(`stations.nextSong?id=${station.id}`, 1);
  223. } else {
  224. notifications.schedule(`stations.nextSong?id=${station.id}`, timeLeft);
  225. }
  226. }
  227. return cb(null, station);
  228. });
  229. },
  230. defaultSong: {
  231. _id: '60ItHLz5WEA',
  232. title: 'Faded',
  233. artists: ['Alan Walker'],
  234. duration: 212,
  235. skipDuration: 0,
  236. thumbnail: 'https://i.scdn.co/image/2ddde58427f632037093857ebb71a67ddbdec34b'
  237. }
  238. };