songs.js 9.3 KB

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