songs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. db.models.user.findOne({ _id: userId }, (err, user) => {
  203. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  204. db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  205. if (!err) {
  206. db.models.user.count({"liked": songId}, (err, likes) => {
  207. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  208. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  209. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  210. db.models.song.update({songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  211. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  212. songs.updateSong(songId, (err, song) => {});
  213. cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  214. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  215. });
  216. });
  217. });
  218. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  219. });
  220. });
  221. }),
  222. /**
  223. * Dislikes a song
  224. *
  225. * @param session
  226. * @param songId - the song id
  227. * @param cb
  228. * @param userId
  229. */
  230. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  231. db.models.user.findOne({ _id: userId }, (err, user) => {
  232. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  233. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  234. if (!err) {
  235. db.models.user.count({"liked": songId}, (err, likes) => {
  236. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  237. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  238. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  239. db.models.song.update({songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  240. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  241. songs.updateSong(songId, (err, song) => {});
  242. cache.pub('song.dislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  243. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  244. });
  245. });
  246. });
  247. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  248. });
  249. });
  250. }),
  251. /**
  252. * Undislikes a song
  253. *
  254. * @param session
  255. * @param songId - the song id
  256. * @param cb
  257. * @param userId
  258. */
  259. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  260. db.models.user.findOne({ _id: userId }, (err, user) => {
  261. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  262. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  263. if (!err) {
  264. db.models.user.count({"liked": songId}, (err, likes) => {
  265. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  266. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  267. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  268. db.models.song.update({songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  269. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  270. songs.updateSong(songId, (err, song) => {});
  271. cache.pub('song.undislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  272. return cb({ status: 'success', message: 'You have successfully undisliked this song.' });
  273. });
  274. });
  275. });
  276. } else return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  277. });
  278. });
  279. }),
  280. /**
  281. * Unlikes a song
  282. *
  283. * @param session
  284. * @param songId - the song id
  285. * @param cb
  286. * @param userId
  287. */
  288. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  289. db.models.user.findOne({ _id: userId }, (err, user) => {
  290. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  291. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  292. if (!err) {
  293. db.models.user.count({"liked": songId}, (err, likes) => {
  294. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  295. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  296. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
  297. db.models.song.update({songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  298. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  299. songs.updateSong(songId, (err, song) => {});
  300. cache.pub('song.unlike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  301. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  302. });
  303. });
  304. });
  305. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  306. });
  307. });
  308. }),
  309. /**
  310. * Gets user's own song ratings
  311. *
  312. * @param session
  313. * @param songId - the song id
  314. * @param cb
  315. * @param userId
  316. */
  317. getOwnSongRatings: hooks.loginRequired((session, songId, cb, userId) => {
  318. db.models.user.findOne({_id: userId}, (err, user) => {
  319. if (!err && user) {
  320. return cb({
  321. status: 'success',
  322. songId: songId,
  323. liked: (user.liked.indexOf(songId) !== -1),
  324. disliked: (user.disliked.indexOf(songId) !== -1)
  325. });
  326. } else {
  327. return cb({
  328. status: 'failure',
  329. message: utils.getError(err)
  330. });
  331. }
  332. });
  333. })
  334. };