songs.js 7.6 KB

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