songs.js 33 KB

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