stations.js 43 KB

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