songs.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 hooks = require('./hooks');
  8. const queueSongs = require('./queueSongs');
  9. cache.sub('song.removed', songId => {
  10. utils.emitToRoom('admin.songs', 'event:admin.song.removed', songId);
  11. });
  12. cache.sub('song.added', songId => {
  13. db.models.song.findOne({_id: songId}, (err, song) => {
  14. utils.emitToRoom('admin.songs', 'event:admin.song.added', song);
  15. });
  16. });
  17. cache.sub('song.updated', songId => {
  18. db.models.song.findOne({_id: songId}, (err, song) => {
  19. utils.emitToRoom('admin.songs', 'event:admin.song.updated', song);
  20. });
  21. });
  22. cache.sub('song.like', (data) => {
  23. utils.emitToRoom(`song.${data.songId}`, 'event:song.like', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  24. utils.socketsFromUser(data.userId, (sockets) => {
  25. sockets.forEach((socket) => {
  26. socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
  27. });
  28. });
  29. });
  30. cache.sub('song.dislike', (data) => {
  31. utils.emitToRoom(`song.${data.songId}`, 'event:song.dislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  32. utils.socketsFromUser(data.userId, (sockets) => {
  33. sockets.forEach((socket) => {
  34. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: true});
  35. });
  36. });
  37. });
  38. cache.sub('song.unlike', (data) => {
  39. utils.emitToRoom(`song.${data.songId}`, 'event:song.unlike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  40. utils.socketsFromUser(data.userId, (sockets) => {
  41. sockets.forEach((socket) => {
  42. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  43. });
  44. });
  45. });
  46. cache.sub('song.undislike', (data) => {
  47. utils.emitToRoom(`song.${data.songId}`, 'event:song.undislike', {songId: data.songId, likes: data.likes, dislikes: data.dislikes});
  48. utils.socketsFromUser(data.userId, (sockets) => {
  49. sockets.forEach((socket) => {
  50. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  51. });
  52. });
  53. });
  54. module.exports = {
  55. length: hooks.adminRequired((session, cb) => {
  56. db.models.song.find({}, (err, songs) => {
  57. if (err) console.error(err);
  58. cb(songs.length);
  59. })
  60. }),
  61. getSet: hooks.adminRequired((session, set, cb) => {
  62. db.models.song.find({}).limit(15 * set).exec((err, songs) => {
  63. if (err) console.error(err);
  64. cb(songs.splice(Math.max(songs.length - 15, 0)));
  65. });
  66. }),
  67. update: hooks.adminRequired((session, songId, song, cb) => {
  68. db.models.song.update({ _id: songId }, song, { upsert: true }, err => {
  69. if (err) console.error(err);
  70. songs.updateSong(songId, (err, song) => {
  71. if (err) console.error(err);
  72. cache.pub('song.updated', song._id);
  73. cb({ status: 'success', message: 'Song has been successfully updated', data: song });
  74. });
  75. });
  76. }),
  77. remove: hooks.adminRequired((session, songId, cb) => {
  78. db.models.song.remove({ _id: songId }, (err) => {
  79. if (err) return cb({status: 'failure', message: err.message});
  80. cache.hdel('songs', songId, (err) => {
  81. if (err) return cb({status: 'failure', message: err.message});
  82. cache.pub('song.removed', songId);
  83. cb({status: 'success', message: 'Successfully removed the song.'});
  84. });
  85. });
  86. }),
  87. add: hooks.adminRequired((session, song, cb, userId) => {
  88. queueSongs.remove(session, song._id, () => {
  89. const newSong = new db.models.song(song);
  90. db.models.song.findOne({ _id: song._id }, (err, existingSong) => {
  91. if (err) console.error(err);
  92. newSong.acceptedBy = userId;
  93. newSong.acceptedAt = Date.now();
  94. if (!existingSong) newSong.save(err => {
  95. if (err) {
  96. console.error(err);
  97. cb({ status: 'failure', message: 'Something went wrong while adding the song to the queue.' });
  98. } else {
  99. cache.pub('song.added', song._id);
  100. cb({ status: 'success', message: 'Song has been moved from Queue' });
  101. }
  102. });
  103. });
  104. //TODO Check if video is in queue and Add the song to the appropriate stations
  105. });
  106. }),
  107. like: hooks.loginRequired((session, songId, cb, userId) => {
  108. db.models.user.findOne({ _id: userId }, (err, user) => {
  109. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  110. db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  111. if (!err) {
  112. db.models.user.count({"liked": songId}, (err, likes) => {
  113. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  114. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  115. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  116. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  117. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  118. songs.updateSong(songId, (err, song) => {});
  119. cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  120. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  121. });
  122. });
  123. });
  124. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  125. });
  126. });
  127. }),
  128. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  129. db.models.user.findOne({ _id: userId }, (err, user) => {
  130. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  131. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  132. if (!err) {
  133. db.models.user.count({"liked": songId}, (err, likes) => {
  134. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  135. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  136. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  137. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  138. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  139. songs.updateSong(songId, (err, song) => {});
  140. cache.pub('song.dislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  141. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  142. });
  143. });
  144. });
  145. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  146. });
  147. });
  148. }),
  149. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  150. db.models.user.findOne({ _id: userId }, (err, user) => {
  151. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  152. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  153. if (!err) {
  154. db.models.user.count({"liked": songId}, (err, likes) => {
  155. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  156. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  157. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  158. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  159. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  160. songs.updateSong(songId, (err, song) => {});
  161. cache.pub('song.undislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  162. return cb({ status: 'success', message: 'You have successfully undisliked this song.' });
  163. });
  164. });
  165. });
  166. } else return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  167. });
  168. });
  169. }),
  170. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  171. db.models.user.findOne({ _id: userId }, (err, user) => {
  172. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  173. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  174. if (!err) {
  175. db.models.user.count({"liked": songId}, (err, likes) => {
  176. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  177. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  178. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
  179. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  180. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  181. songs.updateSong(songId, (err, song) => {});
  182. cache.pub('song.unlike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  183. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  184. });
  185. });
  186. });
  187. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  188. });
  189. });
  190. }),
  191. getOwnSongRatings: hooks.loginRequired(function(session, songId, cb, userId) {
  192. db.models.user.findOne({_id: userId}, (err, user) => {
  193. if (!err && user) {
  194. return cb({
  195. status: 'success',
  196. songId: songId,
  197. liked: (user.liked.indexOf(songId) !== -1),
  198. disliked: (user.disliked.indexOf(songId) !== -1)
  199. });
  200. } else {
  201. return cb({
  202. status: 'failure',
  203. message: 'You are not logged in.'
  204. });
  205. }
  206. });
  207. })
  208. };