songs.js 36 KB

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