songs.js 7.3 KB

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