songs.js 7.6 KB

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