songs.js 32 KB

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