playlists.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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.includeOwn - include own user playlists
  1406. * @param {string} payload.userId - the user id of the person requesting
  1407. * @param {string} payload.includeSongs - include songs
  1408. * @param {string} payload.page - page (default 1)
  1409. * @returns {Promise} - returns promise (reject, resolve)
  1410. */
  1411. SEARCH(payload) {
  1412. return new Promise((resolve, reject) => {
  1413. async.waterfall(
  1414. [
  1415. next => {
  1416. const types = [];
  1417. if (payload.includeStation) types.push("station");
  1418. if (payload.includeUser) types.push("user");
  1419. if (payload.includeGenre) types.push("genre");
  1420. if (payload.includeArtist) types.push("artist");
  1421. if (types.length === 0 && !payload.includeOwn) return next("No types have been included.");
  1422. const privacies = ["public"];
  1423. if (payload.includePrivate) privacies.push("private");
  1424. const includeObject = payload.includeSongs ? null : { songs: false };
  1425. const filterArray = [
  1426. {
  1427. displayName: new RegExp(`${payload.query}`, "i"),
  1428. privacy: { $in: privacies },
  1429. type: { $in: types }
  1430. }
  1431. ];
  1432. if (payload.includeOwn && payload.userId)
  1433. filterArray.push({
  1434. displayName: new RegExp(`${payload.query}`, "i"),
  1435. type: "user",
  1436. createdBy: payload.userId
  1437. });
  1438. return next(null, filterArray, includeObject);
  1439. },
  1440. (filterArray, includeObject, next) => {
  1441. const page = payload.page ? payload.page : 1;
  1442. const pageSize = 15;
  1443. const skipAmount = pageSize * (page - 1);
  1444. PlaylistsModule.playlistModel.find({ $or: filterArray }).count((err, count) => {
  1445. if (err) next(err);
  1446. else {
  1447. PlaylistsModule.playlistModel
  1448. .find({ $or: filterArray }, includeObject)
  1449. .skip(skipAmount)
  1450. .limit(pageSize)
  1451. .exec((err, playlists) => {
  1452. if (err) next(err);
  1453. else {
  1454. next(null, {
  1455. playlists,
  1456. page,
  1457. pageSize,
  1458. skipAmount,
  1459. count
  1460. });
  1461. }
  1462. });
  1463. }
  1464. });
  1465. },
  1466. (data, next) => {
  1467. if (data.playlists.length > 0) next(null, data);
  1468. else next("No playlists found");
  1469. }
  1470. ],
  1471. (err, data) => {
  1472. if (err && err !== true) return reject(new Error(err));
  1473. return resolve(data);
  1474. }
  1475. );
  1476. });
  1477. }
  1478. /**
  1479. * Clears and refills a station playlist
  1480. *
  1481. * @param {object} payload - object that contains the payload
  1482. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1483. * @returns {Promise} - returns promise (reject, resolve)
  1484. */
  1485. CLEAR_AND_REFILL_STATION_PLAYLIST(payload) {
  1486. return new Promise((resolve, reject) => {
  1487. const { playlistId } = payload;
  1488. async.waterfall(
  1489. [
  1490. next => {
  1491. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1492. .then(playlist => {
  1493. next(null, playlist);
  1494. })
  1495. .catch(err => {
  1496. next(err);
  1497. });
  1498. },
  1499. (playlist, next) => {
  1500. if (playlist.type !== "station") next("This playlist is not a station playlist.");
  1501. else next(null, playlist.createdFor);
  1502. },
  1503. (stationId, next) => {
  1504. PlaylistsModule.runJob("AUTOFILL_STATION_PLAYLIST", { stationId }, this)
  1505. .then(() => {
  1506. next();
  1507. })
  1508. .catch(err => {
  1509. next(err);
  1510. });
  1511. }
  1512. ],
  1513. err => {
  1514. if (err && err !== true) return reject(new Error(err));
  1515. return resolve();
  1516. }
  1517. );
  1518. });
  1519. }
  1520. /**
  1521. * Clears and refills a genre playlist
  1522. *
  1523. * @param {object} payload - object that contains the payload
  1524. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1525. * @returns {Promise} - returns promise (reject, resolve)
  1526. */
  1527. CLEAR_AND_REFILL_GENRE_PLAYLIST(payload) {
  1528. return new Promise((resolve, reject) => {
  1529. const { playlistId } = payload;
  1530. async.waterfall(
  1531. [
  1532. next => {
  1533. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1534. .then(playlist => {
  1535. next(null, playlist);
  1536. })
  1537. .catch(err => {
  1538. next(err);
  1539. });
  1540. },
  1541. (playlist, next) => {
  1542. if (playlist.type !== "genre") next("This playlist is not a genre playlist.");
  1543. else next(null, playlist.createdFor);
  1544. },
  1545. (genre, next) => {
  1546. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre, createPlaylist: true }, this)
  1547. .then(() => {
  1548. next();
  1549. })
  1550. .catch(err => {
  1551. next(err);
  1552. });
  1553. }
  1554. ],
  1555. err => {
  1556. if (err && err !== true) return reject(new Error(err));
  1557. return resolve();
  1558. }
  1559. );
  1560. });
  1561. }
  1562. /**
  1563. * Clears and refills a artist playlist
  1564. *
  1565. * @param {object} payload - object that contains the payload
  1566. * @param {string} payload.playlistId - the id of the playlist we are trying to clear and refill
  1567. * @returns {Promise} - returns promise (reject, resolve)
  1568. */
  1569. CLEAR_AND_REFILL_ARTIST_PLAYLIST(payload) {
  1570. return new Promise((resolve, reject) => {
  1571. const { playlistId } = payload;
  1572. async.waterfall(
  1573. [
  1574. next => {
  1575. PlaylistsModule.runJob("GET_PLAYLIST", { playlistId }, this)
  1576. .then(playlist => {
  1577. next(null, playlist);
  1578. })
  1579. .catch(err => {
  1580. next(err);
  1581. });
  1582. },
  1583. (playlist, next) => {
  1584. if (playlist.type !== "artist") next("This playlist is not a artist playlist.");
  1585. else next(null, playlist.createdFor);
  1586. },
  1587. (artist, next) => {
  1588. PlaylistsModule.runJob("AUTOFILL_ARTIST_PLAYLIST", { artist }, this)
  1589. .then(() => {
  1590. next();
  1591. })
  1592. .catch(err => {
  1593. next(err);
  1594. });
  1595. }
  1596. ],
  1597. err => {
  1598. if (err && err !== true) return reject(new Error(err));
  1599. return resolve();
  1600. }
  1601. );
  1602. });
  1603. }
  1604. }
  1605. export default new _PlaylistsModule();