songs.js 37 KB

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