songs.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. SongsModule.SongModel.find({ _id: songIds }, next);
  486. },
  487. // Any songs that were not in Mongo, remove from cache, if they're in the cache
  488. (songs, next) => {
  489. const { songIds } = payload;
  490. async.eachLimit(
  491. songIds,
  492. 1,
  493. (songId, next) => {
  494. if (songs.findIndex(song => song._id.toString() === songId) === -1) {
  495. // NOTE: could be made lower priority
  496. CacheModule.runJob("HDEL", {
  497. table: "songs",
  498. key: songId
  499. });
  500. next();
  501. } else next();
  502. },
  503. () => {
  504. next(null, songs);
  505. }
  506. );
  507. },
  508. // Adds/updates all songs in the cache
  509. (songs, next) => {
  510. async.eachLimit(
  511. songs,
  512. 1,
  513. (song, next) => {
  514. CacheModule.runJob(
  515. "HSET",
  516. {
  517. table: "songs",
  518. key: song._id,
  519. value: song
  520. },
  521. this
  522. )
  523. .then(() => {
  524. next();
  525. })
  526. .catch(next);
  527. },
  528. () => {
  529. next(null, songs);
  530. }
  531. );
  532. },
  533. // Updates all playlists that the songs are in by setting the new trimmed song
  534. (songs, next) => {
  535. const trimmedSongs = songs.map(song => {
  536. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  537. return {
  538. _id,
  539. youtubeId,
  540. title,
  541. artists,
  542. thumbnail,
  543. duration,
  544. verified
  545. };
  546. });
  547. const playlistsToUpdate = new Set();
  548. async.eachLimit(
  549. trimmedSongs,
  550. 1,
  551. (trimmedSong, next) => {
  552. async.waterfall(
  553. [
  554. next => {
  555. playlistModel.updateMany(
  556. { "songs._id": trimmedSong._id },
  557. { $set: { "songs.$": trimmedSong } },
  558. next
  559. );
  560. },
  561. (res, next) => {
  562. playlistModel.find({ "songs._id": trimmedSong._id }, next);
  563. },
  564. (playlists, next) => {
  565. playlists.forEach(playlist => {
  566. playlistsToUpdate.add(playlist._id.toString());
  567. });
  568. next();
  569. }
  570. ],
  571. next
  572. );
  573. },
  574. err => {
  575. next(err, songs, playlistsToUpdate);
  576. }
  577. );
  578. },
  579. // Updates all playlists that the songs are in
  580. (songs, playlistsToUpdate, next) => {
  581. async.eachLimit(
  582. playlistsToUpdate,
  583. 1,
  584. (playlistId, next) => {
  585. PlaylistsModule.runJob(
  586. "UPDATE_PLAYLIST",
  587. {
  588. playlistId
  589. },
  590. this
  591. )
  592. .then(() => {
  593. next();
  594. })
  595. .catch(err => {
  596. next(err);
  597. });
  598. },
  599. err => {
  600. next(err, songs);
  601. }
  602. );
  603. },
  604. // Updates all station queues that the songs are in by setting the new trimmed song
  605. (songs, next) => {
  606. const stationsToUpdate = new Set();
  607. async.eachLimit(
  608. songs,
  609. 1,
  610. (song, next) => {
  611. async.waterfall(
  612. [
  613. next => {
  614. const { youtubeId, title, artists, thumbnail, duration, verified } = song;
  615. stationModel.updateMany(
  616. { "queue._id": song._id },
  617. {
  618. $set: {
  619. "queue.$.youtubeId": youtubeId,
  620. "queue.$.title": title,
  621. "queue.$.artists": artists,
  622. "queue.$.thumbnail": thumbnail,
  623. "queue.$.duration": duration,
  624. "queue.$.verified": verified
  625. }
  626. },
  627. next
  628. );
  629. },
  630. (res, next) => {
  631. stationModel.find({ "queue._id": song._id }, next);
  632. },
  633. (stations, next) => {
  634. stations.forEach(station => {
  635. stationsToUpdate.add(station._id.toString());
  636. });
  637. next();
  638. }
  639. ],
  640. next
  641. );
  642. },
  643. err => {
  644. next(err, songs, stationsToUpdate);
  645. }
  646. );
  647. },
  648. // Updates all playlists that the songs are in
  649. (songs, stationsToUpdate, next) => {
  650. async.eachLimit(
  651. stationsToUpdate,
  652. 1,
  653. (stationId, next) => {
  654. StationsModule.runJob(
  655. "UPDATE_STATION",
  656. {
  657. stationId
  658. },
  659. this
  660. )
  661. .then(() => {
  662. next();
  663. })
  664. .catch(err => {
  665. next(err);
  666. });
  667. },
  668. err => {
  669. next(err, songs);
  670. }
  671. );
  672. },
  673. // Autofill the genre playlists of all genres of all songs
  674. (songs, next) => {
  675. const genresToAutofill = new Set();
  676. songs.forEach(song => {
  677. if (song.verified)
  678. song.genres.forEach(genre => {
  679. genresToAutofill.add(genre);
  680. });
  681. });
  682. async.eachLimit(
  683. genresToAutofill,
  684. 1,
  685. (genre, next) => {
  686. PlaylistsModule.runJob("AUTOFILL_GENRE_PLAYLIST", { genre, createPlaylist: true }, this)
  687. .then(() => {
  688. next();
  689. })
  690. .catch(err => next(err));
  691. },
  692. err => {
  693. next(err, songs);
  694. }
  695. );
  696. },
  697. // Send event that the song was updated
  698. (songs, next) => {
  699. async.eachLimit(
  700. songs,
  701. 1,
  702. (song, next) => {
  703. CacheModule.runJob("PUB", {
  704. channel: "song.updated",
  705. value: { songId: song._id, oldStatus: null }
  706. });
  707. next();
  708. },
  709. () => {
  710. next();
  711. }
  712. );
  713. }
  714. ],
  715. err => {
  716. if (err && err !== true) return reject(new Error(err));
  717. return resolve();
  718. }
  719. );
  720. });
  721. }
  722. /**
  723. * Updates all songs
  724. *
  725. * @returns {Promise} - returns a promise (resolve, reject)
  726. */
  727. UPDATE_ALL_SONGS() {
  728. return new Promise((resolve, reject) => {
  729. async.waterfall(
  730. [
  731. next => {
  732. SongsModule.SongModel.find({}, next);
  733. },
  734. (songs, next) => {
  735. let index = 0;
  736. const { length } = songs;
  737. async.eachLimit(
  738. songs,
  739. 2,
  740. (song, next) => {
  741. index += 1;
  742. console.log(`Updating song #${index} out of ${length}: ${song._id}`);
  743. SongsModule.runJob("UPDATE_SONG", { songId: song._id }, this)
  744. .then(() => {
  745. next();
  746. })
  747. .catch(err => {
  748. next(err);
  749. });
  750. },
  751. err => {
  752. next(err);
  753. }
  754. );
  755. }
  756. ],
  757. err => {
  758. if (err && err !== true) return reject(new Error(err));
  759. return resolve();
  760. }
  761. );
  762. });
  763. }
  764. // /**
  765. // * Deletes song from id from Mongo and cache
  766. // *
  767. // * @param {object} payload - returns an object containing the payload
  768. // * @param {string} payload.songId - the song id of the song we are trying to delete
  769. // * @returns {Promise} - returns a promise (resolve, reject)
  770. // */
  771. // DELETE_SONG(payload) {
  772. // return new Promise((resolve, reject) =>
  773. // async.waterfall(
  774. // [
  775. // next => {
  776. // SongsModule.SongModel.deleteOne({ _id: payload.songId }, next);
  777. // },
  778. // next => {
  779. // CacheModule.runJob(
  780. // "HDEL",
  781. // {
  782. // table: "songs",
  783. // key: payload.songId
  784. // },
  785. // this
  786. // )
  787. // .then(() => next())
  788. // .catch(next);
  789. // },
  790. // next => {
  791. // this.log("INFO", `Going to update playlists and stations now for deleted song ${payload.songId}`);
  792. // DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  793. // playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  794. // if (err) this.log("ERROR", err);
  795. // else {
  796. // playlistModel.updateMany(
  797. // { "songs._id": payload.songId },
  798. // { $pull: { "songs.$._id": payload.songId} },
  799. // err => {
  800. // if (err) this.log("ERROR", err);
  801. // else {
  802. // playlists.forEach(playlist => {
  803. // PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  804. // playlistId: playlist._id
  805. // });
  806. // });
  807. // }
  808. // }
  809. // );
  810. // }
  811. // });
  812. // });
  813. // DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  814. // stationModel.find({ "queue._id": payload.songId }, (err, stations) => {
  815. // stationModel.updateMany(
  816. // { "queue._id": payload.songId },
  817. // {
  818. // $pull: { "queue._id": }
  819. // },
  820. // err => {
  821. // if (err) this.log("ERROR", err);
  822. // else {
  823. // stations.forEach(station => {
  824. // StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  825. // });
  826. // }
  827. // }
  828. // );
  829. // });
  830. // });
  831. // }
  832. // ],
  833. // err => {
  834. // if (err && err !== true) return reject(new Error(err));
  835. // return resolve();
  836. // }
  837. // )
  838. // );
  839. // }
  840. /**
  841. * Searches through songs
  842. *
  843. * @param {object} payload - object that contains the payload
  844. * @param {string} payload.query - the query
  845. * @param {string} payload.includeUnverified - include unverified songs
  846. * @param {string} payload.includeVerified - include verified songs
  847. * @param {string} payload.trimmed - include trimmed songs
  848. * @param {string} payload.page - page (default 1)
  849. * @returns {Promise} - returns promise (reject, resolve)
  850. */
  851. SEARCH(payload) {
  852. return new Promise((resolve, reject) => {
  853. async.waterfall(
  854. [
  855. next => {
  856. const isVerified = [];
  857. if (payload.includeUnverified) isVerified.push(false);
  858. if (payload.includeVerified) isVerified.push(true);
  859. if (isVerified.length === 0) return next("No verified status has been included.");
  860. let { query } = payload;
  861. const isRegex =
  862. query.length > 2 && query.indexOf("/") === 0 && query.lastIndexOf("/") === query.length - 1;
  863. if (isRegex) query = query.slice(1, query.length - 1);
  864. else query = query.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&");
  865. const filterArray = [
  866. {
  867. title: new RegExp(`${query}`, "i"),
  868. verified: { $in: isVerified }
  869. },
  870. {
  871. artists: new RegExp(`${query}`, "i"),
  872. verified: { $in: isVerified }
  873. }
  874. ];
  875. return next(null, filterArray);
  876. },
  877. (filterArray, next) => {
  878. const page = payload.page ? payload.page : 1;
  879. const pageSize = 15;
  880. const skipAmount = pageSize * (page - 1);
  881. SongsModule.SongModel.find({ $or: filterArray }).count((err, count) => {
  882. if (err) next(err);
  883. else {
  884. SongsModule.SongModel.find({ $or: filterArray })
  885. .skip(skipAmount)
  886. .limit(pageSize)
  887. .exec((err, songs) => {
  888. if (err) next(err);
  889. else {
  890. next(null, {
  891. songs,
  892. page,
  893. pageSize,
  894. skipAmount,
  895. count
  896. });
  897. }
  898. });
  899. }
  900. });
  901. },
  902. (data, next) => {
  903. if (data.songs.length === 0) next("No songs found");
  904. else if (payload.trimmed) {
  905. next(null, {
  906. songs: data.songs.map(song => {
  907. const { _id, youtubeId, title, artists, thumbnail, duration, verified } = song;
  908. return {
  909. _id,
  910. youtubeId,
  911. title,
  912. artists,
  913. thumbnail,
  914. duration,
  915. verified
  916. };
  917. }),
  918. ...data
  919. });
  920. } else next(null, data);
  921. }
  922. ],
  923. (err, data) => {
  924. if (err && err !== true) return reject(new Error(err));
  925. return resolve(data);
  926. }
  927. );
  928. });
  929. }
  930. /**
  931. * Gets an array of all genres
  932. *
  933. * @returns {Promise} - returns a promise (resolve, reject)
  934. */
  935. GET_ALL_GENRES() {
  936. return new Promise((resolve, reject) => {
  937. async.waterfall(
  938. [
  939. next => {
  940. SongsModule.SongModel.find({ verified: true }, { genres: 1, _id: false }, next);
  941. },
  942. (songs, next) => {
  943. let allGenres = [];
  944. songs.forEach(song => {
  945. allGenres = allGenres.concat(song.genres);
  946. });
  947. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  948. const uniqueGenres = lowerCaseGenres.filter(
  949. (value, index, self) => self.indexOf(value) === index
  950. );
  951. next(null, uniqueGenres);
  952. }
  953. ],
  954. (err, genres) => {
  955. if (err && err !== true) return reject(new Error(err));
  956. return resolve({ genres });
  957. }
  958. );
  959. });
  960. }
  961. /**
  962. * Gets an array of all songs with a specific genre
  963. *
  964. * @param {object} payload - returns an object containing the payload
  965. * @param {string} payload.genre - the genre
  966. * @returns {Promise} - returns a promise (resolve, reject)
  967. */
  968. GET_ALL_SONGS_WITH_GENRE(payload) {
  969. return new Promise((resolve, reject) => {
  970. async.waterfall(
  971. [
  972. next => {
  973. SongsModule.SongModel.find(
  974. {
  975. verified: true,
  976. genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") }
  977. },
  978. next
  979. );
  980. }
  981. ],
  982. (err, songs) => {
  983. if (err && err !== true) return reject(new Error(err));
  984. return resolve({ songs });
  985. }
  986. );
  987. });
  988. }
  989. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  990. /**
  991. * Gets a orphaned playlist songs
  992. *
  993. * @returns {Promise} - returns promise (reject, resolve)
  994. */
  995. GET_ORPHANED_PLAYLIST_SONGS() {
  996. return new Promise((resolve, reject) => {
  997. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  998. playlistModel.find({}, (err, playlists) => {
  999. if (err) reject(new Error(err));
  1000. else {
  1001. SongsModule.SongModel.find({}, { _id: true, youtubeId: true }, (err, songs) => {
  1002. if (err) reject(new Error(err));
  1003. else {
  1004. const songIds = songs.map(song => song._id.toString());
  1005. const orphanedYoutubeIds = new Set();
  1006. async.eachLimit(
  1007. playlists,
  1008. 1,
  1009. (playlist, next) => {
  1010. playlist.songs.forEach(song => {
  1011. if (
  1012. (!song._id || songIds.indexOf(song._id.toString() === -1)) &&
  1013. !orphanedYoutubeIds.has(song.youtubeId)
  1014. ) {
  1015. orphanedYoutubeIds.add(song.youtubeId);
  1016. }
  1017. });
  1018. next();
  1019. },
  1020. () => {
  1021. resolve({ youtubeIds: Array.from(orphanedYoutubeIds) });
  1022. }
  1023. );
  1024. }
  1025. });
  1026. }
  1027. });
  1028. });
  1029. });
  1030. }
  1031. /**
  1032. * Requests all orphaned playlist songs, adding them to the database
  1033. *
  1034. * @returns {Promise} - returns promise (reject, resolve)
  1035. */
  1036. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  1037. return new Promise((resolve, reject) => {
  1038. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  1039. .then(playlistModel => {
  1040. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  1041. const { youtubeIds } = response;
  1042. const playlistsToUpdate = new Set();
  1043. async.eachLimit(
  1044. youtubeIds,
  1045. 1,
  1046. (youtubeId, next) => {
  1047. async.waterfall(
  1048. [
  1049. next => {
  1050. console.log(
  1051. youtubeId,
  1052. `this is song ${youtubeIds.indexOf(youtubeId) + 1}/${youtubeIds.length}`
  1053. );
  1054. setTimeout(next, 150);
  1055. },
  1056. next => {
  1057. SongsModule.runJob(
  1058. "ENSURE_SONG_EXISTS_BY_SONG_ID",
  1059. { youtubeId, automaticallyRequested: true },
  1060. this
  1061. )
  1062. .then(() => next())
  1063. .catch(next);
  1064. },
  1065. next => {
  1066. console.log(444, youtubeId);
  1067. SongsModule.SongModel.findOne({ youtubeId }, next);
  1068. },
  1069. (song, next) => {
  1070. const { _id, title, artists, thumbnail, duration, verified } = song;
  1071. const trimmedSong = {
  1072. _id,
  1073. youtubeId,
  1074. title,
  1075. artists,
  1076. thumbnail,
  1077. duration,
  1078. verified
  1079. };
  1080. playlistModel.updateMany(
  1081. { "songs.youtubeId": song.youtubeId },
  1082. { $set: { "songs.$": trimmedSong } },
  1083. err => {
  1084. next(err, song);
  1085. }
  1086. );
  1087. },
  1088. (song, next) => {
  1089. playlistModel.find({ "songs._id": song._id }, next);
  1090. },
  1091. (playlists, next) => {
  1092. playlists.forEach(playlist => {
  1093. playlistsToUpdate.add(playlist._id.toString());
  1094. });
  1095. next();
  1096. }
  1097. ],
  1098. next
  1099. );
  1100. },
  1101. err => {
  1102. if (err) reject(err);
  1103. else {
  1104. async.eachLimit(
  1105. Array.from(playlistsToUpdate),
  1106. 1,
  1107. (playlistId, next) => {
  1108. PlaylistsModule.runJob(
  1109. "UPDATE_PLAYLIST",
  1110. {
  1111. playlistId
  1112. },
  1113. this
  1114. )
  1115. .then(() => {
  1116. next();
  1117. })
  1118. .catch(next);
  1119. },
  1120. err => {
  1121. if (err) reject(err);
  1122. else resolve();
  1123. }
  1124. );
  1125. }
  1126. }
  1127. );
  1128. });
  1129. })
  1130. .catch(reject);
  1131. });
  1132. }
  1133. /**
  1134. * Gets a list of all genres
  1135. *
  1136. * @returns {Promise} - returns promise (reject, resolve)
  1137. */
  1138. GET_GENRES() {
  1139. return new Promise((resolve, reject) => {
  1140. async.waterfall(
  1141. [
  1142. next => {
  1143. SongsModule.SongModel.distinct("genres", next);
  1144. }
  1145. ],
  1146. (err, genres) => {
  1147. if (err) reject(err);
  1148. resolve({ genres });
  1149. }
  1150. );
  1151. });
  1152. }
  1153. /**
  1154. * Gets a list of all artists
  1155. *
  1156. * @returns {Promise} - returns promise (reject, resolve)
  1157. */
  1158. GET_ARTISTS() {
  1159. return new Promise((resolve, reject) => {
  1160. async.waterfall(
  1161. [
  1162. next => {
  1163. SongsModule.SongModel.distinct("artists", next);
  1164. }
  1165. ],
  1166. (err, artists) => {
  1167. if (err) reject(err);
  1168. resolve({ artists });
  1169. }
  1170. );
  1171. });
  1172. }
  1173. /**
  1174. * Gets a list of all tags
  1175. *
  1176. * @returns {Promise} - returns promise (reject, resolve)
  1177. */
  1178. GET_TAGS() {
  1179. return new Promise((resolve, reject) => {
  1180. async.waterfall(
  1181. [
  1182. next => {
  1183. SongsModule.SongModel.distinct("tags", next);
  1184. }
  1185. ],
  1186. (err, tags) => {
  1187. if (err) reject(err);
  1188. resolve({ tags });
  1189. }
  1190. );
  1191. });
  1192. }
  1193. }
  1194. export default new _SongsModule();