songs.js 31 KB

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