songs.js 33 KB

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