songs.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  1. import async from "async";
  2. import mongoose from "mongoose";
  3. import CoreClass from "../core";
  4. let SongsModule;
  5. let CacheModule;
  6. let DBModule;
  7. let UtilsModule;
  8. let YouTubeModule;
  9. let StationsModule;
  10. let PlaylistsModule;
  11. let RatingsModule;
  12. let WSModule;
  13. class _SongsModule extends CoreClass {
  14. // eslint-disable-next-line require-jsdoc
  15. constructor() {
  16. super("songs");
  17. SongsModule = this;
  18. }
  19. /**
  20. * Initialises the songs module
  21. *
  22. * @returns {Promise} - returns promise (reject, resolve)
  23. */
  24. async initialize() {
  25. this.setStage(1);
  26. CacheModule = this.moduleManager.modules.cache;
  27. DBModule = this.moduleManager.modules.db;
  28. UtilsModule = this.moduleManager.modules.utils;
  29. YouTubeModule = this.moduleManager.modules.youtube;
  30. StationsModule = this.moduleManager.modules.stations;
  31. PlaylistsModule = this.moduleManager.modules.playlists;
  32. RatingsModule = this.moduleManager.modules.ratings;
  33. WSModule = this.moduleManager.modules.ws;
  34. this.SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  35. this.SongSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
  36. this.setStage(2);
  37. return new Promise((resolve, reject) => {
  38. CacheModule.runJob("SUB", {
  39. channel: "song.created",
  40. cb: async data =>
  41. WSModule.runJob("EMIT_TO_ROOMS", {
  42. rooms: ["import-album", `edit-song.${data.song._id}`, "edit-songs"],
  43. args: ["event:admin.song.created", { data }]
  44. })
  45. });
  46. async.waterfall(
  47. [
  48. next => {
  49. this.setStage(2);
  50. CacheModule.runJob("HGETALL", { table: "songs" })
  51. .then(songs => {
  52. next(null, songs);
  53. })
  54. .catch(next);
  55. },
  56. (songs, next) => {
  57. this.setStage(3);
  58. if (!songs) return next();
  59. const youtubeIds = Object.keys(songs);
  60. return async.each(
  61. youtubeIds,
  62. (youtubeId, next) => {
  63. SongsModule.SongModel.findOne({ youtubeId }, (err, song) => {
  64. if (err) next(err);
  65. else if (!song)
  66. CacheModule.runJob("HDEL", {
  67. table: "songs",
  68. key: youtubeId
  69. })
  70. .then(() => next())
  71. .catch(next);
  72. else next();
  73. });
  74. },
  75. next
  76. );
  77. },
  78. next => {
  79. this.setStage(4);
  80. SongsModule.SongModel.find({}, next);
  81. },
  82. (songs, next) => {
  83. this.setStage(5);
  84. async.each(
  85. songs,
  86. (song, next) => {
  87. CacheModule.runJob("HSET", {
  88. table: "songs",
  89. key: song.youtubeId,
  90. value: SongsModule.SongSchemaCache(song)
  91. })
  92. .then(() => next())
  93. .catch(next);
  94. },
  95. next
  96. );
  97. }
  98. ],
  99. async err => {
  100. if (err) {
  101. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  102. reject(new Error(err));
  103. } else resolve();
  104. }
  105. );
  106. });
  107. }
  108. /**
  109. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  110. *
  111. * @param {object} payload - object containing the payload
  112. * @param {string} payload.songId - the id of the song we are trying to get
  113. * @returns {Promise} - returns a promise (resolve, reject)
  114. */
  115. GET_SONG(payload) {
  116. return new Promise((resolve, reject) => {
  117. async.waterfall(
  118. [
  119. next => {
  120. if (!mongoose.Types.ObjectId.isValid(payload.songId))
  121. return next("songId is not a valid ObjectId.");
  122. return CacheModule.runJob("HGET", { table: "songs", key: payload.songId }, this)
  123. .then(song => next(null, song))
  124. .catch(next);
  125. },
  126. (song, next) => {
  127. if (song) return next(true, song);
  128. return SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  129. },
  130. (song, next) => {
  131. if (song) {
  132. CacheModule.runJob(
  133. "HSET",
  134. {
  135. table: "songs",
  136. key: payload.songId,
  137. value: song
  138. },
  139. this
  140. ).then(song => next(null, song));
  141. } else next("Song not found.");
  142. }
  143. ],
  144. (err, song) => {
  145. if (err && err !== true) return reject(new Error(err));
  146. return resolve({ song });
  147. }
  148. );
  149. });
  150. }
  151. /**
  152. * Gets songs by id from Mongo
  153. *
  154. * @param {object} payload - object containing the payload
  155. * @param {string} payload.youtubeIds - the youtube ids of the songs we are trying to get
  156. * @returns {Promise} - returns a promise (resolve, reject)
  157. */
  158. GET_SONGS(payload) {
  159. return new Promise((resolve, reject) => {
  160. async.waterfall(
  161. [
  162. next => SongsModule.SongModel.find({ youtubeId: { $in: payload.youtubeIds } }, next),
  163. (songs, next) => {
  164. const youtubeIds = payload.youtubeIds.filter(
  165. youtubeId => !songs.find(song => song.youtubeId === youtubeId)
  166. );
  167. return YouTubeModule.youtubeVideoModel.find(
  168. { youtubeId: { $in: youtubeIds } },
  169. (err, videos) => {
  170. if (err) next(err);
  171. else {
  172. const youtubeVideos = videos.map(video => {
  173. const { youtubeId, title, author, duration, thumbnail } = video;
  174. return {
  175. youtubeId,
  176. title,
  177. artists: [author],
  178. genres: [],
  179. tags: [],
  180. duration,
  181. skipDuration: 0,
  182. thumbnail:
  183. thumbnail || `https://img.youtube.com/vi/${youtubeId}/mqdefault.jpg`,
  184. requestedBy: null,
  185. requestedAt: Date.now(),
  186. verified: false
  187. };
  188. });
  189. next(null, [...songs, ...youtubeVideos]);
  190. }
  191. }
  192. );
  193. }
  194. ],
  195. (err, songs) => {
  196. if (err && err !== true) return reject(new Error(err));
  197. return resolve({ songs });
  198. }
  199. );
  200. });
  201. }
  202. /**
  203. * Makes sure that if a song is not currently in the songs db, to add it
  204. *
  205. * @param {object} payload - an object containing the payload
  206. * @param {string} payload.youtubeId - the youtube song id of the song we are trying to ensure is in the songs db
  207. * @param {string} payload.userId - the youtube song id of the song we are trying to ensure is in the songs db
  208. * @param {string} payload.automaticallyRequested - whether the song was automatically requested or not
  209. * @returns {Promise} - returns a promise (resolve, reject)
  210. */
  211. ENSURE_SONG_EXISTS_BY_YOUTUBE_ID(payload) {
  212. return new Promise((resolve, reject) => {
  213. async.waterfall(
  214. [
  215. next => {
  216. SongsModule.SongModel.findOne({ youtubeId: payload.youtubeId }, next);
  217. },
  218. (song, next) => {
  219. if (song && song.duration > 0) next(true, song);
  220. else {
  221. YouTubeModule.runJob(
  222. "GET_VIDEO",
  223. { identifier: payload.youtubeId, createMissing: true },
  224. this
  225. )
  226. .then(response => {
  227. const { youtubeId, title, author, duration } = response.video;
  228. next(null, song, { youtubeId, title, artists: [author], duration });
  229. })
  230. .catch(next);
  231. }
  232. },
  233. (song, youtubeVideo, next) => {
  234. if (song && song.duration <= 0) {
  235. song.duration = youtubeVideo.duration;
  236. song.save({ validateBeforeSave: true }, err => {
  237. if (err) next(err, song);
  238. next(null, song);
  239. });
  240. } else {
  241. next(null, {
  242. ...youtubeVideo,
  243. skipDuration: 0,
  244. requestedBy: payload.userId,
  245. requestedAt: Date.now(),
  246. verified: false
  247. });
  248. }
  249. }
  250. ],
  251. (err, song) => {
  252. if (err && err !== true) return reject(new Error(err));
  253. return resolve({ song });
  254. }
  255. );
  256. });
  257. }
  258. /**
  259. * Create song
  260. *
  261. * @param {object} payload - an object containing the payload
  262. * @param {string} payload.song - the song object
  263. * @param {string} payload.userId - the user id of the person requesting the song
  264. * @returns {Promise} - returns a promise (resolve, reject)
  265. */
  266. CREATE_SONG(payload) {
  267. return new Promise((resolve, reject) => {
  268. async.waterfall(
  269. [
  270. next => {
  271. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  272. .then(UserModel => {
  273. UserModel.findOne(
  274. { _id: payload.userId },
  275. { "preferences.anonymousSongRequests": 1 },
  276. next
  277. );
  278. })
  279. .catch(next);
  280. },
  281. (user, next) => {
  282. const song = new SongsModule.SongModel({
  283. ...payload.song,
  284. requestedBy: user.preferences.anonymousSongRequests ? null : payload.userId,
  285. requestedAt: Date.now()
  286. });
  287. if (song.verified) {
  288. song.verifiedBy = payload.userId;
  289. song.verifiedAt = Date.now();
  290. }
  291. song.save({ validateBeforeSave: true }, err => {
  292. if (err) return next(err, song);
  293. return next(null, song);
  294. });
  295. },
  296. (song, next) => {
  297. SongsModule.runJob("UPDATE_SONG", { songId: song._id });
  298. return next(null, song);
  299. },
  300. (song, next) => {
  301. RatingsModule.runJob("RECALCULATE_RATINGS", { youtubeId: song.youtubeId }, this)
  302. .then(() => next(null, song))
  303. .catch(next);
  304. },
  305. (song, next) => {
  306. CacheModule.runJob("PUB", {
  307. channel: "song.created",
  308. value: { song }
  309. })
  310. .then(() => next(null, song))
  311. .catch(next);
  312. }
  313. ],
  314. (err, song) => {
  315. if (err && err !== true) return reject(new Error(err));
  316. return resolve({ song });
  317. }
  318. );
  319. });
  320. }
  321. /**
  322. * Gets a song from id from Mongo and updates the cache with it
  323. *
  324. * @param {object} payload - an object containing the payload
  325. * @param {string} payload.songId - the id of the song we are trying to update
  326. * @param {string} payload.oldStatus - old status of song being updated (optional)
  327. * @returns {Promise} - returns a promise (resolve, reject)
  328. */
  329. UPDATE_SONG(payload) {
  330. return new Promise((resolve, reject) => {
  331. async.waterfall(
  332. [
  333. next => {
  334. SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  335. },
  336. (song, next) => {
  337. if (!song) {
  338. CacheModule.runJob("HDEL", {
  339. table: "songs",
  340. key: payload.songId
  341. });
  342. return next("Song not found.");
  343. }
  344. return CacheModule.runJob(
  345. "HSET",
  346. {
  347. table: "songs",
  348. key: payload.songId,
  349. value: song
  350. },
  351. this
  352. )
  353. .then(song => {
  354. next(null, song);
  355. })
  356. .catch(next);
  357. },
  358. (song, next) => {
  359. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  360. const trimmedSong = {
  361. _id,
  362. youtubeId,
  363. title,
  364. artists,
  365. thumbnail,
  366. duration,
  367. verified
  368. };
  369. this.log("INFO", `Going to update playlists now for song ${_id}`);
  370. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this)
  371. .then(playlistModel => {
  372. playlistModel.updateMany(
  373. { "songs._id": song._id },
  374. { $set: { "songs.$": trimmedSong } },
  375. err => {
  376. if (err) next(err);
  377. else
  378. playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  379. if (err) next(err);
  380. else {
  381. async.eachLimit(
  382. playlists,
  383. 1,
  384. (playlist, next) => {
  385. PlaylistsModule.runJob(
  386. "UPDATE_PLAYLIST",
  387. {
  388. playlistId: playlist._id
  389. },
  390. this
  391. )
  392. .then(() => {
  393. next();
  394. })
  395. .catch(err => {
  396. next(err);
  397. });
  398. },
  399. err => {
  400. if (err) next(err);
  401. else next(null, song);
  402. }
  403. );
  404. }
  405. });
  406. }
  407. );
  408. })
  409. .catch(err => {
  410. next(err);
  411. });
  412. },
  413. (song, next) => {
  414. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  415. this.log("INFO", `Going to update stations now for song ${_id}`);
  416. DBModule.runJob("GET_MODEL", { modelName: "station" }, this)
  417. .then(stationModel => {
  418. stationModel.updateMany(
  419. { "queue._id": song._id },
  420. {
  421. $set: {
  422. "queue.$.youtubeId": youtubeId,
  423. "queue.$.title": title,
  424. "queue.$.artists": artists,
  425. "queue.$.thumbnail": thumbnail,
  426. "queue.$.duration": duration,
  427. "queue.$.verified": verified
  428. }
  429. },
  430. err => {
  431. if (err) this.log("ERROR", err);
  432. else
  433. stationModel.find({ "queue._id": song._id }, (err, stations) => {
  434. if (err) next(err);
  435. else {
  436. async.eachLimit(
  437. stations,
  438. 1,
  439. (station, next) => {
  440. StationsModule.runJob(
  441. "UPDATE_STATION",
  442. { stationId: station._id },
  443. this
  444. )
  445. .then(() => {
  446. next();
  447. })
  448. .catch(err => {
  449. next(err);
  450. });
  451. },
  452. err => {
  453. if (err) next(err);
  454. else next(null, song);
  455. }
  456. );
  457. }
  458. });
  459. }
  460. );
  461. })
  462. .catch(err => {
  463. next(err);
  464. });
  465. },
  466. (song, next) => {
  467. async.eachLimit(
  468. song.genres,
  469. 1,
  470. (genre, next) => {
  471. PlaylistsModule.runJob(
  472. "AUTOFILL_GENRE_PLAYLIST",
  473. { genre, createPlaylist: song.verified },
  474. this
  475. )
  476. .then(() => {
  477. next();
  478. })
  479. .catch(err => next(err));
  480. },
  481. err => {
  482. next(err, song);
  483. }
  484. );
  485. }
  486. ],
  487. (err, song) => {
  488. if (err && err !== true) return reject(new Error(err));
  489. if (!payload.oldStatus) payload.oldStatus = null;
  490. CacheModule.runJob("PUB", {
  491. channel: "song.updated",
  492. value: { songId: song._id, oldStatus: payload.oldStatus }
  493. });
  494. return resolve(song);
  495. }
  496. );
  497. });
  498. }
  499. /**
  500. * Gets multiple songs from id from Mongo and updates the cache with it
  501. *
  502. * @param {object} payload - an object containing the payload
  503. * @param {Array} payload.songIds - the ids of the songs we are trying to update
  504. * @param {string} payload.oldStatus - old status of song being updated (optional)
  505. * @returns {Promise} - returns a promise (resolve, reject)
  506. */
  507. async UPDATE_SONGS(payload) {
  508. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  509. const stationModel = await DBModule.runJob("GET_MODEL", { modelName: "station" }, this);
  510. return new Promise((resolve, reject) => {
  511. async.waterfall(
  512. [
  513. // Get songs from Mongo
  514. next => {
  515. const { songIds } = payload;
  516. this.publishProgress({ status: "update", message: `Updating songs (stage 1)` });
  517. SongsModule.SongModel.find({ _id: songIds }, next);
  518. },
  519. // Any songs that were not in Mongo, remove from cache, if they're in the cache
  520. (songs, next) => {
  521. const { songIds } = payload;
  522. this.publishProgress({ status: "update", message: `Updating songs (stage 2)` });
  523. async.eachLimit(
  524. songIds,
  525. 1,
  526. (songId, next) => {
  527. if (songs.findIndex(song => song._id.toString() === songId) === -1) {
  528. // NOTE: could be made lower priority
  529. CacheModule.runJob("HDEL", {
  530. table: "songs",
  531. key: songId
  532. });
  533. next();
  534. } else next();
  535. },
  536. () => {
  537. next(null, songs);
  538. }
  539. );
  540. },
  541. // Adds/updates all songs in the cache
  542. (songs, next) => {
  543. this.publishProgress({ status: "update", message: `Updating songs (stage 3)` });
  544. async.eachLimit(
  545. songs,
  546. 1,
  547. (song, next) => {
  548. CacheModule.runJob(
  549. "HSET",
  550. {
  551. table: "songs",
  552. key: song._id,
  553. value: song
  554. },
  555. this
  556. )
  557. .then(() => {
  558. next();
  559. })
  560. .catch(next);
  561. },
  562. () => {
  563. next(null, songs);
  564. }
  565. );
  566. },
  567. // Updates all playlists that the songs are in by setting the new trimmed song
  568. (songs, next) => {
  569. this.publishProgress({ status: "update", message: `Updating songs (stage 4)` });
  570. const trimmedSongs = songs.map(song => {
  571. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  572. return {
  573. _id,
  574. youtubeId,
  575. title,
  576. artists,
  577. thumbnail,
  578. duration,
  579. verified
  580. };
  581. });
  582. const playlistsToUpdate = new Set();
  583. async.eachLimit(
  584. trimmedSongs,
  585. 1,
  586. (trimmedSong, next) => {
  587. async.waterfall(
  588. [
  589. next => {
  590. playlistModel.updateMany(
  591. { "songs._id": trimmedSong._id },
  592. { $set: { "songs.$": trimmedSong } },
  593. next
  594. );
  595. },
  596. (res, next) => {
  597. playlistModel.find({ "songs._id": trimmedSong._id }, next);
  598. },
  599. (playlists, next) => {
  600. playlists.forEach(playlist => {
  601. playlistsToUpdate.add(playlist._id.toString());
  602. });
  603. next();
  604. }
  605. ],
  606. next
  607. );
  608. },
  609. err => {
  610. next(err, songs, playlistsToUpdate);
  611. }
  612. );
  613. },
  614. // Updates all playlists that the songs are in
  615. (songs, playlistsToUpdate, next) => {
  616. this.publishProgress({ status: "update", message: `Updating songs (stage 5)` });
  617. async.eachLimit(
  618. playlistsToUpdate,
  619. 1,
  620. (playlistId, next) => {
  621. PlaylistsModule.runJob(
  622. "UPDATE_PLAYLIST",
  623. {
  624. playlistId
  625. },
  626. this
  627. )
  628. .then(() => {
  629. next();
  630. })
  631. .catch(err => {
  632. next(err);
  633. });
  634. },
  635. err => {
  636. next(err, songs);
  637. }
  638. );
  639. },
  640. // Updates all station queues that the songs are in by setting the new trimmed song
  641. (songs, next) => {
  642. this.publishProgress({ status: "update", message: `Updating songs (stage 6)` });
  643. const stationsToUpdate = new Set();
  644. async.eachLimit(
  645. songs,
  646. 1,
  647. (song, next) => {
  648. async.waterfall(
  649. [
  650. next => {
  651. const { youtubeId, title, artists, thumbnail, duration, verified } = song;
  652. stationModel.updateMany(
  653. { "queue._id": song._id },
  654. {
  655. $set: {
  656. "queue.$.youtubeId": youtubeId,
  657. "queue.$.title": title,
  658. "queue.$.artists": artists,
  659. "queue.$.thumbnail": thumbnail,
  660. "queue.$.duration": duration,
  661. "queue.$.verified": verified
  662. }
  663. },
  664. next
  665. );
  666. },
  667. (res, next) => {
  668. stationModel.find({ "queue._id": song._id }, next);
  669. },
  670. (stations, next) => {
  671. stations.forEach(station => {
  672. stationsToUpdate.add(station._id.toString());
  673. });
  674. next();
  675. }
  676. ],
  677. next
  678. );
  679. },
  680. err => {
  681. next(err, songs, stationsToUpdate);
  682. }
  683. );
  684. },
  685. // Updates all playlists that the songs are in
  686. (songs, stationsToUpdate, next) => {
  687. this.publishProgress({ status: "update", message: `Updating songs (stage 7)` });
  688. async.eachLimit(
  689. stationsToUpdate,
  690. 1,
  691. (stationId, next) => {
  692. StationsModule.runJob(
  693. "UPDATE_STATION",
  694. {
  695. stationId
  696. },
  697. this
  698. )
  699. .then(() => {
  700. next();
  701. })
  702. .catch(err => {
  703. next(err);
  704. });
  705. },
  706. err => {
  707. next(err, songs);
  708. }
  709. );
  710. },
  711. // Autofill the genre playlists of all genres of all songs
  712. (songs, next) => {
  713. this.publishProgress({ status: "update", message: `Updating songs (stage 8)` });
  714. const genresToAutofill = new Set();
  715. songs.forEach(song => {
  716. if (song.verified)
  717. song.genres.forEach(genre => {
  718. genresToAutofill.add(genre);
  719. });
  720. });
  721. async.eachLimit(
  722. genresToAutofill,
  723. 1,
  724. (genre, next) => {
  725. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre, createPlaylist: true }, this)
  726. .then(() => {
  727. next();
  728. })
  729. .catch(err => next(err));
  730. },
  731. err => {
  732. next(err, songs);
  733. }
  734. );
  735. },
  736. // Send event that the song was updated
  737. (songs, next) => {
  738. this.publishProgress({ status: "update", message: `Updating songs (stage 9)` });
  739. async.eachLimit(
  740. songs,
  741. 1,
  742. (song, next) => {
  743. CacheModule.runJob("PUB", {
  744. channel: "song.updated",
  745. value: { songId: song._id, oldStatus: null }
  746. });
  747. next();
  748. },
  749. () => {
  750. next();
  751. }
  752. );
  753. }
  754. ],
  755. err => {
  756. if (err && err !== true) return reject(new Error(err));
  757. return resolve();
  758. }
  759. );
  760. });
  761. }
  762. /**
  763. * Updates all songs
  764. *
  765. * @returns {Promise} - returns a promise (resolve, reject)
  766. */
  767. UPDATE_ALL_SONGS() {
  768. return new Promise((resolve, reject) => {
  769. async.waterfall(
  770. [
  771. next => {
  772. SongsModule.SongModel.find({}, next);
  773. },
  774. (songs, next) => {
  775. let index = 0;
  776. const { length } = songs;
  777. async.eachLimit(
  778. songs,
  779. 2,
  780. (song, next) => {
  781. index += 1;
  782. console.log(`Updating song #${index} out of ${length}: ${song._id}`);
  783. SongsModule.runJob("UPDATE_SONG", { songId: song._id }, this)
  784. .then(() => {
  785. next();
  786. })
  787. .catch(err => {
  788. next(err);
  789. });
  790. },
  791. err => {
  792. next(err);
  793. }
  794. );
  795. }
  796. ],
  797. err => {
  798. if (err && err !== true) return reject(new Error(err));
  799. return resolve();
  800. }
  801. );
  802. });
  803. }
  804. // /**
  805. // * Deletes song from id from Mongo and cache
  806. // *
  807. // * @param {object} payload - returns an object containing the payload
  808. // * @param {string} payload.songId - the song id of the song we are trying to delete
  809. // * @returns {Promise} - returns a promise (resolve, reject)
  810. // */
  811. // DELETE_SONG(payload) {
  812. // return new Promise((resolve, reject) =>
  813. // async.waterfall(
  814. // [
  815. // next => {
  816. // SongsModule.SongModel.deleteOne({ _id: payload.songId }, next);
  817. // },
  818. // next => {
  819. // CacheModule.runJob(
  820. // "HDEL",
  821. // {
  822. // table: "songs",
  823. // key: payload.songId
  824. // },
  825. // this
  826. // )
  827. // .then(() => next())
  828. // .catch(next);
  829. // },
  830. // next => {
  831. // this.log("INFO", `Going to update playlists and stations now for deleted song ${payload.songId}`);
  832. // DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  833. // playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  834. // if (err) this.log("ERROR", err);
  835. // else {
  836. // playlistModel.updateMany(
  837. // { "songs._id": payload.songId },
  838. // { $pull: { "songs.$._id": payload.songId} },
  839. // err => {
  840. // if (err) this.log("ERROR", err);
  841. // else {
  842. // playlists.forEach(playlist => {
  843. // PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  844. // playlistId: playlist._id
  845. // });
  846. // });
  847. // }
  848. // }
  849. // );
  850. // }
  851. // });
  852. // });
  853. // DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  854. // stationModel.find({ "queue._id": payload.songId }, (err, stations) => {
  855. // stationModel.updateMany(
  856. // { "queue._id": payload.songId },
  857. // {
  858. // $pull: { "queue._id": }
  859. // },
  860. // err => {
  861. // if (err) this.log("ERROR", err);
  862. // else {
  863. // stations.forEach(station => {
  864. // StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  865. // });
  866. // }
  867. // }
  868. // );
  869. // });
  870. // });
  871. // }
  872. // ],
  873. // err => {
  874. // if (err && err !== true) return reject(new Error(err));
  875. // return resolve();
  876. // }
  877. // )
  878. // );
  879. // }
  880. /**
  881. * Searches through songs
  882. *
  883. * @param {object} payload - object that contains the payload
  884. * @param {string} payload.query - the query
  885. * @param {string} payload.includeUnverified - include unverified songs
  886. * @param {string} payload.includeVerified - include verified songs
  887. * @param {string} payload.trimmed - include trimmed songs
  888. * @param {string} payload.page - page (default 1)
  889. * @returns {Promise} - returns promise (reject, resolve)
  890. */
  891. SEARCH(payload) {
  892. return new Promise((resolve, reject) => {
  893. async.waterfall(
  894. [
  895. next => {
  896. const isVerified = [];
  897. if (payload.includeUnverified) isVerified.push(false);
  898. if (payload.includeVerified) isVerified.push(true);
  899. if (isVerified.length === 0) return next("No verified status has been included.");
  900. let { query } = payload;
  901. const isRegex =
  902. query.length > 2 && query.indexOf("/") === 0 && query.lastIndexOf("/") === query.length - 1;
  903. if (isRegex) query = query.slice(1, query.length - 1);
  904. else query = query.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
  905. const filterArray = [
  906. {
  907. title: new RegExp(`${query}`, "i"),
  908. verified: { $in: isVerified }
  909. },
  910. {
  911. artists: new RegExp(`${query}`, "i"),
  912. verified: { $in: isVerified }
  913. }
  914. ];
  915. return next(null, filterArray);
  916. },
  917. (filterArray, next) => {
  918. const page = payload.page ? payload.page : 1;
  919. const pageSize = 15;
  920. const skipAmount = pageSize * (page - 1);
  921. SongsModule.SongModel.find({ $or: filterArray }).count((err, count) => {
  922. if (err) next(err);
  923. else {
  924. SongsModule.SongModel.find({ $or: filterArray })
  925. .skip(skipAmount)
  926. .limit(pageSize)
  927. .exec((err, songs) => {
  928. if (err) next(err);
  929. else {
  930. next(null, {
  931. songs,
  932. page,
  933. pageSize,
  934. skipAmount,
  935. count
  936. });
  937. }
  938. });
  939. }
  940. });
  941. },
  942. (data, next) => {
  943. if (data.songs.length === 0) next("No songs found");
  944. else if (payload.trimmed) {
  945. next(null, {
  946. songs: data.songs.map(song => {
  947. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  948. return {
  949. _id,
  950. youtubeId,
  951. title,
  952. artists,
  953. thumbnail,
  954. duration,
  955. verified
  956. };
  957. }),
  958. ...data
  959. });
  960. } else next(null, data);
  961. }
  962. ],
  963. (err, data) => {
  964. if (err && err !== true) return reject(new Error(err));
  965. return resolve(data);
  966. }
  967. );
  968. });
  969. }
  970. /**
  971. * Gets an array of all genres
  972. *
  973. * @returns {Promise} - returns a promise (resolve, reject)
  974. */
  975. GET_ALL_GENRES() {
  976. return new Promise((resolve, reject) => {
  977. async.waterfall(
  978. [
  979. next => {
  980. SongsModule.SongModel.find({ verified: true }, { genres: 1, _id: false }, next);
  981. },
  982. (songs, next) => {
  983. let allGenres = [];
  984. songs.forEach(song => {
  985. allGenres = allGenres.concat(song.genres);
  986. });
  987. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  988. const uniqueGenres = lowerCaseGenres.filter(
  989. (value, index, self) => self.indexOf(value) === index
  990. );
  991. next(null, uniqueGenres);
  992. }
  993. ],
  994. (err, genres) => {
  995. if (err && err !== true) return reject(new Error(err));
  996. return resolve({ genres });
  997. }
  998. );
  999. });
  1000. }
  1001. /**
  1002. * Gets an array of all songs with a specific genre
  1003. *
  1004. * @param {object} payload - returns an object containing the payload
  1005. * @param {string} payload.genre - the genre
  1006. * @returns {Promise} - returns a promise (resolve, reject)
  1007. */
  1008. GET_ALL_SONGS_WITH_GENRE(payload) {
  1009. return new Promise((resolve, reject) => {
  1010. async.waterfall(
  1011. [
  1012. next => {
  1013. SongsModule.SongModel.find(
  1014. {
  1015. verified: true,
  1016. genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") }
  1017. },
  1018. next
  1019. );
  1020. }
  1021. ],
  1022. (err, songs) => {
  1023. if (err && err !== true) return reject(new Error(err));
  1024. return resolve({ songs });
  1025. }
  1026. );
  1027. });
  1028. }
  1029. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  1030. /**
  1031. * Gets a orphaned playlist songs
  1032. *
  1033. * @returns {Promise} - returns promise (reject, resolve)
  1034. */
  1035. GET_ORPHANED_PLAYLIST_SONGS() {
  1036. return new Promise((resolve, reject) => {
  1037. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  1038. playlistModel.find({}, (err, playlists) => {
  1039. if (err) reject(new Error(err));
  1040. else {
  1041. SongsModule.SongModel.find({}, { _id: true, youtubeId: true }, (err, songs) => {
  1042. if (err) reject(new Error(err));
  1043. else {
  1044. const songIds = songs.map(song => song._id.toString());
  1045. const orphanedYoutubeIds = new Set();
  1046. async.eachLimit(
  1047. playlists,
  1048. 1,
  1049. (playlist, next) => {
  1050. playlist.songs.forEach(song => {
  1051. if (
  1052. (!song._id || songIds.indexOf(song._id.toString() === -1)) &&
  1053. !orphanedYoutubeIds.has(song.youtubeId)
  1054. ) {
  1055. orphanedYoutubeIds.add(song.youtubeId);
  1056. }
  1057. });
  1058. next();
  1059. },
  1060. () => {
  1061. resolve({ youtubeIds: Array.from(orphanedYoutubeIds) });
  1062. }
  1063. );
  1064. }
  1065. });
  1066. }
  1067. });
  1068. });
  1069. });
  1070. }
  1071. /**
  1072. * Requests all orphaned playlist songs, adding them to the database
  1073. *
  1074. * @returns {Promise} - returns promise (reject, resolve)
  1075. */
  1076. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  1077. return new Promise((resolve, reject) => {
  1078. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  1079. .then(playlistModel => {
  1080. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  1081. const { youtubeIds } = response;
  1082. const playlistsToUpdate = new Set();
  1083. async.eachLimit(
  1084. youtubeIds,
  1085. 1,
  1086. (youtubeId, next) => {
  1087. async.waterfall(
  1088. [
  1089. next => {
  1090. console.log(
  1091. youtubeId,
  1092. `this is song ${youtubeIds.indexOf(youtubeId) + 1}/${youtubeIds.length}`
  1093. );
  1094. setTimeout(next, 150);
  1095. },
  1096. next => {
  1097. SongsModule.runJob(
  1098. "ENSURE_SONG_EXISTS_BY_SONG_ID",
  1099. { youtubeId, automaticallyRequested: true },
  1100. this
  1101. )
  1102. .then(() => next())
  1103. .catch(next);
  1104. },
  1105. next => {
  1106. console.log(444, youtubeId);
  1107. SongsModule.SongModel.findOne({ youtubeId }, next);
  1108. },
  1109. (song, next) => {
  1110. const { _id, title, artists, thumbnail, duration, verified } = song;
  1111. const trimmedSong = {
  1112. _id,
  1113. youtubeId,
  1114. title,
  1115. artists,
  1116. thumbnail,
  1117. duration,
  1118. verified
  1119. };
  1120. playlistModel.updateMany(
  1121. { "songs.youtubeId": song.youtubeId },
  1122. { $set: { "songs.$": trimmedSong } },
  1123. err => {
  1124. next(err, song);
  1125. }
  1126. );
  1127. },
  1128. (song, next) => {
  1129. playlistModel.find({ "songs._id": song._id }, next);
  1130. },
  1131. (playlists, next) => {
  1132. playlists.forEach(playlist => {
  1133. playlistsToUpdate.add(playlist._id.toString());
  1134. });
  1135. next();
  1136. }
  1137. ],
  1138. next
  1139. );
  1140. },
  1141. err => {
  1142. if (err) reject(err);
  1143. else {
  1144. async.eachLimit(
  1145. Array.from(playlistsToUpdate),
  1146. 1,
  1147. (playlistId, next) => {
  1148. PlaylistsModule.runJob(
  1149. "UPDATE_PLAYLIST",
  1150. {
  1151. playlistId
  1152. },
  1153. this
  1154. )
  1155. .then(() => {
  1156. next();
  1157. })
  1158. .catch(next);
  1159. },
  1160. err => {
  1161. if (err) reject(err);
  1162. else resolve();
  1163. }
  1164. );
  1165. }
  1166. }
  1167. );
  1168. });
  1169. })
  1170. .catch(reject);
  1171. });
  1172. }
  1173. /**
  1174. * Gets a list of all genres
  1175. *
  1176. * @returns {Promise} - returns promise (reject, resolve)
  1177. */
  1178. GET_GENRES() {
  1179. return new Promise((resolve, reject) => {
  1180. async.waterfall(
  1181. [
  1182. next => {
  1183. SongsModule.SongModel.distinct("genres", next);
  1184. }
  1185. ],
  1186. (err, genres) => {
  1187. if (err) reject(err);
  1188. resolve({ genres });
  1189. }
  1190. );
  1191. });
  1192. }
  1193. /**
  1194. * Gets a list of all artists
  1195. *
  1196. * @returns {Promise} - returns promise (reject, resolve)
  1197. */
  1198. GET_ARTISTS() {
  1199. return new Promise((resolve, reject) => {
  1200. async.waterfall(
  1201. [
  1202. next => {
  1203. SongsModule.SongModel.distinct("artists", next);
  1204. }
  1205. ],
  1206. (err, artists) => {
  1207. if (err) reject(err);
  1208. resolve({ artists });
  1209. }
  1210. );
  1211. });
  1212. }
  1213. /**
  1214. * Gets a list of all tags
  1215. *
  1216. * @returns {Promise} - returns promise (reject, resolve)
  1217. */
  1218. GET_TAGS() {
  1219. return new Promise((resolve, reject) => {
  1220. async.waterfall(
  1221. [
  1222. next => {
  1223. SongsModule.SongModel.distinct("tags", next);
  1224. }
  1225. ],
  1226. (err, tags) => {
  1227. if (err) reject(err);
  1228. resolve({ tags });
  1229. }
  1230. );
  1231. });
  1232. }
  1233. }
  1234. export default new _SongsModule();