songs.js 32 KB

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