songs.js 33 KB

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