songs.js 7.0 KB

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