songs.js 6.7 KB

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