songs.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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, status } = song;
  256. const trimmedSong = {
  257. _id,
  258. songId,
  259. title,
  260. artists,
  261. thumbnail,
  262. duration,
  263. status
  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.$.status": status
  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({ status: "verified" }, { 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. {
  446. status: "verified",
  447. genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") }
  448. },
  449. next
  450. );
  451. }
  452. ],
  453. (err, songs) => {
  454. if (err && err !== true) return reject(new Error(err));
  455. return resolve({ songs });
  456. }
  457. )
  458. );
  459. }
  460. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  461. /**
  462. * Gets a orphaned playlist songs
  463. *
  464. * @returns {Promise} - returns promise (reject, resolve)
  465. */
  466. GET_ORPHANED_PLAYLIST_SONGS() {
  467. return new Promise((resolve, reject) => {
  468. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  469. playlistModel.find({}, (err, playlists) => {
  470. if (err) reject(new Error(err));
  471. else {
  472. SongsModule.SongModel.find({}, { _id: true, songId: true }, (err, songs) => {
  473. if (err) reject(new Error(err));
  474. else {
  475. const musareSongIds = songs.map(song => song._id.toString());
  476. const orphanedSongIds = new Set();
  477. async.eachLimit(
  478. playlists,
  479. 1,
  480. (playlist, next) => {
  481. playlist.songs.forEach(song => {
  482. if (
  483. (!song._id || musareSongIds.indexOf(song._id.toString() === -1)) &&
  484. !orphanedSongIds.has(song.songId)
  485. ) {
  486. orphanedSongIds.add(song.songId);
  487. }
  488. });
  489. next();
  490. },
  491. () => {
  492. resolve({ songIds: Array.from(orphanedSongIds) });
  493. }
  494. );
  495. }
  496. });
  497. }
  498. });
  499. });
  500. });
  501. }
  502. /**
  503. * Requests a song, adding it to the DB
  504. *
  505. * @param {object} payload - The payload
  506. * @param {string} payload.songId - The YouTube song id of the song
  507. * @param {string} payload.userId - The user id of the person requesting the song
  508. * @returns {Promise} - returns promise (reject, resolve)
  509. */
  510. REQUEST_SONG(payload) {
  511. return new Promise((resolve, reject) => {
  512. const { songId, userId } = payload;
  513. const requestedAt = Date.now();
  514. async.waterfall(
  515. [
  516. next => {
  517. SongsModule.SongModel.findOne({ songId }, next);
  518. },
  519. // Get YouTube data from id
  520. (song, next) => {
  521. if (song) return next("This song is already in the database.");
  522. // TODO Add err object as first param of callback
  523. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  524. .then(response => {
  525. const { song } = response;
  526. song.artists = [];
  527. song.genres = [];
  528. song.skipDuration = 0;
  529. song.explicit = false;
  530. song.requestedBy = userId;
  531. song.requestedAt = requestedAt;
  532. song.status = "unverified";
  533. next(null, song);
  534. })
  535. .catch(next);
  536. },
  537. (newSong, next) => {
  538. const song = new SongsModule.SongModel(newSong);
  539. song.save({ validateBeforeSave: false }, err => {
  540. if (err) return next(err, song);
  541. return next(null, song);
  542. });
  543. },
  544. (song, next) => {
  545. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  546. .then(UserModel => {
  547. UserModel.findOne({ _id: userId }, (err, user) => {
  548. if (err) return next(err);
  549. if (!user) return next(null, song);
  550. user.statistics.songsRequested += 1;
  551. return user.save(err => {
  552. if (err) return next(err);
  553. return next(null, song);
  554. });
  555. });
  556. })
  557. .catch(next);
  558. }
  559. ],
  560. async (err, song) => {
  561. if (err) reject(err);
  562. CacheModule.runJob("PUB", {
  563. channel: "song.newUnverifiedSong",
  564. value: song._id
  565. });
  566. resolve();
  567. }
  568. );
  569. });
  570. }
  571. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  572. /**
  573. * Requests all orphaned playlist songs, adding them to the database
  574. *
  575. * @returns {Promise} - returns promise (reject, resolve)
  576. */
  577. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  578. return new Promise((resolve, reject) => {
  579. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  580. .then(playlistModel => {
  581. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  582. const { songIds } = response;
  583. const playlistsToUpdate = new Set();
  584. async.eachLimit(
  585. songIds,
  586. 1,
  587. (songId, next) => {
  588. async.waterfall(
  589. [
  590. next => {
  591. console.log(
  592. songId,
  593. `this is song ${songIds.indexOf(songId) + 1}/${songIds.length}`
  594. );
  595. setTimeout(next, 150);
  596. },
  597. next => {
  598. SongsModule.runJob("ENSURE_SONG_EXISTS_BY_SONG_ID", { songId }, this)
  599. .then(() => next())
  600. .catch(next);
  601. // SongsModule.runJob("REQUEST_SONG", { songId, userId: null }, this)
  602. // .then(() => {
  603. // next();
  604. // })
  605. // .catch(next);
  606. },
  607. next => {
  608. console.log(444, songId);
  609. SongsModule.SongModel.findOne({ songId }, next);
  610. },
  611. (song, next) => {
  612. const { _id, title, artists, thumbnail, duration, status } = song;
  613. const trimmedSong = {
  614. _id,
  615. songId,
  616. title,
  617. artists,
  618. thumbnail,
  619. duration,
  620. status
  621. };
  622. playlistModel.updateMany(
  623. { "songs.songId": song.songId },
  624. { $set: { "songs.$": trimmedSong } },
  625. (err, res) => {
  626. next(err, song);
  627. }
  628. );
  629. },
  630. (song, next) => {
  631. playlistModel.find({ "songs._id": song._id }, next);
  632. },
  633. (playlists, next) => {
  634. playlists.forEach(playlist => {
  635. playlistsToUpdate.add(playlist._id.toString());
  636. });
  637. next();
  638. }
  639. ],
  640. next
  641. );
  642. },
  643. err => {
  644. if (err) reject(err);
  645. else {
  646. async.eachLimit(
  647. Array.from(playlistsToUpdate),
  648. 1,
  649. (playlistId, next) => {
  650. PlaylistsModule.runJob(
  651. "UPDATE_PLAYLIST",
  652. {
  653. playlistId
  654. },
  655. this
  656. )
  657. .then(() => {
  658. next();
  659. })
  660. .catch(next);
  661. },
  662. err => {
  663. if (err) reject(err);
  664. else resolve();
  665. }
  666. );
  667. }
  668. }
  669. );
  670. });
  671. })
  672. .catch(reject);
  673. });
  674. }
  675. }
  676. export default new _SongsModule();