songs.js 32 KB

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