songs.js 34 KB

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