songs.js 15 KB

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