songs.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. 'use strict';
  2. const db = require('../db');
  3. const io = require('../io');
  4. const songs = require('../songs');
  5. const cache = require('../cache');
  6. const async = require('async');
  7. const utils = require('../utils');
  8. const logger = require('../logger');
  9. const hooks = require('./hooks');
  10. const queueSongs = require('./queueSongs');
  11. cache.sub('song.removed', songId => {
  12. utils.emitToRoom('admin.songs', 'event:admin.song.removed', songId);
  13. });
  14. cache.sub('song.added', songId => {
  15. db.models.song.findOne({songId}, (err, song) => {
  16. utils.emitToRoom('admin.songs', 'event:admin.song.added', song);
  17. });
  18. });
  19. cache.sub('song.updated', songId => {
  20. db.models.song.findOne({songId}, (err, song) => {
  21. utils.emitToRoom('admin.songs', 'event:admin.song.updated', song);
  22. });
  23. });
  24. cache.sub('song.like', (data) => {
  25. utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  26. utils.socketsFromUser(data.userId, (sockets) => {
  27. sockets.forEach((socket) => {
  28. socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
  29. });
  30. });
  31. });
  32. cache.sub('song.dislike', (data) => {
  33. utils.emitToRoom(`song.${data.songId}`, 'event:song.dislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  34. utils.socketsFromUser(data.userId, (sockets) => {
  35. sockets.forEach((socket) => {
  36. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: true});
  37. });
  38. });
  39. });
  40. cache.sub('song.unlike', (data) => {
  41. utils.emitToRoom(`song.${data.songId}`, 'event:song.unlike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  42. utils.socketsFromUser(data.userId, (sockets) => {
  43. sockets.forEach((socket) => {
  44. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  45. });
  46. });
  47. });
  48. cache.sub('song.undislike', (data) => {
  49. utils.emitToRoom(`song.${data.songId}`, 'event:song.undislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  50. utils.socketsFromUser(data.userId, (sockets) => {
  51. sockets.forEach((socket) => {
  52. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  53. });
  54. });
  55. });
  56. module.exports = {
  57. /**
  58. * Returns the length of the songs list
  59. *
  60. * @param session
  61. * @param cb
  62. */
  63. length: hooks.adminRequired((session, cb) => {
  64. async.waterfall([
  65. (next) => {
  66. db.models.song.countDocuments({}, next);
  67. }
  68. ], (err, count) => {
  69. if (err) {
  70. err = utils.getError(err);
  71. logger.error("SONGS_LENGTH", `Failed to get length from songs. "${err}"`);
  72. return cb({'status': 'failure', 'message': err});
  73. }
  74. logger.success("SONGS_LENGTH", `Got length from songs successfully.`);
  75. cb(count);
  76. });
  77. }),
  78. /**
  79. * Gets a set of songs
  80. *
  81. * @param session
  82. * @param set - the set number to return
  83. * @param cb
  84. */
  85. getSet: hooks.adminRequired((session, set, cb) => {
  86. async.waterfall([
  87. (next) => {
  88. db.models.song.find({}).limit(15 * set).exec(next);
  89. }
  90. ], (err, songs) => {
  91. if (err) {
  92. err = utils.getError(err);
  93. logger.error("SONGS_GET_SET", `Failed to get set from songs. "${err}"`);
  94. return cb({'status': 'failure', 'message': err});
  95. }
  96. logger.success("SONGS_GET_SET", `Got set from songs successfully.`);
  97. logger.stationIssue(songs.length, true);
  98. logger.stationIssue(Math.max(songs.length - 15, 0), true);
  99. cb(songs.splice(Math.max(songs.length - 15, 0)));
  100. });
  101. }),
  102. /**
  103. * Gets a song
  104. *
  105. * @param session
  106. * @param songId - the song id
  107. * @param cb
  108. */
  109. getSong: hooks.adminRequired((session, songId, cb) => {
  110. console.log(songId);
  111. async.waterfall([
  112. (next) => {
  113. db.models.song.findOne({ songId }).exec(next);
  114. }
  115. ], (err, song) => {
  116. if (err) {
  117. err = utils.getError(err);
  118. logger.error("SONGS_GET_SONG", `Failed to get song ${songId}. "${err}"`);
  119. return cb({ status: 'failure', message: err });
  120. } else {
  121. logger.success("SONGS_GET_SONG", `Got song ${songId} successfully.`);
  122. cb({ status: "success", data: song });
  123. }
  124. });
  125. }),
  126. /**
  127. * Updates a song
  128. *
  129. * @param session
  130. * @param songId - the song id
  131. * @param song - the updated song object
  132. * @param cb
  133. */
  134. update: hooks.adminRequired((session, songId, song, cb) => {
  135. async.waterfall([
  136. (next) => {
  137. db.models.song.updateOne({_id: songId}, song, {runValidators: true}, next);
  138. },
  139. (res, next) => {
  140. songs.updateSong(songId, next);
  141. }
  142. ], (err, song) => {
  143. if (err) {
  144. err = utils.getError(err);
  145. logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  146. return cb({'status': 'failure', 'message': err});
  147. }
  148. logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);
  149. cache.pub('song.updated', song.songId);
  150. cb({ status: 'success', message: 'Song has been successfully updated', data: song });
  151. });
  152. }),
  153. /**
  154. * Removes a song
  155. *
  156. * @param session
  157. * @param songId - the song id
  158. * @param cb
  159. */
  160. remove: hooks.adminRequired((session, songId, cb) => {
  161. async.waterfall([
  162. (next) => {
  163. db.models.song.deleteOne({_id: songId}, next);
  164. },
  165. (res, next) => {//TODO Check if res gets returned from above
  166. cache.hdel('songs', songId, next);
  167. }
  168. ], (err) => {
  169. if (err) {
  170. err = utils.getError(err);
  171. logger.error("SONGS_UPDATE", `Failed to remove song "${songId}". "${err}"`);
  172. return cb({'status': 'failure', 'message': err});
  173. }
  174. logger.success("SONGS_UPDATE", `Successfully remove song "${songId}".`);
  175. cache.pub('song.removed', songId);
  176. cb({status: 'success', message: 'Song has been successfully updated'});
  177. });
  178. }),
  179. /**
  180. * Adds a song
  181. *
  182. * @param session
  183. * @param song - the song object
  184. * @param cb
  185. * @param userId
  186. */
  187. add: hooks.adminRequired((session, song, cb, userId) => {
  188. async.waterfall([
  189. (next) => {
  190. db.models.song.findOne({songId: song.songId}, next);
  191. },
  192. (existingSong, next) => {
  193. if (existingSong) return next('Song is already in rotation.');
  194. next();
  195. },
  196. (next) => {
  197. const newSong = new db.models.song(song);
  198. newSong.acceptedBy = userId;
  199. newSong.acceptedAt = Date.now();
  200. newSong.save(next);
  201. },
  202. (res, next) => {
  203. queueSongs.remove(session, song._id, () => {
  204. next();
  205. });
  206. },
  207. ], (err) => {
  208. if (err) {
  209. err = utils.getError(err);
  210. logger.error("SONGS_ADD", `User "${userId}" failed to add song. "${err}"`);
  211. return cb({'status': 'failure', 'message': err});
  212. }
  213. logger.success("SONGS_ADD", `User "${userId}" successfully added song "${song.songId}".`);
  214. cache.pub('song.added', song.songId);
  215. cb({status: 'success', message: 'Song has been moved from the queue successfully.'});
  216. });
  217. //TODO Check if video is in queue and Add the song to the appropriate stations
  218. }),
  219. /**
  220. * Likes a song
  221. *
  222. * @param session
  223. * @param songId - the song id
  224. * @param cb
  225. * @param userId
  226. */
  227. like: hooks.loginRequired((session, songId, cb, userId) => {
  228. async.waterfall([
  229. (next) => {
  230. db.models.song.findOne({songId}, next);
  231. },
  232. (song, next) => {
  233. if (!song) return next('No song found with that id.');
  234. next(null, song);
  235. }
  236. ], (err, song) => {
  237. if (err) {
  238. err = utils.getError(err);
  239. logger.error("SONGS_LIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  240. return cb({'status': 'failure', 'message': err});
  241. }
  242. let oldSongId = songId;
  243. songId = song._id;
  244. db.models.user.findOne({ _id: userId }, (err, user) => {
  245. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  246. db.models.user.updateOne({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  247. if (!err) {
  248. db.models.user.countDocuments({"liked": songId}, (err, likes) => {
  249. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  250. db.models.user.countDocuments({"disliked": songId}, (err, dislikes) => {
  251. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  252. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  253. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  254. songs.updateSong(songId, (err, song) => {});
  255. cache.pub('song.like', JSON.stringify({ songId: oldSongId, userId: session.userId, likes: likes, dislikes: dislikes }));
  256. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  257. });
  258. });
  259. });
  260. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  261. });
  262. });
  263. });
  264. }),
  265. /**
  266. * Dislikes a song
  267. *
  268. * @param session
  269. * @param songId - the song id
  270. * @param cb
  271. * @param userId
  272. */
  273. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  274. async.waterfall([
  275. (next) => {
  276. db.models.song.findOne({songId}, next);
  277. },
  278. (song, next) => {
  279. if (!song) return next('No song found with that id.');
  280. next(null, song);
  281. }
  282. ], (err, song) => {
  283. if (err) {
  284. err = utils.getError(err);
  285. logger.error("SONGS_DISLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  286. return cb({'status': 'failure', 'message': err});
  287. }
  288. let oldSongId = songId;
  289. songId = song._id;
  290. db.models.user.findOne({ _id: userId }, (err, user) => {
  291. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  292. db.models.user.updateOne({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  293. if (!err) {
  294. db.models.user.countDocuments({"liked": songId}, (err, likes) => {
  295. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  296. db.models.user.countDocuments({"disliked": songId}, (err, dislikes) => {
  297. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  298. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err, res) => {
  299. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  300. songs.updateSong(songId, (err, song) => {});
  301. cache.pub('song.dislike', JSON.stringify({ songId: oldSongId, userId: session.userId, likes: likes, dislikes: dislikes }));
  302. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  303. });
  304. });
  305. });
  306. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  307. });
  308. });
  309. });
  310. }),
  311. /**
  312. * Undislikes a song
  313. *
  314. * @param session
  315. * @param songId - the song id
  316. * @param cb
  317. * @param userId
  318. */
  319. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  320. async.waterfall([
  321. (next) => {
  322. db.models.song.findOne({songId}, next);
  323. },
  324. (song, next) => {
  325. if (!song) return next('No song found with that id.');
  326. next(null, song);
  327. }
  328. ], (err, song) => {
  329. if (err) {
  330. err = utils.getError(err);
  331. logger.error("SONGS_UNDISLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  332. return cb({'status': 'failure', 'message': err});
  333. }
  334. let oldSongId = songId;
  335. songId = song._id;
  336. db.models.user.findOne({_id: userId}, (err, user) => {
  337. if (user.disliked.indexOf(songId) === -1) return cb({
  338. status: 'failure',
  339. message: 'You have not disliked this song.'
  340. });
  341. db.models.user.updateOne({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  342. if (!err) {
  343. db.models.user.countDocuments({"liked": songId}, (err, likes) => {
  344. if (err) return cb({
  345. status: 'failure',
  346. message: 'Something went wrong while undisliking this song.'
  347. });
  348. db.models.user.countDocuments({"disliked": songId}, (err, dislikes) => {
  349. if (err) return cb({
  350. status: 'failure',
  351. message: 'Something went wrong while undisliking this song.'
  352. });
  353. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  354. if (err) return cb({
  355. status: 'failure',
  356. message: 'Something went wrong while undisliking this song.'
  357. });
  358. songs.updateSong(songId, (err, song) => {
  359. });
  360. cache.pub('song.undislike', JSON.stringify({
  361. songId: oldSongId,
  362. userId: session.userId,
  363. likes: likes,
  364. dislikes: dislikes
  365. }));
  366. return cb({
  367. status: 'success',
  368. message: 'You have successfully undisliked this song.'
  369. });
  370. });
  371. });
  372. });
  373. } else return cb({status: 'failure', message: 'Something went wrong while undisliking this song.'});
  374. });
  375. });
  376. });
  377. }),
  378. /**
  379. * Unlikes a song
  380. *
  381. * @param session
  382. * @param songId - the song id
  383. * @param cb
  384. * @param userId
  385. */
  386. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  387. async.waterfall([
  388. (next) => {
  389. db.models.song.findOne({songId}, next);
  390. },
  391. (song, next) => {
  392. if (!song) return next('No song found with that id.');
  393. next(null, song);
  394. }
  395. ], (err, song) => {
  396. if (err) {
  397. err = utils.getError(err);
  398. logger.error("SONGS_UNLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  399. return cb({'status': 'failure', 'message': err});
  400. }
  401. let oldSongId = songId;
  402. songId = song._id;
  403. db.models.user.findOne({ _id: userId }, (err, user) => {
  404. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  405. db.models.user.updateOne({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  406. if (!err) {
  407. db.models.user.countDocuments({"liked": songId}, (err, likes) => {
  408. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  409. db.models.user.countDocuments({"disliked": songId}, (err, dislikes) => {
  410. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
  411. db.models.song.updateOne({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  412. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  413. songs.updateSong(songId, (err, song) => {});
  414. cache.pub('song.unlike', JSON.stringify({ songId: oldSongId, userId: session.userId, likes: likes, dislikes: dislikes }));
  415. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  416. });
  417. });
  418. });
  419. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  420. });
  421. });
  422. });
  423. }),
  424. /**
  425. * Gets user's own song ratings
  426. *
  427. * @param session
  428. * @param songId - the song id
  429. * @param cb
  430. * @param userId
  431. */
  432. getOwnSongRatings: hooks.loginRequired((session, songId, cb, userId) => {
  433. async.waterfall([
  434. (next) => {
  435. db.models.song.findOne({songId}, next);
  436. },
  437. (song, next) => {
  438. if (!song) return next('No song found with that id.');
  439. next(null, song);
  440. }
  441. ], (err, song) => {
  442. if (err) {
  443. err = utils.getError(err);
  444. logger.error("SONGS_GET_OWN_RATINGS", `User "${userId}" failed to get ratings for ${songId}. "${err}"`);
  445. return cb({'status': 'failure', 'message': err});
  446. }
  447. let newSongId = song._id;
  448. db.models.user.findOne({_id: userId}, (err, user) => {
  449. if (!err && user) {
  450. return cb({
  451. status: 'success',
  452. songId: songId,
  453. liked: (user.liked.indexOf(newSongId) !== -1),
  454. disliked: (user.disliked.indexOf(newSongId) !== -1)
  455. });
  456. } else {
  457. return cb({
  458. status: 'failure',
  459. message: utils.getError(err)
  460. });
  461. }
  462. });
  463. });
  464. })
  465. };