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