songs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. length: hooks.adminRequired((session, cb) => {
  57. async.waterfall([
  58. (next) => {
  59. db.models.song.count({}, next);
  60. }
  61. ], (err, count) => {
  62. if (err) {
  63. err = utils.getError(err);
  64. logger.error("SONGS_LENGTH", `Failed to get length from songs. "${err}"`);
  65. return cb({'status': 'failure', 'message': err});
  66. }
  67. logger.success("SONGS_LENGTH", `Got length from songs successfully.`);
  68. cb(count);
  69. });
  70. }),
  71. getSet: hooks.adminRequired((session, set, cb) => {
  72. async.waterfall([
  73. (next) => {
  74. db.models.song.find({}).limit(15 * set).exec(next);
  75. }
  76. ], (err, songs) => {
  77. if (err) {
  78. err = utils.getError(err);
  79. logger.error("SONGS_GET_SET", `Failed to get set from songs. "${err}"`);
  80. return cb({'status': 'failure', 'message': err});
  81. }
  82. logger.success("SONGS_GET_SET", `Got set from songs successfully.`);
  83. cb(songs.splice(Math.max(songs.length - 15, 0)));
  84. });
  85. }),
  86. update: hooks.adminRequired((session, songId, song, cb) => {
  87. async.waterfall([
  88. (next) => {
  89. db.models.song.update({_id: songId}, song, {upsert: true}, next);
  90. },
  91. (res, next) => {
  92. songs.updateSong(songId, next);
  93. }
  94. ], (err, song) => {
  95. if (err) {
  96. err = utils.getError(err);
  97. logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  98. return cb({'status': 'failure', 'message': err});
  99. }
  100. logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);
  101. cache.pub('song.updated', song._id);
  102. cb({ status: 'success', message: 'Song has been successfully updated', data: song });
  103. });
  104. }),
  105. remove: hooks.adminRequired((session, songId, cb) => {
  106. async.waterfall([
  107. (next) => {
  108. db.models.song.remove({_id: songId}, next);
  109. },
  110. (res, next) => {//TODO Check if res gets returned from above
  111. cache.hdel('songs', songId, next);
  112. }
  113. ], (err) => {
  114. if (err) {
  115. err = utils.getError(err);
  116. logger.error("SONGS_UPDATE", `Failed to update song "${songId}". "${err}"`);
  117. return cb({'status': 'failure', 'message': err});
  118. }
  119. logger.success("SONGS_UPDATE", `Successfully updated song "${songId}".`);
  120. cache.pub('song.removed', songId);
  121. cb({status: 'success', message: 'Song has been successfully updated'});
  122. });
  123. }),
  124. add: hooks.adminRequired((session, song, cb, userId) => {
  125. async.waterfall([
  126. (next) => {
  127. queueSongs.remove(session, song._id, () => {
  128. next();
  129. });
  130. },
  131. (next) => {
  132. db.models.song.findOne({_id: song._id}, next);
  133. },
  134. (existingSong, next) => {
  135. if (existingSong) return next('Song is already in rotation.');
  136. next();
  137. },
  138. (next) => {
  139. const newSong = new db.models.song(song);
  140. newSong.acceptedBy = userId;
  141. newSong.acceptedAt = Date.now();
  142. newSong.save(next);
  143. }
  144. ], (err) => {
  145. if (err) {
  146. err = utils.getError(err);
  147. logger.error("SONGS_ADD", `User "${userId}" failed to add song. "${err}"`);
  148. return cb({'status': 'failure', 'message': err});
  149. }
  150. logger.success("SONGS_ADD", `User "${userId}" successfully added song "${song._id}".`);
  151. cache.pub('song.added', song._id);
  152. cb({status: 'success', message: 'Song has been moved from the queue successfully.'});
  153. });
  154. //TODO Check if video is in queue and Add the song to the appropriate stations
  155. }),
  156. like: hooks.loginRequired((session, songId, cb, userId) => {
  157. db.models.user.findOne({ _id: userId }, (err, user) => {
  158. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  159. db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  160. if (!err) {
  161. db.models.user.count({"liked": songId}, (err, likes) => {
  162. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  163. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  164. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  165. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  166. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  167. songs.updateSong(songId, (err, song) => {});
  168. cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  169. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  170. });
  171. });
  172. });
  173. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  174. });
  175. });
  176. }),
  177. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  178. db.models.user.findOne({ _id: userId }, (err, user) => {
  179. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  180. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  181. if (!err) {
  182. db.models.user.count({"liked": songId}, (err, likes) => {
  183. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  184. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  185. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  186. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  187. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  188. songs.updateSong(songId, (err, song) => {});
  189. cache.pub('song.dislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  190. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  191. });
  192. });
  193. });
  194. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  195. });
  196. });
  197. }),
  198. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  199. db.models.user.findOne({ _id: userId }, (err, user) => {
  200. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  201. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  202. if (!err) {
  203. db.models.user.count({"liked": songId}, (err, likes) => {
  204. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  205. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  206. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  207. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  208. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  209. songs.updateSong(songId, (err, song) => {});
  210. cache.pub('song.undislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  211. return cb({ status: 'success', message: 'You have successfully undisliked this song.' });
  212. });
  213. });
  214. });
  215. } else return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  216. });
  217. });
  218. }),
  219. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  220. db.models.user.findOne({ _id: userId }, (err, user) => {
  221. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  222. db.models.user.update({_id: userId}, {$pull: {liked: songId, 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 unliking this song.' });
  226. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  227. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking 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 unliking this song.' });
  230. songs.updateSong(songId, (err, song) => {});
  231. cache.pub('song.unlike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  232. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  233. });
  234. });
  235. });
  236. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  237. });
  238. });
  239. }),
  240. getOwnSongRatings: hooks.loginRequired(function(session, songId, cb, userId) {
  241. db.models.user.findOne({_id: userId}, (err, user) => {
  242. if (!err && user) {
  243. return cb({
  244. status: 'success',
  245. songId: songId,
  246. liked: (user.liked.indexOf(songId) !== -1),
  247. disliked: (user.disliked.indexOf(songId) !== -1)
  248. });
  249. } else {
  250. return cb({
  251. status: 'failure',
  252. message: 'You are not logged in.'
  253. });
  254. }
  255. });
  256. })
  257. };