songs.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 IOModule = moduleManager.modules.io;
  7. const CacheModule = moduleManager.modules.cache;
  8. const SongsModule = moduleManager.modules.songs;
  9. const ActivitiesModule = moduleManager.modules.activities;
  10. CacheModule.runJob("SUB", {
  11. channel: "song.removed",
  12. cb: songId => {
  13. IOModule.runJob("EMIT_TO_ROOM", {
  14. room: "admin.songs",
  15. args: ["event:admin.song.removed", songId]
  16. });
  17. }
  18. });
  19. CacheModule.runJob("SUB", {
  20. channel: "song.added",
  21. cb: async songId => {
  22. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  23. songModel.findOne({ _id: songId }, (err, song) => {
  24. IOModule.runJob("EMIT_TO_ROOM", {
  25. room: "admin.songs",
  26. args: ["event:admin.song.added", song]
  27. });
  28. });
  29. }
  30. });
  31. CacheModule.runJob("SUB", {
  32. channel: "song.updated",
  33. cb: async songId => {
  34. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" });
  35. songModel.findOne({ _id: songId }, (err, song) => {
  36. IOModule.runJob("EMIT_TO_ROOM", {
  37. room: "admin.songs",
  38. args: ["event:admin.song.updated", song]
  39. });
  40. });
  41. }
  42. });
  43. CacheModule.runJob("SUB", {
  44. channel: "song.like",
  45. cb: data => {
  46. IOModule.runJob("EMIT_TO_ROOM", {
  47. room: `song.${data.songId}`,
  48. args: [
  49. "event:song.like",
  50. {
  51. songId: data.songId,
  52. likes: data.likes,
  53. dislikes: data.dislikes
  54. }
  55. ]
  56. });
  57. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  58. response.sockets.forEach(socket => {
  59. socket.emit("event:song.newRatings", {
  60. songId: data.songId,
  61. liked: true,
  62. disliked: false
  63. });
  64. });
  65. });
  66. }
  67. });
  68. CacheModule.runJob("SUB", {
  69. channel: "song.dislike",
  70. cb: data => {
  71. IOModule.runJob("EMIT_TO_ROOM", {
  72. room: `song.${data.songId}`,
  73. args: [
  74. "event:song.dislike",
  75. {
  76. songId: data.songId,
  77. likes: data.likes,
  78. dislikes: data.dislikes
  79. }
  80. ]
  81. });
  82. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  83. response.sockets.forEach(socket => {
  84. socket.emit("event:song.newRatings", {
  85. songId: data.songId,
  86. liked: false,
  87. disliked: true
  88. });
  89. });
  90. });
  91. }
  92. });
  93. CacheModule.runJob("SUB", {
  94. channel: "song.unlike",
  95. cb: data => {
  96. IOModule.runJob("EMIT_TO_ROOM", {
  97. room: `song.${data.songId}`,
  98. args: [
  99. "event:song.unlike",
  100. {
  101. songId: data.songId,
  102. likes: data.likes,
  103. dislikes: data.dislikes
  104. }
  105. ]
  106. });
  107. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  108. response.sockets.forEach(socket => {
  109. socket.emit("event:song.newRatings", {
  110. songId: data.songId,
  111. liked: false,
  112. disliked: false
  113. });
  114. });
  115. });
  116. }
  117. });
  118. CacheModule.runJob("SUB", {
  119. channel: "song.undislike",
  120. cb: data => {
  121. IOModule.runJob("EMIT_TO_ROOM", {
  122. room: `song.${data.songId}`,
  123. args: [
  124. "event:song.undislike",
  125. {
  126. songId: data.songId,
  127. likes: data.likes,
  128. dislikes: data.dislikes
  129. }
  130. ]
  131. });
  132. IOModule.runJob("SOCKETS_FROM_USER", { userId: data.userId }).then(response => {
  133. response.sockets.forEach(socket => {
  134. socket.emit("event:song.newRatings", {
  135. songId: data.songId,
  136. liked: false,
  137. disliked: false
  138. });
  139. });
  140. });
  141. }
  142. });
  143. export default {
  144. /**
  145. * Returns the length of the songs list
  146. *
  147. * @param {object} session - the session object automatically added by socket.io
  148. * @param cb
  149. */
  150. length: isAdminRequired(async function length(session, cb) {
  151. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  152. async.waterfall(
  153. [
  154. next => {
  155. songModel.countDocuments({}, next);
  156. }
  157. ],
  158. async (err, count) => {
  159. if (err) {
  160. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  161. this.log("ERROR", "SONGS_LENGTH", `Failed to get length from songs. "${err}"`);
  162. return cb({ status: "failure", message: err });
  163. }
  164. this.log("SUCCESS", "SONGS_LENGTH", `Got length from songs successfully.`);
  165. return cb(count);
  166. }
  167. );
  168. }),
  169. /**
  170. * Gets a set of songs
  171. *
  172. * @param {object} session - the session object automatically added by socket.io
  173. * @param set - the set number to return
  174. * @param cb
  175. */
  176. getSet: isAdminRequired(async function getSet(session, set, cb) {
  177. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  178. async.waterfall(
  179. [
  180. next => {
  181. songModel
  182. .find({})
  183. .skip(15 * (set - 1))
  184. .limit(15)
  185. .exec(next);
  186. }
  187. ],
  188. async (err, songs) => {
  189. if (err) {
  190. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  191. this.log("ERROR", "SONGS_GET_SET", `Failed to get set from songs. "${err}"`);
  192. return cb({ status: "failure", message: err });
  193. }
  194. this.log("SUCCESS", "SONGS_GET_SET", `Got set from songs successfully.`);
  195. return cb(songs);
  196. }
  197. );
  198. }),
  199. /**
  200. * Gets a song
  201. *
  202. * @param {object} session - the session object automatically added by socket.io
  203. * @param {string} songId - the song id
  204. * @param {Function} cb
  205. */
  206. getSong: isAdminRequired(function getSong(session, songId, cb) {
  207. async.waterfall(
  208. [
  209. next => {
  210. SongsModule.runJob("GET_SONG_FROM_ID", { songId }, this)
  211. .then(song => {
  212. next(null, song);
  213. })
  214. .catch(err => {
  215. next(err);
  216. });
  217. }
  218. ],
  219. async (err, song) => {
  220. if (err) {
  221. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  222. this.log("ERROR", "SONGS_GET_SONG", `Failed to get song ${songId}. "${err}"`);
  223. return cb({ status: "failure", message: err });
  224. }
  225. this.log("SUCCESS", "SONGS_GET_SONG", `Got song ${songId} successfully.`);
  226. return cb({ status: "success", data: song });
  227. }
  228. );
  229. }),
  230. /**
  231. * Obtains basic metadata of a song in order to format an activity
  232. *
  233. * @param {object} session - the session object automatically added by socket.io
  234. * @param {string} songId - the song id
  235. * @param {Function} cb - callback
  236. */
  237. getSongForActivity(session, songId, cb) {
  238. async.waterfall(
  239. [
  240. next => {
  241. SongsModule.runJob("GET_SONG_FROM_ID", { songId }, this)
  242. .then(response => next(null, response.song))
  243. .catch(next);
  244. }
  245. ],
  246. async (err, song) => {
  247. if (err) {
  248. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  249. this.log(
  250. "ERROR",
  251. "SONGS_GET_SONG_FOR_ACTIVITY",
  252. `Failed to obtain metadata of song ${songId} for activity formatting. "${err}"`
  253. );
  254. return cb({ status: "failure", message: err });
  255. }
  256. if (song) {
  257. this.log(
  258. "SUCCESS",
  259. "SONGS_GET_SONG_FOR_ACTIVITY",
  260. `Obtained metadata of song ${songId} for activity formatting successfully.`
  261. );
  262. return cb({
  263. status: "success",
  264. data: {
  265. title: song.title,
  266. thumbnail: song.thumbnail
  267. }
  268. });
  269. }
  270. this.log(
  271. "ERROR",
  272. "SONGS_GET_SONG_FOR_ACTIVITY",
  273. `Song ${songId} does not exist so failed to obtain for activity formatting.`
  274. );
  275. return cb({ status: "failure" });
  276. }
  277. );
  278. },
  279. /**
  280. * Updates a song
  281. *
  282. * @param {object} session - the session object automatically added by socket.io
  283. * @param {string} songId - the song id
  284. * @param {object} song - the updated song object
  285. * @param {Function} cb
  286. */
  287. update: isAdminRequired(async function update(session, songId, song, cb) {
  288. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  289. async.waterfall(
  290. [
  291. next => {
  292. songModel.updateOne({ _id: songId }, song, { runValidators: true }, next);
  293. },
  294. (res, next) => {
  295. SongsModule.runJob("UPDATE_SONG", { songId }, this)
  296. .then(song => {
  297. next(null, song);
  298. })
  299. .catch(next);
  300. }
  301. ],
  302. async (err, song) => {
  303. if (err) {
  304. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  305. this.log("ERROR", "SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  306. return cb({ status: "failure", message: err });
  307. }
  308. this.log("SUCCESS", "SONGS_UPDATE", `Successfully updated song "${songId}".`);
  309. CacheModule.runJob("PUB", {
  310. channel: "song.updated",
  311. value: song.songId
  312. });
  313. return cb({
  314. status: "success",
  315. message: "Song has been successfully updated",
  316. data: song
  317. });
  318. }
  319. );
  320. }),
  321. /**
  322. * Removes a song
  323. *
  324. * @param session
  325. * @param songId - the song id
  326. * @param cb
  327. */
  328. remove: isAdminRequired(async function remove(session, songId, cb) {
  329. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  330. async.waterfall(
  331. [
  332. next => {
  333. songModel.deleteOne({ _id: songId }, next);
  334. },
  335. (res, next) => {
  336. // TODO Check if res gets returned from above
  337. CacheModule.runJob("HDEL", { table: "songs", key: songId }, this)
  338. .then(() => {
  339. next();
  340. })
  341. .catch(next);
  342. }
  343. ],
  344. async err => {
  345. if (err) {
  346. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  347. this.log("ERROR", "SONGS_UPDATE", `Failed to remove song "${songId}". "${err}"`);
  348. return cb({ status: "failure", message: err });
  349. }
  350. this.log("SUCCESS", "SONGS_UPDATE", `Successfully remove song "${songId}".`);
  351. CacheModule.runJob("PUB", { channel: "song.removed", value: songId });
  352. return cb({
  353. status: "success",
  354. message: "Song has been successfully updated"
  355. });
  356. }
  357. );
  358. }),
  359. /**
  360. * Adds a song
  361. *
  362. * @param session
  363. * @param song - the song object
  364. * @param cb
  365. */
  366. add: isAdminRequired(async function add(session, song, cb) {
  367. const SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  368. async.waterfall(
  369. [
  370. next => {
  371. SongModel.findOne({ songId: song.songId }, next);
  372. },
  373. (existingSong, next) => {
  374. if (existingSong) return next("Song is already in rotation.");
  375. return next();
  376. },
  377. next => {
  378. const newSong = new SongModel(song);
  379. newSong.acceptedBy = session.userId;
  380. newSong.acceptedAt = Date.now();
  381. newSong.save(next);
  382. },
  383. (res, next) => {
  384. this.module
  385. .runJob(
  386. "RUN_ACTION2",
  387. {
  388. session,
  389. namespace: "queueSongs",
  390. action: "remove",
  391. args: [song._id]
  392. },
  393. this
  394. )
  395. .finally(() => {
  396. next();
  397. });
  398. }
  399. ],
  400. async err => {
  401. if (err) {
  402. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  403. this.log("ERROR", "SONGS_ADD", `User "${session.userId}" failed to add song. "${err}"`);
  404. return cb({ status: "failure", message: err });
  405. }
  406. this.log("SUCCESS", "SONGS_ADD", `User "${session.userId}" successfully added song "${song.songId}".`);
  407. CacheModule.runJob("PUB", {
  408. channel: "song.added",
  409. value: song.songId
  410. });
  411. return cb({
  412. status: "success",
  413. message: "Song has been moved from the queue successfully."
  414. });
  415. }
  416. );
  417. // TODO Check if video is in queue and Add the song to the appropriate stations
  418. }),
  419. /**
  420. * Likes a song
  421. *
  422. * @param session
  423. * @param songId - the song id
  424. * @param cb
  425. */
  426. like: isLoginRequired(async function like(session, songId, cb) {
  427. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  428. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  429. async.waterfall(
  430. [
  431. next => {
  432. songModel.findOne({ songId }, next);
  433. },
  434. (song, next) => {
  435. if (!song) return next("No song found with that id.");
  436. return next(null, song);
  437. }
  438. ],
  439. async (err, song) => {
  440. if (err) {
  441. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  442. this.log("ERROR", "SONGS_LIKE", `User "${session.userId}" failed to like song ${songId}. "${err}"`);
  443. return cb({ status: "failure", message: err });
  444. }
  445. const oldSongId = songId;
  446. songId = song._id;
  447. return userModel.findOne({ _id: session.userId }, (err, user) => {
  448. if (user.liked.indexOf(songId) !== -1)
  449. return cb({
  450. status: "failure",
  451. message: "You have already liked this song."
  452. });
  453. return userModel.updateOne(
  454. { _id: session.userId },
  455. {
  456. $push: { liked: songId },
  457. $pull: { disliked: songId }
  458. },
  459. err => {
  460. if (!err) {
  461. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  462. if (err)
  463. return cb({
  464. status: "failure",
  465. message: "Something went wrong while liking this song."
  466. });
  467. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  468. if (err)
  469. return cb({
  470. status: "failure",
  471. message: "Something went wrong while liking this song."
  472. });
  473. return songModel.update(
  474. { _id: songId },
  475. {
  476. $set: {
  477. likes,
  478. dislikes
  479. }
  480. },
  481. err => {
  482. if (err)
  483. return cb({
  484. status: "failure",
  485. message: "Something went wrong while liking this song."
  486. });
  487. SongsModule.runJob("UPDATE_SONG", { songId });
  488. CacheModule.runJob("PUB", {
  489. channel: "song.like",
  490. value: JSON.stringify({
  491. songId: oldSongId,
  492. userId: session.userId,
  493. likes,
  494. dislikes
  495. })
  496. });
  497. ActivitiesModule.runJob("ADD_ACTIVITY", {
  498. userId: session.userId,
  499. activityType: "liked_song",
  500. payload: [songId]
  501. });
  502. return cb({
  503. status: "success",
  504. message: "You have successfully liked this song."
  505. });
  506. }
  507. );
  508. });
  509. });
  510. }
  511. return cb({
  512. status: "failure",
  513. message: "Something went wrong while liking this song."
  514. });
  515. }
  516. );
  517. });
  518. }
  519. );
  520. }),
  521. /**
  522. * Dislikes a song
  523. *
  524. * @param session
  525. * @param songId - the song id
  526. * @param cb
  527. */
  528. dislike: isLoginRequired(async function dislike(session, songId, cb) {
  529. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  530. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  531. async.waterfall(
  532. [
  533. next => {
  534. songModel.findOne({ songId }, next);
  535. },
  536. (song, next) => {
  537. if (!song) return next("No song found with that id.");
  538. return next(null, song);
  539. }
  540. ],
  541. async (err, song) => {
  542. if (err) {
  543. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  544. this.log(
  545. "ERROR",
  546. "SONGS_DISLIKE",
  547. `User "${session.userId}" failed to like song ${songId}. "${err}"`
  548. );
  549. return cb({ status: "failure", message: err });
  550. }
  551. const oldSongId = songId;
  552. songId = song._id;
  553. return userModel.findOne({ _id: session.userId }, (err, user) => {
  554. if (user.disliked.indexOf(songId) !== -1)
  555. return cb({
  556. status: "failure",
  557. message: "You have already disliked this song."
  558. });
  559. return userModel.updateOne(
  560. { _id: session.userId },
  561. {
  562. $push: { disliked: songId },
  563. $pull: { liked: songId }
  564. },
  565. err => {
  566. if (!err) {
  567. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  568. if (err)
  569. return cb({
  570. status: "failure",
  571. message: "Something went wrong while disliking this song."
  572. });
  573. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  574. if (err)
  575. return cb({
  576. status: "failure",
  577. message: "Something went wrong while disliking this song."
  578. });
  579. return songModel.update(
  580. { _id: songId },
  581. {
  582. $set: {
  583. likes,
  584. dislikes
  585. }
  586. },
  587. err => {
  588. if (err)
  589. return cb({
  590. status: "failure",
  591. message: "Something went wrong while disliking this song."
  592. });
  593. SongsModule.runJob("UPDATE_SONG", { songId });
  594. CacheModule.runJob("PUB", {
  595. channel: "song.dislike",
  596. value: JSON.stringify({
  597. songId: oldSongId,
  598. userId: session.userId,
  599. likes,
  600. dislikes
  601. })
  602. });
  603. return cb({
  604. status: "success",
  605. message: "You have successfully disliked this song."
  606. });
  607. }
  608. );
  609. });
  610. });
  611. }
  612. return cb({
  613. status: "failure",
  614. message: "Something went wrong while disliking this song."
  615. });
  616. }
  617. );
  618. });
  619. }
  620. );
  621. }),
  622. /**
  623. * Undislikes a song
  624. *
  625. * @param session
  626. * @param songId - the song id
  627. * @param cb
  628. */
  629. undislike: isLoginRequired(async function undislike(session, songId, cb) {
  630. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  631. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  632. async.waterfall(
  633. [
  634. next => {
  635. songModel.findOne({ songId }, next);
  636. },
  637. (song, next) => {
  638. if (!song) return next("No song found with that id.");
  639. return next(null, song);
  640. }
  641. ],
  642. async (err, song) => {
  643. if (err) {
  644. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  645. this.log(
  646. "ERROR",
  647. "SONGS_UNDISLIKE",
  648. `User "${session.userId}" failed to like song ${songId}. "${err}"`
  649. );
  650. return cb({ status: "failure", message: err });
  651. }
  652. const oldSongId = songId;
  653. songId = song._id;
  654. return userModel.findOne({ _id: session.userId }, (err, user) => {
  655. if (user.disliked.indexOf(songId) === -1)
  656. return cb({
  657. status: "failure",
  658. message: "You have not disliked this song."
  659. });
  660. return userModel.updateOne(
  661. { _id: session.userId },
  662. { $pull: { liked: songId, disliked: songId } },
  663. err => {
  664. if (!err) {
  665. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  666. if (err)
  667. return cb({
  668. status: "failure",
  669. message: "Something went wrong while undisliking this song."
  670. });
  671. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  672. if (err)
  673. return cb({
  674. status: "failure",
  675. message: "Something went wrong while undisliking this song."
  676. });
  677. return songModel.update(
  678. { _id: songId },
  679. {
  680. $set: {
  681. likes,
  682. dislikes
  683. }
  684. },
  685. err => {
  686. if (err)
  687. return cb({
  688. status: "failure",
  689. message: "Something went wrong while undisliking this song."
  690. });
  691. SongsModule.runJob("UPDATE_SONG", { songId });
  692. CacheModule.runJob("PUB", {
  693. channel: "song.undislike",
  694. value: JSON.stringify({
  695. songId: oldSongId,
  696. userId: session.userId,
  697. likes,
  698. dislikes
  699. })
  700. });
  701. return cb({
  702. status: "success",
  703. message: "You have successfully undisliked this song."
  704. });
  705. }
  706. );
  707. });
  708. });
  709. }
  710. return cb({
  711. status: "failure",
  712. message: "Something went wrong while undisliking this song."
  713. });
  714. }
  715. );
  716. });
  717. }
  718. );
  719. }),
  720. /**
  721. * Unlikes a song
  722. *
  723. * @param session
  724. * @param songId - the song id
  725. * @param cb
  726. */
  727. unlike: isLoginRequired(async function unlike(session, songId, cb) {
  728. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  729. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  730. async.waterfall(
  731. [
  732. next => {
  733. songModel.findOne({ songId }, next);
  734. },
  735. (song, next) => {
  736. if (!song) return next("No song found with that id.");
  737. return next(null, song);
  738. }
  739. ],
  740. async (err, song) => {
  741. if (err) {
  742. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  743. this.log(
  744. "ERROR",
  745. "SONGS_UNLIKE",
  746. `User "${session.userId}" failed to like song ${songId}. "${err}"`
  747. );
  748. return cb({ status: "failure", message: err });
  749. }
  750. const oldSongId = songId;
  751. songId = song._id;
  752. return userModel.findOne({ _id: session.userId }, (err, user) => {
  753. if (user.liked.indexOf(songId) === -1)
  754. return cb({
  755. status: "failure",
  756. message: "You have not liked this song."
  757. });
  758. return userModel.updateOne(
  759. { _id: session.userId },
  760. { $pull: { liked: songId, disliked: songId } },
  761. err => {
  762. if (!err) {
  763. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  764. if (err)
  765. return cb({
  766. status: "failure",
  767. message: "Something went wrong while unliking this song."
  768. });
  769. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  770. if (err)
  771. return cb({
  772. status: "failure",
  773. message: "Something went wrong while undiking this song."
  774. });
  775. return songModel.updateOne(
  776. { _id: songId },
  777. {
  778. $set: {
  779. likes,
  780. dislikes
  781. }
  782. },
  783. err => {
  784. if (err)
  785. return cb({
  786. status: "failure",
  787. message: "Something went wrong while unliking this song."
  788. });
  789. SongsModule.runJob("UPDATE_SONG", { songId });
  790. CacheModule.runJob("PUB", {
  791. channel: "song.unlike",
  792. value: JSON.stringify({
  793. songId: oldSongId,
  794. userId: session.userId,
  795. likes,
  796. dislikes
  797. })
  798. });
  799. return cb({
  800. status: "success",
  801. message: "You have successfully unliked this song."
  802. });
  803. }
  804. );
  805. });
  806. });
  807. }
  808. return cb({
  809. status: "failure",
  810. message: "Something went wrong while unliking this song."
  811. });
  812. }
  813. );
  814. });
  815. }
  816. );
  817. }),
  818. /**
  819. * Gets user's own song ratings
  820. *
  821. * @param session
  822. * @param songId - the song id
  823. * @param cb
  824. */
  825. getOwnSongRatings: isLoginRequired(async function getOwnSongRatings(session, songId, cb) {
  826. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  827. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  828. async.waterfall(
  829. [
  830. next => {
  831. songModel.findOne({ songId }, next);
  832. },
  833. (song, next) => {
  834. if (!song) return next("No song found with that id.");
  835. return next(null, song);
  836. }
  837. ],
  838. async (err, song) => {
  839. if (err) {
  840. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  841. this.log(
  842. "ERROR",
  843. "SONGS_GET_OWN_RATINGS",
  844. `User "${session.userId}" failed to get ratings for ${songId}. "${err}"`
  845. );
  846. return cb({ status: "failure", message: err });
  847. }
  848. const newSongId = song._id;
  849. return userModel.findOne({ _id: session.userId }, async (err, user) => {
  850. if (!err && user) {
  851. return cb({
  852. status: "success",
  853. songId,
  854. liked: user.liked.indexOf(newSongId) !== -1,
  855. disliked: user.disliked.indexOf(newSongId) !== -1
  856. });
  857. }
  858. return cb({
  859. status: "failure",
  860. message: await UtilsModule.runJob(
  861. "GET_ERROR",
  862. {
  863. error: err
  864. },
  865. this
  866. )
  867. });
  868. });
  869. }
  870. );
  871. })
  872. };