songs.js 26 KB

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