stations.js 41 KB

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