songs.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. console.log(err, 1);
  76. if (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. console.log(JSON.stringify({ songId, userId: userId }));
  97. songs.updateSong(songId, (err, song) => {});
  98. cache.pub('song.like', JSON.stringify({ songId, userId: session.userId, undisliked: (dislikes === -1) }));
  99. } else db.models.song.update({ _id: songId }, { $inc: { likes: -1, dislikes: -dislikes } }, err => {
  100. return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  101. });
  102. });
  103. } else {
  104. return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  105. }
  106. });
  107. });
  108. }),
  109. dislike: hooks.loginRequired((session, songId, cb, userId) => {
  110. db.models.user.findOne({_id: userId}, (err, user) => {
  111. if (user.disliked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already disliked this song.' });
  112. let likes = 0;
  113. if (user.liked.indexOf(songId) !== -1) likes = -1;
  114. db.models.song.update({_id: songId}, {$inc: {likes: likes, dislikes: 1}}, (err) => {
  115. if (!err) {
  116. db.models.user.update({_id: userId}, {$push: {disliked: songId}, $pull: {liked: songId}}, (err) => {
  117. if (!err) {
  118. songs.updateSong(songId, (err, song) => {});
  119. cache.pub('song.dislike', JSON.stringify({songId, userId: userId, unliked: (likes === -1)}));
  120. } else db.models.song.update({_id: songId}, {$inc: {likes: -likes, dislikes: -1}}, (err) => {
  121. return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  122. });
  123. });
  124. } else {
  125. return cb({ status: 'failure', message: 'Something went wrong while disliking this song.' });
  126. }
  127. });
  128. });
  129. }),
  130. undislike: hooks.loginRequired((session, songId, cb, userId) => {
  131. db.models.user.findOne({_id: userId}, (err, user) => {
  132. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  133. db.models.song.update({_id: songId}, {$inc: {dislikes: -1}}, (err) => {
  134. if (!err) {
  135. db.models.user.update({_id: userId}, {$pull: {disliked: songId}}, (err) => {
  136. if (!err) {
  137. songs.updateSong(songId, (err, song) => {});
  138. cache.pub('song.undislike', JSON.stringify({songId, userId: userId}));
  139. } else db.models.song.update({_id: songId}, {$inc: {dislikes: 1}}, (err) => {
  140. return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  141. });
  142. });
  143. } else {
  144. return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  145. }
  146. });
  147. });
  148. }),
  149. unlike: hooks.loginRequired((session, songId, cb, userId) => {
  150. db.models.user.findOne({_id: userId}, (err, user) => {
  151. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  152. db.models.song.update({_id: songId}, {$inc: {likes: -1}}, (err) => {
  153. if (!err) {
  154. db.models.user.update({_id: userId}, {$pull: {liked: songId}}, (err) => {
  155. if (!err) {
  156. songs.updateSong(songId, (err, song) => {});
  157. cache.pub('song.unlike', JSON.stringify({songId, userId: userId}));
  158. } else db.models.song.update({_id: songId}, {$inc: {likes: 1}}, (err) => {
  159. return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  160. });
  161. });
  162. } else {
  163. return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  164. }
  165. });
  166. });
  167. }),
  168. getOwnSongRatings: hooks.loginRequired(function(session, songId, cb, userId) {
  169. db.models.user.findOne({_id: userId}, (err, user) => {
  170. return cb({
  171. status: 'success',
  172. songId: songId,
  173. liked: (user.liked.indexOf(songId) !== -1),
  174. disliked: (user.disliked.indexOf(songId) !== -1)
  175. });
  176. });
  177. })
  178. };