stations.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. playlistSongs
  466. .every(song => {
  467. if (
  468. songsToAdd.length < songsStillNeeded &&
  469. currentYoutubeIds.indexOf(song.youtubeId) === -1
  470. ) {
  471. lastSongAdded = song;
  472. songsToAdd.push(song);
  473. return true;
  474. }
  475. if (songsToAdd.length >= songsStillNeeded) return false;
  476. return true;
  477. });
  478. let { currentSongIndex } = station;
  479. if (station.playMode === "sequential" && lastSongAdded) {
  480. const indexOfLastSong = _playlistSongs
  481. .map(song => song.youtubeId)
  482. .indexOf(lastSongAdded.youtubeId);
  483. if (indexOfLastSong !== -1) currentSongIndex = indexOfLastSong;
  484. }
  485. next(null, currentSongs, songsToAdd, currentSongIndex);
  486. },
  487. (currentSongs, songsToAdd, currentSongIndex, next) => {
  488. SongsModule.runJob("GET_SONGS", {
  489. songIds: songsToAdd.map(song => song._id),
  490. properties: [
  491. "youtubeId",
  492. "title",
  493. "duration",
  494. "skipDuration",
  495. "artists",
  496. "thumbnail",
  497. "status"
  498. ]
  499. })
  500. .then(response => {
  501. const newSongsToAdd = songsToAdd.map(song =>
  502. response.songs.find(newSong => newSong._id.toString() === song._id.toString())
  503. );
  504. next(null, currentSongs, newSongsToAdd, currentSongIndex);
  505. })
  506. .catch(err => next(err));
  507. },
  508. (currentSongs, songsToAdd, currentSongIndex, next) => {
  509. const newPlaylist = [...currentSongs, ...songsToAdd].map(song => {
  510. if (!song._id) song._id = null;
  511. return song;
  512. });
  513. next(null, newPlaylist, currentSongIndex);
  514. },
  515. (newPlaylist, currentSongIndex, next) => {
  516. StationsModule.stationModel.updateOne(
  517. { _id: stationId },
  518. { $set: { queue: newPlaylist, currentSongIndex } },
  519. { runValidators: true },
  520. err => {
  521. if (err) next(err);
  522. else
  523. StationsModule.runJob(
  524. "UPDATE_STATION",
  525. {
  526. stationId
  527. },
  528. this
  529. )
  530. .then(() => {
  531. next(null);
  532. })
  533. .catch(next);
  534. }
  535. );
  536. }
  537. ],
  538. err => {
  539. if (err) reject(err);
  540. else resolve();
  541. }
  542. );
  543. });
  544. }
  545. /**
  546. * Gets next station song
  547. *
  548. * @param {object} payload - object that contains the payload
  549. * @param {string} payload.stationId - the id of the station
  550. * @returns {Promise} - returns a promise (resolve, reject)
  551. */
  552. GET_NEXT_STATION_SONG(payload) {
  553. return new Promise((resolve, reject) => {
  554. const { stationId } = payload;
  555. async.waterfall(
  556. [
  557. next => {
  558. StationsModule.runJob("GET_STATION", { stationId }, this)
  559. .then(station => {
  560. next(null, station);
  561. })
  562. .catch(next);
  563. },
  564. (station, next) => {
  565. if (station.queue.length === 0) next("No songs available.");
  566. else {
  567. next(null, station.queue[0]);
  568. }
  569. },
  570. (queueSong, next) => {
  571. if (!queueSong._id) next(null, queueSong);
  572. else
  573. SongsModule.runJob("GET_SONG", { songId: queueSong._id }, this)
  574. .then(response => {
  575. const { song } = response;
  576. if (song) {
  577. const newSong = {
  578. _id: song._id,
  579. youtubeId: song.youtubeId,
  580. title: song.title,
  581. artists: song.artists,
  582. duration: song.duration,
  583. skipDuration: song.skipDuration,
  584. thumbnail: song.thumbnail,
  585. requestedAt: queueSong.requestedAt,
  586. requestedBy: queueSong.requestedBy,
  587. likes: song.likes,
  588. dislikes: song.dislikes,
  589. status: song.status
  590. };
  591. return next(null, newSong);
  592. }
  593. return next(null, song);
  594. })
  595. .catch(err => {
  596. next(err);
  597. });
  598. }
  599. ],
  600. (err, song) => {
  601. if (err) console.log(33333, err, payload);
  602. if (err) reject(err);
  603. else resolve({ song });
  604. }
  605. );
  606. });
  607. }
  608. /**
  609. * Removes first station queue song
  610. *
  611. * @param {object} payload - object that contains the payload
  612. * @param {string} payload.stationId - the id of the station
  613. * @returns {Promise} - returns a promise (resolve, reject)
  614. */
  615. REMOVE_FIRST_QUEUE_SONG(payload) {
  616. return new Promise((resolve, reject) => {
  617. const { stationId } = payload;
  618. async.waterfall(
  619. [
  620. next => {
  621. StationsModule.stationModel.updateOne(
  622. { _id: stationId },
  623. { $pop: { queue: -1 } },
  624. { runValidators: true },
  625. err => {
  626. if (err) next(err);
  627. else
  628. StationsModule.runJob(
  629. "UPDATE_STATION",
  630. {
  631. stationId
  632. },
  633. this
  634. )
  635. .then(() => {
  636. next(null);
  637. })
  638. .catch(next);
  639. }
  640. );
  641. }
  642. ],
  643. err => {
  644. if (err) reject(err);
  645. else resolve();
  646. }
  647. );
  648. });
  649. }
  650. /**
  651. * Skips a station
  652. *
  653. * @param {object} payload - object that contains the payload
  654. * @param {string} payload.stationId - the id of the station to skip
  655. * @param {string} payload.natural - whether to skip naturally or forcefully
  656. * @returns {Promise} - returns a promise (resolve, reject)
  657. */
  658. SKIP_STATION(payload) {
  659. return new Promise((resolve, reject) => {
  660. StationsModule.log("INFO", `Skipping station ${payload.stationId}.`);
  661. StationsModule.log("STATION_ISSUE", `SKIP_STATION_CB - Station ID: ${payload.stationId}.`);
  662. async.waterfall(
  663. [
  664. // Clears up any existing timers that would skip the station if the song ends
  665. next => {
  666. NotificationsModule.runJob("UNSCHEDULE", {
  667. name: `stations.nextSong?id=${payload.stationId}`
  668. })
  669. .then(() => {
  670. next();
  671. })
  672. .catch(next);
  673. },
  674. // Gets the station object
  675. next => {
  676. StationsModule.runJob(
  677. "GET_STATION",
  678. {
  679. stationId: payload.stationId
  680. },
  681. this
  682. )
  683. .then(station => next(null, station))
  684. .catch(next);
  685. },
  686. // eslint-disable-next-line consistent-return
  687. (station, next) => {
  688. if (!station) return next("Station not found.");
  689. if (station.type === "community" && station.partyMode && station.queue.length === 0)
  690. return next(null, null, station); // Community station with party mode enabled and no songs in the queue
  691. if (station.type === "community" && station.partyMode && station.queue.length > 0) {
  692. // Community station with party mode enabled and songs in the queue
  693. if (station.paused) return next(null, null, station);
  694. StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
  695. .then(response => {
  696. StationsModule.runJob(
  697. "REMOVE_FIRST_QUEUE_SONG",
  698. { stationId: station._id },
  699. this
  700. ).then(() => {
  701. next(null, response.song, station);
  702. });
  703. })
  704. .catch(err => {
  705. if (err === "No songs available.") next(null, null, station);
  706. else next(err);
  707. });
  708. }
  709. if (station.type === "community" && !station.partyMode) {
  710. StationsModule.runJob(
  711. "FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
  712. { stationId: station._id },
  713. this
  714. )
  715. .then(() => {
  716. StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
  717. .then(response => {
  718. StationsModule.runJob(
  719. "REMOVE_FIRST_QUEUE_SONG",
  720. { stationId: station._id },
  721. this
  722. ).then(() => {
  723. next(null, response.song, station);
  724. });
  725. })
  726. .catch(err => {
  727. if (err === "No songs available.") next(null, null, station);
  728. else next(err);
  729. });
  730. })
  731. .catch(next);
  732. }
  733. if (station.type === "official") {
  734. StationsModule.runJob(
  735. "FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
  736. { stationId: station._id },
  737. this
  738. )
  739. .then(() => {
  740. StationsModule.runJob("GET_NEXT_STATION_SONG", { stationId: station._id }, this)
  741. .then(response => {
  742. StationsModule.runJob(
  743. "REMOVE_FIRST_QUEUE_SONG",
  744. { stationId: station._id },
  745. this
  746. )
  747. .then(() => {
  748. next(null, response.song, station);
  749. })
  750. .catch(next);
  751. })
  752. .catch(err => {
  753. if (err === "No songs available.") next(null, null, station);
  754. else next(err);
  755. });
  756. })
  757. .catch(next);
  758. }
  759. },
  760. (song, station, next) => {
  761. const $set = {};
  762. if (song === null) $set.currentSong = null;
  763. else {
  764. $set.currentSong = {
  765. _id: song._id,
  766. youtubeId: song.youtubeId,
  767. title: song.title,
  768. artists: song.artists,
  769. duration: song.duration,
  770. skipDuration: song.skipDuration,
  771. thumbnail: song.thumbnail,
  772. requestedAt: song.requestedAt,
  773. requestedBy: song.requestedBy,
  774. status: song.status
  775. };
  776. }
  777. $set.startedAt = Date.now();
  778. $set.timePaused = 0;
  779. if (station.paused) $set.pausedAt = Date.now();
  780. next(null, $set, song, station);
  781. },
  782. ($set, song, station, next) => {
  783. StationsModule.stationModel.updateOne({ _id: station._id }, { $set }, err => {
  784. if (err) return next(err);
  785. return StationsModule.runJob("UPDATE_STATION", { stationId: station._id }, this)
  786. .then(station => {
  787. CacheModule.runJob("PUB", {
  788. channel: "station.queueUpdate",
  789. value: payload.stationId
  790. })
  791. .then()
  792. .catch();
  793. next(null, station, song);
  794. })
  795. .catch(next);
  796. });
  797. },
  798. (station, song, next) => {
  799. if (station.currentSong !== null && station.currentSong.youtubeId !== undefined) {
  800. station.currentSong.likes = song.likes;
  801. station.currentSong.dislikes = song.dislikes;
  802. station.currentSong.skipVotes = 0;
  803. }
  804. next(null, station);
  805. }
  806. ],
  807. async (err, station) => {
  808. if (err) {
  809. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  810. StationsModule.log("ERROR", `Skipping station "${payload.stationId}" failed. "${err}"`);
  811. return reject(new Error(err));
  812. }
  813. // TODO Pub/Sub this
  814. const { currentSong } = station;
  815. WSModule.runJob("EMIT_TO_ROOM", {
  816. room: `station.${station._id}`,
  817. args: [
  818. "event:station.nextSong",
  819. {
  820. data: {
  821. currentSong,
  822. startedAt: station.startedAt,
  823. paused: station.paused,
  824. timePaused: 0,
  825. natural: payload.natural
  826. }
  827. }
  828. ]
  829. });
  830. WSModule.runJob("EMIT_TO_ROOM", {
  831. room: `manage-station.${station._id}`,
  832. args: ["event:station.nextSong", { data: { stationId: station._id, currentSong } }]
  833. });
  834. if (station.privacy === "public")
  835. WSModule.runJob("EMIT_TO_ROOM", {
  836. room: "home",
  837. args: ["event:station.nextSong", { data: { stationId: station._id, currentSong } }]
  838. });
  839. else {
  840. const sockets = await WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: "home" }, this);
  841. sockets.forEach(async socketId => {
  842. const socket = await WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId });
  843. if (!socket) return;
  844. const { session } = socket;
  845. if (session.sessionId) {
  846. CacheModule.runJob("HGET", { table: "sessions", key: session.sessionId }).then(
  847. session => {
  848. if (session) {
  849. DBModule.runJob("GET_MODEL", { modelName: "user" }).then(userModel => {
  850. userModel.findOne({ _id: session.userId }, (err, user) => {
  851. if (!err && user) {
  852. if (user.role === "admin")
  853. socket.dispatch("event:station.nextSong", {
  854. data: {
  855. stationId: station._id,
  856. currentSong
  857. }
  858. });
  859. else if (
  860. station.type === "community" &&
  861. station.owner === session.userId
  862. )
  863. socket.dispatch("event:station.nextSong", {
  864. data: {
  865. stationId: station._id,
  866. currentSong
  867. }
  868. });
  869. }
  870. });
  871. });
  872. }
  873. }
  874. );
  875. }
  876. });
  877. }
  878. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: `station.${station._id}` }).then(sockets => {
  879. if (station.currentSong !== null && station.currentSong.youtubeId !== undefined) {
  880. WSModule.runJob("SOCKETS_JOIN_SONG_ROOM", {
  881. sockets,
  882. room: `song.${station.currentSong.youtubeId}`
  883. });
  884. if (!station.paused) {
  885. NotificationsModule.runJob("SCHEDULE", {
  886. name: `stations.nextSong?id=${station._id}`,
  887. time: station.currentSong.duration * 1000,
  888. station
  889. });
  890. }
  891. } else WSModule.runJob("SOCKETS_LEAVE_SONG_ROOMS", { sockets });
  892. });
  893. return resolve({ station });
  894. }
  895. );
  896. });
  897. }
  898. /**
  899. * Checks if a user can view/access a station
  900. *
  901. * @param {object} payload - object that contains the payload
  902. * @param {object} payload.station - the station object of the station in question
  903. * @param {string} payload.userId - the id of the user in question
  904. * @param {boolean} payload.hideUnlisted - whether the user is allowed to see unlisted stations or not
  905. * @returns {Promise} - returns a promise (resolve, reject)
  906. */
  907. CAN_USER_VIEW_STATION(payload) {
  908. return new Promise((resolve, reject) => {
  909. async.waterfall(
  910. [
  911. next => {
  912. if (payload.station.privacy === "public") return next(true);
  913. if (payload.station.privacy === "unlisted")
  914. if (payload.hideUnlisted === true) return next();
  915. else return next(true);
  916. if (!payload.userId) return next("Not allowed");
  917. return next();
  918. },
  919. next => {
  920. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  921. userModel.findOne({ _id: payload.userId }, next);
  922. });
  923. },
  924. (user, next) => {
  925. if (!user) return next("Not allowed");
  926. if (user.role === "admin") return next(true);
  927. if (payload.station.type === "official") return next("Not allowed");
  928. if (payload.station.owner === payload.userId) return next(true);
  929. return next("Not allowed");
  930. }
  931. ],
  932. async errOrResult => {
  933. if (errOrResult !== true && errOrResult !== "Not allowed") {
  934. errOrResult = await UtilsModule.runJob(
  935. "GET_ERROR",
  936. {
  937. error: errOrResult
  938. },
  939. this
  940. );
  941. reject(new Error(errOrResult));
  942. } else {
  943. resolve(errOrResult === true);
  944. }
  945. }
  946. );
  947. });
  948. }
  949. /**
  950. * Checks if a user has favorited a station or not
  951. *
  952. * @param {object} payload - object that contains the payload
  953. * @param {object} payload.stationId - the id of the station in question
  954. * @param {string} payload.userId - the id of the user in question
  955. * @returns {Promise} - returns a promise (resolve, reject)
  956. */
  957. HAS_USER_FAVORITED_STATION(payload) {
  958. return new Promise((resolve, reject) => {
  959. async.waterfall(
  960. [
  961. next => {
  962. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(userModel => {
  963. userModel.findOne({ _id: payload.userId }, next);
  964. });
  965. },
  966. (user, next) => {
  967. if (!user) return next("User not found.");
  968. if (user.favoriteStations.indexOf(payload.stationId) !== -1) return next(null, true);
  969. return next(null, false);
  970. }
  971. ],
  972. async (err, isStationFavorited) => {
  973. if (err && err !== true) {
  974. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  975. return reject(new Error(err));
  976. }
  977. return resolve(isStationFavorited);
  978. }
  979. );
  980. });
  981. }
  982. /**
  983. * Returns a list of sockets in a room that can and can't know about a station
  984. *
  985. * @param {object} payload - the payload object
  986. * @param {object} payload.station - the station object
  987. * @param {string} payload.room - the websockets room to get the sockets from
  988. * @returns {Promise} - returns a promise (resolve, reject)
  989. */
  990. GET_SOCKETS_THAT_CAN_KNOW_ABOUT_STATION(payload) {
  991. return new Promise((resolve, reject) => {
  992. WSModule.runJob("GET_SOCKETS_FOR_ROOM", { room: payload.room }, this)
  993. .then(socketIds => {
  994. const sockets = [];
  995. async.eachLimit(
  996. socketIds,
  997. 1,
  998. (socketId, next) => {
  999. WSModule.runJob("SOCKET_FROM_SOCKET_ID", { socketId }, this)
  1000. .then(socket => {
  1001. if (socket) sockets.push(socket);
  1002. next();
  1003. })
  1004. .catch(err => {
  1005. reject(err);
  1006. });
  1007. },
  1008. err => {
  1009. if (err) reject(err);
  1010. else {
  1011. let socketsThatCan = [];
  1012. const socketsThatCannot = [];
  1013. if (payload.station.privacy === "public") {
  1014. socketsThatCan = sockets;
  1015. resolve({ socketsThatCan, socketsThatCannot });
  1016. } else {
  1017. async.eachLimit(
  1018. sockets,
  1019. 1,
  1020. (socket, next) => {
  1021. const { session } = socket;
  1022. async.waterfall(
  1023. [
  1024. next => {
  1025. if (!session.sessionId) next("No session id");
  1026. else next();
  1027. },
  1028. next => {
  1029. CacheModule.runJob(
  1030. "HGET",
  1031. {
  1032. table: "sessions",
  1033. key: session.sessionId
  1034. },
  1035. this
  1036. )
  1037. .then(response => {
  1038. next(null, response);
  1039. })
  1040. .catch(next);
  1041. },
  1042. (session, next) => {
  1043. if (!session) next("No session");
  1044. else {
  1045. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  1046. .then(userModel => {
  1047. next(null, userModel);
  1048. })
  1049. .catch(next);
  1050. }
  1051. },
  1052. (userModel, next) => {
  1053. if (!userModel) next("No user model");
  1054. else
  1055. userModel.findOne(
  1056. {
  1057. _id: session.userId
  1058. },
  1059. next
  1060. );
  1061. },
  1062. (user, next) => {
  1063. if (!user) next("No user found");
  1064. else if (user.role === "admin") {
  1065. socketsThatCan.push(socket);
  1066. next();
  1067. } else if (
  1068. payload.station.type === "community" &&
  1069. payload.station.owner === session.userId
  1070. ) {
  1071. socketsThatCan.push(socket);
  1072. next();
  1073. }
  1074. }
  1075. ],
  1076. err => {
  1077. if (err) socketsThatCannot.push(socket);
  1078. next();
  1079. }
  1080. );
  1081. },
  1082. err => {
  1083. if (err) reject(err);
  1084. else resolve({ socketsThatCan, socketsThatCannot });
  1085. }
  1086. );
  1087. }
  1088. }
  1089. }
  1090. );
  1091. })
  1092. .catch(reject);
  1093. });
  1094. }
  1095. /**
  1096. * Adds a playlist to be included in a station
  1097. *
  1098. * @param {object} payload - object that contains the payload
  1099. * @param {object} payload.stationId - the id of the station to include the playlist in
  1100. * @param {object} payload.playlistId - the id of the playlist to be included
  1101. * @returns {Promise} - returns a promise (resolve, reject)
  1102. */
  1103. INCLUDE_PLAYLIST(payload) {
  1104. return new Promise((resolve, reject) => {
  1105. async.waterfall(
  1106. [
  1107. next => {
  1108. if (!payload.stationId) next("Please specify a station id");
  1109. else if (!payload.playlistId) next("Please specify a playlist id");
  1110. else next();
  1111. },
  1112. next => {
  1113. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1114. .then(station => {
  1115. next(null, station);
  1116. })
  1117. .catch(next);
  1118. },
  1119. (station, next) => {
  1120. if (station.playlist === payload.playlistId) next("You cannot include the station playlist");
  1121. else if (station.includedPlaylists.indexOf(payload.playlistId) !== -1)
  1122. next("This playlist is already included");
  1123. else if (station.excludedPlaylists.indexOf(payload.playlistId) !== -1)
  1124. next(
  1125. "This playlist is currently excluded, please remove it from there before including it"
  1126. );
  1127. else
  1128. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
  1129. .then(() => {
  1130. next(null);
  1131. })
  1132. .catch(next);
  1133. },
  1134. next => {
  1135. DBModule.runJob(
  1136. "GET_MODEL",
  1137. {
  1138. modelName: "station"
  1139. },
  1140. this
  1141. ).then(stationModel => {
  1142. stationModel.updateOne(
  1143. { _id: payload.stationId },
  1144. { $push: { includedPlaylists: payload.playlistId } },
  1145. next
  1146. );
  1147. });
  1148. },
  1149. (res, next) => {
  1150. StationsModule.runJob(
  1151. "UPDATE_STATION",
  1152. {
  1153. stationId: payload.stationId
  1154. },
  1155. this
  1156. )
  1157. .then(() => {
  1158. next();
  1159. })
  1160. .catch(next);
  1161. }
  1162. ],
  1163. async err => {
  1164. if (err && err !== true) {
  1165. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1166. return reject(new Error(err));
  1167. }
  1168. return resolve();
  1169. }
  1170. );
  1171. });
  1172. }
  1173. /**
  1174. * Removes a playlist that is included in a station
  1175. *
  1176. * @param {object} payload - object that contains the payload
  1177. * @param {object} payload.stationId - the id of the station
  1178. * @param {object} payload.playlistId - the id of the playlist
  1179. * @returns {Promise} - returns a promise (resolve, reject)
  1180. */
  1181. REMOVE_INCLUDED_PLAYLIST(payload) {
  1182. return new Promise((resolve, reject) => {
  1183. async.waterfall(
  1184. [
  1185. next => {
  1186. if (!payload.stationId) next("Please specify a station id");
  1187. else if (!payload.playlistId) next("Please specify a playlist id");
  1188. else next();
  1189. },
  1190. next => {
  1191. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1192. .then(station => {
  1193. next(null, station);
  1194. })
  1195. .catch(next);
  1196. },
  1197. (station, next) => {
  1198. if (station.includedPlaylists.indexOf(payload.playlistId) === -1)
  1199. next("This playlist isn't included");
  1200. else next();
  1201. },
  1202. next => {
  1203. DBModule.runJob(
  1204. "GET_MODEL",
  1205. {
  1206. modelName: "station"
  1207. },
  1208. this
  1209. ).then(stationModel => {
  1210. stationModel.updateOne(
  1211. { _id: payload.stationId },
  1212. { $pull: { includedPlaylists: payload.playlistId } },
  1213. next
  1214. );
  1215. });
  1216. },
  1217. (res, next) => {
  1218. StationsModule.runJob(
  1219. "UPDATE_STATION",
  1220. {
  1221. stationId: payload.stationId
  1222. },
  1223. this
  1224. )
  1225. .then(() => {
  1226. next();
  1227. })
  1228. .catch(next);
  1229. }
  1230. ],
  1231. async err => {
  1232. if (err && err !== true) {
  1233. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1234. return reject(new Error(err));
  1235. }
  1236. return resolve();
  1237. }
  1238. );
  1239. });
  1240. }
  1241. /**
  1242. * Adds a playlist to be excluded in a station
  1243. *
  1244. * @param {object} payload - object that contains the payload
  1245. * @param {object} payload.stationId - the id of the station
  1246. * @param {object} payload.playlistId - the id of the playlist
  1247. * @returns {Promise} - returns a promise (resolve, reject)
  1248. */
  1249. EXCLUDE_PLAYLIST(payload) {
  1250. return new Promise((resolve, reject) => {
  1251. async.waterfall(
  1252. [
  1253. next => {
  1254. if (!payload.stationId) next("Please specify a station id");
  1255. else if (!payload.playlistId) next("Please specify a playlist id");
  1256. else next();
  1257. },
  1258. next => {
  1259. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1260. .then(station => {
  1261. next(null, station);
  1262. })
  1263. .catch(next);
  1264. },
  1265. (station, next) => {
  1266. if (station.playlist === payload.playlistId) next("You cannot exclude the station playlist");
  1267. else if (station.excludedPlaylists.indexOf(payload.playlistId) !== -1)
  1268. next("This playlist is already excluded");
  1269. else if (station.includedPlaylists.indexOf(payload.playlistId) !== -1)
  1270. next(
  1271. "This playlist is currently included, please remove it from there before excluding it"
  1272. );
  1273. else
  1274. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: payload.playlistId }, this)
  1275. .then(() => {
  1276. next(null);
  1277. })
  1278. .catch(next);
  1279. },
  1280. next => {
  1281. DBModule.runJob(
  1282. "GET_MODEL",
  1283. {
  1284. modelName: "station"
  1285. },
  1286. this
  1287. ).then(stationModel => {
  1288. stationModel.updateOne(
  1289. { _id: payload.stationId },
  1290. { $push: { excludedPlaylists: payload.playlistId } },
  1291. next
  1292. );
  1293. });
  1294. },
  1295. (res, next) => {
  1296. StationsModule.runJob(
  1297. "UPDATE_STATION",
  1298. {
  1299. stationId: payload.stationId
  1300. },
  1301. this
  1302. )
  1303. .then(() => {
  1304. next();
  1305. })
  1306. .catch(next);
  1307. }
  1308. ],
  1309. async err => {
  1310. if (err && err !== true) {
  1311. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1312. return reject(new Error(err));
  1313. }
  1314. return resolve();
  1315. }
  1316. );
  1317. });
  1318. }
  1319. /**
  1320. * Removes a playlist that is excluded in a station
  1321. *
  1322. * @param {object} payload - object that contains the payload
  1323. * @param {object} payload.stationId - the id of the station
  1324. * @param {object} payload.playlistId - the id of the playlist
  1325. * @returns {Promise} - returns a promise (resolve, reject)
  1326. */
  1327. REMOVE_EXCLUDED_PLAYLIST(payload) {
  1328. return new Promise((resolve, reject) => {
  1329. console.log(112, payload);
  1330. async.waterfall(
  1331. [
  1332. next => {
  1333. if (!payload.stationId) next("Please specify a station id");
  1334. else if (!payload.playlistId) next("Please specify a playlist id");
  1335. else next();
  1336. },
  1337. next => {
  1338. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1339. .then(station => {
  1340. next(null, station);
  1341. })
  1342. .catch(next);
  1343. },
  1344. (station, next) => {
  1345. if (station.excludedPlaylists.indexOf(payload.playlistId) === -1)
  1346. next("This playlist isn't excluded");
  1347. else next();
  1348. },
  1349. next => {
  1350. DBModule.runJob(
  1351. "GET_MODEL",
  1352. {
  1353. modelName: "station"
  1354. },
  1355. this
  1356. ).then(stationModel => {
  1357. stationModel.updateOne(
  1358. { _id: payload.stationId },
  1359. { $pull: { excludedPlaylists: payload.playlistId } },
  1360. next
  1361. );
  1362. });
  1363. },
  1364. (res, next) => {
  1365. StationsModule.runJob(
  1366. "UPDATE_STATION",
  1367. {
  1368. stationId: payload.stationId
  1369. },
  1370. this
  1371. )
  1372. .then(() => {
  1373. next();
  1374. })
  1375. .catch(next);
  1376. }
  1377. ],
  1378. async err => {
  1379. if (err && err !== true) {
  1380. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1381. return reject(new Error(err));
  1382. }
  1383. return resolve();
  1384. }
  1385. );
  1386. });
  1387. }
  1388. /**
  1389. * Gets stations that include or exclude a specific playlist
  1390. *
  1391. * @param {object} payload - object that contains the payload
  1392. * @param {string} payload.playlistId - the playlist id
  1393. * @returns {Promise} - returns promise (reject, resolve)
  1394. */
  1395. GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST(payload) {
  1396. return new Promise((resolve, reject) => {
  1397. DBModule.runJob(
  1398. "GET_MODEL",
  1399. {
  1400. modelName: "station"
  1401. },
  1402. this
  1403. ).then(stationModel => {
  1404. stationModel.find(
  1405. {
  1406. $or: [{ includedPlaylists: payload.playlistId }, { excludedPlaylists: payload.playlistId }]
  1407. },
  1408. (err, stations) => {
  1409. if (err) reject(err);
  1410. else resolve({ stationIds: stations.map(station => station._id) });
  1411. }
  1412. );
  1413. });
  1414. });
  1415. }
  1416. /**
  1417. * Clears every queue
  1418. *
  1419. * @returns {Promise} - returns a promise (resolve, reject)
  1420. */
  1421. CLEAR_EVERY_STATION_QUEUE() {
  1422. return new Promise((resolve, reject) => {
  1423. async.waterfall(
  1424. [
  1425. next => {
  1426. StationsModule.stationModel.updateMany({}, { $set: { queue: [] } }, err => {
  1427. if (err) next(err);
  1428. else {
  1429. StationsModule.stationModel.find({}, (err, stations) => {
  1430. if (err) next(err);
  1431. else {
  1432. async.eachLimit(
  1433. stations,
  1434. 1,
  1435. (station, next) => {
  1436. StationsModule.runJob("UPDATE_STATION", {
  1437. stationId: station._id
  1438. })
  1439. .then(() => next())
  1440. .catch(next);
  1441. CacheModule.runJob("PUB", {
  1442. channel: "station.queueUpdate",
  1443. value: station._id
  1444. })
  1445. .then()
  1446. .catch();
  1447. },
  1448. next
  1449. );
  1450. }
  1451. });
  1452. }
  1453. });
  1454. }
  1455. ],
  1456. err => {
  1457. if (err) reject(err);
  1458. else resolve();
  1459. }
  1460. );
  1461. });
  1462. }
  1463. /**
  1464. * Clears and refills a station queue
  1465. *
  1466. * @param {object} payload - object that contains the payload
  1467. * @param {string} payload.stationId - the station id
  1468. * @returns {Promise} - returns a promise (resolve, reject)
  1469. */
  1470. CLEAR_AND_REFILL_STATION_QUEUE(payload) {
  1471. return new Promise((resolve, reject) => {
  1472. async.waterfall(
  1473. [
  1474. next => {
  1475. StationsModule.runJob(
  1476. "FILL_UP_STATION_QUEUE_FROM_STATION_PLAYLIST",
  1477. { stationId: payload.stationId, ignoreExistingQueue: true },
  1478. this
  1479. )
  1480. .then(() => {
  1481. CacheModule.runJob("PUB", {
  1482. channel: "station.queueUpdate",
  1483. value: payload.stationId
  1484. })
  1485. .then()
  1486. .catch();
  1487. next();
  1488. })
  1489. .catch(err => {
  1490. next(err);
  1491. });
  1492. }
  1493. ],
  1494. err => {
  1495. if (err) reject(err);
  1496. else resolve();
  1497. }
  1498. );
  1499. });
  1500. }
  1501. }
  1502. export default new _StationsModule();