songs.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. import async from "async";
  2. import config from "config";
  3. import mongoose from "mongoose";
  4. import CoreClass from "../core";
  5. let SongsModule;
  6. let CacheModule;
  7. let DBModule;
  8. let UtilsModule;
  9. let YouTubeModule;
  10. let StationsModule;
  11. let PlaylistsModule;
  12. class _SongsModule extends CoreClass {
  13. // eslint-disable-next-line require-jsdoc
  14. constructor() {
  15. super("songs");
  16. SongsModule = this;
  17. }
  18. /**
  19. * Initialises the songs module
  20. *
  21. * @returns {Promise} - returns promise (reject, resolve)
  22. */
  23. async initialize() {
  24. this.setStage(1);
  25. CacheModule = this.moduleManager.modules.cache;
  26. DBModule = this.moduleManager.modules.db;
  27. UtilsModule = this.moduleManager.modules.utils;
  28. YouTubeModule = this.moduleManager.modules.youtube;
  29. StationsModule = this.moduleManager.modules.stations;
  30. PlaylistsModule = this.moduleManager.modules.playlists;
  31. this.SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  32. this.SongSchemaCache = await CacheModule.runJob("GET_SCHEMA", { schemaName: "song" });
  33. this.setStage(2);
  34. return new Promise((resolve, reject) =>
  35. async.waterfall(
  36. [
  37. next => {
  38. this.setStage(2);
  39. CacheModule.runJob("HGETALL", { table: "songs" })
  40. .then(songs => {
  41. next(null, songs);
  42. })
  43. .catch(next);
  44. },
  45. (songs, next) => {
  46. this.setStage(3);
  47. if (!songs) return next();
  48. const songIds = Object.keys(songs);
  49. return async.each(
  50. songIds,
  51. (songId, next) => {
  52. SongsModule.SongModel.findOne({ songId }, (err, song) => {
  53. if (err) next(err);
  54. else if (!song)
  55. CacheModule.runJob("HDEL", {
  56. table: "songs",
  57. key: songId
  58. })
  59. .then(() => next())
  60. .catch(next);
  61. else next();
  62. });
  63. },
  64. next
  65. );
  66. },
  67. next => {
  68. this.setStage(4);
  69. SongsModule.SongModel.find({}, next);
  70. },
  71. (songs, next) => {
  72. this.setStage(5);
  73. async.each(
  74. songs,
  75. (song, next) => {
  76. CacheModule.runJob("HSET", {
  77. table: "songs",
  78. key: song.songId,
  79. value: SongsModule.SongSchemaCache(song)
  80. })
  81. .then(() => next())
  82. .catch(next);
  83. },
  84. next
  85. );
  86. }
  87. ],
  88. async err => {
  89. if (err) {
  90. err = await UtilsModule.runJob("GET_ERROR", { error: err });
  91. reject(new Error(err));
  92. } else resolve();
  93. }
  94. )
  95. );
  96. }
  97. /**
  98. * Gets a song by id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  99. *
  100. * @param {object} payload - object containing the payload
  101. * @param {string} payload.id - the id of the song we are trying to get
  102. * @returns {Promise} - returns a promise (resolve, reject)
  103. */
  104. GET_SONG(payload) {
  105. return new Promise((resolve, reject) =>
  106. async.waterfall(
  107. [
  108. next => {
  109. if (!mongoose.Types.ObjectId.isValid(payload.id)) return next("Id is not a valid ObjectId.");
  110. return CacheModule.runJob("HGET", { table: "songs", key: payload.id }, this)
  111. .then(song => next(null, song))
  112. .catch(next);
  113. },
  114. (song, next) => {
  115. if (song) return next(true, song);
  116. return SongsModule.SongModel.findOne({ _id: payload.id }, next);
  117. },
  118. (song, next) => {
  119. if (song) {
  120. CacheModule.runJob(
  121. "HSET",
  122. {
  123. table: "songs",
  124. key: payload.id,
  125. value: song
  126. },
  127. this
  128. ).then(song => next(null, song));
  129. } else next("Song not found.");
  130. }
  131. ],
  132. (err, song) => {
  133. if (err && err !== true) return reject(new Error(err));
  134. return resolve({ song });
  135. }
  136. )
  137. );
  138. }
  139. /**
  140. * Makes sure that if a song is not currently in the songs db, to add it
  141. *
  142. * @param {object} payload - an object containing the payload
  143. * @param {string} payload.songId - the youtube song id of the song we are trying to ensure is in the songs db
  144. * @returns {Promise} - returns a promise (resolve, reject)
  145. */
  146. ENSURE_SONG_EXISTS_BY_SONG_ID(payload) {
  147. return new Promise((resolve, reject) =>
  148. async.waterfall(
  149. [
  150. next => {
  151. SongsModule.SongModel.findOne({ songId: payload.songId }, next);
  152. },
  153. (song, next) => {
  154. if (song && song.duration > 0) next(true, song);
  155. else {
  156. YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  157. .then(response => {
  158. next(null, song, response.song);
  159. })
  160. .catch(next);
  161. }
  162. // else if (song && song.duration <= 0) {
  163. // YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  164. // .then(response => next(null, { ...response.song }, false))
  165. // .catch(next);
  166. // } else {
  167. // YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  168. // .then(response => next(null, { ...response.song }, false))
  169. // .catch(next);
  170. // }
  171. },
  172. (song, youtubeSong, next) => {
  173. if (song && song.duration <= 0) {
  174. song.duration = youtubeSong.duration;
  175. song.save({ validateBeforeSave: true }, err => {
  176. if (err) return next(err, song);
  177. return next(null, song);
  178. });
  179. } else {
  180. const song = new SongsModule.SongModel({ ...youtubeSong });
  181. song.save({ validateBeforeSave: true }, err => {
  182. if (err) return next(err, song);
  183. return next(null, song);
  184. });
  185. }
  186. }
  187. ],
  188. (err, song) => {
  189. if (err && err !== true) return reject(new Error(err));
  190. return resolve({ song });
  191. }
  192. )
  193. );
  194. }
  195. /**
  196. * Gets a song by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  197. *
  198. * @param {object} payload - an object containing the payload
  199. * @param {string} payload.songId - the mongo id of the song we are trying to get
  200. * @returns {Promise} - returns a promise (resolve, reject)
  201. */
  202. GET_SONG_FROM_ID(payload) {
  203. return new Promise((resolve, reject) =>
  204. async.waterfall(
  205. [
  206. next => {
  207. SongsModule.SongModel.findOne({ songId: payload.songId }, next);
  208. }
  209. ],
  210. (err, song) => {
  211. if (err && err !== true) return reject(new Error(err));
  212. return resolve({ song });
  213. }
  214. )
  215. );
  216. }
  217. /**
  218. * Gets a song from id from Mongo and updates the cache with it
  219. *
  220. * @param {object} payload - an object containing the payload
  221. * @param {string} payload.songId - the id of the song we are trying to update
  222. * @returns {Promise} - returns a promise (resolve, reject)
  223. */
  224. UPDATE_SONG(payload) {
  225. return new Promise((resolve, reject) =>
  226. async.waterfall(
  227. [
  228. next => {
  229. SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  230. },
  231. (song, next) => {
  232. if (!song) {
  233. CacheModule.runJob("HDEL", {
  234. table: "songs",
  235. key: payload.songId
  236. });
  237. return next("Song not found.");
  238. }
  239. return CacheModule.runJob(
  240. "HSET",
  241. {
  242. table: "songs",
  243. key: payload.songId,
  244. value: song
  245. },
  246. this
  247. )
  248. .then(song => {
  249. next(null, song);
  250. })
  251. .catch(next);
  252. },
  253. (song, next) => {
  254. next(null, song);
  255. const { _id, songId, title, artists, thumbnail, duration, verified } = song;
  256. const trimmedSong = {
  257. _id,
  258. songId,
  259. title,
  260. artists,
  261. thumbnail,
  262. duration,
  263. verified
  264. };
  265. this.log("INFO", `Going to update playlists and stations now for song ${_id}`);
  266. DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  267. playlistModel.updateMany(
  268. { "songs._id": song._id },
  269. { $set: { "songs.$": trimmedSong } },
  270. err => {
  271. if (err) this.log("ERROR", err);
  272. else
  273. playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  274. playlists.forEach(playlist => {
  275. PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  276. playlistId: playlist._id
  277. });
  278. });
  279. });
  280. }
  281. );
  282. });
  283. DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  284. stationModel.updateMany(
  285. { "queue._id": song._id },
  286. {
  287. $set: {
  288. "queue.$.songId": songId,
  289. "queue.$.title": title,
  290. "queue.$.artists": artists,
  291. "queue.$.thumbnail": thumbnail,
  292. "queue.$.duration": duration,
  293. "queue.$.verified": verified
  294. }
  295. },
  296. err => {
  297. if (err) this.log("ERROR", err);
  298. else
  299. stationModel.find({ "queue._id": song._id }, (err, stations) => {
  300. stations.forEach(station => {
  301. StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  302. });
  303. });
  304. }
  305. );
  306. });
  307. }
  308. ],
  309. (err, song) => {
  310. if (err && err !== true) return reject(new Error(err));
  311. return resolve(song);
  312. }
  313. )
  314. );
  315. }
  316. /**
  317. * Deletes song from id from Mongo and cache
  318. *
  319. * @param {object} payload - returns an object containing the payload
  320. * @param {string} payload.songId - the id of the song we are trying to delete
  321. * @returns {Promise} - returns a promise (resolve, reject)
  322. */
  323. DELETE_SONG(payload) {
  324. return new Promise((resolve, reject) =>
  325. async.waterfall(
  326. [
  327. next => {
  328. SongsModule.SongModel.deleteOne({ songId: payload.songId }, next);
  329. },
  330. next => {
  331. CacheModule.runJob(
  332. "HDEL",
  333. {
  334. table: "songs",
  335. key: payload.songId
  336. },
  337. this
  338. )
  339. .then(() => next())
  340. .catch(next);
  341. }
  342. ],
  343. err => {
  344. if (err && err !== true) return reject(new Error(err));
  345. return resolve();
  346. }
  347. )
  348. );
  349. }
  350. /**
  351. * Recalculates dislikes and likes for a song
  352. *
  353. * @param {object} payload - returns an object containing the payload
  354. * @param {string} payload.musareSongId - the (musare) id of the song
  355. * @param {string} payload.songId - the (mongodb) id of the song
  356. * @returns {Promise} - returns a promise (resolve, reject)
  357. */
  358. async RECALCULATE_SONG_RATINGS(payload) {
  359. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  360. return new Promise((resolve, reject) => {
  361. async.waterfall(
  362. [
  363. next => {
  364. playlistModel.countDocuments(
  365. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Liked Songs" },
  366. (err, likes) => {
  367. if (err) return next(err);
  368. return next(null, likes);
  369. }
  370. );
  371. },
  372. (likes, next) => {
  373. playlistModel.countDocuments(
  374. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Disliked Songs" },
  375. (err, dislikes) => {
  376. if (err) return next(err);
  377. return next(err, { likes, dislikes });
  378. }
  379. );
  380. },
  381. ({ likes, dislikes }, next) => {
  382. SongsModule.SongModel.updateOne(
  383. { _id: payload.songId },
  384. {
  385. $set: {
  386. likes,
  387. dislikes
  388. }
  389. },
  390. err => next(err, { likes, dislikes })
  391. );
  392. }
  393. ],
  394. (err, { likes, dislikes }) => {
  395. if (err) return reject(new Error(err));
  396. return resolve({ likes, dislikes });
  397. }
  398. );
  399. });
  400. }
  401. /**
  402. * Gets an array of all genres
  403. *
  404. * @returns {Promise} - returns a promise (resolve, reject)
  405. */
  406. GET_ALL_GENRES() {
  407. return new Promise((resolve, reject) =>
  408. async.waterfall(
  409. [
  410. next => {
  411. SongsModule.SongModel.find({ verified: true }, { genres: 1, _id: false }, next);
  412. },
  413. (songs, next) => {
  414. let allGenres = [];
  415. songs.forEach(song => {
  416. allGenres = allGenres.concat(song.genres);
  417. });
  418. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  419. const uniqueGenres = lowerCaseGenres.filter(
  420. (value, index, self) => self.indexOf(value) === index
  421. );
  422. next(null, uniqueGenres);
  423. }
  424. ],
  425. (err, genres) => {
  426. if (err && err !== true) return reject(new Error(err));
  427. return resolve({ genres });
  428. }
  429. )
  430. );
  431. }
  432. /**
  433. * Gets an array of all songs with a specific genre
  434. *
  435. * @param {object} payload - returns an object containing the payload
  436. * @param {string} payload.genre - the genre
  437. * @returns {Promise} - returns a promise (resolve, reject)
  438. */
  439. GET_ALL_SONGS_WITH_GENRE(payload) {
  440. return new Promise((resolve, reject) =>
  441. async.waterfall(
  442. [
  443. next => {
  444. SongsModule.SongModel.find(
  445. { verified: true, genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") } },
  446. next
  447. );
  448. }
  449. ],
  450. (err, songs) => {
  451. if (err && err !== true) return reject(new Error(err));
  452. return resolve({ songs });
  453. }
  454. )
  455. );
  456. }
  457. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  458. /**
  459. * Gets a orphaned playlist songs
  460. *
  461. * @returns {Promise} - returns promise (reject, resolve)
  462. */
  463. GET_ORPHANED_PLAYLIST_SONGS() {
  464. return new Promise((resolve, reject) => {
  465. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  466. playlistModel.find({}, (err, playlists) => {
  467. if (err) reject(new Error(err));
  468. else {
  469. SongsModule.SongModel.find({}, { _id: true, songId: true }, (err, songs) => {
  470. if (err) reject(new Error(err));
  471. else {
  472. const musareSongIds = songs.map(song => song._id.toString());
  473. const orphanedSongIds = new Set();
  474. async.eachLimit(
  475. playlists,
  476. 1,
  477. (playlist, next) => {
  478. playlist.songs.forEach(song => {
  479. if (
  480. (!song._id || musareSongIds.indexOf(song._id.toString() === -1)) &&
  481. !orphanedSongIds.has(song.songId)
  482. ) {
  483. orphanedSongIds.add(song.songId);
  484. }
  485. });
  486. next();
  487. },
  488. () => {
  489. resolve({ songIds: Array.from(orphanedSongIds) });
  490. }
  491. );
  492. }
  493. });
  494. }
  495. });
  496. });
  497. });
  498. }
  499. /**
  500. * Requests a song, adding it to the DB
  501. *
  502. * @param {object} payload - The payload
  503. * @param {string} payload.songId - The YouTube song id of the song
  504. * @param {string} payload.userId - The user id of the person requesting the song
  505. * @returns {Promise} - returns promise (reject, resolve)
  506. */
  507. REQUEST_SONG(payload) {
  508. return new Promise((resolve, reject) => {
  509. const { songId, userId } = payload;
  510. const requestedAt = Date.now();
  511. async.waterfall(
  512. [
  513. next => {
  514. SongsModule.SongModel.findOne({ songId }, next);
  515. },
  516. // Get YouTube data from id
  517. (song, next) => {
  518. if (song) return next("This song is already in the database.");
  519. // TODO Add err object as first param of callback
  520. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  521. .then(response => {
  522. const { song } = response;
  523. song.artists = [];
  524. song.genres = [];
  525. song.skipDuration = 0;
  526. song.explicit = false;
  527. song.requestedBy = userId;
  528. song.requestedAt = requestedAt;
  529. song.verified = false;
  530. next(null, song);
  531. })
  532. .catch(next);
  533. },
  534. (newSong, next) => {
  535. const song = new SongsModule.SongModel(newSong);
  536. song.save({ validateBeforeSave: false }, err => {
  537. if (err) return next(err, song);
  538. return next(null, song);
  539. });
  540. },
  541. (song, next) => {
  542. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  543. .then(UserModel => {
  544. UserModel.findOne({ _id: userId }, (err, user) => {
  545. if (err) return next(err);
  546. if (!user) return next(null, song);
  547. user.statistics.songsRequested += 1;
  548. return user.save(err => {
  549. if (err) return next(err);
  550. return next(null, song);
  551. });
  552. });
  553. })
  554. .catch(next);
  555. }
  556. ],
  557. async (err, song) => {
  558. if (err) reject(err);
  559. CacheModule.runJob("PUB", {
  560. channel: "song.newUnverifiedSong",
  561. value: song._id
  562. });
  563. resolve();
  564. }
  565. );
  566. });
  567. }
  568. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  569. /**
  570. * Requests all orphaned playlist songs, adding them to the database
  571. *
  572. * @returns {Promise} - returns promise (reject, resolve)
  573. */
  574. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  575. return new Promise((resolve, reject) => {
  576. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  577. .then(playlistModel => {
  578. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  579. const { songIds } = response;
  580. const playlistsToUpdate = new Set();
  581. async.eachLimit(
  582. songIds,
  583. 1,
  584. (songId, next) => {
  585. async.waterfall(
  586. [
  587. next => {
  588. console.log(
  589. songId,
  590. `this is song ${songIds.indexOf(songId) + 1}/${songIds.length}`
  591. );
  592. setTimeout(next, 150);
  593. },
  594. next => {
  595. SongsModule.runJob("ENSURE_SONG_EXISTS_BY_SONG_ID", { songId }, this)
  596. .then(() => next())
  597. .catch(next);
  598. // SongsModule.runJob("REQUEST_SONG", { songId, userId: null }, this)
  599. // .then(() => {
  600. // next();
  601. // })
  602. // .catch(next);
  603. },
  604. next => {
  605. console.log(444, songId);
  606. SongsModule.SongModel.findOne({ songId }, next);
  607. },
  608. (song, next) => {
  609. const { _id, title, artists, thumbnail, duration, verified } = song;
  610. const trimmedSong = {
  611. _id,
  612. songId,
  613. title,
  614. artists,
  615. thumbnail,
  616. duration,
  617. verified
  618. };
  619. playlistModel.updateMany(
  620. { "songs.songId": song.songId },
  621. { $set: { "songs.$": trimmedSong } },
  622. (err, res) => {
  623. next(err, song);
  624. }
  625. );
  626. },
  627. (song, next) => {
  628. playlistModel.find({ "songs._id": song._id }, next);
  629. },
  630. (playlists, next) => {
  631. playlists.forEach(playlist => {
  632. playlistsToUpdate.add(playlist._id.toString());
  633. });
  634. next();
  635. }
  636. ],
  637. next
  638. );
  639. },
  640. err => {
  641. if (err) reject(err);
  642. else {
  643. async.eachLimit(
  644. Array.from(playlistsToUpdate),
  645. 1,
  646. (playlistId, next) => {
  647. PlaylistsModule.runJob(
  648. "UPDATE_PLAYLIST",
  649. {
  650. playlistId
  651. },
  652. this
  653. )
  654. .then(() => {
  655. next();
  656. })
  657. .catch(next);
  658. },
  659. err => {
  660. if (err) reject(err);
  661. else resolve();
  662. }
  663. );
  664. }
  665. }
  666. );
  667. });
  668. })
  669. .catch(reject);
  670. });
  671. }
  672. }
  673. export default new _SongsModule();