songs.js 33 KB

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