songs.js 32 KB

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