songs.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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.songs);
  545. })
  546. .catch(err => {
  547. next(err);
  548. });
  549. }
  550. ],
  551. async (err, songs) => {
  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: { songs } });
  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. songId: song._id,
  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. songId: song._id,
  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. songId: song._id,
  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. songId: song._id,
  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. songModel.findOne({ youtubeId }, next);
  1334. },
  1335. (song, next) => {
  1336. if (!song) return next("No song found with that id.");
  1337. return next(null);
  1338. },
  1339. next =>
  1340. playlistModel.findOne(
  1341. { createdBy: session.userId, displayName: "Liked Songs" },
  1342. (err, playlist) => {
  1343. if (err) return next(err);
  1344. if (!playlist) return next("'Liked Songs' playlist does not exist.");
  1345. let isLiked = false;
  1346. Object.values(playlist.songs).forEach(song => {
  1347. // song is found in 'liked songs' playlist
  1348. if (song.youtubeId === youtubeId) isLiked = true;
  1349. });
  1350. return next(null, isLiked);
  1351. }
  1352. ),
  1353. (isLiked, next) =>
  1354. playlistModel.findOne(
  1355. { createdBy: session.userId, displayName: "Disliked Songs" },
  1356. (err, playlist) => {
  1357. if (err) return next(err);
  1358. if (!playlist) return next("'Disliked Songs' playlist does not exist.");
  1359. const ratings = { isLiked, isDisliked: false };
  1360. Object.values(playlist.songs).forEach(song => {
  1361. // song is found in 'disliked songs' playlist
  1362. if (song.youtubeId === youtubeId) ratings.isDisliked = true;
  1363. });
  1364. return next(null, ratings);
  1365. }
  1366. )
  1367. ],
  1368. async (err, ratings) => {
  1369. if (err) {
  1370. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  1371. this.log(
  1372. "ERROR",
  1373. "SONGS_GET_OWN_RATINGS",
  1374. `User "${session.userId}" failed to get ratings for ${youtubeId}. "${err}"`
  1375. );
  1376. return cb({ status: "error", message: err });
  1377. }
  1378. const { isLiked, isDisliked } = ratings;
  1379. return cb({
  1380. status: "success",
  1381. data: {
  1382. youtubeId,
  1383. liked: isLiked,
  1384. disliked: isDisliked
  1385. }
  1386. });
  1387. }
  1388. );
  1389. })
  1390. };