songs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 utils = require('../utils');
  7. const logger = require('../logger');
  8. const hooks = require('./hooks');
  9. const queueSongs = require('./queueSongs');
  10. cache.sub('song.removed', songId => {
  11. utils.emitToRoom('admin.songs', 'event:admin.song.removed', songId);
  12. });
  13. cache.sub('song.added', songId => {
  14. db.models.song.findOne({_id: songId}, (err, song) => {
  15. utils.emitToRoom('admin.songs', 'event:admin.song.added', song);
  16. });
  17. });
  18. cache.sub('song.updated', songId => {
  19. db.models.song.findOne({_id: songId}, (err, song) => {
  20. utils.emitToRoom('admin.songs', 'event:admin.song.updated', song);
  21. });
  22. });
  23. cache.sub('song.like', (data) => {
  24. utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  25. utils.socketsFromUser(data.userId, (sockets) => {
  26. sockets.forEach((socket) => {
  27. socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
  28. });
  29. });
  30. });
  31. cache.sub('song.dislike', (data) => {
  32. utils.emitToRoom(`song.${data.songId}`, 'event:song.dislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  33. utils.socketsFromUser(data.userId, (sockets) => {
  34. sockets.forEach((socket) => {
  35. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: true});
  36. });
  37. });
  38. });
  39. cache.sub('song.unlike', (data) => {
  40. utils.emitToRoom(`song.${data.songId}`, 'event:song.unlike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  41. utils.socketsFromUser(data.userId, (sockets) => {
  42. sockets.forEach((socket) => {
  43. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  44. });
  45. });
  46. });
  47. cache.sub('song.undislike', (data) => {
  48. utils.emitToRoom(`song.${data.songId}`, 'event:song.undislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  49. utils.socketsFromUser(data.userId, (sockets) => {
  50. sockets.forEach((socket) => {
  51. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  52. });
  53. });
  54. });
  55. module.exports = {
  56. /**
  57. * Returns the length of the songs list
  58. *
  59. * @param session
  60. * @param cb
  61. */
  62. length: hooks.adminRequired((session, cb) => {
  63. async.waterfall([
  64. (next) => {
  65. db.models.song.count({}, next);
  66. }
  67. ], (err, count) => {
  68. if (err) {
  69. err = utils.getError(err);
  70. logger.error("SONGS_LENGTH", `Failed to get length from songs. "${err}"`);
  71. return cb({'status': 'failure', 'message': err});
  72. }
  73. logger.success("SONGS_LENGTH", `Got length from songs successfully.`);
  74. cb(count);
  75. });
  76. }),
  77. /**
  78. * Gets a set of songs
  79. *
  80. * @param session
  81. * @param set - the set number to return
  82. * @param cb
  83. */
  84. getSet: hooks.adminRequired((session, set, cb) => {
  85. async.waterfall([
  86. (next) => {
  87. db.models.song.find({}).limit(15 * set).exec(next);
  88. }
  89. ], (err, songs) => {
  90. if (err) {
  91. err = utils.getError(err);
  92. logger.error("SONGS_GET_SET", `Failed to get set from songs. "${err}"`);
  93. return cb({'status': 'failure', 'message': err});
  94. }
  95. logger.success("SONGS_GET_SET", `Got set from songs successfully.`);
  96. cb(songs.splice(Math.max(songs.length - 15, 0)));
  97. });
  98. }),
  99. /**
  100. * Updates a song
  101. *
  102. * @param session
  103. * @param songId - the song id
  104. * @param song - the updated song object
  105. * @param cb
  106. */
  107. update: hooks.adminRequired((session, songId, song, cb) => {
  108. async.waterfall([
  109. (next) => {
  110. db.models.song.update({_id: songId}, song, {upsert: true}, next);
  111. },
  112. (res, next) => {
  113. songs.updateSong(songId, next);
  114. }
  115. ], (err, song) => {
  116. if (err) {
  117. err = utils.getError(err);
  118. logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  119. return cb({'status': 'failure', 'message': err});
  120. }
  121. logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);
  122. cache.pub('song.updated', song._id);
  123. cb({ status: 'success', message: 'Song has been successfully updated', data: song });
  124. });
  125. }),
  126. /**
  127. * Removes a song
  128. *
  129. * @param session
  130. * @param songId - the song id
  131. * @param cb
  132. */
  133. remove: hooks.adminRequired((session, songId, cb) => {
  134. async.waterfall([
  135. (next) => {
  136. db.models.song.remove({_id: songId}, next);
  137. },
  138. (res, next) => {//TODO Check if res gets returned from above
  139. cache.hdel('songs', songId, next);
  140. }
  141. ], (err) => {
  142. if (err) {
  143. err = utils.getError(err);
  144. logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  145. return cb({'status': 'failure', 'message': err});
  146. }
  147. logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);
  148. cache.pub('song.removed', songId);
  149. cb({status: 'success', message: 'Song has been successfully updated'});
  150. });
  151. }),
  152. /**
  153. * Adds a song
  154. *
  155. * @param session
  156. * @param song - the song object
  157. * @param cb
  158. * @param userId
  159. */
  160. add: hooks.adminRequired((session, song, cb, userId) => {
  161. async.waterfall([
  162. (next) => {
  163. queueSongs.remove(session, song._id, () => {
  164. next();
  165. });
  166. },
  167. (next) => {
  168. db.models.song.findOne({_id: song._id}, next);
  169. },
  170. (existingSong, next) => {
  171. if (existingSong) return next('Song is already in rotation.');
  172. next();
  173. },
  174. (next) => {
  175. const newSong = new db.models.song(song);
  176. newSong.acceptedBy = userId;
  177. newSong.acceptedAt = Date.now();
  178. newSong.save(next);
  179. }
  180. ], (err) => {
  181. if (err) {
  182. err = utils.getError(err);
  183. logger.error("SONGS_ADD", `User "${userId}" failed to add song. "${err}"`);
  184. return cb({'status': 'failure', 'message': err});
  185. }
  186. logger.success("SONGS_ADD", `User "${userId}" successfully added song "${song._id}".`);
  187. cache.pub('song.added', song._id);
  188. cb({status: 'success', message: 'Song has been moved from the queue successfully.'});
  189. });
  190. //TODO Check if video is in queue and Add the song to the appropriate stations
  191. }),
  192. /**
  193. * Likes a song
  194. *
  195. * @param session
  196. * @param songId - the song id
  197. * @param cb
  198. * @param userId
  199. */
  200. like: hooks.loginRequired((session, songId, cb, userId) => {
  201. db.models.user.findOne({ _id: userId }, (err, user) => {
  202. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  203. db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  204. if (!err) {
  205. db.models.user.count({"liked": songId}, (err, likes) => {
  206. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  207. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  208. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  209. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  210. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  211. songs.updateSong(songId, (err, song) => {});
  212. cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  213. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  214. });
  215. });
  216. });
  217. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  218. });
  219. });
  220. }),
  221. /**
  222. * Dislikes a song
  223. *
  224. * @param session
  225. * @param songId - the song id
  226. * @param cb
  227. * @param userId
  228. */
  229. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  230. db.models.user.findOne({ _id: userId }, (err, user) => {
  231. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  232. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  233. if (!err) {
  234. db.models.user.count({"liked": songId}, (err, likes) => {
  235. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  236. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  237. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  238. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  239. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  240. songs.updateSong(songId, (err, song) => {});
  241. cache.pub('song.dislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  242. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  243. });
  244. });
  245. });
  246. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  247. });
  248. });
  249. }),
  250. /**
  251. * Undislikes a song
  252. *
  253. * @param session
  254. * @param songId - the song id
  255. * @param cb
  256. * @param userId
  257. */
  258. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  259. db.models.user.findOne({ _id: userId }, (err, user) => {
  260. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  261. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  262. if (!err) {
  263. db.models.user.count({"liked": songId}, (err, likes) => {
  264. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  265. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  266. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  267. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  268. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  269. songs.updateSong(songId, (err, song) => {});
  270. cache.pub('song.undislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  271. return cb({ status: 'success', message: 'You have successfully undisliked this song.' });
  272. });
  273. });
  274. });
  275. } else return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  276. });
  277. });
  278. }),
  279. /**
  280. * Unlikes a song
  281. *
  282. * @param session
  283. * @param songId - the song id
  284. * @param cb
  285. * @param userId
  286. */
  287. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  288. db.models.user.findOne({ _id: userId }, (err, user) => {
  289. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  290. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  291. if (!err) {
  292. db.models.user.count({"liked": songId}, (err, likes) => {
  293. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  294. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  295. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
  296. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  297. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  298. songs.updateSong(songId, (err, song) => {});
  299. cache.pub('song.unlike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  300. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  301. });
  302. });
  303. });
  304. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  305. });
  306. });
  307. }),
  308. /**
  309. * Gets user's own song ratings
  310. *
  311. * @param session
  312. * @param songId - the song id
  313. * @param cb
  314. * @param userId
  315. */
  316. getOwnSongRatings: hooks.loginRequired((session, songId, cb, userId) => {
  317. db.models.user.findOne({_id: userId}, (err, user) => {
  318. if (!err && user) {
  319. return cb({
  320. status: 'success',
  321. songId: songId,
  322. liked: (user.liked.indexOf(songId) !== -1),
  323. disliked: (user.disliked.indexOf(songId) !== -1)
  324. });
  325. } else {
  326. return cb({
  327. status: 'failure',
  328. message: utils.getError(err)
  329. });
  330. }
  331. });
  332. })
  333. };