songs.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 from the YouTube song id
  201. *
  202. * @param {object} session - the session object automatically added by socket.io
  203. * @param {string} songId - the YouTube 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. * Gets a song from the Musare song id
  232. *
  233. * @param {object} session - the session object automatically added by socket.io
  234. * @param {string} songId - the Musare song id
  235. * @param {Function} cb
  236. */
  237. getSongFromMusareId: isAdminRequired(function getSong(session, songId, cb) {
  238. async.waterfall(
  239. [
  240. next => {
  241. SongsModule.runJob("GET_SONG", { id: songId }, this)
  242. .then(response => {
  243. next(null, response.song);
  244. })
  245. .catch(err => {
  246. next(err);
  247. });
  248. }
  249. ],
  250. async (err, song) => {
  251. if (err) {
  252. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  253. this.log("ERROR", "SONGS_GET_SONG_FROM_MUSARE_ID", `Failed to get song ${songId}. "${err}"`);
  254. return cb({ status: "failure", message: err });
  255. }
  256. this.log("SUCCESS", "SONGS_GET_SONG_FROM_MUSARE_ID", `Got song ${songId} successfully.`);
  257. return cb({ status: "success", data: { song } });
  258. }
  259. );
  260. }),
  261. /**
  262. * Obtains basic metadata of a song in order to format an activity
  263. *
  264. * @param {object} session - the session object automatically added by socket.io
  265. * @param {string} songId - the song id
  266. * @param {Function} cb - callback
  267. */
  268. getSongForActivity(session, songId, cb) {
  269. async.waterfall(
  270. [
  271. next => {
  272. SongsModule.runJob("GET_SONG_FROM_ID", { songId }, this)
  273. .then(response => next(null, response.song))
  274. .catch(next);
  275. }
  276. ],
  277. async (err, song) => {
  278. if (err) {
  279. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  280. this.log(
  281. "ERROR",
  282. "SONGS_GET_SONG_FOR_ACTIVITY",
  283. `Failed to obtain metadata of song ${songId} for activity formatting. "${err}"`
  284. );
  285. return cb({ status: "failure", message: err });
  286. }
  287. if (song) {
  288. this.log(
  289. "SUCCESS",
  290. "SONGS_GET_SONG_FOR_ACTIVITY",
  291. `Obtained metadata of song ${songId} for activity formatting successfully.`
  292. );
  293. return cb({
  294. status: "success",
  295. data: {
  296. title: song.title,
  297. thumbnail: song.thumbnail
  298. }
  299. });
  300. }
  301. this.log(
  302. "ERROR",
  303. "SONGS_GET_SONG_FOR_ACTIVITY",
  304. `Song ${songId} does not exist so failed to obtain for activity formatting.`
  305. );
  306. return cb({ status: "failure" });
  307. }
  308. );
  309. },
  310. /**
  311. * Updates a song
  312. *
  313. * @param {object} session - the session object automatically added by socket.io
  314. * @param {string} songId - the song id
  315. * @param {object} song - the updated song object
  316. * @param {Function} cb
  317. */
  318. update: isAdminRequired(async function update(session, songId, song, cb) {
  319. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  320. async.waterfall(
  321. [
  322. next => {
  323. songModel.updateOne({ _id: songId }, song, { runValidators: true }, next);
  324. },
  325. (res, next) => {
  326. SongsModule.runJob("UPDATE_SONG", { songId }, this)
  327. .then(song => {
  328. next(null, song);
  329. })
  330. .catch(next);
  331. }
  332. ],
  333. async (err, song) => {
  334. if (err) {
  335. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  336. this.log("ERROR", "SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  337. return cb({ status: "failure", message: err });
  338. }
  339. this.log("SUCCESS", "SONGS_UPDATE", `Successfully updated song "${songId}".`);
  340. CacheModule.runJob("PUB", {
  341. channel: "song.updated",
  342. value: song.songId
  343. });
  344. return cb({
  345. status: "success",
  346. message: "Song has been successfully updated",
  347. data: song
  348. });
  349. }
  350. );
  351. }),
  352. /**
  353. * Removes a song
  354. *
  355. * @param session
  356. * @param songId - the song id
  357. * @param cb
  358. */
  359. remove: isAdminRequired(async function remove(session, songId, cb) {
  360. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  361. async.waterfall(
  362. [
  363. next => {
  364. songModel.deleteOne({ _id: songId }, next);
  365. },
  366. (res, next) => {
  367. // TODO Check if res gets returned from above
  368. CacheModule.runJob("HDEL", { table: "songs", key: songId }, this)
  369. .then(() => {
  370. next();
  371. })
  372. .catch(next);
  373. }
  374. ],
  375. async err => {
  376. if (err) {
  377. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  378. this.log("ERROR", "SONGS_UPDATE", `Failed to remove song "${songId}". "${err}"`);
  379. return cb({ status: "failure", message: err });
  380. }
  381. this.log("SUCCESS", "SONGS_UPDATE", `Successfully remove song "${songId}".`);
  382. CacheModule.runJob("PUB", { channel: "song.removed", value: songId });
  383. return cb({
  384. status: "success",
  385. message: "Song has been successfully updated"
  386. });
  387. }
  388. );
  389. }),
  390. /**
  391. * Adds a song
  392. *
  393. * @param session
  394. * @param song - the song object
  395. * @param cb
  396. */
  397. add: isAdminRequired(async function add(session, song, cb) {
  398. const SongModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  399. async.waterfall(
  400. [
  401. next => {
  402. SongModel.findOne({ songId: song.songId }, next);
  403. },
  404. (existingSong, next) => {
  405. if (existingSong) return next("Song is already in rotation.");
  406. return next();
  407. },
  408. next => {
  409. const newSong = new SongModel(song);
  410. newSong.acceptedBy = session.userId;
  411. newSong.acceptedAt = Date.now();
  412. newSong.save(next);
  413. },
  414. (res, next) => {
  415. this.module
  416. .runJob(
  417. "RUN_ACTION2",
  418. {
  419. session,
  420. namespace: "queueSongs",
  421. action: "remove",
  422. args: [song._id]
  423. },
  424. this
  425. )
  426. .finally(() => {
  427. next();
  428. });
  429. }
  430. ],
  431. async err => {
  432. if (err) {
  433. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  434. this.log("ERROR", "SONGS_ADD", `User "${session.userId}" failed to add song. "${err}"`);
  435. return cb({ status: "failure", message: err });
  436. }
  437. this.log("SUCCESS", "SONGS_ADD", `User "${session.userId}" successfully added song "${song.songId}".`);
  438. CacheModule.runJob("PUB", {
  439. channel: "song.added",
  440. value: song.songId
  441. });
  442. return cb({
  443. status: "success",
  444. message: "Song has been moved from the queue successfully."
  445. });
  446. }
  447. );
  448. // TODO Check if video is in queue and Add the song to the appropriate stations
  449. }),
  450. /**
  451. * Likes a song
  452. *
  453. * @param session
  454. * @param songId - the song id
  455. * @param cb
  456. */
  457. like: isLoginRequired(async function like(session, songId, cb) {
  458. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  459. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  460. async.waterfall(
  461. [
  462. next => {
  463. songModel.findOne({ songId }, next);
  464. },
  465. (song, next) => {
  466. if (!song) return next("No song found with that id.");
  467. return next(null, song);
  468. }
  469. ],
  470. async (err, song) => {
  471. if (err) {
  472. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  473. this.log("ERROR", "SONGS_LIKE", `User "${session.userId}" failed to like song ${songId}. "${err}"`);
  474. return cb({ status: "failure", message: err });
  475. }
  476. const oldSongId = songId;
  477. songId = song._id;
  478. return userModel.findOne({ _id: session.userId }, (err, user) => {
  479. if (user.liked.indexOf(songId) !== -1)
  480. return cb({
  481. status: "failure",
  482. message: "You have already liked this song."
  483. });
  484. return userModel.updateOne(
  485. { _id: session.userId },
  486. {
  487. $push: { liked: songId },
  488. $pull: { disliked: songId }
  489. },
  490. err => {
  491. if (!err) {
  492. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  493. if (err)
  494. return cb({
  495. status: "failure",
  496. message: "Something went wrong while liking this song."
  497. });
  498. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  499. if (err)
  500. return cb({
  501. status: "failure",
  502. message: "Something went wrong while liking this song."
  503. });
  504. return songModel.update(
  505. { _id: songId },
  506. {
  507. $set: {
  508. likes,
  509. dislikes
  510. }
  511. },
  512. err => {
  513. if (err)
  514. return cb({
  515. status: "failure",
  516. message: "Something went wrong while liking this song."
  517. });
  518. SongsModule.runJob("UPDATE_SONG", { songId });
  519. CacheModule.runJob("PUB", {
  520. channel: "song.like",
  521. value: JSON.stringify({
  522. songId: oldSongId,
  523. userId: session.userId,
  524. likes,
  525. dislikes
  526. })
  527. });
  528. ActivitiesModule.runJob("ADD_ACTIVITY", {
  529. userId: session.userId,
  530. activityType: "liked_song",
  531. payload: [songId]
  532. });
  533. return cb({
  534. status: "success",
  535. message: "You have successfully liked this song."
  536. });
  537. }
  538. );
  539. });
  540. });
  541. }
  542. return cb({
  543. status: "failure",
  544. message: "Something went wrong while liking this song."
  545. });
  546. }
  547. );
  548. });
  549. }
  550. );
  551. }),
  552. /**
  553. * Dislikes a song
  554. *
  555. * @param session
  556. * @param songId - the song id
  557. * @param cb
  558. */
  559. dislike: isLoginRequired(async function dislike(session, songId, cb) {
  560. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  561. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  562. async.waterfall(
  563. [
  564. next => {
  565. songModel.findOne({ songId }, next);
  566. },
  567. (song, next) => {
  568. if (!song) return next("No song found with that id.");
  569. return next(null, song);
  570. }
  571. ],
  572. async (err, song) => {
  573. if (err) {
  574. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  575. this.log(
  576. "ERROR",
  577. "SONGS_DISLIKE",
  578. `User "${session.userId}" failed to like song ${songId}. "${err}"`
  579. );
  580. return cb({ status: "failure", message: err });
  581. }
  582. const oldSongId = songId;
  583. songId = song._id;
  584. return userModel.findOne({ _id: session.userId }, (err, user) => {
  585. if (user.disliked.indexOf(songId) !== -1)
  586. return cb({
  587. status: "failure",
  588. message: "You have already disliked this song."
  589. });
  590. return userModel.updateOne(
  591. { _id: session.userId },
  592. {
  593. $push: { disliked: songId },
  594. $pull: { liked: songId }
  595. },
  596. err => {
  597. if (!err) {
  598. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  599. if (err)
  600. return cb({
  601. status: "failure",
  602. message: "Something went wrong while disliking this song."
  603. });
  604. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  605. if (err)
  606. return cb({
  607. status: "failure",
  608. message: "Something went wrong while disliking this song."
  609. });
  610. return songModel.update(
  611. { _id: songId },
  612. {
  613. $set: {
  614. likes,
  615. dislikes
  616. }
  617. },
  618. err => {
  619. if (err)
  620. return cb({
  621. status: "failure",
  622. message: "Something went wrong while disliking this song."
  623. });
  624. SongsModule.runJob("UPDATE_SONG", { songId });
  625. CacheModule.runJob("PUB", {
  626. channel: "song.dislike",
  627. value: JSON.stringify({
  628. songId: oldSongId,
  629. userId: session.userId,
  630. likes,
  631. dislikes
  632. })
  633. });
  634. return cb({
  635. status: "success",
  636. message: "You have successfully disliked this song."
  637. });
  638. }
  639. );
  640. });
  641. });
  642. }
  643. return cb({
  644. status: "failure",
  645. message: "Something went wrong while disliking this song."
  646. });
  647. }
  648. );
  649. });
  650. }
  651. );
  652. }),
  653. /**
  654. * Undislikes a song
  655. *
  656. * @param session
  657. * @param songId - the song id
  658. * @param cb
  659. */
  660. undislike: isLoginRequired(async function undislike(session, songId, cb) {
  661. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  662. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  663. async.waterfall(
  664. [
  665. next => {
  666. songModel.findOne({ songId }, next);
  667. },
  668. (song, next) => {
  669. if (!song) return next("No song found with that id.");
  670. return next(null, song);
  671. }
  672. ],
  673. async (err, song) => {
  674. if (err) {
  675. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  676. this.log(
  677. "ERROR",
  678. "SONGS_UNDISLIKE",
  679. `User "${session.userId}" failed to like song ${songId}. "${err}"`
  680. );
  681. return cb({ status: "failure", message: err });
  682. }
  683. const oldSongId = songId;
  684. songId = song._id;
  685. return userModel.findOne({ _id: session.userId }, (err, user) => {
  686. if (user.disliked.indexOf(songId) === -1)
  687. return cb({
  688. status: "failure",
  689. message: "You have not disliked this song."
  690. });
  691. return userModel.updateOne(
  692. { _id: session.userId },
  693. { $pull: { liked: songId, disliked: songId } },
  694. err => {
  695. if (!err) {
  696. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  697. if (err)
  698. return cb({
  699. status: "failure",
  700. message: "Something went wrong while undisliking this song."
  701. });
  702. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  703. if (err)
  704. return cb({
  705. status: "failure",
  706. message: "Something went wrong while undisliking this song."
  707. });
  708. return songModel.update(
  709. { _id: songId },
  710. {
  711. $set: {
  712. likes,
  713. dislikes
  714. }
  715. },
  716. err => {
  717. if (err)
  718. return cb({
  719. status: "failure",
  720. message: "Something went wrong while undisliking this song."
  721. });
  722. SongsModule.runJob("UPDATE_SONG", { songId });
  723. CacheModule.runJob("PUB", {
  724. channel: "song.undislike",
  725. value: JSON.stringify({
  726. songId: oldSongId,
  727. userId: session.userId,
  728. likes,
  729. dislikes
  730. })
  731. });
  732. return cb({
  733. status: "success",
  734. message: "You have successfully undisliked this song."
  735. });
  736. }
  737. );
  738. });
  739. });
  740. }
  741. return cb({
  742. status: "failure",
  743. message: "Something went wrong while undisliking this song."
  744. });
  745. }
  746. );
  747. });
  748. }
  749. );
  750. }),
  751. /**
  752. * Unlikes a song
  753. *
  754. * @param session
  755. * @param songId - the song id
  756. * @param cb
  757. */
  758. unlike: isLoginRequired(async function unlike(session, songId, cb) {
  759. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  760. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  761. async.waterfall(
  762. [
  763. next => {
  764. songModel.findOne({ songId }, next);
  765. },
  766. (song, next) => {
  767. if (!song) return next("No song found with that id.");
  768. return next(null, song);
  769. }
  770. ],
  771. async (err, song) => {
  772. if (err) {
  773. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  774. this.log(
  775. "ERROR",
  776. "SONGS_UNLIKE",
  777. `User "${session.userId}" failed to like song ${songId}. "${err}"`
  778. );
  779. return cb({ status: "failure", message: err });
  780. }
  781. const oldSongId = songId;
  782. songId = song._id;
  783. return userModel.findOne({ _id: session.userId }, (err, user) => {
  784. if (user.liked.indexOf(songId) === -1)
  785. return cb({
  786. status: "failure",
  787. message: "You have not liked this song."
  788. });
  789. return userModel.updateOne(
  790. { _id: session.userId },
  791. { $pull: { liked: songId, disliked: songId } },
  792. err => {
  793. if (!err) {
  794. return userModel.countDocuments({ liked: songId }, (err, likes) => {
  795. if (err)
  796. return cb({
  797. status: "failure",
  798. message: "Something went wrong while unliking this song."
  799. });
  800. return userModel.countDocuments({ disliked: songId }, (err, dislikes) => {
  801. if (err)
  802. return cb({
  803. status: "failure",
  804. message: "Something went wrong while undiking this song."
  805. });
  806. return songModel.updateOne(
  807. { _id: songId },
  808. {
  809. $set: {
  810. likes,
  811. dislikes
  812. }
  813. },
  814. err => {
  815. if (err)
  816. return cb({
  817. status: "failure",
  818. message: "Something went wrong while unliking this song."
  819. });
  820. SongsModule.runJob("UPDATE_SONG", { songId });
  821. CacheModule.runJob("PUB", {
  822. channel: "song.unlike",
  823. value: JSON.stringify({
  824. songId: oldSongId,
  825. userId: session.userId,
  826. likes,
  827. dislikes
  828. })
  829. });
  830. return cb({
  831. status: "success",
  832. message: "You have successfully unliked this song."
  833. });
  834. }
  835. );
  836. });
  837. });
  838. }
  839. return cb({
  840. status: "failure",
  841. message: "Something went wrong while unliking this song."
  842. });
  843. }
  844. );
  845. });
  846. }
  847. );
  848. }),
  849. /**
  850. * Gets user's own song ratings
  851. *
  852. * @param session
  853. * @param songId - the song id
  854. * @param cb
  855. */
  856. getOwnSongRatings: isLoginRequired(async function getOwnSongRatings(session, songId, cb) {
  857. const userModel = await DBModule.runJob("GET_MODEL", { modelName: "user" }, this);
  858. const songModel = await DBModule.runJob("GET_MODEL", { modelName: "song" }, this);
  859. async.waterfall(
  860. [
  861. next => {
  862. songModel.findOne({ songId }, next);
  863. },
  864. (song, next) => {
  865. if (!song) return next("No song found with that id.");
  866. return next(null, song);
  867. }
  868. ],
  869. async (err, song) => {
  870. if (err) {
  871. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  872. this.log(
  873. "ERROR",
  874. "SONGS_GET_OWN_RATINGS",
  875. `User "${session.userId}" failed to get ratings for ${songId}. "${err}"`
  876. );
  877. return cb({ status: "failure", message: err });
  878. }
  879. const newSongId = song._id;
  880. return userModel.findOne({ _id: session.userId }, async (err, user) => {
  881. if (!err && user) {
  882. return cb({
  883. status: "success",
  884. songId,
  885. liked: user.liked.indexOf(newSongId) !== -1,
  886. disliked: user.disliked.indexOf(newSongId) !== -1
  887. });
  888. }
  889. return cb({
  890. status: "failure",
  891. message: await UtilsModule.runJob(
  892. "GET_ERROR",
  893. {
  894. error: err
  895. },
  896. this
  897. )
  898. });
  899. });
  900. }
  901. );
  902. })
  903. };