songs.js 32 KB

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