stations.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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. Object.keys(sockets).forEach(socketKey => {
  838. const socket = sockets[socketKey];
  839. const { session } = socket;
  840. if (session.sessionId) {
  841. CacheModule.runJob(
  842. "HGET",
  843. {
  844. table: "sessions",
  845. key: session.sessionId
  846. },
  847. this
  848. // eslint-disable-next-line no-loop-func
  849. ).then(session => {
  850. if (session) {
  851. DBModule.runJob("GET_MODEL", { modelName: "user" }, this).then(
  852. userModel => {
  853. userModel.findOne(
  854. {
  855. _id: session.userId
  856. },
  857. (err, user) => {
  858. if (!err && user) {
  859. if (user.role === "admin")
  860. socket.emit(
  861. "event:station.nextSong",
  862. station._id,
  863. station.currentSong
  864. );
  865. else if (
  866. station.type === "community" &&
  867. station.owner === session.userId
  868. )
  869. socket.emit(
  870. "event:station.nextSong",
  871. station._id,
  872. station.currentSong
  873. );
  874. }
  875. }
  876. );
  877. }
  878. );
  879. }
  880. });
  881. }
  882. });
  883. }
  884. if (station.currentSong !== null && station.currentSong.songId !== undefined) {
  885. UtilsModule.runJob("SOCKETS_JOIN_SONG_ROOM", {
  886. sockets: await UtilsModule.runJob(
  887. "GET_ROOM_SOCKETS",
  888. {
  889. room: `station.${station._id}`
  890. },
  891. this
  892. ),
  893. room: `song.${station.currentSong.songId}`
  894. });
  895. if (!station.paused) {
  896. NotificationsModule.runJob("SCHEDULE", {
  897. name: `stations.nextSong?id=${station._id}`,
  898. time: station.currentSong.duration * 1000,
  899. station
  900. });
  901. }
  902. } else {
  903. UtilsModule.runJob("SOCKETS_LEAVE_SONG_ROOMS", {
  904. sockets: await UtilsModule.runJob(
  905. "GET_ROOM_SOCKETS",
  906. {
  907. room: `station.${station._id}`
  908. },
  909. this
  910. )
  911. })
  912. .then()
  913. .catch();
  914. }
  915. resolve({ station });
  916. }
  917. }
  918. );
  919. });
  920. }
  921. /**
  922. * Checks if a user can view/access a station
  923. *
  924. * @param {object} payload - object that contains the payload
  925. * @param {object} payload.station - the station object of the station in question
  926. * @param {string} payload.userId - the id of the user in question
  927. * @param {boolean} payload.hideUnlisted - whether the user is allowed to see unlisted stations or not
  928. * @returns {Promise} - returns a promise (resolve, reject)
  929. */
  930. CAN_USER_VIEW_STATION(payload) {
  931. return new Promise((resolve, reject) => {
  932. async.waterfall(
  933. [
  934. next => {
  935. if (payload.station.privacy === "public") return next(true);
  936. if (payload.station.privacy === "unlisted")
  937. if (payload.hideUnlisted === true) return next();
  938. else return next(true);
  939. if (!payload.userId) return next("Not allowed");
  940. return next();
  941. },
  942. next => {
  943. DBModule.runJob(
  944. "GET_MODEL",
  945. {
  946. modelName: "user"
  947. },
  948. this
  949. ).then(userModel => {
  950. userModel.findOne({ _id: payload.userId }, next);
  951. });
  952. },
  953. (user, next) => {
  954. if (!user) return next("Not allowed");
  955. if (user.role === "admin") return next(true);
  956. if (payload.station.type === "official") return next("Not allowed");
  957. if (payload.station.owner === payload.userId) return next(true);
  958. return next("Not allowed");
  959. }
  960. ],
  961. async errOrResult => {
  962. if (errOrResult !== true && errOrResult !== "Not allowed") {
  963. errOrResult = await UtilsModule.runJob(
  964. "GET_ERROR",
  965. {
  966. error: errOrResult
  967. },
  968. this
  969. );
  970. reject(new Error(errOrResult));
  971. } else {
  972. resolve(errOrResult === true);
  973. }
  974. }
  975. );
  976. });
  977. }
  978. }
  979. export default new _StationsModule();