stations.js 42 KB

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