songs.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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) next(true, song);
  155. else {
  156. YouTubeModule.runJob("GET_SONG", { songId: payload.songId }, this)
  157. .then(response => next(null, { ...response.song }))
  158. .catch(next);
  159. }
  160. },
  161. (_song, next) => {
  162. const song = new SongsModule.SongModel({ ..._song });
  163. song.save({ validateBeforeSave: true }, err => {
  164. if (err) return next(err, song);
  165. return next(null, song);
  166. });
  167. }
  168. ],
  169. (err, song) => {
  170. if (err && err !== true) return reject(new Error(err));
  171. return resolve({ song });
  172. }
  173. )
  174. );
  175. }
  176. /**
  177. * Gets a song by song id from the cache or Mongo, and if it isn't in the cache yet, adds it the cache
  178. *
  179. * @param {object} payload - an object containing the payload
  180. * @param {string} payload.songId - the mongo id of the song we are trying to get
  181. * @returns {Promise} - returns a promise (resolve, reject)
  182. */
  183. GET_SONG_FROM_ID(payload) {
  184. return new Promise((resolve, reject) =>
  185. async.waterfall(
  186. [
  187. next => {
  188. SongsModule.SongModel.findOne({ songId: payload.songId }, next);
  189. }
  190. ],
  191. (err, song) => {
  192. if (err && err !== true) return reject(new Error(err));
  193. return resolve({ song });
  194. }
  195. )
  196. );
  197. }
  198. /**
  199. * Gets a song from id from Mongo and updates the cache with it
  200. *
  201. * @param {object} payload - an object containing the payload
  202. * @param {string} payload.songId - the id of the song we are trying to update
  203. * @returns {Promise} - returns a promise (resolve, reject)
  204. */
  205. UPDATE_SONG(payload) {
  206. return new Promise((resolve, reject) =>
  207. async.waterfall(
  208. [
  209. next => {
  210. SongsModule.SongModel.findOne({ _id: payload.songId }, next);
  211. },
  212. (song, next) => {
  213. if (!song) {
  214. CacheModule.runJob("HDEL", {
  215. table: "songs",
  216. key: payload.songId
  217. });
  218. return next("Song not found.");
  219. }
  220. return CacheModule.runJob(
  221. "HSET",
  222. {
  223. table: "songs",
  224. key: payload.songId,
  225. value: song
  226. },
  227. this
  228. )
  229. .then(song => {
  230. next(null, song);
  231. })
  232. .catch(next);
  233. },
  234. (song, next) => {
  235. next(null, song);
  236. const { _id, songId, title, artists, thumbnail, duration, verified } = song;
  237. const trimmedSong = {
  238. _id,
  239. songId,
  240. title,
  241. artists,
  242. thumbnail,
  243. duration,
  244. verified
  245. };
  246. this.log("INFO", `Going to update playlists and stations now for song ${_id}`);
  247. DBModule.runJob("GET_MODEL", { modelName: "playlist" }).then(playlistModel => {
  248. playlistModel.updateMany(
  249. { "songs._id": song._id },
  250. { $set: { "songs.$": trimmedSong } },
  251. err => {
  252. if (err) this.log("ERROR", err);
  253. else
  254. playlistModel.find({ "songs._id": song._id }, (err, playlists) => {
  255. playlists.forEach(playlist => {
  256. PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  257. playlistId: playlist._id
  258. });
  259. });
  260. });
  261. }
  262. );
  263. });
  264. DBModule.runJob("GET_MODEL", { modelName: "station" }).then(stationModel => {
  265. stationModel.updateMany(
  266. { "queue._id": song._id },
  267. {
  268. $set: {
  269. "queue.$.songId": songId,
  270. "queue.$.title": title,
  271. "queue.$.artists": artists,
  272. "queue.$.thumbnail": thumbnail,
  273. "queue.$.duration": duration,
  274. "queue.$.verified": verified
  275. }
  276. },
  277. err => {
  278. if (err) this.log("ERROR", err);
  279. else
  280. stationModel.find({ "queue._id": song._id }, (err, stations) => {
  281. stations.forEach(station => {
  282. StationsModule.runJob("UPDATE_STATION", { stationId: station._id });
  283. });
  284. });
  285. }
  286. );
  287. });
  288. }
  289. ],
  290. (err, song) => {
  291. if (err && err !== true) return reject(new Error(err));
  292. return resolve(song);
  293. }
  294. )
  295. );
  296. }
  297. /**
  298. * Deletes song from id from Mongo and cache
  299. *
  300. * @param {object} payload - returns an object containing the payload
  301. * @param {string} payload.songId - the id of the song we are trying to delete
  302. * @returns {Promise} - returns a promise (resolve, reject)
  303. */
  304. DELETE_SONG(payload) {
  305. return new Promise((resolve, reject) =>
  306. async.waterfall(
  307. [
  308. next => {
  309. SongsModule.SongModel.deleteOne({ songId: payload.songId }, next);
  310. },
  311. next => {
  312. CacheModule.runJob(
  313. "HDEL",
  314. {
  315. table: "songs",
  316. key: payload.songId
  317. },
  318. this
  319. )
  320. .then(() => next())
  321. .catch(next);
  322. }
  323. ],
  324. err => {
  325. if (err && err !== true) return reject(new Error(err));
  326. return resolve();
  327. }
  328. )
  329. );
  330. }
  331. /**
  332. * Recalculates dislikes and likes for a song
  333. *
  334. * @param {object} payload - returns an object containing the payload
  335. * @param {string} payload.musareSongId - the (musare) id of the song
  336. * @param {string} payload.songId - the (mongodb) id of the song
  337. * @returns {Promise} - returns a promise (resolve, reject)
  338. */
  339. async RECALCULATE_SONG_RATINGS(payload) {
  340. const playlistModel = await DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this);
  341. return new Promise((resolve, reject) => {
  342. async.waterfall(
  343. [
  344. next => {
  345. playlistModel.countDocuments(
  346. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Liked Songs" },
  347. (err, likes) => {
  348. if (err) return next(err);
  349. return next(null, likes);
  350. }
  351. );
  352. },
  353. (likes, next) => {
  354. playlistModel.countDocuments(
  355. { songs: { $elemMatch: { songId: payload.musareSongId } }, displayName: "Disliked Songs" },
  356. (err, dislikes) => {
  357. if (err) return next(err);
  358. return next(err, { likes, dislikes });
  359. }
  360. );
  361. },
  362. ({ likes, dislikes }, next) => {
  363. SongsModule.SongModel.updateOne(
  364. { _id: payload.songId },
  365. {
  366. $set: {
  367. likes,
  368. dislikes
  369. }
  370. },
  371. err => next(err, { likes, dislikes })
  372. );
  373. }
  374. ],
  375. (err, { likes, dislikes }) => {
  376. if (err) return reject(new Error(err));
  377. return resolve({ likes, dislikes });
  378. }
  379. );
  380. });
  381. }
  382. /**
  383. * Gets an array of all genres
  384. *
  385. * @returns {Promise} - returns a promise (resolve, reject)
  386. */
  387. GET_ALL_GENRES() {
  388. return new Promise((resolve, reject) =>
  389. async.waterfall(
  390. [
  391. next => {
  392. SongsModule.SongModel.find({ verified: true }, { genres: 1, _id: false }, next);
  393. },
  394. (songs, next) => {
  395. let allGenres = [];
  396. songs.forEach(song => {
  397. allGenres = allGenres.concat(song.genres);
  398. });
  399. const lowerCaseGenres = allGenres.map(genre => genre.toLowerCase());
  400. const uniqueGenres = lowerCaseGenres.filter(
  401. (value, index, self) => self.indexOf(value) === index
  402. );
  403. next(null, uniqueGenres);
  404. }
  405. ],
  406. (err, genres) => {
  407. if (err && err !== true) return reject(new Error(err));
  408. return resolve({ genres });
  409. }
  410. )
  411. );
  412. }
  413. /**
  414. * Gets an array of all songs with a specific genre
  415. *
  416. * @param {object} payload - returns an object containing the payload
  417. * @param {string} payload.genre - the genre
  418. * @returns {Promise} - returns a promise (resolve, reject)
  419. */
  420. GET_ALL_SONGS_WITH_GENRE(payload) {
  421. return new Promise((resolve, reject) =>
  422. async.waterfall(
  423. [
  424. next => {
  425. SongsModule.SongModel.find(
  426. { verified: true, genres: { $regex: new RegExp(`^${payload.genre.toLowerCase()}$`, "i") } },
  427. next
  428. );
  429. }
  430. ],
  431. (err, songs) => {
  432. if (err && err !== true) return reject(new Error(err));
  433. return resolve({ songs });
  434. }
  435. )
  436. );
  437. }
  438. // runjob songs GET_ORPHANED_PLAYLIST_SONGS {}
  439. /**
  440. * Gets a orphaned playlist songs
  441. *
  442. * @returns {Promise} - returns promise (reject, resolve)
  443. */
  444. GET_ORPHANED_PLAYLIST_SONGS() {
  445. return new Promise((resolve, reject) => {
  446. DBModule.runJob("GET_MODEL", { modelName: "playlist" }, this).then(playlistModel => {
  447. playlistModel.find({}, (err, playlists) => {
  448. if (err) reject(new Error(err));
  449. else {
  450. SongsModule.SongModel.find({}, { songId: true }, (err, songs) => {
  451. if (err) reject(new Error(err));
  452. else {
  453. const songIds = songs.map(song => song.songId);
  454. const orphanedSongIds = new Set();
  455. async.eachLimit(
  456. playlists,
  457. 1,
  458. (playlist, next) => {
  459. playlist.songs.forEach(song => {
  460. if (
  461. songIds.indexOf(song.songId) === -1 &&
  462. !orphanedSongIds.has(song.songId)
  463. ) {
  464. orphanedSongIds.add(song.songId);
  465. }
  466. });
  467. next();
  468. },
  469. () => {
  470. resolve({ songIds: Array.from(orphanedSongIds) });
  471. }
  472. );
  473. }
  474. });
  475. }
  476. });
  477. });
  478. });
  479. }
  480. /**
  481. * Requests a song, adding it to the DB
  482. *
  483. * @param {object} payload - The payload
  484. * @param {string} payload.songId - The YouTube song id of the song
  485. * @param {string} payload.userId - The user id of the person requesting the song
  486. * @returns {Promise} - returns promise (reject, resolve)
  487. */
  488. REQUEST_SONG(payload) {
  489. return new Promise((resolve, reject) => {
  490. const { songId, userId } = payload;
  491. const requestedAt = Date.now();
  492. async.waterfall(
  493. [
  494. next => {
  495. SongsModule.SongModel.findOne({ songId }, next);
  496. },
  497. // Get YouTube data from id
  498. (song, next) => {
  499. if (song) return next("This song is already in the database.");
  500. // TODO Add err object as first param of callback
  501. return YouTubeModule.runJob("GET_SONG", { songId }, this)
  502. .then(response => {
  503. const { song } = response;
  504. song.artists = [];
  505. song.genres = [];
  506. song.skipDuration = 0;
  507. song.explicit = false;
  508. song.requestedBy = userId;
  509. song.requestedAt = requestedAt;
  510. song.verified = false;
  511. next(null, song);
  512. })
  513. .catch(next);
  514. },
  515. (newSong, next) => {
  516. const song = new SongsModule.SongModel(newSong);
  517. song.save({ validateBeforeSave: false }, err => {
  518. if (err) return next(err, song);
  519. return next(null, song);
  520. });
  521. },
  522. (song, next) => {
  523. DBModule.runJob("GET_MODEL", { modelName: "user" }, this)
  524. .then(UserModel => {
  525. UserModel.findOne({ _id: userId }, (err, user) => {
  526. if (err) return next(err);
  527. if (!user) return next(null, song);
  528. user.statistics.songsRequested += 1;
  529. return user.save(err => {
  530. if (err) return next(err);
  531. return next(null, song);
  532. });
  533. });
  534. })
  535. .catch(next);
  536. }
  537. ],
  538. async (err, song) => {
  539. if (err) reject(err);
  540. CacheModule.runJob("PUB", {
  541. channel: "song.newUnverifiedSong",
  542. value: song._id
  543. });
  544. resolve();
  545. }
  546. );
  547. });
  548. }
  549. // runjob songs REQUEST_ORPHANED_PLAYLIST_SONGS {}
  550. /**
  551. * Requests all orphaned playlist songs, adding them to the database
  552. *
  553. * @returns {Promise} - returns promise (reject, resolve)
  554. */
  555. REQUEST_ORPHANED_PLAYLIST_SONGS() {
  556. return new Promise((resolve, reject) => {
  557. DBModule.runJob("GET_MODEL", { modelName: "playlist" })
  558. .then(playlistModel => {
  559. SongsModule.runJob("GET_ORPHANED_PLAYLIST_SONGS", {}, this).then(response => {
  560. const { songIds } = response;
  561. async.eachLimit(
  562. songIds,
  563. 1,
  564. (songId, next) => {
  565. async.waterfall(
  566. [
  567. next => {
  568. SongsModule.runJob("ENSURE_SONG_EXISTS_BY_SONG_ID", { songId }, this)
  569. .then(() => next())
  570. .catch(next);
  571. // SongsModule.runJob("REQUEST_SONG", { songId, userId: null }, this)
  572. // .then(() => {
  573. // next();
  574. // })
  575. // .catch(next);
  576. },
  577. next => {
  578. SongsModule.SongModel.findOne({ songId }, next);
  579. },
  580. (song, next) => {
  581. const { _id, title, artists, thumbnail, duration, verified } = song;
  582. const trimmedSong = {
  583. _id,
  584. songId,
  585. title,
  586. artists,
  587. thumbnail,
  588. duration,
  589. verified
  590. };
  591. playlistModel.updateMany(
  592. { "songs.songId": song.songId },
  593. { $set: { "songs.$": trimmedSong } },
  594. err => {
  595. next(err, song);
  596. }
  597. );
  598. },
  599. (song, next) => {
  600. playlistModel.find({ "songs._id": song._id }, next);
  601. },
  602. (playlists, next) => {
  603. playlists.forEach(playlist => {
  604. PlaylistsModule.runJob("UPDATE_PLAYLIST", {
  605. playlistId: playlist._id
  606. });
  607. });
  608. next();
  609. }
  610. ],
  611. next
  612. );
  613. },
  614. err => {
  615. if (err) reject(err);
  616. else resolve();
  617. }
  618. );
  619. });
  620. })
  621. .catch(reject);
  622. });
  623. }
  624. }
  625. export default new _SongsModule();