songs.js 31 KB

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