songs.js 32 KB

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