stations.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. import async from "async";
  2. import CoreClass from "../core";
  3. let StationsModule;
  4. let CacheModule;
  5. let DBModule;
  6. let UtilsModule;
  7. let SongsModule;
  8. let NotificationsModule;
  9. class _StationsModule extends CoreClass {
  10. // eslint-disable-next-line require-jsdoc
  11. constructor() {
  12. super("stations");
  13. StationsModule = this;
  14. }
  15. /**
  16. * Initialises the stations module
  17. *
  18. * @returns {Promise} - returns promise (reject, resolve)
  19. */
  20. async initialize() {
  21. CacheModule = this.moduleManager.modules.cache;
  22. DBModule = this.moduleManager.modules.db;
  23. UtilsModule = this.moduleManager.modules.utils;
  24. SongsModule = this.moduleManager.modules.songs;
  25. NotificationsModule = this.moduleManager.modules.notifications;
  26. this.defaultSong = {
  27. songId: "60ItHLz5WEA",
  28. title: "Faded - Alan Walker",
  29. duration: 212,
  30. skipDuration: 0,
  31. likes: -1,
  32. dislikes: -1
  33. };
  34. // TEMP
  35. CacheModule.runJob("SUB", {
  36. channel: "station.pause",
  37. cb: async stationId => {
  38. NotificationsModule.runJob("REMOVE", {
  39. subscription: `stations.nextSong?id=${stationId}`
  40. }).then();
  41. }
  42. });
  43. CacheModule.runJob("SUB", {
  44. channel: "station.resume",
  45. cb: async stationId => {
  46. StationsModule.runJob("INITIALIZE_STATION", { stationId }).then();
  47. }
  48. });
  49. CacheModule.runJob("SUB", {
  50. channel: "station.queueUpdate",
  51. cb: async stationId => {
  52. StationsModule.runJob("GET_STATION", { stationId }).then(station => {
  53. if (!station.currentSong && station.queue.length > 0) {
  54. StationsModule.runJob("INITIALIZE_STATION", {
  55. stationId
  56. }).then();
  57. }
  58. });
  59. }
  60. });
  61. CacheModule.runJob("SUB", {
  62. channel: "station.newOfficialPlaylist",
  63. cb: async stationId => {
  64. CacheModule.runJob("HGET", {
  65. table: "officialPlaylists",
  66. key: stationId
  67. }).then(playlistObj => {
  68. if (playlistObj) {
  69. UtilsModule.runJob("EMIT_TO_ROOM", {
  70. room: `station.${stationId}`,
  71. args: ["event:newOfficialPlaylist", playlistObj.songs]
  72. });
  73. }
  74. });
  75. }
  76. });
  77. const stationModel = (this.stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }));
  78. const stationSchema = (this.stationSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "station" }));
  79. return new Promise((resolve, reject) =>
  80. async.waterfall(
  81. [
  82. next => {
  83. this.setStage(2);
  84. CacheModule.runJob("HGETALL", { table: "stations" })
  85. .then(stations => {
  86. next(null, stations);
  87. })
  88. .catch(next);
  89. },
  90. (stations, next) => {
  91. this.setStage(3);
  92. if (!stations) return next();
  93. const stationIds = Object.keys(stations);
  94. return async.each(
  95. stationIds,
  96. (stationId, next) => {
  97. stationModel.findOne({ _id: stationId }, (err, station) => {
  98. if (err) next(err);
  99. else if (!station) {
  100. CacheModule.runJob("HDEL", {
  101. table: "stations",
  102. key: stationId
  103. })
  104. .then(() => {
  105. next();
  106. })
  107. .catch(next);
  108. } else next();
  109. });
  110. },
  111. next
  112. );
  113. },
  114. next => {
  115. this.setStage(4);
  116. stationModel.find({}, next);
  117. },
  118. (stations, next) => {
  119. this.setStage(5);
  120. async.each(
  121. stations,
  122. (station, next2) => {
  123. async.waterfall(
  124. [
  125. next => {
  126. CacheModule.runJob("HSET", {
  127. table: "stations",
  128. key: station._id,
  129. value: stationSchema(station)
  130. })
  131. .then(station => next(null, station))
  132. .catch(next);
  133. },
  134. (station, next) => {
  135. StationsModule.runJob(
  136. "INITIALIZE_STATION",
  137. {
  138. stationId: station._id
  139. },
  140. null,
  141. -1
  142. )
  143. .then(() => {
  144. next();
  145. })
  146. .catch(next);
  147. }
  148. ],
  149. err => {
  150. next2(err);
  151. }
  152. );
  153. },
  154. next
  155. );
  156. }
  157. ],
  158. async err => {
  159. if (err) {
  160. err = await UtilsModule.runJob("GET_ERROR", {
  161. error: err
  162. });
  163. reject(new Error(err));
  164. } else {
  165. resolve();
  166. }
  167. }
  168. )
  169. );
  170. }
  171. /**
  172. * Initialises a station
  173. *
  174. * @param {object} payload - object that contains the payload
  175. * @param {string} payload.stationId - id of the station to initialise
  176. * @returns {Promise} - returns a promise (resolve, reject)
  177. */
  178. INITIALIZE_STATION(payload) {
  179. return new Promise((resolve, reject) => {
  180. // if (typeof cb !== 'function') cb = ()=>{};
  181. async.waterfall(
  182. [
  183. next => {
  184. StationsModule.runJob(
  185. "GET_STATION",
  186. {
  187. stationId: payload.stationId
  188. },
  189. this
  190. )
  191. .then(station => {
  192. next(null, station);
  193. })
  194. .catch(next);
  195. },
  196. (station, next) => {
  197. if (!station) return next("Station not found.");
  198. NotificationsModule.runJob("UNSCHEDULE", {
  199. name: `stations.nextSong?id=${station._id}`
  200. })
  201. .then()
  202. .catch();
  203. NotificationsModule.runJob(
  204. "SUBSCRIBE",
  205. {
  206. name: `stations.nextSong?id=${station._id}`,
  207. cb: () =>
  208. StationsModule.runJob("SKIP_STATION", {
  209. stationId: station._id
  210. }),
  211. unique: true,
  212. station
  213. },
  214. this
  215. )
  216. .then()
  217. .catch();
  218. if (station.paused) return next(true, station);
  219. return next(null, station);
  220. },
  221. (station, next) => {
  222. if (!station.currentSong) {
  223. return StationsModule.runJob(
  224. "SKIP_STATION",
  225. {
  226. stationId: station._id
  227. },
  228. this
  229. )
  230. .then(station => {
  231. next(true, station);
  232. })
  233. .catch(next)
  234. .finally(() => {});
  235. }
  236. let timeLeft =
  237. station.currentSong.duration * 1000 - (Date.now() - station.startedAt - station.timePaused);
  238. if (Number.isNaN(timeLeft)) timeLeft = -1;
  239. if (station.currentSong.duration * 1000 < timeLeft || timeLeft < 0) {
  240. return StationsModule.runJob(
  241. "SKIP_STATION",
  242. {
  243. stationId: station._id
  244. },
  245. this
  246. )
  247. .then(station => {
  248. next(null, station);
  249. })
  250. .catch(next);
  251. }
  252. // name, time, cb, station
  253. NotificationsModule.runJob("SCHEDULE", {
  254. name: `stations.nextSong?id=${station._id}`,
  255. time: timeLeft,
  256. station
  257. });
  258. return next(null, station);
  259. }
  260. ],
  261. async (err, station) => {
  262. if (err && err !== true) {
  263. err = await UtilsModule.runJob(
  264. "GET_ERROR",
  265. {
  266. error: err
  267. },
  268. this
  269. );
  270. reject(new Error(err));
  271. } else resolve(station);
  272. }
  273. );
  274. });
  275. }
  276. /**
  277. * Calculates the next song for the station
  278. *
  279. * @param {object} payload - object that contains the payload
  280. * @param {string} payload.station - station object to calculate song for
  281. * @returns {Promise} - returns a promise (resolve, reject)
  282. */
  283. async CALCULATE_SONG_FOR_STATION(payload) {
  284. // station, bypassValidate = false
  285. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  286. return new Promise((resolve, reject) => {
  287. const songList = [];
  288. return async.waterfall(
  289. [
  290. next => {
  291. if (payload.station.genres.length === 0) return next();
  292. const genresDone = [];
  293. return payload.station.genres.forEach(genre => {
  294. songModel.find({ genres: genre }, (err, songs) => {
  295. if (!err) {
  296. songs.forEach(song => {
  297. if (songList.indexOf(song._id) === -1) {
  298. let found = false;
  299. song.genres.forEach(songGenre => {
  300. if (payload.station.blacklistedGenres.indexOf(songGenre) !== -1)
  301. found = true;
  302. });
  303. if (!found) {
  304. songList.push(song._id);
  305. }
  306. }
  307. });
  308. }
  309. genresDone.push(genre);
  310. if (genresDone.length === payload.station.genres.length) next();
  311. });
  312. });
  313. },
  314. next => {
  315. const playlist = [];
  316. songList.forEach(songId => {
  317. if (payload.station.playlist.indexOf(songId) === -1) playlist.push(songId);
  318. });
  319. // eslint-disable-next-line array-callback-return
  320. payload.station.playlist.filter(songId => {
  321. if (songList.indexOf(songId) !== -1) playlist.push(songId);
  322. });
  323. UtilsModule.runJob("SHUFFLE", { array: playlist })
  324. .then(result => {
  325. next(null, result.array);
  326. }, this)
  327. .catch(next);
  328. },
  329. (playlist, next) => {
  330. StationsModule.runJob(
  331. "CALCULATE_OFFICIAL_PLAYLIST_LIST",
  332. {
  333. stationId: payload.station._id,
  334. songList: playlist
  335. },
  336. this
  337. )
  338. .then(() => {
  339. next(null, playlist);
  340. })
  341. .catch(next);
  342. },
  343. (playlist, next) => {
  344. StationsModule.stationModel.updateOne(
  345. { _id: payload.station._id },
  346. { $set: { playlist } },
  347. { runValidators: true },
  348. () => {
  349. StationsModule.runJob(
  350. "UPDATE_STATION",
  351. {
  352. stationId: payload.station._id
  353. },
  354. this
  355. )
  356. .then(() => {
  357. next(null, playlist);
  358. })
  359. .catch(next);
  360. }
  361. );
  362. }
  363. ],
  364. (err, newPlaylist) => {
  365. if (err) return reject(new Error(err));
  366. return resolve(newPlaylist);
  367. }
  368. );
  369. });
  370. }
  371. /**
  372. * Attempts to get the station from Redis. If it's not in Redis, get it from Mongo and add it to Redis.
  373. *
  374. * @param {object} payload - object that contains the payload
  375. * @param {string} payload.stationId - id of the station
  376. * @returns {Promise} - returns a promise (resolve, reject)
  377. */
  378. GET_STATION(payload) {
  379. return new Promise((resolve, reject) => {
  380. async.waterfall(
  381. [
  382. next => {
  383. CacheModule.runJob(
  384. "HGET",
  385. {
  386. table: "stations",
  387. key: payload.stationId
  388. },
  389. this
  390. )
  391. .then(station => {
  392. next(null, station);
  393. })
  394. .catch(next);
  395. },
  396. (station, next) => {
  397. if (station) return next(true, station);
  398. return StationsModule.stationModel.findOne({ _id: payload.stationId }, next);
  399. },
  400. (station, next) => {
  401. if (station) {
  402. if (station.type === "official") {
  403. StationsModule.runJob("CALCULATE_OFFICIAL_PLAYLIST_LIST", {
  404. stationId: station._id,
  405. songList: station.playlist
  406. })
  407. .then()
  408. .catch();
  409. }
  410. station = StationsModule.stationSchema(station);
  411. CacheModule.runJob("HSET", {
  412. table: "stations",
  413. key: payload.stationId,
  414. value: station
  415. })
  416. .then()
  417. .catch();
  418. next(true, station);
  419. } else next("Station not found");
  420. }
  421. ],
  422. async (err, station) => {
  423. if (err && err !== true) {
  424. err = await UtilsModule.runJob(
  425. "GET_ERROR",
  426. {
  427. error: err
  428. },
  429. this
  430. );
  431. reject(new Error(err));
  432. } else resolve(station);
  433. }
  434. );
  435. });
  436. }
  437. /**
  438. * 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.
  439. *
  440. * @param {object} payload - object that contains the payload
  441. * @param {string} payload.stationName - the unique name of the station
  442. * @returns {Promise} - returns a promise (resolve, reject)
  443. */
  444. async GET_STATION_BY_NAME(payload) {
  445. return new Promise((resolve, reject) =>
  446. async.waterfall(
  447. [
  448. next => {
  449. StationsModule.stationModel.findOne({ name: payload.stationName }, next);
  450. },
  451. (station, next) => {
  452. if (station) {
  453. if (station.type === "official") {
  454. StationsModule.runJob("CALCULATE_OFFICIAL_PLAYLIST_LIST", {
  455. stationId: station._id,
  456. songList: station.playlist
  457. });
  458. }
  459. station = StationsModule.stationSchema(station);
  460. CacheModule.runJob("HSET", {
  461. table: "stations",
  462. key: station._id,
  463. value: station
  464. });
  465. next(true, station);
  466. } else next("Station not found");
  467. }
  468. ],
  469. (err, station) => {
  470. if (err && err !== true) return reject(new Error(err));
  471. return resolve(station);
  472. }
  473. )
  474. );
  475. }
  476. /**
  477. * Updates the station in cache from mongo or deletes station in cache if no longer in mongo.
  478. *
  479. * @param {object} payload - object that contains the payload
  480. * @param {string} payload.stationId - the id of the station to update
  481. * @returns {Promise} - returns a promise (resolve, reject)
  482. */
  483. UPDATE_STATION(payload) {
  484. return new Promise((resolve, reject) => {
  485. async.waterfall(
  486. [
  487. next => {
  488. StationsModule.stationModel.findOne({ _id: payload.stationId }, next);
  489. },
  490. (station, next) => {
  491. if (!station) {
  492. CacheModule.runJob("HDEL", {
  493. table: "stations",
  494. key: payload.stationId
  495. })
  496. .then()
  497. .catch();
  498. return next("Station not found");
  499. }
  500. return CacheModule.runJob(
  501. "HSET",
  502. {
  503. table: "stations",
  504. key: payload.stationId,
  505. value: station
  506. },
  507. this
  508. )
  509. .then(station => {
  510. next(null, station);
  511. })
  512. .catch(next);
  513. }
  514. ],
  515. async (err, station) => {
  516. if (err && err !== true) {
  517. err = await UtilsModule.runJob(
  518. "GET_ERROR",
  519. {
  520. error: err
  521. },
  522. this
  523. );
  524. reject(new Error(err));
  525. } else resolve(station);
  526. }
  527. );
  528. });
  529. }
  530. /**
  531. * Creates the official playlist for a station
  532. *
  533. * @param {object} payload - object that contains the payload
  534. * @param {string} payload.stationId - the id of the station
  535. * @param {Array} payload.songList - list of songs to put in official playlist
  536. * @returns {Promise} - returns a promise (resolve, reject)
  537. */
  538. async CALCULATE_OFFICIAL_PLAYLIST_LIST(payload) {
  539. const officialPlaylistSchema = await CacheModule.runJob("GET_SCHEMA", { schemaName: "officialPlaylist" }, this);
  540. console.log(typeof payload.songList, payload.songList);
  541. return new Promise(resolve => {
  542. const lessInfoPlaylist = [];
  543. return async.each(
  544. payload.songList,
  545. (song, next) => {
  546. SongsModule.runJob("GET_SONG", { id: song }, this)
  547. .then(response => {
  548. const { song } = response;
  549. if (song) {
  550. const newSong = {
  551. songId: song.songId,
  552. title: song.title,
  553. artists: song.artists,
  554. duration: song.duration
  555. };
  556. lessInfoPlaylist.push(newSong);
  557. }
  558. })
  559. .finally(() => {
  560. next();
  561. });
  562. },
  563. () => {
  564. CacheModule.runJob(
  565. "HSET",
  566. {
  567. table: "officialPlaylists",
  568. key: payload.stationId,
  569. value: officialPlaylistSchema(payload.stationId, lessInfoPlaylist)
  570. },
  571. this
  572. ).finally(() => {
  573. CacheModule.runJob("PUB", {
  574. channel: "station.newOfficialPlaylist",
  575. value: payload.stationId
  576. });
  577. resolve();
  578. });
  579. }
  580. );
  581. });
  582. }
  583. /**
  584. * Skips a station
  585. *
  586. * @param {object} payload - object that contains the payload
  587. * @param {string} payload.stationId - the id of the station to skip
  588. * @returns {Promise} - returns a promise (resolve, reject)
  589. */
  590. SKIP_STATION(payload) {
  591. return new Promise((resolve, reject) => {
  592. StationsModule.log("INFO", `Skipping station ${payload.stationId}.`);
  593. StationsModule.log("STATION_ISSUE", `SKIP_STATION_CB - Station ID: ${payload.stationId}.`);
  594. async.waterfall(
  595. [
  596. next => {
  597. StationsModule.runJob(
  598. "GET_STATION",
  599. {
  600. stationId: payload.stationId
  601. },
  602. this
  603. )
  604. .then(station => {
  605. next(null, station);
  606. })
  607. .catch(() => {});
  608. },
  609. // eslint-disable-next-line consistent-return
  610. (station, next) => {
  611. if (!station) return next("Station not found.");
  612. if (station.type === "community" && station.partyMode && station.queue.length === 0)
  613. return next(null, null, -11, station); // Community station with party mode enabled and no songs in the queue
  614. if (station.type === "community" && station.partyMode && station.queue.length > 0) {
  615. // Community station with party mode enabled and songs in the queue
  616. if (station.paused) return next(null, null, -19, station);
  617. return StationsModule.stationModel.updateOne(
  618. { _id: payload.stationId },
  619. {
  620. $pull: {
  621. queue: {
  622. _id: station.queue[0]._id
  623. }
  624. }
  625. },
  626. err => {
  627. if (err) return next(err);
  628. return next(null, station.queue[0], -12, station);
  629. }
  630. );
  631. }
  632. if (station.type === "community" && !station.partyMode) {
  633. return DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel =>
  634. playlistModel.findOne({ _id: station.privatePlaylist }, (err, playlist) => {
  635. if (err) return next(err);
  636. if (!playlist) return next(null, null, -13, station);
  637. playlist = playlist.songs;
  638. if (playlist.length > 0) {
  639. let currentSongIndex;
  640. if (station.currentSongIndex < playlist.length - 1)
  641. currentSongIndex = station.currentSongIndex + 1;
  642. else currentSongIndex = 0;
  643. const callback = (err, song) => {
  644. if (err) return next(err);
  645. if (song) return next(null, song, currentSongIndex, station);
  646. const currentSong = {
  647. songId: playlist[currentSongIndex].songId,
  648. title: playlist[currentSongIndex].title,
  649. duration: playlist[currentSongIndex].duration,
  650. likes: -1,
  651. dislikes: -1
  652. };
  653. return next(null, currentSong, currentSongIndex, station);
  654. };
  655. if (playlist[currentSongIndex]._id)
  656. return SongsModule.runJob(
  657. "GET_SONG",
  658. {
  659. id: playlist[currentSongIndex]._id
  660. },
  661. this
  662. )
  663. .then(response => callback(null, response.song))
  664. .catch(callback);
  665. return SongsModule.runJob(
  666. "GET_SONG_FROM_ID",
  667. {
  668. songId: playlist[currentSongIndex].songId
  669. },
  670. this
  671. )
  672. .then(response => callback(null, response.song))
  673. .catch(callback);
  674. }
  675. return next(null, null, -14, station);
  676. })
  677. );
  678. }
  679. if (station.type === "official" && station.playlist.length === 0) {
  680. return StationsModule.runJob("CALCULATE_SONG_FOR_STATION", { station }, this)
  681. .then(playlist => {
  682. if (playlist.length === 0)
  683. return next(null, StationsModule.defaultSong, 0, station);
  684. return SongsModule.runJob(
  685. "GET_SONG",
  686. {
  687. id: playlist[0]
  688. },
  689. this
  690. )
  691. .then(response => {
  692. next(null, response.song, 0, station);
  693. })
  694. .catch(() => next(null, StationsModule.defaultSong, 0, station));
  695. })
  696. .catch(err => {
  697. next(err);
  698. });
  699. }
  700. if (station.type === "official" && station.playlist.length > 0) {
  701. return async.doUntil(
  702. next => {
  703. if (station.currentSongIndex < station.playlist.length - 1) {
  704. SongsModule.runJob(
  705. "GET_SONG",
  706. {
  707. id: station.playlist[station.currentSongIndex + 1]
  708. },
  709. this
  710. )
  711. .then(response => next(null, response.song, station.currentSongIndex + 1))
  712. .catch(() => {
  713. station.currentSongIndex += 1;
  714. next(null, null, null);
  715. });
  716. } else {
  717. StationsModule.runJob(
  718. "CALCULATE_SONG_FOR_STATION",
  719. {
  720. station
  721. },
  722. this
  723. )
  724. .then(newPlaylist => {
  725. SongsModule.runJob("GET_SONG", { id: newPlaylist[0] }, this)
  726. .then(response => {
  727. station.playlist = newPlaylist;
  728. next(null, response.song, 0);
  729. })
  730. .catch(() => next(null, StationsModule.defaultSong, 0));
  731. })
  732. .catch(() => {
  733. next(null, StationsModule.defaultSong, 0);
  734. });
  735. }
  736. },
  737. (song, currentSongIndex, next) => {
  738. if (song) return next(null, true, currentSongIndex);
  739. return next(null, false);
  740. },
  741. (err, song, currentSongIndex) => next(err, song, currentSongIndex, station)
  742. );
  743. }
  744. },
  745. (song, currentSongIndex, station, next) => {
  746. const $set = {};
  747. if (song === null) $set.currentSong = null;
  748. else if (song.likes === -1 && song.dislikes === -1) {
  749. $set.currentSong = {
  750. songId: song.songId,
  751. title: song.title,
  752. duration: song.duration,
  753. skipDuration: 0,
  754. likes: -1,
  755. dislikes: -1
  756. };
  757. } else {
  758. $set.currentSong = {
  759. songId: song.songId,
  760. title: song.title,
  761. artists: song.artists,
  762. duration: song.duration,
  763. likes: song.likes,
  764. dislikes: song.dislikes,
  765. skipDuration: song.skipDuration,
  766. thumbnail: song.thumbnail
  767. };
  768. }
  769. if (currentSongIndex >= 0) $set.currentSongIndex = currentSongIndex;
  770. $set.startedAt = Date.now();
  771. $set.timePaused = 0;
  772. if (station.paused) $set.pausedAt = Date.now();
  773. next(null, $set, station);
  774. },
  775. ($set, station, next) => {
  776. StationsModule.stationModel.updateOne({ _id: station._id }, { $set }, () => {
  777. StationsModule.runJob(
  778. "UPDATE_STATION",
  779. {
  780. stationId: station._id
  781. },
  782. this
  783. )
  784. .then(station => {
  785. if (station.type === "community" && station.partyMode === true)
  786. CacheModule.runJob("PUB", {
  787. channel: "station.queueUpdate",
  788. value: payload.stationId
  789. })
  790. .then()
  791. .catch();
  792. next(null, station);
  793. })
  794. .catch(next);
  795. });
  796. }
  797. ],
  798. async (err, station) => {
  799. if (err) {
  800. err = await UtilsModule.runJob(
  801. "GET_ERROR",
  802. {
  803. error: err
  804. },
  805. this
  806. );
  807. StationsModule.log("ERROR", `Skipping station "${payload.stationId}" failed. "${err}"`);
  808. reject(new Error(err));
  809. } else {
  810. if (station.currentSong !== null && station.currentSong.songId !== undefined) {
  811. station.currentSong.skipVotes = 0;
  812. }
  813. // TODO Pub/Sub this
  814. UtilsModule.runJob("EMIT_TO_ROOM", {
  815. room: `station.${station._id}`,
  816. args: [
  817. "event:songs.next",
  818. {
  819. currentSong: station.currentSong,
  820. startedAt: station.startedAt,
  821. paused: station.paused,
  822. timePaused: 0
  823. }
  824. ]
  825. })
  826. .then()
  827. .catch();
  828. if (station.privacy === "public") {
  829. UtilsModule.runJob("EMIT_TO_ROOM", {
  830. room: "home",
  831. args: ["event:station.nextSong", station._id, station.currentSong]
  832. })
  833. .then()
  834. .catch();
  835. } else {
  836. const sockets = await UtilsModule.runJob("GET_ROOM_SOCKETS", { room: "home" }, this);
  837. for (
  838. let socketId = 0, socketKeys = Object.keys(sockets);
  839. socketId < socketKeys.length;
  840. socketId += 1
  841. ) {
  842. const socket = sockets[socketId];
  843. const { session } = sockets[socketId];
  844. if (session.sessionId) {
  845. CacheModule.runJob(
  846. "HGET",
  847. {
  848. table: "sessions",
  849. key: session.sessionId
  850. },
  851. this
  852. // eslint-disable-next-line no-loop-func
  853. ).then(session => {
  854. if (session) {
  855. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(
  856. userModel => {
  857. userModel.findOne(
  858. {
  859. _id: session.userId
  860. },
  861. (err, user) => {
  862. if (!err && user) {
  863. if (user.role === "admin")
  864. socket.emit(
  865. "event:station.nextSong",
  866. station._id,
  867. station.currentSong
  868. );
  869. else if (
  870. station.type === "community" &&
  871. station.owner === session.userId
  872. )
  873. socket.emit(
  874. "event:station.nextSong",
  875. station._id,
  876. station.currentSong
  877. );
  878. }
  879. }
  880. );
  881. }
  882. );
  883. }
  884. });
  885. }
  886. }
  887. }
  888. if (station.currentSong !== null && station.currentSong.songId !== undefined) {
  889. UtilsModule.runJob("SOCKETS_JOIN_SONG_ROOM", {
  890. sockets: await UtilsModule.runJob(
  891. "GET_ROOM_SOCKETS",
  892. {
  893. room: `station.${station._id}`
  894. },
  895. this
  896. ),
  897. room: `song.${station.currentSong.songId}`
  898. });
  899. if (!station.paused) {
  900. NotificationsModule.runJob("SCHEDULE", {
  901. name: `stations.nextSong?id=${station._id}`,
  902. time: station.currentSong.duration * 1000,
  903. station
  904. });
  905. }
  906. } else {
  907. UtilsModule.runJob("SOCKETS_LEAVE_SONG_ROOMS", {
  908. sockets: await UtilsModule.runJob(
  909. "GET_ROOM_SOCKETS",
  910. {
  911. room: `station.${station._id}`
  912. },
  913. this
  914. )
  915. })
  916. .then()
  917. .catch();
  918. }
  919. resolve({ station });
  920. }
  921. }
  922. );
  923. });
  924. }
  925. /**
  926. * Checks if a user can view/access a station
  927. *
  928. * @param {object} payload - object that contains the payload
  929. * @param {object} payload.station - the station object of the station in question
  930. * @param {string} payload.userId - the id of the user in question
  931. * @param {boolean} payload.hideUnlisted - whether the user is allowed to see unlisted stations or not
  932. * @returns {Promise} - returns a promise (resolve, reject)
  933. */
  934. CAN_USER_VIEW_STATION(payload) {
  935. return new Promise((resolve, reject) => {
  936. async.waterfall(
  937. [
  938. next => {
  939. if (payload.station.privacy === "public") return next(true);
  940. if (payload.station.privacy === "unlisted")
  941. if (payload.hideUnlisted === true) return next();
  942. else return next(true);
  943. if (!payload.userId) return next("Not allowed");
  944. return next();
  945. },
  946. next => {
  947. DBModule.runJob(
  948. "GET_MODEL",
  949. {
  950. modelName: "user"
  951. },
  952. this
  953. ).then(userModel => {
  954. userModel.findOne({ _id: payload.userId }, next);
  955. });
  956. },
  957. (user, next) => {
  958. if (!user) return next("Not allowed");
  959. if (user.role === "admin") return next(true);
  960. if (payload.station.type === "official") return next("Not allowed");
  961. if (payload.station.owner === payload.userId) return next(true);
  962. return next("Not allowed");
  963. }
  964. ],
  965. async errOrResult => {
  966. if (errOrResult !== true && errOrResult !== "Not allowed") {
  967. errOrResult = await UtilsModule.runJob(
  968. "GET_ERROR",
  969. {
  970. error: errOrResult
  971. },
  972. this
  973. );
  974. reject(new Error(errOrResult));
  975. } else {
  976. resolve(errOrResult === true);
  977. }
  978. }
  979. );
  980. });
  981. }
  982. }
  983. export default new _StationsModule();