songs.js 7.7 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. cache.sub('song.like', (data) => {
  8. io.io.to(`song.${data.songId}`).emit('event:song.like', {songId: data.songId, undisliked: data.undisliked});
  9. utils.socketsFromUser(data.userId, (sockets) => {
  10. sockets.forEach((socket) => {
  11. socket.emit('event:song.newRatings', {songId: data.songId, liked: true, disliked: false});
  12. });
  13. });
  14. });
  15. cache.sub('song.dislike', (data) => {
  16. io.io.to(`song.${data.songId}`).emit('event:song.dislike', {songId: data.songId, unliked: data.unliked});
  17. utils.socketsFromUser(data.userId, (sockets) => {
  18. sockets.forEach((socket) => {
  19. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: true});
  20. });
  21. });
  22. });
  23. cache.sub('song.unlike', (data) => {
  24. io.io.to(`song.${data.songId}`).emit('event:song.unlike', {songId: data.songId});
  25. utils.socketsFromUser(data.userId, (sockets) => {
  26. sockets.forEach((socket) => {
  27. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  28. });
  29. });
  30. });
  31. cache.sub('song.undislike', (data) => {
  32. io.io.to(`song.${data.songId}`).emit('event:song.undislike', {songId: data.songId});
  33. utils.socketsFromUser(data.userId, (sockets) => {
  34. sockets.forEach((socket) => {
  35. socket.emit('event:song.newRatings', {songId: data.songId, liked: false, disliked: false});
  36. });
  37. });
  38. });
  39. module.exports = {
  40. index: (session, cb) => {
  41. db.models.song.find({}, (err, songs) => {
  42. if (err) throw err;
  43. cb(songs);
  44. });
  45. },
  46. update: (session, id, song, cb) => {
  47. //TODO Require admin/login
  48. db.models.song.findOneAndUpdate({ id }, song, { upsert: true }, (err, updatedSong) => {
  49. if (err) throw err;
  50. cb(updatedSong);
  51. });
  52. },
  53. remove: (session, _id, cb) => {
  54. //TODO Require admin/login
  55. db.models.song.find({ _id }).remove().exec();
  56. },
  57. add: (session, song, cb) => {
  58. //TODO Require admin/login
  59. console.log(session.logged_in);
  60. // if (!session.logged_in) return cb({ status: 'failure', message: 'You must be logged in to add a song' });
  61. const newSong = new db.models.song(song);
  62. newSong.save(err => {
  63. if (err) throw err;
  64. else cb({ status: 'success', message: 'Song has been moved from Queue' })
  65. });
  66. //TODO Check if video already in songs list
  67. //TODO Check if video is in queue
  68. //TODO Add the song to the appropriate stations
  69. },
  70. like: (sessionId, songId, cb) => {
  71. cache.hget('sessions', sessionId, (err, session) => {
  72. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  73. db.models.user.findOne({_id: userSession.userId}, (err, user) => {
  74. if (user.liked.indexOf(songId) !== -1) return cb({ status: 'failure', message: 'You have already liked this song.' });
  75. let dislikes = 0;
  76. if (user.disliked.indexOf(songId) !== -1) dislikes = -1;
  77. db.models.song.update({_id: songId}, {$inc: {likes: 1, dislikes: dislikes}}, (err) => {
  78. if (!err) {
  79. db.models.user.update({_id: userSession.userId}, {$push: {liked: songId}, $pull: {disliked: songId}}, (err) => {
  80. if (!err) {
  81. console.log(JSON.stringify({songId, userId: userSession.userId}));
  82. songs.updateSong(songId, (err, song) => {});
  83. cache.pub('song.like', JSON.stringify({songId, userId: userSession.userId, undisliked: (dislikes === -1)}));
  84. } else db.models.song.update({_id: songId}, {$inc: {likes: -1, dislikes: -dislikes}}, (err) => {
  85. return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  86. });
  87. });
  88. } else {
  89. return cb({ status: 'failure', message: 'Something went wrong while liking this song.' });
  90. }
  91. });
  92. });
  93. });
  94. });
  95. },
  96. dislike: (sessionId, songId, cb) => {
  97. cache.hget('sessions', sessionId, (err, session) => {
  98. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  99. db.models.user.findOne({_id: userSession.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: userSession.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: userSession.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. });
  120. },
  121. undislike: (sessionId, songId, cb) => {
  122. cache.hget('sessions', sessionId, (err, session) => {
  123. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  124. db.models.user.findOne({_id: userSession.userId}, (err, user) => {
  125. if (user.disliked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not disliked this song.' });
  126. db.models.song.update({_id: songId}, {$inc: {dislikes: -1}}, (err) => {
  127. if (!err) {
  128. db.models.user.update({_id: userSession.userId}, {$pull: {disliked: songId}}, (err) => {
  129. if (!err) {
  130. songs.updateSong(songId, (err, song) => {});
  131. cache.pub('song.undislike', JSON.stringify({songId, userId: userSession.userId}));
  132. } else db.models.song.update({_id: songId}, {$inc: {dislikes: 1}}, (err) => {
  133. return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  134. });
  135. });
  136. } else {
  137. return cb({ status: 'failure', message: 'Something went wrong while undisliking this song.' });
  138. }
  139. });
  140. });
  141. });
  142. });
  143. },
  144. unlike: (sessionId, songId, cb) => {
  145. cache.hget('sessions', sessionId, (err, session) => {
  146. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  147. db.models.user.findOne({_id: userSession.userId}, (err, user) => {
  148. if (user.liked.indexOf(songId) === -1) return cb({ status: 'failure', message: 'You have not liked this song.' });
  149. db.models.song.update({_id: songId}, {$inc: {likes: -1}}, (err) => {
  150. if (!err) {
  151. db.models.user.update({_id: userSession.userId}, {$pull: {liked: songId}}, (err) => {
  152. if (!err) {
  153. songs.updateSong(songId, (err, song) => {});
  154. cache.pub('song.unlike', JSON.stringify({songId, userId: userSession.userId}));
  155. } else db.models.song.update({_id: songId}, {$inc: {likes: 1}}, (err) => {
  156. return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  157. });
  158. });
  159. } else {
  160. return cb({ status: 'failure', message: 'Something went wrong while unliking this song.' });
  161. }
  162. });
  163. });
  164. });
  165. });
  166. },
  167. getOwnSongRatings: (sessionId, songId, cb) => {
  168. cache.hget('sessions', sessionId, (err, session) => {
  169. cache.hget('userSessions', session.userSessionId, (err, userSession) => {
  170. db.models.user.findOne({_id: userSession.userId}, (err, user) => {
  171. console.log({ status: 'success', songId: songId, liked: (user.liked.indexOf(songId) !== -1), disliked: (user.disliked.indexOf(songId) !== -1) })
  172. console.log(user.liked)
  173. console.log(user.disliked)
  174. return cb({ status: 'success', songId: songId, liked: (user.liked.indexOf(songId) !== -1), disliked: (user.disliked.indexOf(songId) !== -1) });
  175. });
  176. });
  177. });
  178. },
  179. };