stations.js 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  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. {
  1456. $or: [{ includedPlaylists: payload.playlistId }, { excludedPlaylists: payload.playlistId }]
  1457. },
  1458. (err, stations) => {
  1459. if (err) reject(err);
  1460. else resolve({ stationIds: stations.map(station => station._id) });
  1461. }
  1462. );
  1463. });
  1464. });
  1465. }
  1466. }
  1467. export default new _StationsModule();