songs.js 34 KB

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