stations.js 43 KB

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