stations.js 41 KB

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