playlists.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  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. let MediaModule;
  10. let WSModule;
  11. class _PlaylistsModule extends CoreClass {
  12. // eslint-disable-next-line require-jsdoc
  13. constructor() {
  14. super("playlists");
  15. PlaylistsModule = this;
  16. }
  17. /**
  18. * Initialises the playlists module
  19. *
  20. * @returns {Promise} - returns promise (reject, resolve)
  21. */
  22. async initialize() {
  23. this.setStage(1);
  24. StationsModule = this.moduleManager.modules.stations;
  25. CacheModule = this.moduleManager.modules.cache;
  26. DBModule = this.moduleManager.modules.db;
  27. UtilsModule = this.moduleManager.modules.utils;
  28. SongsModule = this.moduleManager.modules.songs;
  29. MediaModule = this.moduleManager.modules.media;
  30. WSModule = this.moduleManager.modules.ws;
  31. this.playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" });
  32. this.playlistSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "playlist" });
  33. CacheModule.runJob("SUB", {
  34. channel: "playlist.updated",
  35. cb: async data => {
  36. PlaylistsModule.playlistModel.findOne(
  37. { _id: data.playlistId },
  38. ["_id", "displayName", "type", "privacy", "songs", "createdBy", "createdAt", "createdFor"],
  39. (err, playlist) => {
  40. const newPlaylist = {
  41. ...playlist._doc,
  42. songsCount: playlist.songs.length,
  43. songsLength: playlist.songs.reduce(
  44. (previous, current) => ({
  45. duration: previous.duration + current.duration
  46. }),
  47. { duration: 0 }
  48. ).duration
  49. };
  50. delete newPlaylist.songs;
  51. WSModule.runJob("EMIT_TO_ROOM", {
  52. room: "admin.playlists",
  53. args: ["event:admin.playlist.updated", { data: { playlist: newPlaylist } }]
  54. });
  55. }
  56. );
  57. }
  58. });
  59. this.setStage(2);
  60. return new Promise((resolve, reject) => {
  61. async.waterfall(
  62. [
  63. next => {
  64. this.setStage(3);
  65. CacheModule.runJob("HGETALL", { table: "playlists" })
  66. .then(playlists => {
  67. next(null, playlists);
  68. })
  69. .catch(next);
  70. },
  71. (playlists, next) => {
  72. this.setStage(4);
  73. if (!playlists) return next();
  74. const playlistIds = Object.keys(playlists);
  75. return async.each(
  76. playlistIds,
  77. (playlistId, next) => {
  78. PlaylistsModule.playlistModel.findOne({ _id: playlistId }, (err, playlist) => {
  79. if (err) next(err);
  80. else if (!playlist) {
  81. CacheModule.runJob("HDEL", {
  82. table: "playlists",
  83. key: playlistId
  84. })
  85. .then(() => next())
  86. .catch(next);
  87. } else next();
  88. });
  89. },
  90. next
  91. );
  92. },
  93. next => {
  94. this.setStage(5);
  95. PlaylistsModule.playlistModel.find({}, next);
  96. },
  97. (playlists, next) => {
  98. this.setStage(6);
  99. async.each(
  100. playlists,
  101. (playlist, cb) => {
  102. CacheModule.runJob("HSET", {
  103. table: "playlists",
  104. key: playlist._id,
  105. value: PlaylistsModule.playlistSchemaCache(playlist)
  106. })
  107. .then(() => cb())
  108. .catch(next);
  109. },
  110. next
  111. );
  112. }
  113. ],
  114. async err => {
  115. if (err) {
  116. const formattedErr = await UtilsModule.runJob("GET_ERROR", {
  117. error: err
  118. });
  119. reject(new Error(formattedErr));
  120. } else {
  121. resolve();
  122. }
  123. }
  124. );
  125. });
  126. }
  127. /**
  128. * Returns a list of playlists that include a specific song
  129. *
  130. * @param {object} payload - object that contains the payload
  131. * @param {string} payload.songId - the song id
  132. * @param {string} payload.includeSongs - include the songs
  133. * @returns {Promise} - returns promise (reject, resolve)
  134. */
  135. GET_PLAYLISTS_WITH_SONG(payload) {
  136. return new Promise((resolve, reject) => {
  137. const includeObject = payload.includeSongs ? null : { songs: false };
  138. PlaylistsModule.playlistModel.find({ "songs._id": payload.songId }, includeObject, (err, playlists) => {
  139. if (err) reject(err);
  140. else resolve({ playlists });
  141. });
  142. });
  143. }
  144. /**
  145. * Creates a playlist owned by a user
  146. *
  147. * @param {object} payload - object that contains the payload
  148. * @param {string} payload.userId - the id of the user to create the playlist for
  149. * @param {string} payload.displayName - the display name of the playlist
  150. * @param {string} payload.type - the type of the playlist
  151. * @returns {Promise} - returns promise (reject, resolve)
  152. */
  153. CREATE_USER_PLAYLIST(payload) {
  154. return new Promise((resolve, reject) => {
  155. PlaylistsModule.playlistModel.create(
  156. {
  157. displayName: payload.displayName,
  158. songs: [],
  159. createdBy: payload.userId,
  160. createdAt: Date.now(),
  161. createdFor: null,
  162. type: payload.type
  163. },
  164. (err, playlist) => {
  165. if (err) return reject(new Error(err));
  166. return resolve(playlist._id);
  167. }
  168. );
  169. });
  170. }
  171. /**
  172. * Creates a playlist that contains all songs of a specific genre
  173. *
  174. * @param {object} payload - object that contains the payload
  175. * @param {string} payload.genre - the genre
  176. * @returns {Promise} - returns promise (reject, resolve)
  177. */
  178. CREATE_GENRE_PLAYLIST(payload) {
  179. return new Promise((resolve, reject) => {
  180. PlaylistsModule.runJob("GET_GENRE_PLAYLIST", { genre: payload.genre.toLowerCase() }, this)
  181. .then(() => {
  182. reject(new Error("Playlist already exists"));
  183. })
  184. .catch(err => {
  185. if (err.message === "Playlist not found") {
  186. PlaylistsModule.playlistModel.create(
  187. {
  188. displayName: `Genre - ${payload.genre}`,
  189. songs: [],
  190. createdBy: "Musare",
  191. createdFor: `${payload.genre.toLowerCase()}`,
  192. createdAt: Date.now(),
  193. type: "genre"
  194. },
  195. (err, playlist) => {
  196. if (err) return reject(new Error(err));
  197. return resolve(playlist._id);
  198. }
  199. );
  200. } else reject(new Error(err));
  201. });
  202. });
  203. }
  204. /**
  205. * Creates a playlist that contains all songs of a specific artist
  206. *
  207. * @param {object} payload - object that contains the payload
  208. * @param {string} payload.artist - the artist
  209. * @returns {Promise} - returns promise (reject, resolve)
  210. */
  211. CREATE_ARTIST_PLAYLIST(payload) {
  212. return new Promise((resolve, reject) => {
  213. PlaylistsModule.runJob("GET_ARTIST_PLAYLIST", { artist: payload.artist.toLowerCase() }, this)
  214. .then(() => {
  215. reject(new Error("Playlist already exists"));
  216. })
  217. .catch(err => {
  218. if (err.message === "Playlist not found") {
  219. PlaylistsModule.playlistModel.create(
  220. {
  221. isUserModifiable: false,
  222. displayName: `Artist - ${payload.artist}`,
  223. songs: [],
  224. createdBy: "Musare",
  225. createdFor: `${payload.artist.toLowerCase()}`,
  226. createdAt: Date.now(),
  227. type: "artist",
  228. privacy: "public"
  229. },
  230. (err, playlist) => {
  231. if (err) return reject(new Error(err));
  232. return resolve(playlist._id);
  233. }
  234. );
  235. } else reject(new Error(err));
  236. });
  237. });
  238. }
  239. /**
  240. * Gets all genre playlists
  241. *
  242. * @param {object} payload - object that contains the payload
  243. * @param {string} payload.includeSongs - include the songs
  244. * @returns {Promise} - returns promise (reject, resolve)
  245. */
  246. GET_ALL_GENRE_PLAYLISTS(payload) {
  247. return new Promise((resolve, reject) => {
  248. const includeObject = payload.includeSongs ? null : { songs: false };
  249. PlaylistsModule.playlistModel.find({ type: "genre" }, includeObject, (err, playlists) => {
  250. if (err) reject(new Error(err));
  251. else resolve({ playlists });
  252. });
  253. });
  254. }
  255. /**
  256. * Gets all artist playlists
  257. *
  258. * @param {object} payload - object that contains the payload
  259. * @param {string} payload.includeSongs - include the songs
  260. * @returns {Promise} - returns promise (reject, resolve)
  261. */
  262. GET_ALL_ARTIST_PLAYLISTS(payload) {
  263. return new Promise((resolve, reject) => {
  264. const includeObject = payload.includeSongs ? null : { songs: false };
  265. PlaylistsModule.playlistModel.find({ type: "artist" }, includeObject, (err, playlists) => {
  266. if (err) reject(new Error(err));
  267. else resolve({ playlists });
  268. });
  269. });
  270. }
  271. /**
  272. * Gets all station playlists
  273. *
  274. * @param {object} payload - object that contains the payload
  275. * @param {string} payload.includeSongs - include the songs
  276. * @returns {Promise} - returns promise (reject, resolve)
  277. */
  278. GET_ALL_STATION_PLAYLISTS(payload) {
  279. return new Promise((resolve, reject) => {
  280. const includeObject = payload.includeSongs ? null : { songs: false };
  281. PlaylistsModule.playlistModel.find({ type: "station" }, includeObject, (err, playlists) => {
  282. if (err) reject(new Error(err));
  283. else resolve({ playlists });
  284. });
  285. });
  286. }
  287. /**
  288. * Gets a genre playlist
  289. *
  290. * @param {object} payload - object that contains the payload
  291. * @param {string} payload.genre - the genre
  292. * @param {string} payload.includeSongs - include the songs
  293. * @returns {Promise} - returns promise (reject, resolve)
  294. */
  295. GET_GENRE_PLAYLIST(payload) {
  296. return new Promise((resolve, reject) => {
  297. const includeObject = payload.includeSongs ? null : { songs: false };
  298. PlaylistsModule.playlistModel.findOne(
  299. { type: "genre", createdFor: payload.genre },
  300. includeObject,
  301. (err, playlist) => {
  302. if (err) reject(new Error(err));
  303. else if (!playlist) reject(new Error("Playlist not found"));
  304. else resolve({ playlist });
  305. }
  306. );
  307. });
  308. }
  309. /**
  310. * Gets all missing genre playlists
  311. *
  312. * @returns {Promise} - returns promise (reject, resolve)
  313. */
  314. GET_MISSING_GENRE_PLAYLISTS() {
  315. return new Promise((resolve, reject) => {
  316. SongsModule.runJob("GET_ALL_GENRES", {}, this)
  317. .then(response => {
  318. const { genres } = response;
  319. const missingGenres = [];
  320. async.eachLimit(
  321. genres,
  322. 1,
  323. (genre, next) => {
  324. PlaylistsModule.runJob(
  325. "GET_GENRE_PLAYLIST",
  326. { genre: genre.toLowerCase(), includeSongs: false },
  327. this
  328. )
  329. .then(() => {
  330. next();
  331. })
  332. .catch(err => {
  333. if (err.message === "Playlist not found") {
  334. missingGenres.push(genre);
  335. next();
  336. } else next(err);
  337. });
  338. },
  339. err => {
  340. if (err) reject(err);
  341. else resolve({ genres: missingGenres });
  342. }
  343. );
  344. })
  345. .catch(err => {
  346. reject(err);
  347. });
  348. });
  349. }
  350. /**
  351. * Creates all missing genre playlists
  352. *
  353. * @returns {Promise} - returns promise (reject, resolve)
  354. */
  355. CREATE_MISSING_GENRE_PLAYLISTS() {
  356. return new Promise((resolve, reject) => {
  357. PlaylistsModule.runJob("GET_MISSING_GENRE_PLAYLISTS", {}, this)
  358. .then(response => {
  359. const { genres } = response;
  360. async.eachLimit(
  361. genres,
  362. 1,
  363. (genre, next) => {
  364. PlaylistsModule.runJob("CREATE_GENRE_PLAYLIST", { genre }, this)
  365. .then(() => {
  366. next();
  367. })
  368. .catch(err => {
  369. next(err);
  370. });
  371. },
  372. err => {
  373. if (err) reject(err);
  374. else resolve();
  375. }
  376. );
  377. })
  378. .catch(err => {
  379. reject(err);
  380. });
  381. });
  382. }
  383. /**
  384. * Gets a artist playlist
  385. *
  386. * @param {object} payload - object that contains the payload
  387. * @param {string} payload.artist - the artist
  388. * @param {string} payload.includeSongs - include the songs
  389. * @returns {Promise} - returns promise (reject, resolve)
  390. */
  391. GET_ARTIST_PLAYLIST(payload) {
  392. return new Promise((resolve, reject) => {
  393. const includeObject = payload.includeSongs ? null : { songs: false };
  394. PlaylistsModule.playlistModel.findOne(
  395. { type: "artist", createdFor: payload.artist },
  396. includeObject,
  397. (err, playlist) => {
  398. if (err) reject(new Error(err));
  399. else if (!playlist) reject(new Error("Playlist not found"));
  400. else resolve({ playlist });
  401. }
  402. );
  403. });
  404. }
  405. /**
  406. * Gets all missing artist playlists
  407. *
  408. * @returns {Promise} - returns promise (reject, resolve)
  409. */
  410. GET_MISSING_ARTIST_PLAYLISTS() {
  411. return new Promise((resolve, reject) => {
  412. SongsModule.runJob("GET_ALL_ARTISTS", {}, this)
  413. .then(response => {
  414. const { artists } = response;
  415. const missingArtists = [];
  416. async.eachLimit(
  417. artists,
  418. 1,
  419. (artist, next) => {
  420. PlaylistsModule.runJob(
  421. "GET_ARTIST_PLAYLIST",
  422. { artist: artist.toLowerCase(), includeSongs: false },
  423. this
  424. )
  425. .then(() => {
  426. next();
  427. })
  428. .catch(err => {
  429. if (err.message === "Playlist not found") {
  430. missingArtists.push(artist);
  431. next();
  432. } else next(err);
  433. });
  434. },
  435. err => {
  436. if (err) reject(err);
  437. else resolve({ artists: missingArtists });
  438. }
  439. );
  440. })
  441. .catch(err => {
  442. reject(err);
  443. });
  444. });
  445. }
  446. /**
  447. * Creates all missing artist playlists
  448. *
  449. * @returns {Promise} - returns promise (reject, resolve)
  450. */
  451. CREATE_MISSING_ARTIST_PLAYLISTS() {
  452. return new Promise((resolve, reject) => {
  453. PlaylistsModule.runJob("GET_MISSING_ARTIST_PLAYLISTS", {}, this)
  454. .then(response => {
  455. const { artists } = response;
  456. async.eachLimit(
  457. artists,
  458. 1,
  459. (artist, next) => {
  460. PlaylistsModule.runJob("CREATE_ARTIST_PLAYLIST", { artist }, this)
  461. .then(() => {
  462. next();
  463. })
  464. .catch(err => {
  465. next(err);
  466. });
  467. },
  468. err => {
  469. if (err) reject(err);
  470. else resolve();
  471. }
  472. );
  473. })
  474. .catch(err => {
  475. reject(err);
  476. });
  477. });
  478. }
  479. /**
  480. * Gets a station playlist
  481. *
  482. * @param {object} payload - object that contains the payload
  483. * @param {string} payload.staationId - the station id
  484. * @param {string} payload.includeSongs - include the songs
  485. * @returns {Promise} - returns promise (reject, resolve)
  486. */
  487. GET_STATION_PLAYLIST(payload) {
  488. return new Promise((resolve, reject) => {
  489. const includeObject = payload.includeSongs ? null : { songs: false };
  490. PlaylistsModule.playlistModel.findOne(
  491. { type: "station", createdFor: payload.stationId },
  492. includeObject,
  493. (err, playlist) => {
  494. if (err) reject(new Error(err));
  495. else if (!playlist) reject(new Error("Playlist not found"));
  496. else resolve({ playlist });
  497. }
  498. );
  499. });
  500. }
  501. /**
  502. * Adds a song to a playlist
  503. *
  504. * @param {object} payload - object that contains the payload
  505. * @param {string} payload.playlistId - the playlist id
  506. * @param {string} payload.youtubeId - the youtube id
  507. * @returns {Promise} - returns promise (reject, resolve)
  508. */
  509. ADD_SONG_TO_PLAYLIST(payload) {
  510. return new Promise((resolve, reject) => {
  511. const { playlistId, youtubeId } = payload;
  512. async.waterfall(
  513. [
  514. next => {
  515. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  516. .then(playlist => {
  517. next(null, playlist);
  518. })
  519. .catch(next);
  520. },
  521. (playlist, next) => {
  522. if (!playlist) return next("Playlist not found.");
  523. if (playlist.songs.find(song => song.youtubeId === youtubeId))
  524. return next("That song is already in the playlist.");
  525. return next();
  526. },
  527. next => {
  528. MediaModule.runJob("GET_MEDIA", { youtubeId }, this)
  529. .then(response => {
  530. const { song } = response;
  531. const { _id, title, artists, thumbnail, duration, verified } = song;
  532. next(null, {
  533. _id,
  534. youtubeId,
  535. title,
  536. artists,
  537. thumbnail,
  538. duration,
  539. verified
  540. });
  541. })
  542. .catch(next);
  543. },
  544. (newSong, next) => {
  545. PlaylistsModule.playlistModel.updateOne(
  546. { _id: playlistId },
  547. { $push: { songs: newSong } },
  548. { runValidators: true },
  549. err => {
  550. if (err) return next(err);
  551. return PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  552. .then(playlist => next(null, playlist, newSong))
  553. .catch(next);
  554. }
  555. );
  556. },
  557. (playlist, newSong, next) => {
  558. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId })
  559. .then(response => {
  560. async.each(
  561. response.stationIds,
  562. (stationId, next) => {
  563. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId })
  564. .then()
  565. .catch();
  566. next();
  567. },
  568. err => {
  569. if (err) next(err);
  570. else next(null, playlist, newSong);
  571. }
  572. );
  573. })
  574. .catch(next);
  575. },
  576. (playlist, newSong, next) => {
  577. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  578. MediaModule.runJob("RECALCULATE_RATINGS", {
  579. youtubeId: newSong.youtubeId
  580. })
  581. .then(ratings => next(null, playlist, newSong, ratings))
  582. .catch(next);
  583. } else {
  584. next(null, playlist, newSong, null);
  585. }
  586. }
  587. ],
  588. (err, playlist, song, ratings) => {
  589. if (err) reject(err);
  590. else resolve({ playlist, song, ratings });
  591. }
  592. );
  593. });
  594. }
  595. /**
  596. * Remove from playlist
  597. *
  598. * @param {object} payload - object that contains the payload
  599. * @param {string} payload.playlistId - the playlist id
  600. * @param {string} payload.youtubeId - the youtube id
  601. * @returns {Promise} - returns a promise (resolve, reject)
  602. */
  603. REMOVE_FROM_PLAYLIST(payload) {
  604. return new Promise((resolve, reject) => {
  605. const { playlistId, youtubeId } = payload;
  606. async.waterfall(
  607. [
  608. next => {
  609. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  610. .then(playlist => {
  611. next(null, playlist);
  612. })
  613. .catch(next);
  614. },
  615. (playlist, next) => {
  616. if (!playlist) return next("Playlist not found.");
  617. if (!playlist.songs.find(song => song.youtubeId === youtubeId))
  618. return next("That song is not currently in the playlist.");
  619. return PlaylistsModule.playlistModel.updateOne(
  620. { _id: playlistId },
  621. { $pull: { songs: { youtubeId } } },
  622. next
  623. );
  624. },
  625. (res, next) => {
  626. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  627. .then(playlist => next(null, playlist))
  628. .catch(next);
  629. },
  630. (playlist, next) => {
  631. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId })
  632. .then(response => {
  633. async.each(
  634. response.stationIds,
  635. (stationId, next) => {
  636. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId })
  637. .then()
  638. .catch();
  639. next();
  640. },
  641. err => {
  642. if (err) next(err);
  643. else next(null, playlist);
  644. }
  645. );
  646. })
  647. .catch(next);
  648. },
  649. (playlist, next) => {
  650. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  651. MediaModule.runJob("RECALCULATE_RATINGS", { youtubeId })
  652. .then(ratings => next(null, playlist, ratings))
  653. .catch(next);
  654. } else next(null, playlist, null);
  655. },
  656. (playlist, ratings, next) =>
  657. CacheModule.runJob(
  658. "PUB",
  659. {
  660. channel: "playlist.updated",
  661. value: { playlistId }
  662. },
  663. this
  664. )
  665. .then(() => next(null, playlist, ratings))
  666. .catch(next)
  667. ],
  668. (err, playlist, ratings) => {
  669. if (err) reject(err);
  670. else resolve({ playlist, ratings });
  671. }
  672. );
  673. });
  674. }
  675. /**
  676. * Deletes a song from a playlist based on the youtube id
  677. *
  678. * @param {object} payload - object that contains the payload
  679. * @param {string} payload.playlistId - the playlist id
  680. * @param {string} payload.youtubeId - the youtube id
  681. * @returns {Promise} - returns promise (reject, resolve)
  682. */
  683. DELETE_SONG_FROM_PLAYLIST_BY_YOUTUBE_ID(payload) {
  684. return new Promise((resolve, reject) => {
  685. PlaylistsModule.playlistModel.updateOne(
  686. { _id: payload.playlistId },
  687. { $pull: { songs: { youtubeId: payload.youtubeId } } },
  688. err => {
  689. if (err) reject(new Error(err));
  690. else {
  691. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: payload.playlistId }, this)
  692. .then(() => resolve())
  693. .catch(err => {
  694. reject(new Error(err));
  695. });
  696. }
  697. }
  698. );
  699. });
  700. }
  701. /**
  702. * Fills a genre playlist with songs
  703. *
  704. * @param {object} payload - object that contains the payload
  705. * @param {string} payload.genre - the genre
  706. * @param {string} payload.createPlaylist - create playlist if it doesn't exist, default false
  707. * @returns {Promise} - returns promise (reject, resolve)
  708. */
  709. AUTOFILL_GENRE_PLAYLIST(payload) {
  710. return new Promise((resolve, reject) => {
  711. async.waterfall(
  712. [
  713. next => {
  714. PlaylistsModule.runJob(
  715. "GET_GENRE_PLAYLIST",
  716. { genre: payload.genre.toLowerCase(), includeSongs: true },
  717. this
  718. )
  719. .then(response => {
  720. next(null, response.playlist._id);
  721. })
  722. .catch(err => {
  723. if (err.message === "Playlist not found") {
  724. if (payload.createPlaylist)
  725. PlaylistsModule.runJob("CREATE_GENRE_PLAYLIST", { genre: payload.genre }, this)
  726. .then(playlistId => {
  727. next(null, playlistId);
  728. })
  729. .catch(err => {
  730. next(err);
  731. });
  732. } else next(err);
  733. });
  734. },
  735. (playlistId, next) => {
  736. SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: payload.genre }, this)
  737. .then(response => {
  738. next(null, playlistId, response.songs);
  739. })
  740. .catch(err => {
  741. console.log(err);
  742. next(err);
  743. });
  744. },
  745. (playlistId, _songs, next) => {
  746. const songs = _songs.map(song => {
  747. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  748. return {
  749. _id,
  750. youtubeId,
  751. title,
  752. artists,
  753. thumbnail,
  754. duration,
  755. verified
  756. };
  757. });
  758. PlaylistsModule.playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, err => {
  759. next(err, playlistId);
  760. });
  761. },
  762. (playlistId, next) => {
  763. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  764. .then(() => {
  765. next(null, playlistId);
  766. })
  767. .catch(next);
  768. },
  769. (playlistId, next) => {
  770. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId }, this)
  771. .then(response => {
  772. async.eachLimit(
  773. response.stationIds,
  774. 1,
  775. (stationId, next) => {
  776. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  777. .then(() => {
  778. next();
  779. })
  780. .catch(err => {
  781. next(err);
  782. });
  783. },
  784. err => {
  785. if (err) next(err);
  786. else next();
  787. }
  788. );
  789. })
  790. .catch(err => {
  791. next(err);
  792. });
  793. }
  794. ],
  795. err => {
  796. if (err && err !== true) return reject(new Error(err));
  797. return resolve({});
  798. }
  799. );
  800. });
  801. }
  802. /**
  803. * Gets orphaned genre playlists
  804. *
  805. * @returns {Promise} - returns promise (reject, resolve)
  806. */
  807. GET_ORPHANED_GENRE_PLAYLISTS() {
  808. return new Promise((resolve, reject) => {
  809. PlaylistsModule.playlistModel.find({ type: "genre" }, { songs: false }, (err, playlists) => {
  810. if (err) reject(new Error(err));
  811. else {
  812. const orphanedPlaylists = [];
  813. async.eachLimit(
  814. playlists,
  815. 1,
  816. (playlist, next) => {
  817. SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: playlist.createdFor }, this)
  818. .then(response => {
  819. if (response.songs.length === 0) {
  820. StationsModule.runJob(
  821. "GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST",
  822. { playlistId: playlist._id },
  823. this
  824. )
  825. .then(response => {
  826. if (response.stationIds.length === 0) orphanedPlaylists.push(playlist);
  827. next();
  828. })
  829. .catch(next);
  830. } else next();
  831. })
  832. .catch(next);
  833. },
  834. err => {
  835. if (err) reject(new Error(err));
  836. else resolve({ playlists: orphanedPlaylists });
  837. }
  838. );
  839. }
  840. });
  841. });
  842. }
  843. /**
  844. * Deletes all orphaned genre playlists
  845. *
  846. * @returns {Promise} - returns promise (reject, resolve)
  847. */
  848. DELETE_ORPHANED_GENRE_PLAYLISTS() {
  849. return new Promise((resolve, reject) => {
  850. PlaylistsModule.runJob("GET_ORPHANED_GENRE_PLAYLISTS", {}, this)
  851. .then(response => {
  852. async.eachLimit(
  853. response.playlists,
  854. 1,
  855. (playlist, next) => {
  856. this.publishProgress({ status: "update", message: `Deleting "${playlist._id}"` });
  857. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  858. .then(() => {
  859. this.log("INFO", "Deleting orphaned genre playlist");
  860. next();
  861. })
  862. .catch(err => {
  863. next(err);
  864. });
  865. },
  866. err => {
  867. if (err) reject(new Error(err));
  868. else resolve({});
  869. }
  870. );
  871. })
  872. .catch(err => {
  873. reject(new Error(err));
  874. });
  875. });
  876. }
  877. /**
  878. * Fills a artist playlist with songs
  879. *
  880. * @param {object} payload - object that contains the payload
  881. * @param {string} payload.artist - the artist
  882. * @returns {Promise} - returns promise (reject, resolve)
  883. */
  884. AUTOFILL_ARTIST_PLAYLIST(payload) {
  885. return new Promise((resolve, reject) => {
  886. async.waterfall(
  887. [
  888. next => {
  889. PlaylistsModule.runJob(
  890. "GET_ARTIST_PLAYLIST",
  891. { artist: payload.artist.toLowerCase(), includeSongs: true },
  892. this
  893. )
  894. .then(response => {
  895. next(null, response.playlist._id);
  896. })
  897. .catch(err => {
  898. if (err.message === "Playlist not found") {
  899. PlaylistsModule.runJob("CREATE_ARTIST_PLAYLIST", { artist: payload.artist }, this)
  900. .then(playlistId => {
  901. next(null, playlistId);
  902. })
  903. .catch(err => {
  904. next(err);
  905. });
  906. } else next(err);
  907. });
  908. },
  909. (playlistId, next) => {
  910. SongsModule.runJob("GET_ALL_SONGS_WITH_ARTIST", { artist: payload.artist }, this)
  911. .then(response => {
  912. next(null, playlistId, response.songs);
  913. })
  914. .catch(err => {
  915. console.log(err);
  916. next(err);
  917. });
  918. },
  919. (playlistId, _songs, next) => {
  920. const songs = _songs.map(song => {
  921. const { _id, youtubeId, title, artists, thumbnail, duration, status } = song;
  922. return {
  923. _id,
  924. youtubeId,
  925. title,
  926. artists,
  927. thumbnail,
  928. duration,
  929. status
  930. };
  931. });
  932. PlaylistsModule.playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, err => {
  933. next(err, playlistId);
  934. });
  935. },
  936. (playlistId, next) => {
  937. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  938. .then(() => {
  939. next(null, playlistId);
  940. })
  941. .catch(next);
  942. },
  943. (playlistId, next) => {
  944. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId }, this)
  945. .then(response => {
  946. async.eachLimit(
  947. response.stationIds,
  948. 1,
  949. (stationId, next) => {
  950. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  951. .then(() => {
  952. next();
  953. })
  954. .catch(err => {
  955. next(err);
  956. });
  957. },
  958. err => {
  959. if (err) next(err);
  960. else next();
  961. }
  962. );
  963. })
  964. .catch(err => {
  965. next(err);
  966. });
  967. }
  968. ],
  969. err => {
  970. if (err && err !== true) return reject(new Error(err));
  971. return resolve({});
  972. }
  973. );
  974. });
  975. }
  976. /**
  977. * Gets orphaned artist playlists
  978. *
  979. * @returns {Promise} - returns promise (reject, resolve)
  980. */
  981. GET_ORPHANED_ARTIST_PLAYLISTS() {
  982. return new Promise((resolve, reject) => {
  983. PlaylistsModule.playlistModel.find({ type: "artist" }, { songs: false }, (err, playlists) => {
  984. if (err) reject(new Error(err));
  985. else {
  986. const orphanedPlaylists = [];
  987. async.eachLimit(
  988. playlists,
  989. 1,
  990. (playlist, next) => {
  991. SongsModule.runJob("GET_ALL_SONGS_WITH_ARTIST", { artist: playlist.createdFor }, this)
  992. .then(response => {
  993. if (response.songs.length === 0) {
  994. StationsModule.runJob(
  995. "GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST",
  996. { playlistId: playlist._id },
  997. this
  998. )
  999. .then(response => {
  1000. if (response.stationIds.length === 0) orphanedPlaylists.push(playlist);
  1001. next();
  1002. })
  1003. .catch(next);
  1004. } else next();
  1005. })
  1006. .catch(next);
  1007. },
  1008. err => {
  1009. if (err) reject(new Error(err));
  1010. else resolve({ playlists: orphanedPlaylists });
  1011. }
  1012. );
  1013. }
  1014. });
  1015. });
  1016. }
  1017. /**
  1018. * Deletes all orphaned artist playlists
  1019. *
  1020. * @returns {Promise} - returns promise (reject, resolve)
  1021. */
  1022. DELETE_ORPHANED_ARTIST_PLAYLISTS() {
  1023. return new Promise((resolve, reject) => {
  1024. PlaylistsModule.runJob("GET_ORPHANED_ARTIST_PLAYLISTS", {}, this)
  1025. .then(response => {
  1026. async.eachLimit(
  1027. response.playlists,
  1028. 1,
  1029. (playlist, next) => {
  1030. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  1031. .then(() => {
  1032. this.log("INFO", "Deleting orphaned artist playlist");
  1033. next();
  1034. })
  1035. .catch(err => {
  1036. next(err);
  1037. });
  1038. },
  1039. err => {
  1040. if (err) reject(new Error(err));
  1041. else resolve({});
  1042. }
  1043. );
  1044. })
  1045. .catch(err => {
  1046. reject(new Error(err));
  1047. });
  1048. });
  1049. }
  1050. /**
  1051. * Gets a orphaned station playlists
  1052. *
  1053. * @returns {Promise} - returns promise (reject, resolve)
  1054. */
  1055. GET_ORPHANED_STATION_PLAYLISTS() {
  1056. return new Promise((resolve, reject) => {
  1057. PlaylistsModule.playlistModel.find({ type: "station" }, { songs: false }, (err, playlists) => {
  1058. if (err) reject(new Error(err));
  1059. else {
  1060. const orphanedPlaylists = [];
  1061. async.eachLimit(
  1062. playlists,
  1063. 1,
  1064. (playlist, next) => {
  1065. StationsModule.runJob("GET_STATION", { stationId: playlist.createdFor }, this)
  1066. .then(station => {
  1067. if (station.playlist !== playlist._id.toString()) {
  1068. orphanedPlaylists.push(playlist);
  1069. }
  1070. next();
  1071. })
  1072. .catch(err => {
  1073. if (err.message === "Station not found") {
  1074. orphanedPlaylists.push(playlist);
  1075. next();
  1076. } else next(err);
  1077. });
  1078. },
  1079. err => {
  1080. if (err) reject(new Error(err));
  1081. else resolve({ playlists: orphanedPlaylists });
  1082. }
  1083. );
  1084. }
  1085. });
  1086. });
  1087. }
  1088. /**
  1089. * Deletes all orphaned station playlists
  1090. *
  1091. * @returns {Promise} - returns promise (reject, resolve)
  1092. */
  1093. DELETE_ORPHANED_STATION_PLAYLISTS() {
  1094. return new Promise((resolve, reject) => {
  1095. PlaylistsModule.runJob("GET_ORPHANED_STATION_PLAYLISTS", {}, this)
  1096. .then(response => {
  1097. async.eachLimit(
  1098. response.playlists,
  1099. 1,
  1100. (playlist, next) => {
  1101. this.publishProgress({ status: "update", message: `Deleting "${playlist._id}"` });
  1102. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  1103. .then(() => {
  1104. this.log("INFO", "Deleting orphaned station playlist");
  1105. next();
  1106. })
  1107. .catch(err => {
  1108. next(err);
  1109. });
  1110. },
  1111. err => {
  1112. if (err) reject(new Error(err));
  1113. else resolve({});
  1114. }
  1115. );
  1116. })
  1117. .catch(err => {
  1118. reject(new Error(err));
  1119. });
  1120. });
  1121. }
  1122. /**
  1123. * Fills a station playlist with songs
  1124. *
  1125. * @param {object} payload - object that contains the payload
  1126. * @param {string} payload.stationId - the station id
  1127. * @returns {Promise} - returns promise (reject, resolve)
  1128. */
  1129. AUTOFILL_STATION_PLAYLIST(payload) {
  1130. return new Promise((resolve, reject) => {
  1131. let originalPlaylist = null;
  1132. async.waterfall(
  1133. [
  1134. next => {
  1135. if (!payload.stationId) next("Please specify a station id");
  1136. else next();
  1137. },
  1138. next => {
  1139. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  1140. .then(station => {
  1141. next(null, station);
  1142. })
  1143. .catch(next);
  1144. },
  1145. (station, next) => {
  1146. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: station.playlist }, this)
  1147. .then(playlist => {
  1148. originalPlaylist = playlist;
  1149. next(null, station);
  1150. })
  1151. .catch(err => {
  1152. next(err);
  1153. });
  1154. },
  1155. (station, next) => {
  1156. const playlists = [];
  1157. async.eachLimit(
  1158. station.autofill.playlists,
  1159. 1,
  1160. (playlistId, next) => {
  1161. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1162. .then(playlist => {
  1163. playlists.push(playlist);
  1164. next();
  1165. })
  1166. .catch(next);
  1167. },
  1168. err => {
  1169. next(err, station, playlists);
  1170. }
  1171. );
  1172. },
  1173. (station, playlists, next) => {
  1174. const blacklist = [];
  1175. async.eachLimit(
  1176. station.blacklist,
  1177. 1,
  1178. (playlistId, next) => {
  1179. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1180. .then(playlist => {
  1181. blacklist.push(playlist);
  1182. next();
  1183. })
  1184. .catch(next);
  1185. },
  1186. err => {
  1187. next(err, station, playlists, blacklist);
  1188. }
  1189. );
  1190. },
  1191. (station, playlists, blacklist, next) => {
  1192. const blacklistedSongs = blacklist
  1193. .flatMap(blacklistedPlaylist => blacklistedPlaylist.songs)
  1194. .reduce(
  1195. (items, item) =>
  1196. items.find(x => x.youtubeId === item.youtubeId) ? [...items] : [...items, item],
  1197. []
  1198. );
  1199. const includedSongs = playlists
  1200. .flatMap(playlist => playlist.songs)
  1201. .reduce(
  1202. (songs, song) =>
  1203. songs.find(x => x.youtubeId === song.youtubeId) ? [...songs] : [...songs, song],
  1204. []
  1205. )
  1206. .filter(song => !blacklistedSongs.find(x => x.youtubeId === song.youtubeId));
  1207. next(null, station, includedSongs);
  1208. },
  1209. (station, includedSongs, next) => {
  1210. PlaylistsModule.playlistModel.updateOne(
  1211. { _id: station.playlist },
  1212. { $set: { songs: includedSongs } },
  1213. err => {
  1214. next(err, includedSongs);
  1215. }
  1216. );
  1217. },
  1218. (includedSongs, next) => {
  1219. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: originalPlaylist._id }, this)
  1220. .then(() => {
  1221. next(null, includedSongs);
  1222. })
  1223. .catch(next);
  1224. },
  1225. (includedSongs, next) => {
  1226. if (originalPlaylist.songs.length === 0 && includedSongs.length > 0)
  1227. StationsModule.runJob("SKIP_STATION", { stationId: payload.stationId, natural: false });
  1228. next();
  1229. }
  1230. ],
  1231. err => {
  1232. if (err && err !== true) return reject(new Error(err));
  1233. return resolve({});
  1234. }
  1235. );
  1236. });
  1237. }
  1238. /**
  1239. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  1240. *
  1241. * @param {object} payload - object that contains the payload
  1242. * @param {string} payload.playlistId - the id of the playlist we are trying to get
  1243. * @returns {Promise} - returns promise (reject, resolve)
  1244. */
  1245. GET_PLAYLIST(payload) {
  1246. return new Promise((resolve, reject) => {
  1247. async.waterfall(
  1248. [
  1249. next => {
  1250. CacheModule.runJob(
  1251. "HGET",
  1252. {
  1253. table: "playlists",
  1254. key: payload.playlistId
  1255. },
  1256. this
  1257. )
  1258. .then(playlist => next(null, playlist))
  1259. .catch(next);
  1260. },
  1261. (playlist, next) => {
  1262. if (playlist)
  1263. PlaylistsModule.playlistModel.exists({ _id: payload.playlistId }, (err, exists) => {
  1264. if (err) next(err);
  1265. else if (exists) next(null, playlist);
  1266. else {
  1267. CacheModule.runJob(
  1268. "HDEL",
  1269. {
  1270. table: "playlists",
  1271. key: payload.playlistId
  1272. },
  1273. this
  1274. )
  1275. .then(() => next())
  1276. .catch(next);
  1277. }
  1278. });
  1279. else PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  1280. },
  1281. (playlist, next) => {
  1282. if (playlist) {
  1283. CacheModule.runJob(
  1284. "HSET",
  1285. {
  1286. table: "playlists",
  1287. key: payload.playlistId,
  1288. value: playlist
  1289. },
  1290. this
  1291. )
  1292. .then(playlist => {
  1293. next(null, playlist);
  1294. })
  1295. .catch(next);
  1296. } else next("Playlist not found");
  1297. }
  1298. ],
  1299. (err, playlist) => {
  1300. if (err && err !== true) return reject(new Error(err));
  1301. return resolve(playlist);
  1302. }
  1303. );
  1304. });
  1305. }
  1306. /**
  1307. * Gets a playlist from id from Mongo and updates the cache with it
  1308. *
  1309. * @param {object} payload - object that contains the payload
  1310. * @param {string} payload.playlistId - the id of the playlist we are trying to update
  1311. * @returns {Promise} - returns promise (reject, resolve)
  1312. */
  1313. UPDATE_PLAYLIST(payload) {
  1314. return new Promise((resolve, reject) => {
  1315. async.waterfall(
  1316. [
  1317. next => {
  1318. PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  1319. },
  1320. (playlist, next) => {
  1321. if (!playlist) {
  1322. CacheModule.runJob("HDEL", {
  1323. table: "playlists",
  1324. key: payload.playlistId
  1325. });
  1326. return next("Playlist not found");
  1327. }
  1328. return CacheModule.runJob(
  1329. "HSET",
  1330. {
  1331. table: "playlists",
  1332. key: payload.playlistId,
  1333. value: playlist
  1334. },
  1335. this
  1336. )
  1337. .then(playlist => {
  1338. next(null, playlist);
  1339. })
  1340. .catch(next);
  1341. }
  1342. ],
  1343. (err, playlist) => {
  1344. if (err && err !== true) return reject(new Error(err));
  1345. return resolve(playlist);
  1346. }
  1347. );
  1348. });
  1349. }
  1350. /**
  1351. * Deletes playlist from id from Mongo and cache
  1352. *
  1353. * @param {object} payload - object that contains the payload
  1354. * @param {string} payload.playlistId - the id of the playlist we are trying to delete
  1355. * @returns {Promise} - returns promise (reject, resolve)
  1356. */
  1357. DELETE_PLAYLIST(payload) {
  1358. return new Promise((resolve, reject) => {
  1359. async.waterfall(
  1360. [
  1361. next => {
  1362. PlaylistsModule.playlistModel.deleteOne({ _id: payload.playlistId }, next);
  1363. },
  1364. (res, next) => {
  1365. CacheModule.runJob(
  1366. "HDEL",
  1367. {
  1368. table: "playlists",
  1369. key: payload.playlistId
  1370. },
  1371. this
  1372. )
  1373. .then(() => next())
  1374. .catch(next);
  1375. },
  1376. next => {
  1377. StationsModule.runJob(
  1378. "REMOVE_AUTOFILLED_OR_BLACKLISTED_PLAYLIST_FROM_STATIONS",
  1379. { playlistId: payload.playlistId },
  1380. this
  1381. )
  1382. .then(() => {
  1383. next();
  1384. })
  1385. .catch(err => next(err));
  1386. }
  1387. ],
  1388. err => {
  1389. if (err && err !== true) return reject(new Error(err));
  1390. return resolve();
  1391. }
  1392. );
  1393. });
  1394. }
  1395. /**
  1396. * Searches through playlists
  1397. *
  1398. * @param {object} payload - object that contains the payload
  1399. * @param {string} payload.query - the query
  1400. * @param {string} payload.includePrivate - include private playlists
  1401. * @param {string} payload.includeStation - include station playlists
  1402. * @param {string} payload.includeUser - include user playlists
  1403. * @param {string} payload.includeGenre - include genre playlists
  1404. * @param {string} payload.includeArtist - include artist playlists
  1405. * @param {string} payload.includeAdmin - include admin playlists
  1406. * @param {string} payload.includeOwn - include own user playlists
  1407. * @param {string} payload.userId - the user id of the person requesting
  1408. * @param {string} payload.includeSongs - include songs
  1409. * @param {string} payload.page - page (default 1)
  1410. * @returns {Promise} - returns promise (reject, resolve)
  1411. */
  1412. SEARCH(payload) {
  1413. return new Promise((resolve, reject) => {
  1414. async.waterfall(
  1415. [
  1416. next => {
  1417. const types = [];
  1418. if (payload.includeStation) types.push("station");
  1419. if (payload.includeUser) types.push("user");
  1420. if (payload.includeGenre) types.push("genre");
  1421. if (payload.includeArtist) types.push("artist");
  1422. if (payload.includeAdmin) types.push("admin");
  1423. if (types.length === 0 && !payload.includeOwn) return next("No types have been included.");
  1424. const privacies = ["public"];
  1425. if (payload.includePrivate) privacies.push("private");
  1426. const includeObject = payload.includeSongs ? null : { songs: false };
  1427. const filterArray = [
  1428. {
  1429. displayName: new RegExp(`${payload.query}`, "i"),
  1430. privacy: { $in: privacies },
  1431. type: { $in: types }
  1432. }
  1433. ];
  1434. if (payload.includeOwn && payload.userId)
  1435. filterArray.push({
  1436. displayName: new RegExp(`${payload.query}`, "i"),
  1437. type: "user",
  1438. createdBy: payload.userId
  1439. });
  1440. return next(null, filterArray, includeObject);
  1441. },
  1442. (filterArray, includeObject, next) => {
  1443. const page = payload.page ? payload.page : 1;
  1444. const pageSize = 15;
  1445. const skipAmount = pageSize * (page - 1);
  1446. PlaylistsModule.playlistModel.find({ $or: filterArray }).count((err, count) => {
  1447. if (err) next(err);
  1448. else {
  1449. PlaylistsModule.playlistModel
  1450. .find({ $or: filterArray }, includeObject)
  1451. .skip(skipAmount)
  1452. .limit(pageSize)
  1453. .exec((err, playlists) => {
  1454. if (err) next(err);
  1455. else {
  1456. next(null, {
  1457. playlists,
  1458. page,
  1459. pageSize,
  1460. skipAmount,
  1461. count
  1462. });
  1463. }
  1464. });
  1465. }
  1466. });
  1467. },
  1468. (data, next) => {
  1469. if (data.playlists.length > 0) next(null, data);
  1470. else next("No playlists found");
  1471. }
  1472. ],
  1473. (err, data) => {
  1474. if (err && err !== true) return reject(new Error(err));
  1475. return resolve(data);
  1476. }
  1477. );
  1478. });
  1479. }
  1480. /**
  1481. * Clears and refills a station playlist
  1482. *
  1483. * @param {object} payload - object that contains the payload
  1484. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1485. * @returns {Promise} - returns promise (reject, resolve)
  1486. */
  1487. CLEAR_AND_REFILL_STATION_PLAYLIST(payload) {
  1488. return new Promise((resolve, reject) => {
  1489. const { playlistId } = payload;
  1490. async.waterfall(
  1491. [
  1492. next => {
  1493. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1494. .then(playlist => {
  1495. next(null, playlist);
  1496. })
  1497. .catch(err => {
  1498. next(err);
  1499. });
  1500. },
  1501. (playlist, next) => {
  1502. if (playlist.type !== "station") next("This playlist is not a station playlist.");
  1503. else next(null, playlist.createdFor);
  1504. },
  1505. (stationId, next) => {
  1506. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  1507. .then(() => {
  1508. next();
  1509. })
  1510. .catch(err => {
  1511. next(err);
  1512. });
  1513. }
  1514. ],
  1515. err => {
  1516. if (err && err !== true) return reject(new Error(err));
  1517. return resolve();
  1518. }
  1519. );
  1520. });
  1521. }
  1522. /**
  1523. * Clears and refills a genre playlist
  1524. *
  1525. * @param {object} payload - object that contains the payload
  1526. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1527. * @returns {Promise} - returns promise (reject, resolve)
  1528. */
  1529. CLEAR_AND_REFILL_GENRE_PLAYLIST(payload) {
  1530. return new Promise((resolve, reject) => {
  1531. const { playlistId } = payload;
  1532. async.waterfall(
  1533. [
  1534. next => {
  1535. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1536. .then(playlist => {
  1537. next(null, playlist);
  1538. })
  1539. .catch(err => {
  1540. next(err);
  1541. });
  1542. },
  1543. (playlist, next) => {
  1544. if (playlist.type !== "genre") next("This playlist is not a genre playlist.");
  1545. else next(null, playlist.createdFor);
  1546. },
  1547. (genre, next) => {
  1548. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre, createPlaylist: true }, this)
  1549. .then(() => {
  1550. next();
  1551. })
  1552. .catch(err => {
  1553. next(err);
  1554. });
  1555. }
  1556. ],
  1557. err => {
  1558. if (err && err !== true) return reject(new Error(err));
  1559. return resolve();
  1560. }
  1561. );
  1562. });
  1563. }
  1564. /**
  1565. * Clears and refills a artist playlist
  1566. *
  1567. * @param {object} payload - object that contains the payload
  1568. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1569. * @returns {Promise} - returns promise (reject, resolve)
  1570. */
  1571. CLEAR_AND_REFILL_ARTIST_PLAYLIST(payload) {
  1572. return new Promise((resolve, reject) => {
  1573. const { playlistId } = payload;
  1574. async.waterfall(
  1575. [
  1576. next => {
  1577. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1578. .then(playlist => {
  1579. next(null, playlist);
  1580. })
  1581. .catch(err => {
  1582. next(err);
  1583. });
  1584. },
  1585. (playlist, next) => {
  1586. if (playlist.type !== "artist") next("This playlist is not a artist playlist.");
  1587. else next(null, playlist.createdFor);
  1588. },
  1589. (artist, next) => {
  1590. PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist }, this)
  1591. .then(() => {
  1592. next();
  1593. })
  1594. .catch(err => {
  1595. next(err);
  1596. });
  1597. }
  1598. ],
  1599. err => {
  1600. if (err && err !== true) return reject(new Error(err));
  1601. return resolve();
  1602. }
  1603. );
  1604. });
  1605. }
  1606. }
  1607. export default new _PlaylistsModule();