songs.js 32 KB

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