songs.js 39 KB

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