playlists.js 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. import async from "async";
  2. import CoreClass from "../core";
  3. let PlaylistsModule;
  4. let StationsModule;
  5. let SongsModule;
  6. let CacheModule;
  7. let DBModule;
  8. let UtilsModule;
  9. class _PlaylistsModule extends CoreClass {
  10. // eslint-disable-next-line require-jsdoc
  11. constructor() {
  12. super("playlists");
  13. PlaylistsModule = this;
  14. }
  15. /**
  16. * Initialises the playlists module
  17. *
  18. * @returns {Promise} - returns promise (reject, resolve)
  19. */
  20. async initialize() {
  21. this.setStage(1);
  22. StationsModule = this.moduleManager.modules.stations;
  23. CacheModule = this.moduleManager.modules.cache;
  24. DBModule = this.moduleManager.modules.db;
  25. UtilsModule = this.moduleManager.modules.utils;
  26. SongsModule = this.moduleManager.modules.songs;
  27. this.playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" });
  28. this.playlistSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "playlist" });
  29. this.setStage(2);
  30. return new Promise((resolve, reject) =>
  31. async.waterfall(
  32. [
  33. next => {
  34. this.setStage(3);
  35. CacheModule.runJob("HGETALL", { table: "playlists" })
  36. .then(playlists => {
  37. next(null, playlists);
  38. })
  39. .catch(next);
  40. },
  41. (playlists, next) => {
  42. this.setStage(4);
  43. if (!playlists) return next();
  44. const playlistIds = Object.keys(playlists);
  45. return async.each(
  46. playlistIds,
  47. (playlistId, next) => {
  48. PlaylistsModule.playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
  49. if (err) next(err);
  50. else if (!playlist) {
  51. CacheModule.runJob("HDEL", {
  52. table: "playlists",
  53. key: playlistId
  54. })
  55. .then(() => next())
  56. .catch(next);
  57. } else next();
  58. });
  59. },
  60. next
  61. );
  62. },
  63. next => {
  64. this.setStage(5);
  65. PlaylistsModule.playlistModel.find({}, next);
  66. },
  67. (playlists, next) => {
  68. this.setStage(6);
  69. async.each(
  70. playlists,
  71. (playlist, cb) => {
  72. CacheModule.runJob("HSET", {
  73. table: "playlists",
  74. key: playlist._id,
  75. value: PlaylistsModule.playlistSchemaCache(playlist)
  76. })
  77. .then(() => cb())
  78. .catch(next);
  79. },
  80. next
  81. );
  82. }
  83. ],
  84. async err => {
  85. if (err) {
  86. const formattedErr = await UtilsModule.runJob("GET_ERROR", {
  87. error: err
  88. });
  89. reject(new Error(formattedErr));
  90. } else {
  91. resolve();
  92. // PlaylistsModule.runJob("CREATE_MISSING_GENRE_PLAYLISTS", {})
  93. // .then()
  94. // .catch()
  95. // .finally(() => {
  96. // SongsModule.runJob("GET_ALL_GENRES", {})
  97. // .then(response => {
  98. // const { genres } = response;
  99. // genres.forEach(genre => {
  100. // PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre }).then().catch();
  101. // });
  102. // })
  103. // .catch();
  104. // });
  105. }
  106. }
  107. )
  108. );
  109. }
  110. /**
  111. * Creates a playlist that is not generated or editable by a user e.g. liked songs playlist
  112. *
  113. * @param {object} payload - object that contains the payload
  114. * @param {string} payload.userId - the id of the user to create the playlist for
  115. * @param {string} payload.displayName - the display name of the playlist
  116. * @returns {Promise} - returns promise (reject, resolve)
  117. */
  118. CREATE_READ_ONLY_PLAYLIST(payload) {
  119. return new Promise((resolve, reject) => {
  120. PlaylistsModule.playlistModel.create(
  121. {
  122. isUserModifiable: false,
  123. displayName: payload.displayName,
  124. songs: [],
  125. createdBy: payload.userId,
  126. createdAt: Date.now(),
  127. createdFor: null,
  128. type: payload.type
  129. },
  130. (err, playlist) => {
  131. if (err) return reject(new Error(err));
  132. return resolve(playlist._id);
  133. }
  134. );
  135. });
  136. }
  137. /**
  138. * Creates a playlist that contains all songs of a specific genre
  139. *
  140. * @param {object} payload - object that contains the payload
  141. * @param {string} payload.genre - the genre
  142. * @returns {Promise} - returns promise (reject, resolve)
  143. */
  144. CREATE_GENRE_PLAYLIST(payload) {
  145. return new Promise((resolve, reject) => {
  146. PlaylistsModule.runJob("GET_GENRE_PLAYLIST", { genre: payload.genre.toLowerCase() }, this)
  147. .then(() => {
  148. reject(new Error("Playlist already exists"));
  149. })
  150. .catch(err => {
  151. if (err.message === "Playlist not found") {
  152. PlaylistsModule.playlistModel.create(
  153. {
  154. isUserModifiable: false,
  155. displayName: `Genre - ${payload.genre}`,
  156. songs: [],
  157. createdBy: "Musare",
  158. createdFor: `${payload.genre.toLowerCase()}`,
  159. createdAt: Date.now(),
  160. type: "genre"
  161. },
  162. (err, playlist) => {
  163. if (err) return reject(new Error(err));
  164. return resolve(playlist._id);
  165. }
  166. );
  167. } else reject(new Error(err));
  168. });
  169. });
  170. }
  171. /**
  172. * Gets all genre playlists
  173. *
  174. * @param {object} payload - object that contains the payload
  175. * @param {string} payload.includeSongs - include the songs
  176. * @returns {Promise} - returns promise (reject, resolve)
  177. */
  178. GET_ALL_GENRE_PLAYLISTS(payload) {
  179. return new Promise((resolve, reject) => {
  180. const includeObject = payload.includeSongs ? null : { songs: false };
  181. PlaylistsModule.playlistModel.find({ type: "genre" }, includeObject, (err, playlists) => {
  182. if (err) reject(new Error(err));
  183. else resolve({ playlists });
  184. });
  185. });
  186. }
  187. /**
  188. * Gets all station playlists
  189. *
  190. * @param {object} payload - object that contains the payload
  191. * @param {string} payload.includeSongs - include the songs
  192. * @returns {Promise} - returns promise (reject, resolve)
  193. */
  194. GET_ALL_STATION_PLAYLISTS(payload) {
  195. return new Promise((resolve, reject) => {
  196. const includeObject = payload.includeSongs ? null : { songs: false };
  197. PlaylistsModule.playlistModel.find({ type: "station" }, includeObject, (err, playlists) => {
  198. if (err) reject(new Error(err));
  199. else resolve({ playlists });
  200. });
  201. });
  202. }
  203. /**
  204. * Gets a genre playlist
  205. *
  206. * @param {object} payload - object that contains the payload
  207. * @param {string} payload.genre - the genre
  208. * @param {string} payload.includeSongs - include the songs
  209. * @returns {Promise} - returns promise (reject, resolve)
  210. */
  211. GET_GENRE_PLAYLIST(payload) {
  212. return new Promise((resolve, reject) => {
  213. const includeObject = payload.includeSongs ? null : { songs: false };
  214. PlaylistsModule.playlistModel.findOne(
  215. { type: "genre", createdFor: payload.genre },
  216. includeObject,
  217. (err, playlist) => {
  218. if (err) reject(new Error(err));
  219. else if (!playlist) reject(new Error("Playlist not found"));
  220. else resolve({ playlist });
  221. }
  222. );
  223. });
  224. }
  225. /**
  226. * Gets all missing genre playlists
  227. *
  228. * @returns {Promise} - returns promise (reject, resolve)
  229. */
  230. GET_MISSING_GENRE_PLAYLISTS() {
  231. return new Promise((resolve, reject) => {
  232. SongsModule.runJob("GET_ALL_GENRES", {}, this)
  233. .then(response => {
  234. const { genres } = response;
  235. const missingGenres = [];
  236. async.eachLimit(
  237. genres,
  238. 1,
  239. (genre, next) => {
  240. PlaylistsModule.runJob(
  241. "GET_GENRE_PLAYLIST",
  242. { genre: genre.toLowerCase(), includeSongs: false },
  243. this
  244. )
  245. .then(() => {
  246. next();
  247. })
  248. .catch(err => {
  249. if (err.message === "Playlist not found") {
  250. missingGenres.push(genre);
  251. next();
  252. } else next(err);
  253. });
  254. },
  255. err => {
  256. if (err) reject(err);
  257. else resolve({ genres: missingGenres });
  258. }
  259. );
  260. })
  261. .catch(err => {
  262. reject(err);
  263. });
  264. });
  265. }
  266. /**
  267. * Creates all missing genre playlists
  268. *
  269. * @returns {Promise} - returns promise (reject, resolve)
  270. */
  271. CREATE_MISSING_GENRE_PLAYLISTS() {
  272. return new Promise((resolve, reject) => {
  273. PlaylistsModule.runJob("GET_MISSING_GENRE_PLAYLISTS", {}, this)
  274. .then(response => {
  275. const { genres } = response;
  276. async.eachLimit(
  277. genres,
  278. 1,
  279. (genre, next) => {
  280. PlaylistsModule.runJob("CREATE_GENRE_PLAYLIST", { genre }, this)
  281. .then(() => {
  282. next();
  283. })
  284. .catch(err => {
  285. next(err);
  286. });
  287. },
  288. err => {
  289. if (err) reject(err);
  290. else resolve();
  291. }
  292. );
  293. })
  294. .catch(err => {
  295. reject(err);
  296. });
  297. });
  298. }
  299. /**
  300. * Gets a station playlist
  301. *
  302. * @param {object} payload - object that contains the payload
  303. * @param {string} payload.staationId - the station id
  304. * @param {string} payload.includeSongs - include the songs
  305. * @returns {Promise} - returns promise (reject, resolve)
  306. */
  307. GET_STATION_PLAYLIST(payload) {
  308. return new Promise((resolve, reject) => {
  309. const includeObject = payload.includeSongs ? null : { songs: false };
  310. PlaylistsModule.playlistModel.findOne(
  311. { type: "station", createdFor: payload.stationId },
  312. includeObject,
  313. (err, playlist) => {
  314. if (err) reject(new Error(err));
  315. else if (!playlist) reject(new Error("Playlist not found"));
  316. else resolve({ playlist });
  317. }
  318. );
  319. });
  320. }
  321. /**
  322. * Adds a song to a playlist
  323. *
  324. * @param {object} payload - object that contains the payload
  325. * @param {string} payload.playlistId - the playlist id
  326. * @param {string} payload.song - the song
  327. * @returns {Promise} - returns promise (reject, resolve)
  328. */
  329. ADD_SONG_TO_PLAYLIST(payload) {
  330. return new Promise((resolve, reject) => {
  331. const { _id, youtubeId, title, artists, thumbnail, duration, status } = payload.song;
  332. const trimmedSong = {
  333. _id,
  334. youtubeId,
  335. title,
  336. artists,
  337. thumbnail,
  338. duration,
  339. status
  340. };
  341. PlaylistsModule.playlistModel.updateOne(
  342. { _id: payload.playlistId },
  343. { $push: { songs: trimmedSong } },
  344. { runValidators: true },
  345. err => {
  346. if (err) reject(new Error(err));
  347. else {
  348. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: payload.playlistId }, this)
  349. .then(() => resolve())
  350. .catch(err => {
  351. reject(new Error(err));
  352. });
  353. }
  354. }
  355. );
  356. });
  357. }
  358. /**
  359. * Deletes a song from a playlist based on the youtube id
  360. *
  361. * @param {object} payload - object that contains the payload
  362. * @param {string} payload.playlistId - the playlist id
  363. * @param {string} payload.youtubeId - the youtube id
  364. * @returns {Promise} - returns promise (reject, resolve)
  365. */
  366. DELETE_SONG_FROM_PLAYLIST_BY_YOUTUBE_ID(payload) {
  367. return new Promise((resolve, reject) => {
  368. PlaylistsModule.playlistModel.updateOne(
  369. { _id: payload.playlistId },
  370. { $pull: { songs: { youtubeId: payload.youtubeId } } },
  371. err => {
  372. if (err) reject(new Error(err));
  373. else {
  374. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: payload.playlistId }, this)
  375. .then(() => resolve())
  376. .catch(err => {
  377. reject(new Error(err));
  378. });
  379. }
  380. }
  381. );
  382. });
  383. }
  384. /**
  385. * Fills a genre playlist with songs
  386. *
  387. * @param {object} payload - object that contains the payload
  388. * @param {string} payload.genre - the genre
  389. * @returns {Promise} - returns promise (reject, resolve)
  390. */
  391. AUTOFILL_GENRE_PLAYLIST(payload) {
  392. return new Promise((resolve, reject) => {
  393. async.waterfall(
  394. [
  395. next => {
  396. PlaylistsModule.runJob(
  397. "GET_GENRE_PLAYLIST",
  398. { genre: payload.genre.toLowerCase(), includeSongs: true },
  399. this
  400. )
  401. .then(response => {
  402. next(null, response.playlist._id);
  403. })
  404. .catch(err => {
  405. if (err.message === "Playlist not found") {
  406. PlaylistsModule.runJob("CREATE_GENRE_PLAYLIST", { genre: payload.genre }, this)
  407. .then(playlistId => {
  408. next(null, playlistId);
  409. })
  410. .catch(err => {
  411. next(err);
  412. });
  413. } else next(err);
  414. });
  415. },
  416. (playlistId, next) => {
  417. SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: payload.genre }, this)
  418. .then(response => {
  419. next(null, playlistId, response.songs);
  420. })
  421. .catch(err => {
  422. console.log(err);
  423. next(err);
  424. });
  425. },
  426. (playlistId, _songs, next) => {
  427. const songs = _songs.map(song => {
  428. const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
  429. return {
  430. _id,
  431. youtubeId,
  432. title,
  433. artists,
  434. thumbnail,
  435. duration,
  436. status
  437. };
  438. });
  439. PlaylistsModule.playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, err => {
  440. next(err, playlistId);
  441. });
  442. },
  443. (playlistId, next) => {
  444. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  445. .then(() => {
  446. next(null, playlistId);
  447. })
  448. .catch(next);
  449. },
  450. // (data, next) => {
  451. // data.songsToDelete = [];
  452. // data.songsToAdd = [];
  453. // data.playlist.songs.forEach(playlistSong => {
  454. // const found = data.songs.find(song => playlistSong.youtubeId === song.youtubeId);
  455. // if (!found) data.songsToDelete.push(playlistSong);
  456. // });
  457. // data.songs.forEach(song => {
  458. // const found = data.playlist.songs.find(playlistSong => song.youtubeId === playlistSong.youtubeId);
  459. // if (!found) data.songsToAdd.push(song);
  460. // });
  461. // next(null, data);
  462. // },
  463. // (data, next) => {
  464. // const promises = [];
  465. // data.songsToAdd.forEach(song => {
  466. // promises.push(
  467. // PlaylistsModule.runJob(
  468. // "ADD_SONG_TO_PLAYLIST",
  469. // { playlistId: data.playlist._id, song },
  470. // this
  471. // )
  472. // );
  473. // });
  474. // data.songsToDelete.forEach(song => {
  475. // promises.push(
  476. // PlaylistsModule.runJob(
  477. // "DELETE_SONG_FROM_PLAYLIST_BY_YOUTUBE_ID",
  478. // {
  479. // playlistId: data.playlist._id,
  480. // youtubeId: song.youtubeId
  481. // },
  482. // this
  483. // )
  484. // );
  485. // });
  486. // Promise.allSettled(promises)
  487. // .then(() => {
  488. // next(null, data.playlist._id);
  489. // })
  490. // .catch(err => {
  491. // next(err);
  492. // });
  493. // },
  494. (playlistId, next) => {
  495. StationsModule.runJob("GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST", { playlistId })
  496. .then(response => {
  497. response.stationIds.forEach(stationId => {
  498. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }).then().catch();
  499. });
  500. })
  501. .catch();
  502. next();
  503. }
  504. ],
  505. err => {
  506. if (err && err !== true) return reject(new Error(err));
  507. return resolve({});
  508. }
  509. );
  510. });
  511. }
  512. /**
  513. * Gets orphaned genre playlists
  514. *
  515. * @returns {Promise} - returns promise (reject, resolve)
  516. */
  517. GET_ORPHANED_GENRE_PLAYLISTS() {
  518. return new Promise((resolve, reject) => {
  519. PlaylistsModule.playlistModel.find({ type: "genre" }, { songs: false }, (err, playlists) => {
  520. if (err) reject(new Error(err));
  521. else {
  522. const orphanedPlaylists = [];
  523. async.eachLimit(
  524. playlists,
  525. 1,
  526. (playlist, next) => {
  527. SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: playlist.createdFor }, this)
  528. .then(response => {
  529. if (response.songs.length === 0) {
  530. StationsModule.runJob(
  531. "GET_STATIONS_THAT_INCLUDE_OR_EXCLUDE_PLAYLIST",
  532. { playlistId: playlist._id },
  533. this
  534. )
  535. .then(response => {
  536. if (response.stationIds.length === 0) orphanedPlaylists.push(playlist);
  537. next();
  538. })
  539. .catch(next);
  540. } else next();
  541. })
  542. .catch(next);
  543. },
  544. err => {
  545. if (err) reject(new Error(err));
  546. else resolve({ playlists: orphanedPlaylists });
  547. }
  548. );
  549. }
  550. });
  551. });
  552. }
  553. /**
  554. * Deletes all orphaned genre playlists
  555. *
  556. * @returns {Promise} - returns promise (reject, resolve)
  557. */
  558. DELETE_ORPHANED_GENRE_PLAYLISTS() {
  559. return new Promise((resolve, reject) => {
  560. PlaylistsModule.runJob("GET_ORPHANED_GENRE_PLAYLISTS", {}, this)
  561. .then(response => {
  562. async.eachLimit(
  563. response.playlists,
  564. 1,
  565. (playlist, next) => {
  566. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  567. .then(() => {
  568. this.log("INFO", "Deleting orphaned genre playlist");
  569. next();
  570. })
  571. .catch(err => {
  572. next(err);
  573. });
  574. },
  575. err => {
  576. if (err) reject(new Error(err));
  577. else resolve({});
  578. }
  579. );
  580. })
  581. .catch(err => {
  582. reject(new Error(err));
  583. });
  584. });
  585. }
  586. /**
  587. * Gets a orphaned station playlists
  588. *
  589. * @returns {Promise} - returns promise (reject, resolve)
  590. */
  591. GET_ORPHANED_STATION_PLAYLISTS() {
  592. return new Promise((resolve, reject) => {
  593. PlaylistsModule.playlistModel.find({ type: "station" }, { songs: false }, (err, playlists) => {
  594. if (err) reject(new Error(err));
  595. else {
  596. const orphanedPlaylists = [];
  597. async.eachLimit(
  598. playlists,
  599. 1,
  600. (playlist, next) => {
  601. StationsModule.runJob("GET_STATION", { stationId: playlist.createdFor }, this)
  602. .then(station => {
  603. if (station.playlist !== playlist._id.toString()) {
  604. orphanedPlaylists.push(playlist);
  605. }
  606. next();
  607. })
  608. .catch(err => {
  609. if (err.message === "Station not found") {
  610. orphanedPlaylists.push(playlist);
  611. next();
  612. } else next(err);
  613. });
  614. },
  615. err => {
  616. if (err) reject(new Error(err));
  617. else resolve({ playlists: orphanedPlaylists });
  618. }
  619. );
  620. }
  621. });
  622. });
  623. }
  624. /**
  625. * Deletes all orphaned station playlists
  626. *
  627. * @returns {Promise} - returns promise (reject, resolve)
  628. */
  629. DELETE_ORPHANED_STATION_PLAYLISTS() {
  630. return new Promise((resolve, reject) => {
  631. PlaylistsModule.runJob("GET_ORPHANED_STATION_PLAYLISTS", {}, this)
  632. .then(response => {
  633. async.eachLimit(
  634. response.playlists,
  635. 1,
  636. (playlist, next) => {
  637. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  638. .then(() => {
  639. this.log("INFO", "Deleting orphaned station playlist");
  640. next();
  641. })
  642. .catch(err => {
  643. next(err);
  644. });
  645. },
  646. err => {
  647. if (err) reject(new Error(err));
  648. else resolve({});
  649. }
  650. );
  651. })
  652. .catch(err => {
  653. reject(new Error(err));
  654. });
  655. });
  656. }
  657. /**
  658. * Fills a station playlist with songs
  659. *
  660. * @param {object} payload - object that contains the payload
  661. * @param {string} payload.stationId - the station id
  662. * @returns {Promise} - returns promise (reject, resolve)
  663. */
  664. AUTOFILL_STATION_PLAYLIST(payload) {
  665. return new Promise((resolve, reject) => {
  666. let originalPlaylist = null;
  667. async.waterfall(
  668. [
  669. next => {
  670. if (!payload.stationId) next("Please specify a station id");
  671. else next();
  672. },
  673. next => {
  674. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  675. .then(station => {
  676. next(null, station);
  677. })
  678. .catch(next);
  679. },
  680. (station, next) => {
  681. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: station.playlist }, this)
  682. .then(playlist => {
  683. originalPlaylist = playlist;
  684. next(null, station);
  685. })
  686. .catch(err => {
  687. next(err);
  688. });
  689. },
  690. (station, next) => {
  691. const includedPlaylists = [];
  692. async.eachLimit(
  693. station.includedPlaylists,
  694. 1,
  695. (playlistId, next) => {
  696. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  697. .then(playlist => {
  698. includedPlaylists.push(playlist);
  699. next();
  700. })
  701. .catch(next);
  702. },
  703. err => {
  704. next(err, station, includedPlaylists);
  705. }
  706. );
  707. },
  708. (station, includedPlaylists, next) => {
  709. const excludedPlaylists = [];
  710. async.eachLimit(
  711. station.excludedPlaylists,
  712. 1,
  713. (playlistId, next) => {
  714. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  715. .then(playlist => {
  716. excludedPlaylists.push(playlist);
  717. next();
  718. })
  719. .catch(next);
  720. },
  721. err => {
  722. next(err, station, includedPlaylists, excludedPlaylists);
  723. }
  724. );
  725. },
  726. (station, includedPlaylists, excludedPlaylists, next) => {
  727. const excludedSongs = excludedPlaylists
  728. .flatMap(excludedPlaylist => excludedPlaylist.songs)
  729. .reduce(
  730. (items, item) =>
  731. items.find(x => x.youtubeId === item.youtubeId) ? [...items] : [...items, item],
  732. []
  733. );
  734. const includedSongs = includedPlaylists
  735. .flatMap(includedPlaylist => includedPlaylist.songs)
  736. .reduce(
  737. (songs, song) =>
  738. songs.find(x => x.youtubeId === song.youtubeId) ? [...songs] : [...songs, song],
  739. []
  740. )
  741. .filter(song => !excludedSongs.find(x => x.youtubeId === song.youtubeId));
  742. next(null, station, includedSongs);
  743. },
  744. (station, includedSongs, next) => {
  745. PlaylistsModule.playlistModel.updateOne(
  746. { _id: station.playlist },
  747. { $set: { songs: includedSongs } },
  748. err => {
  749. next(err, includedSongs);
  750. }
  751. );
  752. },
  753. (includedSongs, next) => {
  754. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: originalPlaylist._id }, this)
  755. .then(() => {
  756. next(null, includedSongs);
  757. })
  758. .catch(next);
  759. },
  760. (includedSongs, next) => {
  761. if (originalPlaylist.songs.length === 0 && includedSongs.length > 0)
  762. StationsModule.runJob("SKIP_STATION", { stationId: payload.stationId });
  763. next();
  764. }
  765. ],
  766. err => {
  767. if (err && err !== true) return reject(new Error(err));
  768. return resolve({});
  769. }
  770. );
  771. });
  772. }
  773. /**
  774. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  775. *
  776. * @param {object} payload - object that contains the payload
  777. * @param {string} payload.playlistId - the id of the playlist we are trying to get
  778. * @returns {Promise} - returns promise (reject, resolve)
  779. */
  780. GET_PLAYLIST(payload) {
  781. return new Promise((resolve, reject) =>
  782. async.waterfall(
  783. [
  784. next => {
  785. CacheModule.runJob(
  786. "HGET",
  787. {
  788. table: "playlists",
  789. key: payload.playlistId
  790. },
  791. this
  792. )
  793. .then(playlist => next(null, playlist))
  794. .catch(next);
  795. },
  796. (playlist, next) => {
  797. if (playlist)
  798. PlaylistsModule.playlistModel.exists({ _id: payload.playlistId }, (err, exists) => {
  799. if (err) next(err);
  800. else if (exists) next(null, playlist);
  801. else {
  802. CacheModule.runJob(
  803. "HDEL",
  804. {
  805. table: "playlists",
  806. key: payload.playlistId
  807. },
  808. this
  809. )
  810. .then(() => next())
  811. .catch(next);
  812. }
  813. });
  814. else PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  815. },
  816. (playlist, next) => {
  817. if (playlist) {
  818. CacheModule.runJob(
  819. "HSET",
  820. {
  821. table: "playlists",
  822. key: payload.playlistId,
  823. value: playlist
  824. },
  825. this
  826. )
  827. .then(playlist => {
  828. next(null, playlist);
  829. })
  830. .catch(next);
  831. } else next("Playlist not found");
  832. }
  833. ],
  834. (err, playlist) => {
  835. if (err && err !== true) return reject(new Error(err));
  836. return resolve(playlist);
  837. }
  838. )
  839. );
  840. }
  841. /**
  842. * Gets a playlist from id from Mongo and updates the cache with it
  843. *
  844. * @param {object} payload - object that contains the payload
  845. * @param {string} payload.playlistId - the id of the playlist we are trying to update
  846. * @returns {Promise} - returns promise (reject, resolve)
  847. */
  848. UPDATE_PLAYLIST(payload) {
  849. return new Promise((resolve, reject) =>
  850. async.waterfall(
  851. [
  852. next => {
  853. PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  854. },
  855. (playlist, next) => {
  856. if (!playlist) {
  857. CacheModule.runJob("HDEL", {
  858. table: "playlists",
  859. key: payload.playlistId
  860. });
  861. return next("Playlist not found");
  862. }
  863. return CacheModule.runJob(
  864. "HSET",
  865. {
  866. table: "playlists",
  867. key: payload.playlistId,
  868. value: playlist
  869. },
  870. this
  871. )
  872. .then(playlist => {
  873. next(null, playlist);
  874. })
  875. .catch(next);
  876. }
  877. ],
  878. (err, playlist) => {
  879. if (err && err !== true) return reject(new Error(err));
  880. return resolve(playlist);
  881. }
  882. )
  883. );
  884. }
  885. /**
  886. * Deletes playlist from id from Mongo and cache
  887. *
  888. * @param {object} payload - object that contains the payload
  889. * @param {string} payload.playlistId - the id of the playlist we are trying to delete
  890. * @returns {Promise} - returns promise (reject, resolve)
  891. */
  892. DELETE_PLAYLIST(payload) {
  893. return new Promise((resolve, reject) =>
  894. async.waterfall(
  895. [
  896. next => {
  897. PlaylistsModule.playlistModel.deleteOne({ _id: payload.playlistId }, next);
  898. },
  899. (res, next) => {
  900. CacheModule.runJob(
  901. "HDEL",
  902. {
  903. table: "playlists",
  904. key: payload.playlistId
  905. },
  906. this
  907. )
  908. .then(() => next())
  909. .catch(next);
  910. }
  911. ],
  912. err => {
  913. if (err && err !== true) return reject(new Error(err));
  914. return resolve();
  915. }
  916. )
  917. );
  918. }
  919. /**
  920. * Clears and refills a station playlist
  921. *
  922. * @param {object} payload - object that contains the payload
  923. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  924. * @returns {Promise} - returns promise (reject, resolve)
  925. */
  926. CLEAR_AND_REFILL_STATION_PLAYLIST(payload) {
  927. return new Promise((resolve, reject) => {
  928. const { playlistId } = payload;
  929. async.waterfall(
  930. [
  931. next => {
  932. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  933. .then(playlist => {
  934. next(null, playlist);
  935. })
  936. .catch(err => {
  937. next(err);
  938. });
  939. },
  940. (playlist, next) => {
  941. if (playlist.type !== "station") next("This playlist is not a station playlist.");
  942. else next(null, playlist.createdFor);
  943. },
  944. (stationId, next) => {
  945. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  946. .then(() => {
  947. next();
  948. })
  949. .catch(err => {
  950. next(err);
  951. });
  952. }
  953. ],
  954. err => {
  955. if (err && err !== true) return reject(new Error(err));
  956. return resolve();
  957. }
  958. );
  959. });
  960. }
  961. /**
  962. * Clears and refills a genre playlist
  963. *
  964. * @param {object} payload - object that contains the payload
  965. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  966. * @returns {Promise} - returns promise (reject, resolve)
  967. */
  968. CLEAR_AND_REFILL_GENRE_PLAYLIST(payload) {
  969. return new Promise((resolve, reject) => {
  970. const { playlistId } = payload;
  971. async.waterfall(
  972. [
  973. next => {
  974. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  975. .then(playlist => {
  976. next(null, playlist);
  977. })
  978. .catch(err => {
  979. next(err);
  980. });
  981. },
  982. (playlist, next) => {
  983. if (playlist.type !== "genre") next("This playlist is not a genre playlist.");
  984. else next(null, playlist.createdFor);
  985. },
  986. (genre, next) => {
  987. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre }, this)
  988. .then(() => {
  989. next();
  990. })
  991. .catch(err => {
  992. next(err);
  993. });
  994. }
  995. ],
  996. err => {
  997. if (err && err !== true) return reject(new Error(err));
  998. return resolve();
  999. }
  1000. );
  1001. });
  1002. }
  1003. }
  1004. export default new _PlaylistsModule();