songs.js 41 KB

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