songs.js 6.9 KB

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