songs.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. index: hooks.adminRequired((session, cb) => {
  56. db.models.song.find({}, (err, songs) => {
  57. if (err) throw err;
  58. cb(songs);
  59. });
  60. }),
  61. update: hooks.adminRequired((session, songId, song, cb) => {
  62. db.models.song.update({ _id: songId }, song, { upsert: true }, (err, updatedSong) => {
  63. if (err) console.error(err);
  64. songs.updateSong(songId, (err, song) => {
  65. if (err) console.error(err);
  66. cache.pub('song.updated', song._id);
  67. cb({ status: 'success', message: 'Song has been successfully updated', data: song });
  68. });
  69. });
  70. }),
  71. remove: hooks.adminRequired((session, songId, cb) => {
  72. db.models.song.remove({ _id: songId }, (err) => {
  73. if (err) return cb({status: 'failure', message: err.message});
  74. cache.hdel('songs', songId, (err) => {
  75. if (err) return cb({status: 'failure', message: err.message});
  76. cache.pub('song.removed', songId);
  77. cb({status: 'success', message: 'Successfully removed the song.'});
  78. });
  79. });
  80. }),
  81. add: hooks.adminRequired((session, song, cb, userId) => {
  82. queueSongs.remove(session, song._id, () => {
  83. const newSong = new db.models.song(song);
  84. db.models.song.findOne({ _id: song._id }, (err, existingSong) => {
  85. if (err) console.error(err);
  86. newSong.acceptedBy = userId;
  87. newSong.acceptedAt = Date.now();
  88. if (!existingSong) newSong.save(err => {
  89. if (err) {
  90. console.error(err);
  91. cb({ status: 'failure', message: 'Something went wrong while adding the song to the queue.' });
  92. } else {
  93. cache.pub('song.added', song._id);
  94. cb({ status: 'success', message: 'Song has been moved from Queue' });
  95. }
  96. });
  97. });
  98. //TODO Check if video is in queue and Add the song to the appropriate stations
  99. });
  100. }),
  101. like: hooks.loginRequired((session, songId, cb, userId) => {
  102. db.models.user.findOne({ _id: userId }, (err, user) => {
  103. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  104. db.models.user.update({_id: userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, err => {
  105. if (!err) {
  106. db.models.user.count({"liked": songId}, (err, likes) => {
  107. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  108. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  109. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  110. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  111. if (err) return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  112. songs.updateSong(songId, (err, song) => {});
  113. cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  114. return cb({ status: 'success', message: 'You have successfully liked this song.' });
  115. });
  116. });
  117. });
  118. } else return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  119. });
  120. });
  121. }),
  122. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  123. db.models.user.findOne({ _id: userId }, (err, user) => {
  124. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  125. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, err => {
  126. if (!err) {
  127. db.models.user.count({"liked": songId}, (err, likes) => {
  128. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  129. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  130. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  131. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  132. if (err) return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  133. songs.updateSong(songId, (err, song) => {});
  134. cache.pub('song.dislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  135. return cb({ status: 'success', message: 'You have successfully disliked this song.' });
  136. });
  137. });
  138. });
  139. } else return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  140. });
  141. });
  142. }),
  143. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  144. db.models.user.findOne({ _id: userId }, (err, user) => {
  145. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  146. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  147. if (!err) {
  148. db.models.user.count({"liked": songId}, (err, likes) => {
  149. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  150. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  151. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  152. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  153. if (err) return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  154. songs.updateSong(songId, (err, song) => {});
  155. cache.pub('song.undislike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  156. return cb({ status: 'success', message: 'You have successfully undisliked this song.' });
  157. });
  158. });
  159. });
  160. } else return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  161. });
  162. });
  163. }),
  164. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  165. db.models.user.findOne({ _id: userId }, (err, user) => {
  166. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  167. db.models.user.update({_id: userId}, {$pull: {liked: songId, disliked: songId}}, err => {
  168. if (!err) {
  169. db.models.user.count({"liked": songId}, (err, likes) => {
  170. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  171. db.models.user.count({"disliked": songId}, (err, dislikes) => {
  172. if (err) return cb({ status: 'failure', message: 'Something went wrong while undiking this song.' });
  173. db.models.song.update({_id: songId}, {$set: {likes: likes, dislikes: dislikes}}, (err) => {
  174. if (err) return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  175. songs.updateSong(songId, (err, song) => {});
  176. cache.pub('song.unlike', JSON.stringify({ songId, userId: session.userId, likes: likes, dislikes: dislikes }));
  177. return cb({ status: 'success', message: 'You have successfully unliked this song.' });
  178. });
  179. });
  180. });
  181. } else return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  182. });
  183. });
  184. }),
  185. getOwnSongRatings: hooks.loginRequired(function(session, songId, cb, userId) {
  186. db.models.user.findOne({_id: userId}, (err, user) => {
  187. if (!err && user) {
  188. return cb({
  189. status: 'success',
  190. songId: songId,
  191. liked: (user.liked.indexOf(songId) !== -1),
  192. disliked: (user.disliked.indexOf(songId) !== -1)
  193. });
  194. } else {
  195. return cb({
  196. status: 'failure',
  197. message: 'You are not logged in.'
  198. });
  199. }
  200. });
  201. })
  202. };