stations.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  1. import async from "async";
  2. import CoreClass from "../core";
  3. let StationsModule;
  4. let CacheModule;
  5. let DBModule;
  6. let UtilsModule;
  7. let WSModule;
  8. let SongsModule;
  9. let PlaylistsModule;
  10. let NotificationsModule;
  11. class _StationsModule extends CoreClass {
  12. // eslint-disable-next-line require-jsdoc
  13. constructor() {
  14. super("stations");
  15. StationsModule = this;
  16. }
  17. /**
  18. * Initialises the stations module
  19. *
  20. * @returns {Promise} - returns promise (reject, resolve)
  21. */
  22. async initialize() {
  23. CacheModule = this.moduleManager.modules.cache;
  24. DBModule = this.moduleManager.modules.db;
  25. UtilsModule = this.moduleManager.modules.utils;
  26. WSModule = this.moduleManager.modules.ws;
  27. SongsModule = this.moduleManager.modules.songs;
  28. PlaylistsModule = this.moduleManager.modules.playlists;
  29. NotificationsModule = this.moduleManager.modules.notifications;
  30. this.userList = {};
  31. this.usersPerStation = {};
  32. this.usersPerStationCount = {};
  33. // TEMP
  34. CacheModule.runJob("SUB", {
  35. channel: "station.pause",
  36. cb: async stationId => {
  37. NotificationsModule.runJob("REMOVE", {
  38. subscription: `stations.nextSong?id=${stationId}`
  39. }).then();
  40. }
  41. });
  42. CacheModule.runJob("SUB", {
  43. channel: "station.resume",
  44. cb: async stationId => {
  45. StationsModule.runJob("INITIALIZE_STATION", { stationId }).then();
  46. }
  47. });
  48. CacheModule.runJob("SUB", {
  49. channel: "station.queueUpdate",
  50. cb: async stationId => {
  51. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  52. if (!station.currentSong && station.queue.length > 0) {
  53. StationsModule.runJob("INITIALIZE_STATION", {
  54. stationId
  55. }).then();
  56. }
  57. });
  58. }
  59. });
  60. CacheModule.runJob("SUB", {
  61. channel: "station.newOfficialPlaylist",
  62. cb: async stationId => {
  63. CacheModule.runJob("HGET", {
  64. table: "officialPlaylists",
  65. key: stationId
  66. }).then(playlistObj => {
  67. if (playlistObj) {
  68. WSModule.runJob("EMIT_TO_ROOM", {
  69. room: `station.${stationId}`,
  70. args: ["event:newOfficialPlaylist", { data: { playlist: playlistObj.songs } }]
  71. });
  72. }
  73. });
  74. }
  75. });
  76. const stationModel = (this.stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }));
  77. const stationSchema = (this.stationSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "station" }));
  78. return new Promise((resolve, reject) => {
  79. async.waterfall(
  80. [
  81. next => {
  82. this.setStage(2);
  83. CacheModule.runJob("HGETALL", { table: "stations" })
  84. .then(stations => {
  85. next(null, stations);
  86. })
  87. .catch(next);
  88. },
  89. (stations, next) => {
  90. this.setStage(3);
  91. if (!stations) return next();
  92. const stationIds = Object.keys(stations);
  93. return async.each(
  94. stationIds,
  95. (stationId, next) => {
  96. stationModel.findOne({ _id: stationId }, (err, station) => {
  97. if (err) next(err);
  98. else if (!station) {
  99. CacheModule.runJob("HDEL", {
  100. table: "stations",
  101. key: stationId
  102. })
  103. .then(() => {
  104. next();
  105. })
  106. .catch(next);
  107. } else next();
  108. });
  109. },
  110. next
  111. );
  112. },
  113. next => {
  114. this.setStage(4);
  115. stationModel.find({}, next);
  116. },
  117. (stations, next) => {
  118. this.setStage(5);
  119. async.each(
  120. stations,
  121. (station, next2) => {
  122. async.waterfall(
  123. [
  124. next => {
  125. CacheModule.runJob("HSET", {
  126. table: "stations",
  127. key: station._id,
  128. value: stationSchema(station)
  129. })
  130. .then(station => next(null, station))
  131. .catch(next);
  132. },
  133. (station, next) => {
  134. StationsModule.runJob(
  135. "INITIALIZE_STATION",
  136. {
  137. stationId: station._id
  138. },
  139. null,
  140. -1
  141. )
  142. .then(() => {
  143. next();
  144. })
  145. .catch(next);
  146. }
  147. ],
  148. err => {
  149. next2(err);
  150. }
  151. );
  152. },
  153. next
  154. );
  155. }
  156. ],
  157. async err => {
  158. if (err) {
  159. err = await UtilsModule.runJob("GET_ERROR", {
  160. error: err
  161. });
  162. reject(new Error(err));
  163. } else {
  164. resolve();
  165. }
  166. }
  167. );
  168. });
  169. }
  170. /**
  171. * Initialises a station
  172. *
  173. * @param {object} payload - object that contains the payload
  174. * @param {string} payload.stationId - id of the station to initialise
  175. * @returns {Promise} - returns a promise (resolve, reject)
  176. */
  177. INITIALIZE_STATION(payload) {
  178. return new Promise((resolve, reject) => {
  179. async.waterfall(
  180. [
  181. next => {
  182. StationsModule.runJob(
  183. "GET_STATION",
  184. {
  185. stationId: payload.stationId
  186. },
  187. this
  188. )
  189. .then(station => {
  190. next(null, station);
  191. })
  192. .catch(next);
  193. },
  194. (station, next) => {
  195. if (!station) return next("Station not found.");
  196. return NotificationsModule.runJob(
  197. "UNSCHEDULE",
  198. {
  199. name: `stations.nextSong?id=${station._id}`
  200. },
  201. this
  202. )
  203. .then()
  204. .catch()
  205. .finally(() => {
  206. NotificationsModule.runJob("SUBSCRIBE", {
  207. name: `stations.nextSong?id=${station._id}`,
  208. cb: () =>
  209. StationsModule.runJob("SKIP_STATION", {
  210. stationId: station._id,
  211. natural: true
  212. }),
  213. unique: true,
  214. station
  215. })
  216. .then()
  217. .catch();
  218. if (station.paused) return next(true, station);
  219. return next(null, station);
  220. });
  221. },
  222. (station, next) => {
  223. if (!station.currentSong) {
  224. return StationsModule.runJob(
  225. "SKIP_STATION",
  226. {
  227. stationId: station._id,
  228. natural: false
  229. },
  230. this
  231. )
  232. .then(station => {
  233. next(true, station);
  234. })
  235. .catch(next)
  236. .finally(() => {});
  237. }
  238. let timeLeft =
  239. station.currentSong.duration * 1000 - (Date.now() - station.startedAt - station.timePaused);
  240. if (Number.isNaN(timeLeft)) timeLeft = -1;
  241. if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
  242. return StationsModule.runJob(
  243. "SKIP_STATION",
  244. {
  245. stationId: station._id,
  246. natural: false
  247. },
  248. this
  249. )
  250. .then(station => {
  251. next(null, station);
  252. })
  253. .catch(next);
  254. }
  255. // name, time, cb, station
  256. NotificationsModule.runJob("SCHEDULE", {
  257. name: `stations.nextSong?id=${station._id}`,
  258. time: timeLeft,
  259. station
  260. });
  261. return next(null, station);
  262. }
  263. ],
  264. async (err, station) => {
  265. if (err && err !== true) {
  266. err = await UtilsModule.runJob(
  267. "GET_ERROR",
  268. {
  269. error: err
  270. },
  271. this
  272. );
  273. reject(new Error(err));
  274. } else resolve(station);
  275. }
  276. );
  277. });
  278. }
  279. /**
  280. * Attempts to get the station from Redis. If it's not in Redis, get it from Mongo and add it to Redis.
  281. *
  282. * @param {object} payload - object that contains the payload
  283. * @param {string} payload.stationId - id of the station
  284. * @returns {Promise} - returns a promise (resolve, reject)
  285. */
  286. GET_STATION(payload) {
  287. return new Promise((resolve, reject) => {
  288. async.waterfall(
  289. [
  290. next => {
  291. CacheModule.runJob("HGET", { table: "stations", key: payload.stationId }, this)
  292. .then(station => next(null, station))
  293. .catch(next);
  294. },
  295. (station, next) => {
  296. if (station) return next(true, station);
  297. return StationsModule.stationModel.findOne({ _id: payload.stationId }, next);
  298. },
  299. (station, next) => {
  300. if (station) {
  301. station = StationsModule.stationSchema(station);
  302. CacheModule.runJob("HSET", {
  303. table: "stations",
  304. key: payload.stationId,
  305. value: station
  306. })
  307. .then()
  308. .catch();
  309. next(true, station);
  310. } else next("Station not found");
  311. }
  312. ],
  313. async (err, station) => {
  314. if (err && err !== true) {
  315. err = await UtilsModule.runJob(
  316. "GET_ERROR",
  317. {
  318. error: err
  319. },
  320. this
  321. );
  322. reject(new Error(err));
  323. } else resolve(station);
  324. }
  325. );
  326. });
  327. }
  328. /**
  329. * Attempts to get a station by name, firstly from Redis. If it's not in Redis, get it from Mongo and add it to Redis.
  330. *
  331. * @param {object} payload - object that contains the payload
  332. * @param {string} payload.stationName - the unique name of the station
  333. * @returns {Promise} - returns a promise (resolve, reject)
  334. */
  335. async GET_STATION_BY_NAME(payload) {
  336. return new Promise((resolve, reject) => {
  337. async.waterfall(
  338. [
  339. next => {
  340. StationsModule.stationModel.findOne({ name: payload.stationName }, next);
  341. },
  342. (station, next) => {
  343. if (station) {
  344. station = StationsModule.stationSchema(station);
  345. CacheModule.runJob("HSET", {
  346. table: "stations",
  347. key: station._id,
  348. value: station
  349. });
  350. next(true, station);
  351. } else next("Station not found");
  352. }
  353. ],
  354. (err, station) => {
  355. if (err && err !== true) return reject(new Error(err));
  356. return resolve(station);
  357. }
  358. );
  359. });
  360. }
  361. /**
  362. * Updates the station in cache from mongo or deletes station in cache if no longer in mongo.
  363. *
  364. * @param {object} payload - object that contains the payload
  365. * @param {string} payload.stationId - the id of the station to update
  366. * @returns {Promise} - returns a promise (resolve, reject)
  367. */
  368. UPDATE_STATION(payload) {
  369. return new Promise((resolve, reject) => {
  370. async.waterfall(
  371. [
  372. next => {
  373. StationsModule.stationModel.findOne({ _id: payload.stationId }, next);
  374. },
  375. (station, next) => {
  376. if (!station) {
  377. CacheModule.runJob("HDEL", {
  378. table: "stations",
  379. key: payload.stationId
  380. })
  381. .then()
  382. .catch();
  383. return next("Station not found");
  384. }
  385. return CacheModule.runJob(
  386. "HSET",
  387. {
  388. table: "stations",
  389. key: payload.stationId,
  390. value: station
  391. },
  392. this
  393. )
  394. .then(station => {
  395. next(null, station);
  396. })
  397. .catch(next);
  398. }
  399. ],
  400. async (err, station) => {
  401. if (err && err !== true) {
  402. err = await UtilsModule.runJob(
  403. "GET_ERROR",
  404. {
  405. error: err
  406. },
  407. this
  408. );
  409. reject(new Error(err));
  410. } else resolve(station);
  411. }
  412. );
  413. });
  414. }
  415. /**
  416. * Fills up the official station playlist queue using the songs from the official station playlist
  417. *
  418. * @param {object} payload - object that contains the payload
  419. * @param {string} payload.stationId - the id of the station
  420. * @param {string} payload.ignoreExistingQueue - ignore the existing queue songs, replacing the old queue with a completely fresh one
  421. * @returns {Promise} - returns a promise (resolve, reject)
  422. */
  423. FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST(payload) {
  424. return new Promise((resolve, reject) => {
  425. const { stationId, ignoreExistingQueue } = payload;
  426. async.waterfall(
  427. [
  428. next => {
  429. PlaylistsModule.runJob("GET_STATION_PLAYLIST", { stationId, includeSongs: true }, this)
  430. .then(response => {
  431. next(null, response.playlist);
  432. })
  433. .catch(next);
  434. },
  435. (playlist, next) => {
  436. StationsModule.runJob("GET_STATION", { stationId }, this)
  437. .then(station => {
  438. if (ignoreExistingQueue) station.queue = [];
  439. next(null, playlist, station);
  440. })
  441. .catch(next);
  442. },
  443. (playlist, station, next) => {
  444. if (station.playMode === "random") {
  445. UtilsModule.runJob("SHUFFLE", { array: playlist.songs }, this)
  446. .then(response => {
  447. next(null, response.array, station);
  448. })
  449. .catch(next);
  450. } else next(null, playlist.songs, station);
  451. },
  452. (_playlistSongs, station, next) => {
  453. let playlistSongs = JSON.parse(JSON.stringify(_playlistSongs));
  454. if (station.playMode === "sequential") {
  455. if (station.currentSongIndex <= playlistSongs.length) {
  456. const songsToAddToEnd = playlistSongs.splice(0, station.currentSongIndex);
  457. playlistSongs = [...playlistSongs, ...songsToAddToEnd];
  458. }
  459. }
  460. const songsStillNeeded = 50 - station.queue.length;
  461. const currentSongs = station.queue;
  462. const currentYoutubeIds = station.queue.map(song => song.youtubeId);
  463. const songsToAdd = [];
  464. let lastSongAdded = null;
  465. if (station.currentSong && station.currentSong.youtubeId)
  466. currentYoutubeIds.push(station.currentSong.youtubeId);
  467. playlistSongs.every(song => {
  468. if (
  469. songsToAdd.length < songsStillNeeded &&
  470. currentYoutubeIds.indexOf(song.youtubeId) === -1
  471. ) {
  472. lastSongAdded = song;
  473. songsToAdd.push(song);
  474. return true;
  475. }
  476. if (songsToAdd.length >= songsStillNeeded) return false;
  477. return true;
  478. });
  479. let { currentSongIndex } = station;
  480. if (station.playMode === "sequential" && lastSongAdded) {
  481. const indexOfLastSong = _playlistSongs
  482. .map(song => song.youtubeId)
  483. .indexOf(lastSongAdded.youtubeId);
  484. if (indexOfLastSong !== -1) currentSongIndex = indexOfLastSong;
  485. }
  486. next(null, currentSongs, songsToAdd, currentSongIndex);
  487. },
  488. (currentSongs, songsToAdd, currentSongIndex, next) => {
  489. SongsModule.runJob("GET_SONGS", {
  490. songIds: songsToAdd.map(song => song._id),
  491. properties: [
  492. "youtubeId",
  493. "title",
  494. "duration",
  495. "skipDuration",
  496. "artists",
  497. "thumbnail",
  498. "verified"
  499. ]
  500. })
  501. .then(response => {
  502. const newSongsToAdd = songsToAdd.map(song =>
  503. response.songs.find(newSong => newSong._id.toString() === song._id.toString())
  504. );
  505. next(null, currentSongs, newSongsToAdd, currentSongIndex);
  506. })
  507. .catch(err => next(err));
  508. },
  509. (currentSongs, songsToAdd, currentSongIndex, next) => {
  510. const newPlaylist = [...currentSongs, ...songsToAdd].map(song => {
  511. if (!song._id) song._id = null;
  512. song.requestedAt = Date.now();
  513. return song;
  514. });
  515. next(null, newPlaylist, currentSongIndex);
  516. },
  517. (newPlaylist, currentSongIndex, next) => {
  518. StationsModule.stationModel.updateOne(
  519. { _id: stationId },
  520. { $set: { queue: newPlaylist, currentSongIndex } },
  521. { runValidators: true },
  522. err => {
  523. if (err) next(err);
  524. else
  525. StationsModule.runJob(
  526. "UPDATE_STATION",
  527. {
  528. stationId
  529. },
  530. this
  531. )
  532. .then(() => {
  533. next(null);
  534. })
  535. .catch(next);
  536. }
  537. );
  538. }
  539. ],
  540. err => {
  541. if (err) reject(err);
  542. else resolve();
  543. }
  544. );
  545. });
  546. }
  547. /**
  548. * Gets next station song
  549. *
  550. * @param {object} payload - object that contains the payload
  551. * @param {string} payload.stationId - the id of the station
  552. * @returns {Promise} - returns a promise (resolve, reject)
  553. */
  554. GET_NEXT_STATION_SONG(payload) {
  555. return new Promise((resolve, reject) => {
  556. const { stationId } = payload;
  557. async.waterfall(
  558. [
  559. next => {
  560. StationsModule.runJob("GET_STATION", { stationId }, this)
  561. .then(station => {
  562. next(null, station);
  563. })
  564. .catch(next);
  565. },
  566. (station, next) => {
  567. if (station.queue.length === 0) next("No songs available.");
  568. else {
  569. next(null, station.queue[0]);
  570. }
  571. },
  572. (queueSong, next) => {
  573. if (!queueSong._id) next(null, queueSong);
  574. else
  575. SongsModule.runJob("GET_SONG", { songId: queueSong._id }, this)
  576. .then(response => {
  577. const { song } = response;
  578. if (song) {
  579. const newSong = {
  580. _id: song._id,
  581. youtubeId: song.youtubeId,
  582. title: song.title,
  583. artists: song.artists,
  584. duration: song.duration,
  585. skipDuration: song.skipDuration,
  586. thumbnail: song.thumbnail,
  587. requestedAt: queueSong.requestedAt,
  588. requestedBy: queueSong.requestedBy,
  589. likes: song.likes,
  590. dislikes: song.dislikes,
  591. verified: song.verified
  592. };
  593. return next(null, newSong);
  594. }
  595. return next(null, song);
  596. })
  597. .catch(err => {
  598. next(err);
  599. });
  600. }
  601. ],
  602. (err, song) => {
  603. if (err) console.log(33333, err, payload);
  604. if (err) reject(err);
  605. else resolve({ song });
  606. }
  607. );
  608. });
  609. }
  610. /**
  611. * Removes first station queue song
  612. *
  613. * @param {object} payload - object that contains the payload
  614. * @param {string} payload.stationId - the id of the station
  615. * @returns {Promise} - returns a promise (resolve, reject)
  616. */
  617. REMOVE_FIRST_QUEUE_SONG(payload) {
  618. return new Promise((resolve, reject) => {
  619. const { stationId } = payload;
  620. async.waterfall(
  621. [
  622. next => {
  623. StationsModule.stationModel.updateOne(
  624. { _id: stationId },
  625. { $pop: { queue: -1 } },
  626. { runValidators: true },
  627. err => {
  628. if (err) next(err);
  629. else
  630. StationsModule.runJob(
  631. "UPDATE_STATION",
  632. {
  633. stationId
  634. },
  635. this
  636. )
  637. .then(() => {
  638. next(null);
  639. })
  640. .catch(next);
  641. }
  642. );
  643. }
  644. ],
  645. err => {
  646. if (err) reject(err);
  647. else resolve();
  648. }
  649. );
  650. });
  651. }
  652. /**
  653. * Skips a station
  654. *
  655. * @param {object} payload - object that contains the payload
  656. * @param {string} payload.stationId - the id of the station to skip
  657. * @param {string} payload.natural - whether to skip naturally or forcefully
  658. * @returns {Promise} - returns a promise (resolve, reject)
  659. */
  660. SKIP_STATION(payload) {
  661. return new Promise((resolve, reject) => {
  662. StationsModule.log("INFO", `Skipping station ${payload.stationId}.`);
  663. StationsModule.log("STATION_ISSUE", `SKIP_STATION_CB - Station ID: ${payload.stationId}.`);
  664. async.waterfall(
  665. [
  666. // Clears up any existing timers that would skip the station if the song ends
  667. next => {
  668. NotificationsModule.runJob("UNSCHEDULE", {
  669. name: `stations.nextSong?id=${payload.stationId}`
  670. })
  671. .then(() => {
  672. next();
  673. })
  674. .catch(next);
  675. },
  676. // Gets the station object
  677. next => {
  678. StationsModule.runJob(
  679. "GET_STATION",
  680. {
  681. stationId: payload.stationId
  682. },
  683. this
  684. )
  685. .then(station => next(null, station))
  686. .catch(next);
  687. },
  688. // eslint-disable-next-line consistent-return
  689. (station, next) => {
  690. if (!station) return next("Station not found.");
  691. StationsModule.runJob(
  692. "FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
  693. { stationId: station._id },
  694. this
  695. )
  696. .then(() => {
  697. StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
  698. .then(response => {
  699. StationsModule.runJob(
  700. "REMOVE_FIRST_QUEUE_SONG",
  701. { stationId: station._id },
  702. this
  703. )
  704. .then(() => {
  705. next(null, response.song, station);
  706. })
  707. .catch(next);
  708. })
  709. .catch(err => {
  710. if (err === "No songs available.") next(null, null, station);
  711. else next(err);
  712. });
  713. })
  714. .catch(next);
  715. },
  716. (song, station, next) => {
  717. const $set = {};
  718. if (song === null) $set.currentSong = null;
  719. else {
  720. $set.currentSong = {
  721. _id: song._id,
  722. youtubeId: song.youtubeId,
  723. title: song.title,
  724. artists: song.artists,
  725. duration: song.duration,
  726. skipDuration: song.skipDuration,
  727. thumbnail: song.thumbnail,
  728. requestedAt: song.requestedAt,
  729. requestedBy: song.requestedBy,
  730. verified: song.verified
  731. };
  732. }
  733. $set.startedAt = Date.now();
  734. $set.timePaused = 0;
  735. if (station.paused) $set.pausedAt = Date.now();
  736. next(null, $set, song, station);
  737. },
  738. ($set, song, station, next) => {
  739. StationsModule.stationModel.updateOne({ _id: station._id }, { $set }, err => {
  740. if (err) return next(err);
  741. return StationsModule.runJob("UPDATE_STATION", { stationId: station._id }, this)
  742. .then(station => {
  743. CacheModule.runJob("PUB", {
  744. channel: "station.queueUpdate",
  745. value: payload.stationId
  746. })
  747. .then()
  748. .catch();
  749. next(null, station, song);
  750. })
  751. .catch(next);
  752. });
  753. },
  754. (station, song, next) => {
  755. if (station.currentSong !== null && station.currentSong.youtubeId !== undefined) {
  756. station.currentSong.likes = song.likes;
  757. station.currentSong.dislikes = song.dislikes;
  758. station.currentSong.skipVotes = 0;
  759. }
  760. next(null, station);
  761. }
  762. ],
  763. async (err, station) => {
  764. if (err) {
  765. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  766. StationsModule.log("ERROR", `Skipping station "${payload.stationId}" failed. "${err}"`);
  767. return reject(new Error(err));
  768. }
  769. // TODO Pub/Sub this
  770. const { currentSong } = station;
  771. WSModule.runJob("EMIT_TO_ROOM", {
  772. room: `station.${station._id}`,
  773. args: [
  774. "event:station.nextSong",
  775. {
  776. data: {
  777. currentSong,
  778. startedAt: station.startedAt,
  779. paused: station.paused,
  780. timePaused: 0,
  781. natural: payload.natural
  782. }
  783. }
  784. ]
  785. });
  786. WSModule.runJob("EMIT_TO_ROOM", {
  787. room: `manage-station.${station._id}`,
  788. args: ["event:station.nextSong", { data: { stationId: station._id, currentSong } }]
  789. });
  790. if (station.privacy === "public")
  791. WSModule.runJob("EMIT_TO_ROOM", {
  792. room: "home",
  793. args: ["event:station.nextSong", { data: { stationId: station._id, currentSong } }]
  794. });
  795. else {
  796. const sockets = await WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: "home" }, this);
  797. sockets.forEach(async socketId => {
  798. const socket = await WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId });
  799. if (!socket) return;
  800. const { session } = socket;
  801. if (session.sessionId) {
  802. CacheModule.runJob("HGET", { table: "sessions", key: session.sessionId }).then(
  803. session => {
  804. if (session) {
  805. DBModule.runJob("GET_MODEL", { modelName: "user" }).then(userModel => {
  806. userModel.findOne({ _id: session.userId }, (err, user) => {
  807. if (!err && user) {
  808. if (user.role === "admin")
  809. socket.dispatch("event:station.nextSong", {
  810. data: {
  811. stationId: station._id,
  812. currentSong
  813. }
  814. });
  815. else if (
  816. station.type === "community" &&
  817. station.owner === session.userId
  818. )
  819. socket.dispatch("event:station.nextSong", {
  820. data: {
  821. stationId: station._id,
  822. currentSong
  823. }
  824. });
  825. }
  826. });
  827. });
  828. }
  829. }
  830. );
  831. }
  832. });
  833. }
  834. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: `station.${station._id}` }).then(sockets => {
  835. if (station.currentSong !== null && station.currentSong.youtubeId !== undefined) {
  836. WSModule.runJob("SOCKETS_JOIN_SONG_ROOM", {
  837. sockets,
  838. room: `song.${station.currentSong.youtubeId}`
  839. });
  840. if (!station.paused) {
  841. NotificationsModule.runJob("SCHEDULE", {
  842. name: `stations.nextSong?id=${station._id}`,
  843. time: station.currentSong.duration * 1000,
  844. station
  845. });
  846. }
  847. } else WSModule.runJob("SOCKETS_LEAVE_SONG_ROOMS", { sockets });
  848. });
  849. return resolve({ station });
  850. }
  851. );
  852. });
  853. }
  854. /**
  855. * Checks if a user can view/access a station
  856. *
  857. * @param {object} payload - object that contains the payload
  858. * @param {object} payload.station - the station object of the station in question
  859. * @param {string} payload.userId - the id of the user in question
  860. * @param {boolean} payload.homeView - whether to modify output for homepage usage
  861. * @returns {Promise} - returns a promise (resolve, reject)
  862. */
  863. CAN_USER_VIEW_STATION(payload) {
  864. return new Promise((resolve, reject) => {
  865. async.waterfall(
  866. [
  867. next => {
  868. if (payload.station.privacy === "public") return next(true);
  869. if (payload.station.privacy === "unlisted" && !payload.homeView) return next(true);
  870. if (!payload.userId) return next("Not allowed");
  871. return next();
  872. },
  873. next => {
  874. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  875. userModel.findOne({ _id: payload.userId }, next);
  876. });
  877. },
  878. (user, next) => {
  879. if (!user) return next("Not allowed");
  880. if (!payload.homeView && user.role === "admin") return next(true);
  881. if (payload.station.type === "official") return next("Not allowed");
  882. if (payload.station.owner === payload.userId) return next(true);
  883. return next("Not allowed");
  884. }
  885. ],
  886. async errOrResult => {
  887. if (errOrResult !== true && errOrResult !== "Not allowed") {
  888. errOrResult = await UtilsModule.runJob(
  889. "GET_ERROR",
  890. {
  891. error: errOrResult
  892. },
  893. this
  894. );
  895. reject(new Error(errOrResult));
  896. } else {
  897. resolve(errOrResult === true);
  898. }
  899. }
  900. );
  901. });
  902. }
  903. /**
  904. * Checks if a user has favorited a station or not
  905. *
  906. * @param {object} payload - object that contains the payload
  907. * @param {object} payload.stationId - the id of the station in question
  908. * @param {string} payload.userId - the id of the user in question
  909. * @returns {Promise} - returns a promise (resolve, reject)
  910. */
  911. HAS_USER_FAVORITED_STATION(payload) {
  912. return new Promise((resolve, reject) => {
  913. async.waterfall(
  914. [
  915. next => {
  916. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  917. userModel.findOne({ _id: payload.userId }, next);
  918. });
  919. },
  920. (user, next) => {
  921. if (!user) return next("User not found.");
  922. if (user.favoriteStations.indexOf(payload.stationId) !== -1) return next(null, true);
  923. return next(null, false);
  924. }
  925. ],
  926. async (err, isStationFavorited) => {
  927. if (err && err !== true) {
  928. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  929. return reject(new Error(err));
  930. }
  931. return resolve(isStationFavorited);
  932. }
  933. );
  934. });
  935. }
  936. /**
  937. * Returns a list of sockets in a room that can and can't know about a station
  938. *
  939. * @param {object} payload - the payload object
  940. * @param {object} payload.station - the station object
  941. * @param {string} payload.room - the websockets room to get the sockets from
  942. * @returns {Promise} - returns a promise (resolve, reject)
  943. */
  944. GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION(payload) {
  945. return new Promise((resolve, reject) => {
  946. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: payload.room }, this)
  947. .then(socketIds => {
  948. const sockets = [];
  949. async.eachLimit(
  950. socketIds,
  951. 1,
  952. (socketId, next) => {
  953. WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this)
  954. .then(socket => {
  955. if (socket) sockets.push(socket);
  956. next();
  957. })
  958. .catch(err => {
  959. reject(err);
  960. });
  961. },
  962. err => {
  963. if (err) reject(err);
  964. else {
  965. let socketsThatCan = [];
  966. const socketsThatCannot = [];
  967. if (payload.station.privacy === "public") {
  968. socketsThatCan = sockets;
  969. resolve({ socketsThatCan, socketsThatCannot });
  970. } else {
  971. async.eachLimit(
  972. sockets,
  973. 1,
  974. (socket, next) => {
  975. const { session } = socket;
  976. async.waterfall(
  977. [
  978. next => {
  979. if (!session.sessionId) next("No session id");
  980. else next();
  981. },
  982. next => {
  983. CacheModule.runJob(
  984. "HGET",
  985. {
  986. table: "sessions",
  987. key: session.sessionId
  988. },
  989. this
  990. )
  991. .then(response => {
  992. next(null, response);
  993. })
  994. .catch(next);
  995. },
  996. (session, next) => {
  997. if (!session) next("No session");
  998. else {
  999. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  1000. .then(userModel => {
  1001. next(null, userModel);
  1002. })
  1003. .catch(next);
  1004. }
  1005. },
  1006. (userModel, next) => {
  1007. if (!userModel) next("No user model");
  1008. else
  1009. userModel.findOne(
  1010. {
  1011. _id: session.userId
  1012. },
  1013. next
  1014. );
  1015. },
  1016. (user, next) => {
  1017. if (!user) next("No user found");
  1018. else if (user.role === "admin") {
  1019. socketsThatCan.push(socket);
  1020. next();
  1021. } else if (
  1022. payload.station.type === "community" &&
  1023. payload.station.owner === session.userId
  1024. ) {
  1025. socketsThatCan.push(socket);
  1026. next();
  1027. }
  1028. }
  1029. ],
  1030. err => {
  1031. if (err) socketsThatCannot.push(socket);
  1032. next();
  1033. }
  1034. );
  1035. },
  1036. err => {
  1037. if (err) reject(err);
  1038. else resolve({ socketsThatCan, socketsThatCannot });
  1039. }
  1040. );
  1041. }
  1042. }
  1043. }
  1044. );
  1045. })
  1046. .catch(reject);
  1047. });
  1048. }
  1049. /**
  1050. * Adds a playlist to be included in a station
  1051. *
  1052. * @param {object} payload - object that contains the payload
  1053. * @param {object} payload.stationId - the id of the station to include the playlist in
  1054. * @param {object} payload.playlistId - the id of the playlist to be included
  1055. * @returns {Promise} - returns a promise (resolve, reject)
  1056. */
  1057. INCLUDE_PLAYLIST(payload) {
  1058. return new Promise((resolve, reject) => {
  1059. async.waterfall(
  1060. [
  1061. next => {
  1062. if (!payload.stationId) next("Please specify a station id");
  1063. else if (!payload.playlistId) next("Please specify a playlist id");
  1064. else next();
  1065. },
  1066. next => {
  1067. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1068. .then(station => {
  1069. next(null, station);
  1070. })
  1071. .catch(next);
  1072. },
  1073. (station, next) => {
  1074. if (station.playlist === payload.playlistId) next("You cannot include the station playlist");
  1075. else if (station.includedPlaylists.indexOf(payload.playlistId) !== -1)
  1076. next("This playlist is already included");
  1077. else if (station.blacklist.indexOf(payload.playlistId) !== -1)
  1078. next(
  1079. "This playlist is currently blacklisted, please remove it from there before including it"
  1080. );
  1081. else
  1082. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
  1083. .then(() => {
  1084. next(null);
  1085. })
  1086. .catch(next);
  1087. },
  1088. next => {
  1089. DBModule.runJob(
  1090. "GET_MODEL",
  1091. {
  1092. modelName: "station"
  1093. },
  1094. this
  1095. ).then(stationModel => {
  1096. stationModel.updateOne(
  1097. { _id: payload.stationId },
  1098. { $push: { includedPlaylists: payload.playlistId } },
  1099. next
  1100. );
  1101. });
  1102. },
  1103. (res, next) => {
  1104. StationsModule.runJob(
  1105. "UPDATE_STATION",
  1106. {
  1107. stationId: payload.stationId
  1108. },
  1109. this
  1110. )
  1111. .then(() => {
  1112. next();
  1113. })
  1114. .catch(next);
  1115. }
  1116. ],
  1117. async err => {
  1118. if (err && err !== true) {
  1119. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1120. return reject(new Error(err));
  1121. }
  1122. return resolve();
  1123. }
  1124. );
  1125. });
  1126. }
  1127. /**
  1128. * Removes a playlist that is included in a station
  1129. *
  1130. * @param {object} payload - object that contains the payload
  1131. * @param {object} payload.stationId - the id of the station
  1132. * @param {object} payload.playlistId - the id of the playlist
  1133. * @returns {Promise} - returns a promise (resolve, reject)
  1134. */
  1135. REMOVE_INCLUDED_PLAYLIST(payload) {
  1136. return new Promise((resolve, reject) => {
  1137. async.waterfall(
  1138. [
  1139. next => {
  1140. if (!payload.stationId) next("Please specify a station id");
  1141. else if (!payload.playlistId) next("Please specify a playlist id");
  1142. else next();
  1143. },
  1144. next => {
  1145. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1146. .then(station => {
  1147. next(null, station);
  1148. })
  1149. .catch(next);
  1150. },
  1151. (station, next) => {
  1152. if (station.includedPlaylists.indexOf(payload.playlistId) === -1)
  1153. next("This playlist isn't included");
  1154. else next();
  1155. },
  1156. next => {
  1157. DBModule.runJob(
  1158. "GET_MODEL",
  1159. {
  1160. modelName: "station"
  1161. },
  1162. this
  1163. ).then(stationModel => {
  1164. stationModel.updateOne(
  1165. { _id: payload.stationId },
  1166. { $pull: { includedPlaylists: payload.playlistId } },
  1167. next
  1168. );
  1169. });
  1170. },
  1171. (res, next) => {
  1172. StationsModule.runJob(
  1173. "UPDATE_STATION",
  1174. {
  1175. stationId: payload.stationId
  1176. },
  1177. this
  1178. )
  1179. .then(() => {
  1180. next();
  1181. })
  1182. .catch(next);
  1183. }
  1184. ],
  1185. async err => {
  1186. if (err && err !== true) {
  1187. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1188. return reject(new Error(err));
  1189. }
  1190. return resolve();
  1191. }
  1192. );
  1193. });
  1194. }
  1195. /**
  1196. * Add a playlist to station blacklist
  1197. *
  1198. * @param {object} payload - object that contains the payload
  1199. * @param {object} payload.stationId - the id of the station
  1200. * @param {object} payload.playlistId - the id of the playlist
  1201. * @returns {Promise} - returns a promise (resolve, reject)
  1202. */
  1203. BLACKLIST_PLAYLIST(payload) {
  1204. return new Promise((resolve, reject) => {
  1205. async.waterfall(
  1206. [
  1207. next => {
  1208. if (!payload.stationId) next("Please specify a station id");
  1209. else if (!payload.playlistId) next("Please specify a playlist id");
  1210. else next();
  1211. },
  1212. next => {
  1213. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1214. .then(station => {
  1215. next(null, station);
  1216. })
  1217. .catch(next);
  1218. },
  1219. (station, next) => {
  1220. if (station.playlist === payload.playlistId) next("You cannot blacklist the station playlist");
  1221. else if (station.blacklist.indexOf(payload.playlistId) !== -1)
  1222. next("This playlist is already blacklisted");
  1223. else if (station.includedPlaylists.indexOf(payload.playlistId) !== -1)
  1224. next(
  1225. "This playlist is currently included, please remove it from there before blacklisting it"
  1226. );
  1227. else
  1228. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
  1229. .then(() => {
  1230. next(null);
  1231. })
  1232. .catch(next);
  1233. },
  1234. next => {
  1235. DBModule.runJob(
  1236. "GET_MODEL",
  1237. {
  1238. modelName: "station"
  1239. },
  1240. this
  1241. ).then(stationModel => {
  1242. stationModel.updateOne(
  1243. { _id: payload.stationId },
  1244. { $push: { blacklist: payload.playlistId } },
  1245. next
  1246. );
  1247. });
  1248. },
  1249. (res, next) => {
  1250. StationsModule.runJob(
  1251. "UPDATE_STATION",
  1252. {
  1253. stationId: payload.stationId
  1254. },
  1255. this
  1256. )
  1257. .then(() => {
  1258. next();
  1259. })
  1260. .catch(next);
  1261. }
  1262. ],
  1263. async err => {
  1264. if (err && err !== true) {
  1265. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1266. return reject(new Error(err));
  1267. }
  1268. return resolve();
  1269. }
  1270. );
  1271. });
  1272. }
  1273. /**
  1274. * Remove a playlist from station blacklist
  1275. *
  1276. * @param {object} payload - object that contains the payload
  1277. * @param {object} payload.stationId - the id of the station
  1278. * @param {object} payload.playlistId - the id of the playlist
  1279. * @returns {Promise} - returns a promise (resolve, reject)
  1280. */
  1281. REMOVE_BLACKLISTED_PLAYLIST(payload) {
  1282. return new Promise((resolve, reject) => {
  1283. async.waterfall(
  1284. [
  1285. next => {
  1286. if (!payload.stationId) next("Please specify a station id");
  1287. else if (!payload.playlistId) next("Please specify a playlist id");
  1288. else next();
  1289. },
  1290. next => {
  1291. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1292. .then(station => {
  1293. next(null, station);
  1294. })
  1295. .catch(next);
  1296. },
  1297. (station, next) => {
  1298. if (station.blacklist.indexOf(payload.playlistId) === -1)
  1299. next("This playlist isn't blacklisted");
  1300. else next();
  1301. },
  1302. next => {
  1303. DBModule.runJob(
  1304. "GET_MODEL",
  1305. {
  1306. modelName: "station"
  1307. },
  1308. this
  1309. ).then(stationModel => {
  1310. stationModel.updateOne(
  1311. { _id: payload.stationId },
  1312. { $pull: { blacklist: payload.playlistId } },
  1313. next
  1314. );
  1315. });
  1316. },
  1317. (res, next) => {
  1318. StationsModule.runJob(
  1319. "UPDATE_STATION",
  1320. {
  1321. stationId: payload.stationId
  1322. },
  1323. this
  1324. )
  1325. .then(() => {
  1326. next();
  1327. })
  1328. .catch(next);
  1329. }
  1330. ],
  1331. async err => {
  1332. if (err && err !== true) {
  1333. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1334. return reject(new Error(err));
  1335. }
  1336. return resolve();
  1337. }
  1338. );
  1339. });
  1340. }
  1341. /**
  1342. * Removes included or blacklisted playlist from a station
  1343. *
  1344. * @param {object} payload - object that contains the payload
  1345. * @param {string} payload.playlistId - the playlist id
  1346. * @returns {Promise} - returns promise (reject, resolve)
  1347. */
  1348. REMOVE_INCLUDED_OR_BLACKLISTED_PLAYLIST_FROM_STATIONS(payload) {
  1349. return new Promise((resolve, reject) => {
  1350. async.waterfall(
  1351. [
  1352. next => {
  1353. if (!payload.playlistId) next("Please specify a playlist id");
  1354. else next();
  1355. },
  1356. next => {
  1357. StationsModule.stationModel.updateMany(
  1358. {
  1359. $or: [{ includedPlaylists: payload.playlistId }, { blacklist: payload.playlistId }]
  1360. },
  1361. {
  1362. $pull: {
  1363. includedPlaylists: payload.playlistId,
  1364. blacklist: payload.playlistId
  1365. }
  1366. },
  1367. err => {
  1368. if (err) next(err);
  1369. else next();
  1370. }
  1371. );
  1372. }
  1373. ],
  1374. async err => {
  1375. if (err && err !== true) {
  1376. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1377. return reject(new Error(err));
  1378. }
  1379. return resolve();
  1380. }
  1381. );
  1382. });
  1383. }
  1384. /**
  1385. * Gets stations that include or blacklist a specific playlist
  1386. *
  1387. * @param {object} payload - object that contains the payload
  1388. * @param {string} payload.playlistId - the playlist id
  1389. * @returns {Promise} - returns promise (reject, resolve)
  1390. */
  1391. GET_STATIONS_THAT_INCLUDE_OR_BLACKLIST_PLAYLIST(payload) {
  1392. return new Promise((resolve, reject) => {
  1393. DBModule.runJob(
  1394. "GET_MODEL",
  1395. {
  1396. modelName: "station"
  1397. },
  1398. this
  1399. ).then(stationModel => {
  1400. stationModel.find(
  1401. {
  1402. $or: [{ includedPlaylists: payload.playlistId }, { blacklist: payload.playlistId }]
  1403. },
  1404. (err, stations) => {
  1405. if (err) reject(err);
  1406. else resolve({ stationIds: stations.map(station => station._id) });
  1407. }
  1408. );
  1409. });
  1410. });
  1411. }
  1412. /**
  1413. * Clears every queue
  1414. *
  1415. * @returns {Promise} - returns a promise (resolve, reject)
  1416. */
  1417. CLEAR_EVERY_STATION_QUEUE() {
  1418. return new Promise((resolve, reject) => {
  1419. async.waterfall(
  1420. [
  1421. next => {
  1422. StationsModule.stationModel.updateMany({}, { $set: { queue: [] } }, err => {
  1423. if (err) next(err);
  1424. else {
  1425. StationsModule.stationModel.find({}, (err, stations) => {
  1426. if (err) next(err);
  1427. else {
  1428. async.eachLimit(
  1429. stations,
  1430. 1,
  1431. (station, next) => {
  1432. StationsModule.runJob("UPDATE_STATION", {
  1433. stationId: station._id
  1434. })
  1435. .then(() => next())
  1436. .catch(next);
  1437. CacheModule.runJob("PUB", {
  1438. channel: "station.queueUpdate",
  1439. value: station._id
  1440. })
  1441. .then()
  1442. .catch();
  1443. },
  1444. next
  1445. );
  1446. }
  1447. });
  1448. }
  1449. });
  1450. }
  1451. ],
  1452. err => {
  1453. if (err) reject(err);
  1454. else resolve();
  1455. }
  1456. );
  1457. });
  1458. }
  1459. /**
  1460. * Clears and refills a station queue
  1461. *
  1462. * @param {object} payload - object that contains the payload
  1463. * @param {string} payload.stationId - the station id
  1464. * @returns {Promise} - returns a promise (resolve, reject)
  1465. */
  1466. CLEAR_AND_REFILL_STATION_QUEUE(payload) {
  1467. return new Promise((resolve, reject) => {
  1468. async.waterfall(
  1469. [
  1470. next => {
  1471. StationsModule.runJob(
  1472. "FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
  1473. { stationId: payload.stationId, ignoreExistingQueue: true },
  1474. this
  1475. )
  1476. .then(() => {
  1477. CacheModule.runJob("PUB", {
  1478. channel: "station.queueUpdate",
  1479. value: payload.stationId
  1480. })
  1481. .then()
  1482. .catch();
  1483. next();
  1484. })
  1485. .catch(err => {
  1486. next(err);
  1487. });
  1488. }
  1489. ],
  1490. err => {
  1491. if (err) reject(err);
  1492. else resolve();
  1493. }
  1494. );
  1495. });
  1496. }
  1497. }
  1498. export default new _StationsModule();