songs.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.count({}, 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. cb(songs.splice(Math.max(songs.length - 15, 0)));
  98. });
  99. }),
  100. /**
  101. * Updates a song
  102. *
  103. * @param session
  104. * @param songId - the song id
  105. * @param song - the updated song object
  106. * @param cb
  107. */
  108. update: hooks.adminRequired((session, songId, song, cb) => {
  109. async.waterfall([
  110. (next) => {
  111. db.models.song.update({_id: songId}, song, {runValidators: true}, next);
  112. },
  113. (res, next) => {
  114. songs.updateSong(songId, next);
  115. }
  116. ], (err, song) => {
  117. if (err) {
  118. err = utils.getError(err);
  119. logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  120. return cb({'status': 'failure', 'message': err});
  121. }
  122. logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);
  123. cache.pub('song.updated', song.songId);
  124. cb({ status: 'success', message: 'Song has been successfully updated', data: song });
  125. });
  126. }),
  127. /**
  128. * Removes a song
  129. *
  130. * @param session
  131. * @param songId - the song id
  132. * @param cb
  133. */
  134. remove: hooks.adminRequired((session, songId, cb) => {
  135. async.waterfall([
  136. (next) => {
  137. db.models.song.remove({_id: songId}, next);
  138. },
  139. (res, next) => {//TODO Check if res gets returned from above
  140. cache.hdel('songs', songId, next);
  141. }
  142. ], (err) => {
  143. if (err) {
  144. err = utils.getError(err);
  145. logger.error("SONGS_UPDATE", `Failed to remove song "${songId}". "${err}"`);
  146. return cb({'status': 'failure', 'message': err});
  147. }
  148. logger.success("SONGS_UPDATE", `Successfully remove song "${songId}".`);
  149. cache.pub('song.removed', songId);
  150. cb({status: 'success', message: 'Song has been successfully updated'});
  151. });
  152. }),
  153. /**
  154. * Adds a song
  155. *
  156. * @param session
  157. * @param song - the song object
  158. * @param cb
  159. * @param userId
  160. */
  161. add: hooks.adminRequired((session, song, cb, userId) => {
  162. async.waterfall([
  163. (next) => {
  164. db.models.song.findOne({songId: song.songId}, next);
  165. },
  166. (existingSong, next) => {
  167. if (existingSong) return next('Song is already in rotation.');
  168. next();
  169. },
  170. (next) => {
  171. const newSong = new db.models.song(song);
  172. newSong.acceptedBy = userId;
  173. newSong.acceptedAt = Date.now();
  174. newSong.save(next);
  175. },
  176. (next) => {
  177. queueSongs.remove(session, song._id, () => {
  178. next();
  179. });
  180. },
  181. ], (err) => {
  182. if (err) {
  183. err = utils.getError(err);
  184. logger.error("SONGS_ADD", `User "${userId}" failed to add song. "${err}"`);
  185. return cb({'status': 'failure', 'message': err});
  186. }
  187. logger.success("SONGS_ADD", `User "${userId}" successfully added song "${song.songId}".`);
  188. cache.pub('song.added', song.songId);
  189. cb({status: 'success', message: 'Song has been moved from the queue successfully.'});
  190. });
  191. //TODO Check if video is in queue and Add the song to the appropriate stations
  192. }),
  193. /**
  194. * Likes a song
  195. *
  196. * @param session
  197. * @param songId - the song id
  198. * @param cb
  199. * @param userId
  200. */
  201. like: hooks.loginRequired((session, songId, cb, userId) => {
  202. async.waterfall([
  203. (next) => {
  204. db.models.song.findOne({songId}, next);
  205. },
  206. (song, next) => {
  207. if (!song) return next('No song found with that id.');
  208. next(null, song);
  209. }
  210. ], (err, song) => {
  211. if (err) {
  212. err = utils.getError(err);
  213. logger.error("SONGS_LIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  214. return cb({'status': 'failure', 'message': err});
  215. }
  216. let oldSongId = songId;
  217. songId = song._id;
  218. db.models.user.findOne({ _id: userId }, (err, user) => {
  219. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  220. db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  221. if (!err) {
  222. db.models.user.count({"liked": songId}, (err, likes) => {
  223. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  224. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  225. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  226. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  227. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  228. songs.updateSong(songId, (err, song) => {});
  229. cache.pub('song.like', JSON.stringify({ songId: oldSongId, userId: session.userId, likes: likes, dislikes: dislikes }));
  230. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  231. });
  232. });
  233. });
  234. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  235. });
  236. });
  237. });
  238. }),
  239. /**
  240. * Dislikes a song
  241. *
  242. * @param session
  243. * @param songId - the song id
  244. * @param cb
  245. * @param userId
  246. */
  247. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  248. async.waterfall([
  249. (next) => {
  250. db.models.song.findOne({songId}, next);
  251. },
  252. (song, next) => {
  253. if (!song) return next('No song found with that id.');
  254. next(null, song);
  255. }
  256. ], (err, song) => {
  257. if (err) {
  258. err = utils.getError(err);
  259. logger.error("SONGS_DISLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  260. return cb({'status': 'failure', 'message': err});
  261. }
  262. let oldSongId = songId;
  263. songId = song._id;
  264. db.models.user.findOne({ _id: userId }, (err, user) => {
  265. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  266. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  267. if (!err) {
  268. db.models.user.count({"liked": songId}, (err, likes) => {
  269. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  270. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  271. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  272. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err, res) => {
  273. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  274. songs.updateSong(songId, (err, song) => {});
  275. cache.pub('song.dislike', JSON.stringify({ songId: oldSongId, userId: session.userId, likes: likes, dislikes: dislikes }));
  276. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  277. });
  278. });
  279. });
  280. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  281. });
  282. });
  283. });
  284. }),
  285. /**
  286. * Undislikes a song
  287. *
  288. * @param session
  289. * @param songId - the song id
  290. * @param cb
  291. * @param userId
  292. */
  293. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  294. async.waterfall([
  295. (next) => {
  296. db.models.song.findOne({songId}, next);
  297. },
  298. (song, next) => {
  299. if (!song) return next('No song found with that id.');
  300. next(null, song);
  301. }
  302. ], (err, song) => {
  303. if (err) {
  304. err = utils.getError(err);
  305. logger.error("SONGS_UNDISLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  306. return cb({'status': 'failure', 'message': err});
  307. }
  308. let oldSongId = songId;
  309. songId = song._id;
  310. db.models.user.findOne({_id: userId}, (err, user) => {
  311. if (user.disliked.indexOf(songId) === -1) return cb({
  312. status: 'failure',
  313. message: 'You have not disliked this song.'
  314. });
  315. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  316. if (!err) {
  317. db.models.user.count({"liked": songId}, (err, likes) => {
  318. if (err) return cb({
  319. status: 'failure',
  320. message: 'Something went wrong while undisliking this song.'
  321. });
  322. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  323. if (err) return cb({
  324. status: 'failure',
  325. message: 'Something went wrong while undisliking this song.'
  326. });
  327. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  328. if (err) return cb({
  329. status: 'failure',
  330. message: 'Something went wrong while undisliking this song.'
  331. });
  332. songs.updateSong(songId, (err, song) => {
  333. });
  334. cache.pub('song.undislike', JSON.stringify({
  335. songId: oldSongId,
  336. userId: session.userId,
  337. likes: likes,
  338. dislikes: dislikes
  339. }));
  340. return cb({
  341. status: 'success',
  342. message: 'You have successfully undisliked this song.'
  343. });
  344. });
  345. });
  346. });
  347. } else return cb({status: 'failure', message: 'Something went wrong while undisliking this song.'});
  348. });
  349. });
  350. });
  351. }),
  352. /**
  353. * Unlikes a song
  354. *
  355. * @param session
  356. * @param songId - the song id
  357. * @param cb
  358. * @param userId
  359. */
  360. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  361. async.waterfall([
  362. (next) => {
  363. db.models.song.findOne({songId}, next);
  364. },
  365. (song, next) => {
  366. if (!song) return next('No song found with that id.');
  367. next(null, song);
  368. }
  369. ], (err, song) => {
  370. if (err) {
  371. err = utils.getError(err);
  372. logger.error("SONGS_UNLIKE", `User "${userId}" failed to like song ${songId}. "${err}"`);
  373. return cb({'status': 'failure', 'message': err});
  374. }
  375. let oldSongId = songId;
  376. songId = song._id;
  377. db.models.user.findOne({ _id: userId }, (err, user) => {
  378. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  379. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  380. if (!err) {
  381. db.models.user.count({"liked": songId}, (err, likes) => {
  382. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  383. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  384. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
  385. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  386. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  387. songs.updateSong(songId, (err, song) => {});
  388. cache.pub('song.unlike', JSON.stringify({ songId: oldSongId, userId: session.userId, likes: likes, dislikes: dislikes }));
  389. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  390. });
  391. });
  392. });
  393. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  394. });
  395. });
  396. });
  397. }),
  398. /**
  399. * Gets user's own song ratings
  400. *
  401. * @param session
  402. * @param songId - the song id
  403. * @param cb
  404. * @param userId
  405. */
  406. getOwnSongRatings: hooks.loginRequired((session, songId, cb, userId) => {
  407. async.waterfall([
  408. (next) => {
  409. db.models.song.findOne({songId}, next);
  410. },
  411. (song, next) => {
  412. if (!song) return next('No song found with that id.');
  413. next(null, song);
  414. }
  415. ], (err, song) => {
  416. if (err) {
  417. err = utils.getError(err);
  418. logger.error("SONGS_GET_OWN_RATINGS", `User "${userId}" failed to get ratings for ${songId}. "${err}"`);
  419. return cb({'status': 'failure', 'message': err});
  420. }
  421. let newSongId = song._id;
  422. db.models.user.findOne({_id: userId}, (err, user) => {
  423. if (!err && user) {
  424. return cb({
  425. status: 'success',
  426. songId: songId,
  427. liked: (user.liked.indexOf(newSongId) !== -1),
  428. disliked: (user.disliked.indexOf(newSongId) !== -1)
  429. });
  430. } else {
  431. return cb({
  432. status: 'failure',
  433. message: utils.getError(err)
  434. });
  435. }
  436. });
  437. });
  438. })
  439. };