playlists.js 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  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. * Returns a list of youtube ids in all user playlists of the specified user
  146. *
  147. * @param {object} payload - object that contains the payload
  148. * @param {string} payload.userId - the user id
  149. * @returns {Promise} - returns promise (reject, resolve)
  150. */
  151. GET_SONG_YOUTUBE_IDS_FROM_USER_PLAYLISTS(payload) {
  152. return new Promise((resolve, reject) => {
  153. PlaylistsModule.playlistModel.find({ createdBy: payload.userId }, (err, playlists) => {
  154. const youtubeIds = new Set();
  155. if (err) reject(err);
  156. else {
  157. playlists.forEach(playlist => {
  158. playlist.songs.forEach(song => {
  159. youtubeIds.add(song.youtubeId);
  160. });
  161. });
  162. resolve({ youtubeIds: Array.from(youtubeIds) });
  163. }
  164. });
  165. });
  166. }
  167. /**
  168. * Creates a playlist owned by a user
  169. *
  170. * @param {object} payload - object that contains the payload
  171. * @param {string} payload.userId - the id of the user to create the playlist for
  172. * @param {string} payload.displayName - the display name of the playlist
  173. * @param {string} payload.type - the type of the playlist
  174. * @returns {Promise} - returns promise (reject, resolve)
  175. */
  176. CREATE_USER_PLAYLIST(payload) {
  177. return new Promise((resolve, reject) => {
  178. PlaylistsModule.playlistModel.create(
  179. {
  180. displayName: payload.displayName,
  181. songs: [],
  182. createdBy: payload.userId,
  183. createdAt: Date.now(),
  184. createdFor: null,
  185. type: payload.type
  186. },
  187. (err, playlist) => {
  188. if (err) return reject(new Error(err));
  189. return resolve(playlist._id);
  190. }
  191. );
  192. });
  193. }
  194. /**
  195. * Creates a playlist that contains all songs of a specific genre
  196. *
  197. * @param {object} payload - object that contains the payload
  198. * @param {string} payload.genre - the genre
  199. * @returns {Promise} - returns promise (reject, resolve)
  200. */
  201. CREATE_GENRE_PLAYLIST(payload) {
  202. return new Promise((resolve, reject) => {
  203. PlaylistsModule.runJob("GET_GENRE_PLAYLIST", { genre: payload.genre.toLowerCase() }, this)
  204. .then(() => {
  205. reject(new Error("Playlist already exists"));
  206. })
  207. .catch(err => {
  208. if (err.message === "Playlist not found") {
  209. PlaylistsModule.playlistModel.create(
  210. {
  211. displayName: `Genre - ${payload.genre}`,
  212. songs: [],
  213. createdBy: "Musare",
  214. createdFor: `${payload.genre.toLowerCase()}`,
  215. createdAt: Date.now(),
  216. type: "genre"
  217. },
  218. (err, playlist) => {
  219. if (err) return reject(new Error(err));
  220. return resolve(playlist._id);
  221. }
  222. );
  223. } else reject(new Error(err));
  224. });
  225. });
  226. }
  227. /**
  228. * Gets all genre playlists
  229. *
  230. * @param {object} payload - object that contains the payload
  231. * @param {string} payload.includeSongs - include the songs
  232. * @returns {Promise} - returns promise (reject, resolve)
  233. */
  234. GET_ALL_GENRE_PLAYLISTS(payload) {
  235. return new Promise((resolve, reject) => {
  236. const includeObject = payload.includeSongs ? null : { songs: false };
  237. PlaylistsModule.playlistModel.find({ type: "genre" }, includeObject, (err, playlists) => {
  238. if (err) reject(new Error(err));
  239. else resolve({ playlists });
  240. });
  241. });
  242. }
  243. /**
  244. * Gets all station playlists
  245. *
  246. * @param {object} payload - object that contains the payload
  247. * @param {string} payload.includeSongs - include the songs
  248. * @returns {Promise} - returns promise (reject, resolve)
  249. */
  250. GET_ALL_STATION_PLAYLISTS(payload) {
  251. return new Promise((resolve, reject) => {
  252. const includeObject = payload.includeSongs ? null : { songs: false };
  253. PlaylistsModule.playlistModel.find({ type: "station" }, includeObject, (err, playlists) => {
  254. if (err) reject(new Error(err));
  255. else resolve({ playlists });
  256. });
  257. });
  258. }
  259. /**
  260. * Gets a genre playlist
  261. *
  262. * @param {object} payload - object that contains the payload
  263. * @param {string} payload.genre - the genre
  264. * @param {string} payload.includeSongs - include the songs
  265. * @returns {Promise} - returns promise (reject, resolve)
  266. */
  267. GET_GENRE_PLAYLIST(payload) {
  268. return new Promise((resolve, reject) => {
  269. const includeObject = payload.includeSongs ? null : { songs: false };
  270. PlaylistsModule.playlistModel.findOne(
  271. { type: "genre", createdFor: payload.genre },
  272. includeObject,
  273. (err, playlist) => {
  274. if (err) reject(new Error(err));
  275. else if (!playlist) reject(new Error("Playlist not found"));
  276. else resolve({ playlist });
  277. }
  278. );
  279. });
  280. }
  281. /**
  282. * Gets all missing genre playlists
  283. *
  284. * @returns {Promise} - returns promise (reject, resolve)
  285. */
  286. GET_MISSING_GENRE_PLAYLISTS() {
  287. return new Promise((resolve, reject) => {
  288. SongsModule.runJob("GET_ALL_GENRES", {}, this)
  289. .then(response => {
  290. const { genres } = response;
  291. const missingGenres = [];
  292. async.eachLimit(
  293. genres,
  294. 1,
  295. (genre, next) => {
  296. PlaylistsModule.runJob(
  297. "GET_GENRE_PLAYLIST",
  298. { genre: genre.toLowerCase(), includeSongs: false },
  299. this
  300. )
  301. .then(() => {
  302. next();
  303. })
  304. .catch(err => {
  305. if (err.message === "Playlist not found") {
  306. missingGenres.push(genre);
  307. next();
  308. } else next(err);
  309. });
  310. },
  311. err => {
  312. if (err) reject(err);
  313. else resolve({ genres: missingGenres });
  314. }
  315. );
  316. })
  317. .catch(err => {
  318. reject(err);
  319. });
  320. });
  321. }
  322. /**
  323. * Creates all missing genre playlists
  324. *
  325. * @returns {Promise} - returns promise (reject, resolve)
  326. */
  327. CREATE_MISSING_GENRE_PLAYLISTS() {
  328. return new Promise((resolve, reject) => {
  329. PlaylistsModule.runJob("GET_MISSING_GENRE_PLAYLISTS", {}, this)
  330. .then(response => {
  331. const { genres } = response;
  332. async.eachLimit(
  333. genres,
  334. 1,
  335. (genre, next) => {
  336. PlaylistsModule.runJob("CREATE_GENRE_PLAYLIST", { genre }, this)
  337. .then(() => {
  338. next();
  339. })
  340. .catch(err => {
  341. next(err);
  342. });
  343. },
  344. err => {
  345. if (err) reject(err);
  346. else resolve();
  347. }
  348. );
  349. })
  350. .catch(err => {
  351. reject(err);
  352. });
  353. });
  354. }
  355. /**
  356. * Gets a station playlist
  357. *
  358. * @param {object} payload - object that contains the payload
  359. * @param {string} payload.staationId - the station id
  360. * @param {string} payload.includeSongs - include the songs
  361. * @returns {Promise} - returns promise (reject, resolve)
  362. */
  363. GET_STATION_PLAYLIST(payload) {
  364. return new Promise((resolve, reject) => {
  365. const includeObject = payload.includeSongs ? null : { songs: false };
  366. PlaylistsModule.playlistModel.findOne(
  367. { type: "station", createdFor: payload.stationId },
  368. includeObject,
  369. (err, playlist) => {
  370. if (err) reject(new Error(err));
  371. else if (!playlist) reject(new Error("Playlist not found"));
  372. else resolve({ playlist });
  373. }
  374. );
  375. });
  376. }
  377. /**
  378. * Adds a song to a playlist
  379. *
  380. * @param {object} payload - object that contains the payload
  381. * @param {string} payload.playlistId - the playlist id
  382. * @param {string} payload.mediaSource - the media source
  383. * @returns {Promise} - returns promise (reject, resolve)
  384. */
  385. ADD_SONG_TO_PLAYLIST(payload) {
  386. return new Promise((resolve, reject) => {
  387. const { playlistId, mediaSource } = payload;
  388. async.waterfall(
  389. [
  390. next => {
  391. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  392. .then(playlist => {
  393. next(null, playlist);
  394. })
  395. .catch(next);
  396. },
  397. (playlist, next) => {
  398. if (!playlist) return next("Playlist not found.");
  399. if (playlist.songs.find(song => song.mediaSource === mediaSource))
  400. return next("That song is already in the playlist.");
  401. return next();
  402. },
  403. next => {
  404. MediaModule.runJob("GET_MEDIA", { mediaSource }, this)
  405. .then(response => {
  406. const { song } = response;
  407. const { _id, title, artists, thumbnail, duration, verified } = song;
  408. next(null, {
  409. _id,
  410. mediaSource,
  411. title,
  412. artists,
  413. thumbnail,
  414. duration,
  415. verified
  416. });
  417. })
  418. .catch(next);
  419. },
  420. (newSong, next) => {
  421. PlaylistsModule.playlistModel.updateOne(
  422. { _id: playlistId },
  423. { $push: { songs: newSong } },
  424. { runValidators: true },
  425. err => {
  426. if (err) return next(err);
  427. return PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  428. .then(playlist => next(null, playlist, newSong))
  429. .catch(next);
  430. }
  431. );
  432. },
  433. (playlist, newSong, next) => {
  434. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId })
  435. .then(response => {
  436. async.each(
  437. response.stationIds,
  438. (stationId, next) => {
  439. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId })
  440. .then()
  441. .catch();
  442. next();
  443. },
  444. err => {
  445. if (err) next(err);
  446. else next(null, playlist, newSong);
  447. }
  448. );
  449. })
  450. .catch(next);
  451. },
  452. (playlist, newSong, next) => {
  453. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  454. MediaModule.runJob("RECALCULATE_RATINGS", {
  455. mediaSource: newSong.mediaSource
  456. })
  457. .then(ratings => next(null, playlist, newSong, ratings))
  458. .catch(next);
  459. } else {
  460. next(null, playlist, newSong, null);
  461. }
  462. }
  463. ],
  464. (err, playlist, song, ratings) => {
  465. if (err) reject(err);
  466. else resolve({ playlist, song, ratings });
  467. }
  468. );
  469. });
  470. }
  471. /**
  472. * Replaces a song in a playlist
  473. *
  474. * @param {object} payload - object that contains the payload
  475. * @param {string} payload.playlistId - the playlist id
  476. * @param {string} payload.newMediaSource - the new media source
  477. * @param {string} payload.oldMediaSource - the old media source
  478. * @returns {Promise} - returns promise (reject, resolve)
  479. */
  480. REPLACE_SONG_IN_PLAYLIST(payload) {
  481. return new Promise((resolve, reject) => {
  482. const { playlistId, newMediaSource, oldMediaSource } = payload;
  483. async.waterfall(
  484. [
  485. next => {
  486. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  487. .then(playlist => {
  488. next(null, playlist);
  489. })
  490. .catch(next);
  491. },
  492. (playlist, next) => {
  493. if (!playlist) return next("Playlist not found.");
  494. if (playlist.songs.find(song => song.mediaSource === newMediaSource))
  495. return next("The new song is already in the playlist.");
  496. if (!playlist.songs.find(song => song.mediaSource === oldMediaSource))
  497. return next("The old song is not in the playlist.");
  498. return next();
  499. },
  500. next => {
  501. MediaModule.runJob("GET_MEDIA", { mediaSource: newMediaSource }, this)
  502. .then(response => {
  503. const { song } = response;
  504. const { _id, title, artists, thumbnail, duration, verified } = song;
  505. next(null, {
  506. _id,
  507. mediaSource: newMediaSource,
  508. title,
  509. artists,
  510. thumbnail,
  511. duration,
  512. verified
  513. });
  514. })
  515. .catch(next);
  516. },
  517. (newSong, next) => {
  518. PlaylistsModule.playlistModel.updateOne(
  519. { _id: playlistId, "songs.mediaSource": oldMediaSource },
  520. {
  521. $set: { "songs.$": newSong },
  522. $push: { replacements: { oldMediaSource, newMediaSource, replacedAt: new Date() } }
  523. },
  524. { runValidators: true },
  525. err => {
  526. if (err) return next(err);
  527. return PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  528. .then(playlist => next(null, playlist, newSong))
  529. .catch(next);
  530. }
  531. );
  532. },
  533. (playlist, newSong, next) => {
  534. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId })
  535. .then(response => {
  536. async.each(
  537. response.stationIds,
  538. (stationId, next) => {
  539. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId })
  540. .then()
  541. .catch();
  542. next();
  543. },
  544. err => {
  545. if (err) next(err);
  546. else next(null, playlist, newSong);
  547. }
  548. );
  549. })
  550. .catch(next);
  551. },
  552. (playlist, newSong, next) => {
  553. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  554. MediaModule.runJob("RECALCULATE_RATINGS", {
  555. mediaSource: newSong.mediaSource
  556. })
  557. .then(ratings => next(null, playlist, newSong, ratings))
  558. .catch(next);
  559. } else {
  560. next(null, playlist, newSong, null);
  561. }
  562. },
  563. (playlist, newSong, newRatings, next) => {
  564. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  565. MediaModule.runJob("RECALCULATE_RATINGS", {
  566. mediaSource: oldMediaSource
  567. })
  568. .then(oldRatings => next(null, playlist, newSong, newRatings, oldRatings))
  569. .catch(next);
  570. } else {
  571. next(null, playlist, newSong, null, null);
  572. }
  573. }
  574. ],
  575. (err, playlist, song, newRatings, oldRatings) => {
  576. if (err) reject(err);
  577. else resolve({ playlist, song, newRatings, oldRatings });
  578. }
  579. );
  580. });
  581. }
  582. /**
  583. * Remove from playlist
  584. *
  585. * @param {object} payload - object that contains the payload
  586. * @param {string} payload.playlistId - the playlist id
  587. * @param {string} payload.mediaSource - the media source
  588. * @returns {Promise} - returns a promise (resolve, reject)
  589. */
  590. REMOVE_FROM_PLAYLIST(payload) {
  591. return new Promise((resolve, reject) => {
  592. const { playlistId, mediaSource } = payload;
  593. async.waterfall(
  594. [
  595. next => {
  596. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  597. .then(playlist => {
  598. next(null, playlist);
  599. })
  600. .catch(next);
  601. },
  602. (playlist, next) => {
  603. if (!playlist) return next("Playlist not found.");
  604. if (!playlist.songs.find(song => song.mediaSource === mediaSource))
  605. return next("That song is not currently in the playlist.");
  606. return PlaylistsModule.playlistModel.updateOne(
  607. { _id: playlistId },
  608. { $pull: { songs: { mediaSource } } },
  609. next
  610. );
  611. },
  612. (res, next) => {
  613. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  614. .then(playlist => next(null, playlist))
  615. .catch(next);
  616. },
  617. (playlist, next) => {
  618. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId })
  619. .then(response => {
  620. async.each(
  621. response.stationIds,
  622. (stationId, next) => {
  623. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId })
  624. .then()
  625. .catch();
  626. next();
  627. },
  628. err => {
  629. if (err) next(err);
  630. else next(null, playlist);
  631. }
  632. );
  633. })
  634. .catch(next);
  635. },
  636. (playlist, next) => {
  637. if (playlist.type === "user-liked" || playlist.type === "user-disliked") {
  638. MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource })
  639. .then(ratings => next(null, playlist, ratings))
  640. .catch(next);
  641. } else next(null, playlist, null);
  642. },
  643. (playlist, ratings, next) =>
  644. CacheModule.runJob(
  645. "PUB",
  646. {
  647. channel: "playlist.updated",
  648. value: { playlistId }
  649. },
  650. this
  651. )
  652. .then(() => next(null, playlist, ratings))
  653. .catch(next)
  654. ],
  655. (err, playlist, ratings) => {
  656. if (err) reject(err);
  657. else resolve({ playlist, ratings });
  658. }
  659. );
  660. });
  661. }
  662. /**
  663. * Deletes a song from a playlist based on the media source
  664. *
  665. * @param {object} payload - object that contains the payload
  666. * @param {string} payload.playlistId - the playlist id
  667. * @param {string} payload.mediaSource - the media source
  668. * @returns {Promise} - returns promise (reject, resolve)
  669. */
  670. DELETE_SONG_FROM_PLAYLIST_BY_MEDIA_SOURCE_ID(payload) {
  671. return new Promise((resolve, reject) => {
  672. PlaylistsModule.playlistModel.updateOne(
  673. { _id: payload.playlistId },
  674. { $pull: { songs: { mediaSource: payload.mediaSource } } },
  675. err => {
  676. if (err) reject(new Error(err));
  677. else {
  678. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: payload.playlistId }, this)
  679. .then(() => resolve())
  680. .catch(err => {
  681. reject(new Error(err));
  682. });
  683. }
  684. }
  685. );
  686. });
  687. }
  688. /**
  689. * Fills a genre playlist with songs
  690. *
  691. * @param {object} payload - object that contains the payload
  692. * @param {string} payload.genre - the genre
  693. * @param {string} payload.createPlaylist - create playlist if it doesn't exist, default false
  694. * @returns {Promise} - returns promise (reject, resolve)
  695. */
  696. AUTOFILL_GENRE_PLAYLIST(payload) {
  697. return new Promise((resolve, reject) => {
  698. async.waterfall(
  699. [
  700. next => {
  701. PlaylistsModule.runJob(
  702. "GET_GENRE_PLAYLIST",
  703. { genre: payload.genre.toLowerCase(), includeSongs: true },
  704. this
  705. )
  706. .then(response => {
  707. next(null, response.playlist._id);
  708. })
  709. .catch(err => {
  710. if (err.message === "Playlist not found") {
  711. if (payload.createPlaylist)
  712. PlaylistsModule.runJob("CREATE_GENRE_PLAYLIST", { genre: payload.genre }, this)
  713. .then(playlistId => {
  714. next(null, playlistId);
  715. })
  716. .catch(err => {
  717. next(err);
  718. });
  719. } else next(err);
  720. });
  721. },
  722. (playlistId, next) => {
  723. SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: payload.genre }, this)
  724. .then(response => {
  725. next(null, playlistId, response.songs);
  726. })
  727. .catch(err => {
  728. console.log(err);
  729. next(err);
  730. });
  731. },
  732. (playlistId, _songs, next) => {
  733. const songs = _songs.map(song => {
  734. const { _id, mediaSource, title, artists, thumbnail, duration, verified } = song;
  735. return {
  736. _id,
  737. mediaSource,
  738. title,
  739. artists,
  740. thumbnail,
  741. duration,
  742. verified
  743. };
  744. });
  745. PlaylistsModule.playlistModel.updateOne({ _id: playlistId }, { $set: { songs } }, err => {
  746. next(err, playlistId);
  747. });
  748. },
  749. (playlistId, next) => {
  750. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId }, this)
  751. .then(() => {
  752. next(null, playlistId);
  753. })
  754. .catch(next);
  755. },
  756. (playlistId, next) => {
  757. StationsModule.runJob("GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST", { playlistId }, this)
  758. .then(response => {
  759. async.eachLimit(
  760. response.stationIds,
  761. 1,
  762. (stationId, next) => {
  763. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  764. .then(() => {
  765. next();
  766. })
  767. .catch(err => {
  768. next(err);
  769. });
  770. },
  771. err => {
  772. if (err) next(err);
  773. else next();
  774. }
  775. );
  776. })
  777. .catch(err => {
  778. next(err);
  779. });
  780. }
  781. ],
  782. err => {
  783. if (err && err !== true) return reject(new Error(err));
  784. return resolve({});
  785. }
  786. );
  787. });
  788. }
  789. /**
  790. * Gets orphaned genre playlists
  791. *
  792. * @returns {Promise} - returns promise (reject, resolve)
  793. */
  794. GET_ORPHANED_GENRE_PLAYLISTS() {
  795. return new Promise((resolve, reject) => {
  796. PlaylistsModule.playlistModel.find({ type: "genre" }, { songs: false }, (err, playlists) => {
  797. if (err) reject(new Error(err));
  798. else {
  799. const orphanedPlaylists = [];
  800. async.eachLimit(
  801. playlists,
  802. 1,
  803. (playlist, next) => {
  804. SongsModule.runJob("GET_ALL_SONGS_WITH_GENRE", { genre: playlist.createdFor }, this)
  805. .then(response => {
  806. if (response.songs.length === 0) {
  807. StationsModule.runJob(
  808. "GET_STATIONS_THAT_AUTOFILL_OR_BLACKLIST_PLAYLIST",
  809. { playlistId: playlist._id },
  810. this
  811. )
  812. .then(response => {
  813. if (response.stationIds.length === 0) orphanedPlaylists.push(playlist);
  814. next();
  815. })
  816. .catch(next);
  817. } else next();
  818. })
  819. .catch(next);
  820. },
  821. err => {
  822. if (err) reject(new Error(err));
  823. else resolve({ playlists: orphanedPlaylists });
  824. }
  825. );
  826. }
  827. });
  828. });
  829. }
  830. /**
  831. * Deletes all orphaned genre playlists
  832. *
  833. * @returns {Promise} - returns promise (reject, resolve)
  834. */
  835. DELETE_ORPHANED_GENRE_PLAYLISTS() {
  836. return new Promise((resolve, reject) => {
  837. PlaylistsModule.runJob("GET_ORPHANED_GENRE_PLAYLISTS", {}, this)
  838. .then(response => {
  839. async.eachLimit(
  840. response.playlists,
  841. 1,
  842. (playlist, next) => {
  843. this.publishProgress({ status: "update", message: `Deleting "${playlist._id}"` });
  844. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  845. .then(() => {
  846. this.log("INFO", "Deleting orphaned genre playlist");
  847. next();
  848. })
  849. .catch(err => {
  850. next(err);
  851. });
  852. },
  853. err => {
  854. if (err) reject(new Error(err));
  855. else resolve({});
  856. }
  857. );
  858. })
  859. .catch(err => {
  860. reject(new Error(err));
  861. });
  862. });
  863. }
  864. /**
  865. * Gets a orphaned station playlists
  866. *
  867. * @returns {Promise} - returns promise (reject, resolve)
  868. */
  869. GET_ORPHANED_STATION_PLAYLISTS() {
  870. return new Promise((resolve, reject) => {
  871. PlaylistsModule.playlistModel.find({ type: "station" }, { songs: false }, (err, playlists) => {
  872. if (err) reject(new Error(err));
  873. else {
  874. const orphanedPlaylists = [];
  875. async.eachLimit(
  876. playlists,
  877. 1,
  878. (playlist, next) => {
  879. StationsModule.runJob("GET_STATION", { stationId: playlist.createdFor }, this)
  880. .then(station => {
  881. if (station.playlist !== playlist._id.toString()) {
  882. orphanedPlaylists.push(playlist);
  883. }
  884. next();
  885. })
  886. .catch(err => {
  887. if (err.message === "Station not found") {
  888. orphanedPlaylists.push(playlist);
  889. next();
  890. } else next(err);
  891. });
  892. },
  893. err => {
  894. if (err) reject(new Error(err));
  895. else resolve({ playlists: orphanedPlaylists });
  896. }
  897. );
  898. }
  899. });
  900. });
  901. }
  902. /**
  903. * Deletes all orphaned station playlists
  904. *
  905. * @returns {Promise} - returns promise (reject, resolve)
  906. */
  907. DELETE_ORPHANED_STATION_PLAYLISTS() {
  908. return new Promise((resolve, reject) => {
  909. PlaylistsModule.runJob("GET_ORPHANED_STATION_PLAYLISTS", {}, this)
  910. .then(response => {
  911. async.eachLimit(
  912. response.playlists,
  913. 1,
  914. (playlist, next) => {
  915. this.publishProgress({ status: "update", message: `Deleting "${playlist._id}"` });
  916. PlaylistsModule.runJob("DELETE_PLAYLIST", { playlistId: playlist._id }, this)
  917. .then(() => {
  918. this.log("INFO", "Deleting orphaned station playlist");
  919. next();
  920. })
  921. .catch(err => {
  922. next(err);
  923. });
  924. },
  925. err => {
  926. if (err) reject(new Error(err));
  927. else resolve({});
  928. }
  929. );
  930. })
  931. .catch(err => {
  932. reject(new Error(err));
  933. });
  934. });
  935. }
  936. /**
  937. * Fills a station playlist with songs
  938. *
  939. * @param {object} payload - object that contains the payload
  940. * @param {string} payload.stationId - the station id
  941. * @returns {Promise} - returns promise (reject, resolve)
  942. */
  943. AUTOFILL_STATION_PLAYLIST(payload) {
  944. return new Promise((resolve, reject) => {
  945. let originalPlaylist = null;
  946. async.waterfall(
  947. [
  948. next => {
  949. if (!payload.stationId) next("Please specify a station id");
  950. else next();
  951. },
  952. next => {
  953. StationsModule.runJob("GET_STATION", { stationId: payload.stationId }, this)
  954. .then(station => {
  955. next(null, station);
  956. })
  957. .catch(next);
  958. },
  959. (station, next) => {
  960. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId: station.playlist }, this)
  961. .then(playlist => {
  962. originalPlaylist = playlist;
  963. next(null, station);
  964. })
  965. .catch(err => {
  966. next(err);
  967. });
  968. },
  969. (station, next) => {
  970. const playlists = [];
  971. async.eachLimit(
  972. station.autofill.playlists,
  973. 1,
  974. (playlistId, next) => {
  975. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  976. .then(playlist => {
  977. playlists.push(playlist);
  978. next();
  979. })
  980. .catch(next);
  981. },
  982. err => {
  983. next(err, station, playlists);
  984. }
  985. );
  986. },
  987. (station, playlists, next) => {
  988. const blacklist = [];
  989. async.eachLimit(
  990. station.blacklist,
  991. 1,
  992. (playlistId, next) => {
  993. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  994. .then(playlist => {
  995. blacklist.push(playlist);
  996. next();
  997. })
  998. .catch(next);
  999. },
  1000. err => {
  1001. next(err, station, playlists, blacklist);
  1002. }
  1003. );
  1004. },
  1005. (station, playlists, blacklist, next) => {
  1006. const blacklistedSongs = blacklist
  1007. .flatMap(blacklistedPlaylist => blacklistedPlaylist.songs)
  1008. .reduce(
  1009. (items, item) =>
  1010. items.find(x => x.mediaSource === item.mediaSource) ? [...items] : [...items, item],
  1011. []
  1012. );
  1013. const includedSongs = playlists
  1014. .flatMap(playlist => playlist.songs)
  1015. .reduce(
  1016. (songs, song) =>
  1017. songs.find(x => x.mediaSource === song.mediaSource) ? [...songs] : [...songs, song],
  1018. []
  1019. )
  1020. .filter(song => !blacklistedSongs.find(x => x.mediaSource === song.mediaSource));
  1021. next(null, station, includedSongs);
  1022. },
  1023. (station, includedSongs, next) => {
  1024. PlaylistsModule.playlistModel.updateOne(
  1025. { _id: station.playlist },
  1026. { $set: { songs: includedSongs } },
  1027. err => {
  1028. next(err);
  1029. }
  1030. );
  1031. },
  1032. next => {
  1033. PlaylistsModule.runJob("UPDATE_PLAYLIST", { playlistId: originalPlaylist._id }, this)
  1034. .then(() => {
  1035. next();
  1036. })
  1037. .catch(next);
  1038. },
  1039. next => {
  1040. StationsModule.runJob("AUTOFILL_STATION", { stationId: payload.stationId }, this)
  1041. .then(() => next())
  1042. .catch(err => {
  1043. if (err === "Autofill is disabled in this station" || err === "Autofill limit reached")
  1044. return next();
  1045. return next(err);
  1046. });
  1047. },
  1048. next => {
  1049. CacheModule.runJob("PUB", {
  1050. channel: "station.queueUpdate",
  1051. value: payload.stationId
  1052. })
  1053. .then(() => next())
  1054. .catch(next);
  1055. }
  1056. ],
  1057. err => {
  1058. if (err && err !== true) return reject(new Error(err));
  1059. return resolve({});
  1060. }
  1061. );
  1062. });
  1063. }
  1064. /**
  1065. * Gets a playlist by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  1066. *
  1067. * @param {object} payload - object that contains the payload
  1068. * @param {string} payload.playlistId - the id of the playlist we are trying to get
  1069. * @returns {Promise} - returns promise (reject, resolve)
  1070. */
  1071. GET_PLAYLIST(payload) {
  1072. return new Promise((resolve, reject) => {
  1073. async.waterfall(
  1074. [
  1075. next => {
  1076. CacheModule.runJob(
  1077. "HGET",
  1078. {
  1079. table: "playlists",
  1080. key: payload.playlistId
  1081. },
  1082. this
  1083. )
  1084. .then(playlist => next(null, playlist))
  1085. .catch(next);
  1086. },
  1087. (playlist, next) => {
  1088. if (playlist)
  1089. PlaylistsModule.playlistModel.exists({ _id: payload.playlistId }, (err, exists) => {
  1090. if (err) next(err);
  1091. else if (exists) next(null, playlist);
  1092. else {
  1093. CacheModule.runJob(
  1094. "HDEL",
  1095. {
  1096. table: "playlists",
  1097. key: payload.playlistId
  1098. },
  1099. this
  1100. )
  1101. .then(() => next())
  1102. .catch(next);
  1103. }
  1104. });
  1105. else PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  1106. },
  1107. (playlist, next) => {
  1108. if (playlist) {
  1109. CacheModule.runJob(
  1110. "HSET",
  1111. {
  1112. table: "playlists",
  1113. key: payload.playlistId,
  1114. value: playlist
  1115. },
  1116. this
  1117. )
  1118. .then(playlist => {
  1119. next(null, playlist);
  1120. })
  1121. .catch(next);
  1122. } else next("Playlist not found");
  1123. }
  1124. ],
  1125. (err, playlist) => {
  1126. if (err && err !== true) return reject(new Error(err));
  1127. return resolve(playlist);
  1128. }
  1129. );
  1130. });
  1131. }
  1132. /**
  1133. * Gets a playlist from id from Mongo and updates the cache with it
  1134. *
  1135. * @param {object} payload - object that contains the payload
  1136. * @param {string} payload.playlistId - the id of the playlist we are trying to update
  1137. * @returns {Promise} - returns promise (reject, resolve)
  1138. */
  1139. UPDATE_PLAYLIST(payload) {
  1140. return new Promise((resolve, reject) => {
  1141. async.waterfall(
  1142. [
  1143. next => {
  1144. PlaylistsModule.playlistModel.findOne({ _id: payload.playlistId }, next);
  1145. },
  1146. (playlist, next) => {
  1147. if (!playlist) {
  1148. CacheModule.runJob("HDEL", {
  1149. table: "playlists",
  1150. key: payload.playlistId
  1151. });
  1152. return next("Playlist not found");
  1153. }
  1154. return CacheModule.runJob(
  1155. "HSET",
  1156. {
  1157. table: "playlists",
  1158. key: payload.playlistId,
  1159. value: playlist
  1160. },
  1161. this
  1162. )
  1163. .then(playlist => {
  1164. next(null, playlist);
  1165. })
  1166. .catch(next);
  1167. }
  1168. ],
  1169. (err, playlist) => {
  1170. if (err && err !== true) return reject(new Error(err));
  1171. return resolve(playlist);
  1172. }
  1173. );
  1174. });
  1175. }
  1176. /**
  1177. * Deletes playlist from id from Mongo and cache
  1178. *
  1179. * @param {object} payload - object that contains the payload
  1180. * @param {string} payload.playlistId - the id of the playlist we are trying to delete
  1181. * @returns {Promise} - returns promise (reject, resolve)
  1182. */
  1183. DELETE_PLAYLIST(payload) {
  1184. return new Promise((resolve, reject) => {
  1185. async.waterfall(
  1186. [
  1187. next => {
  1188. PlaylistsModule.playlistModel.deleteOne({ _id: payload.playlistId }, next);
  1189. },
  1190. (res, next) => {
  1191. CacheModule.runJob(
  1192. "HDEL",
  1193. {
  1194. table: "playlists",
  1195. key: payload.playlistId
  1196. },
  1197. this
  1198. )
  1199. .then(() => next())
  1200. .catch(next);
  1201. },
  1202. next => {
  1203. StationsModule.runJob(
  1204. "REMOVE_AUTOFILLED_OR_BLACKLISTED_PLAYLIST_FROM_STATIONS",
  1205. { playlistId: payload.playlistId },
  1206. this
  1207. )
  1208. .then(() => {
  1209. next();
  1210. })
  1211. .catch(err => next(err));
  1212. }
  1213. ],
  1214. err => {
  1215. if (err && err !== true) return reject(new Error(err));
  1216. return resolve();
  1217. }
  1218. );
  1219. });
  1220. }
  1221. /**
  1222. * Searches through playlists
  1223. *
  1224. * @param {object} payload - object that contains the payload
  1225. * @param {string} payload.query - the query
  1226. * @param {string} payload.includePrivate - include private playlists
  1227. * @param {string} payload.includeStation - include station playlists
  1228. * @param {string} payload.includeUser - include user playlists
  1229. * @param {string} payload.includeGenre - include genre playlists
  1230. * @param {string} payload.includeAdmin - include admin playlists
  1231. * @param {string} payload.includeOwn - include own user playlists
  1232. * @param {string} payload.userId - the user id of the person requesting
  1233. * @param {string} payload.includeSongs - include songs
  1234. * @param {string} payload.page - page (default 1)
  1235. * @returns {Promise} - returns promise (reject, resolve)
  1236. */
  1237. SEARCH(payload) {
  1238. return new Promise((resolve, reject) => {
  1239. async.waterfall(
  1240. [
  1241. next => {
  1242. const types = [];
  1243. if (payload.includeStation) types.push("station");
  1244. if (payload.includeUser) types.push("user");
  1245. if (payload.includeGenre) types.push("genre");
  1246. if (payload.includeAdmin) types.push("admin");
  1247. if (types.length === 0 && !payload.includeOwn) return next("No types have been included.");
  1248. const privacies = ["public"];
  1249. if (payload.includePrivate) privacies.push("private");
  1250. const includeObject = payload.includeSongs ? null : { songs: false };
  1251. const filterArray = [
  1252. {
  1253. displayName: new RegExp(`${payload.query}`, "i"),
  1254. privacy: { $in: privacies },
  1255. type: { $in: types }
  1256. }
  1257. ];
  1258. if (payload.includeOwn && payload.userId)
  1259. filterArray.push({
  1260. displayName: new RegExp(`${payload.query}`, "i"),
  1261. type: "user",
  1262. createdBy: payload.userId
  1263. });
  1264. return next(null, filterArray, includeObject);
  1265. },
  1266. (filterArray, includeObject, next) => {
  1267. const page = payload.page ? payload.page : 1;
  1268. const pageSize = 15;
  1269. const skipAmount = pageSize * (page - 1);
  1270. PlaylistsModule.playlistModel.find({ $or: filterArray }).count((err, count) => {
  1271. if (err) next(err);
  1272. else {
  1273. PlaylistsModule.playlistModel
  1274. .find({ $or: filterArray }, includeObject)
  1275. .skip(skipAmount)
  1276. .limit(pageSize)
  1277. .exec((err, playlists) => {
  1278. if (err) next(err);
  1279. else {
  1280. next(null, {
  1281. playlists,
  1282. page,
  1283. pageSize,
  1284. skipAmount,
  1285. count
  1286. });
  1287. }
  1288. });
  1289. }
  1290. });
  1291. },
  1292. (data, next) => {
  1293. if (data.playlists.length > 0) next(null, data);
  1294. else next("No playlists found");
  1295. }
  1296. ],
  1297. (err, data) => {
  1298. if (err && err !== true) return reject(new Error(err));
  1299. return resolve(data);
  1300. }
  1301. );
  1302. });
  1303. }
  1304. /**
  1305. * Clears and refills a station playlist
  1306. *
  1307. * @param {object} payload - object that contains the payload
  1308. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1309. * @returns {Promise} - returns promise (reject, resolve)
  1310. */
  1311. CLEAR_AND_REFILL_STATION_PLAYLIST(payload) {
  1312. return new Promise((resolve, reject) => {
  1313. const { playlistId } = payload;
  1314. async.waterfall(
  1315. [
  1316. next => {
  1317. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1318. .then(playlist => {
  1319. next(null, playlist);
  1320. })
  1321. .catch(err => {
  1322. next(err);
  1323. });
  1324. },
  1325. (playlist, next) => {
  1326. if (playlist.type !== "station") next("This playlist is not a station playlist.");
  1327. else next(null, playlist.createdFor);
  1328. },
  1329. (stationId, next) => {
  1330. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  1331. .then(() => {
  1332. next();
  1333. })
  1334. .catch(err => {
  1335. next(err);
  1336. });
  1337. }
  1338. ],
  1339. err => {
  1340. if (err && err !== true) return reject(new Error(err));
  1341. return resolve();
  1342. }
  1343. );
  1344. });
  1345. }
  1346. /**
  1347. * Clears and refills a genre playlist
  1348. *
  1349. * @param {object} payload - object that contains the payload
  1350. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1351. * @returns {Promise} - returns promise (reject, resolve)
  1352. */
  1353. CLEAR_AND_REFILL_GENRE_PLAYLIST(payload) {
  1354. return new Promise((resolve, reject) => {
  1355. const { playlistId } = payload;
  1356. async.waterfall(
  1357. [
  1358. next => {
  1359. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1360. .then(playlist => {
  1361. next(null, playlist);
  1362. })
  1363. .catch(err => {
  1364. next(err);
  1365. });
  1366. },
  1367. (playlist, next) => {
  1368. if (playlist.type !== "genre") next("This playlist is not a genre playlist.");
  1369. else next(null, playlist.createdFor);
  1370. },
  1371. (genre, next) => {
  1372. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre, createPlaylist: true }, this)
  1373. .then(() => {
  1374. next();
  1375. })
  1376. .catch(err => {
  1377. next(err);
  1378. });
  1379. }
  1380. ],
  1381. err => {
  1382. if (err && err !== true) return reject(new Error(err));
  1383. return resolve();
  1384. }
  1385. );
  1386. });
  1387. }
  1388. /**
  1389. * Gets a list of all media sources from playlist songs
  1390. *
  1391. * @returns {Promise} - returns promise (reject, resolve)
  1392. */
  1393. async GET_ALL_MEDIA_SOURCES() {
  1394. return PlaylistsModule.playlistModel.distinct("songs.mediaSource");
  1395. }
  1396. }
  1397. export default new _PlaylistsModule();