queueSongs.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. const db = require('../db');
  3. const utils = require('../utils');
  4. const notifications = require('../notifications');
  5. const cache = require('../cache');
  6. const async = require('async');
  7. const config = require('config');
  8. const request = require('request');
  9. const hooks = require('./hooks');
  10. cache.sub('queue.newSong', songId => {
  11. console.log(123321);
  12. db.models.queueSong.findOne({_id: songId}, (err, song) => {
  13. console.log(err, song);
  14. utils.emitToRoom('admin.queue', 'event:admin.queueSong.added', song);
  15. });
  16. });
  17. cache.sub('queue.removedSong', songId => {
  18. utils.emitToRoom('admin.queue', 'event:admin.queueSong.removed', songId);
  19. });
  20. cache.sub('queue.updatedSong', songId => {
  21. //TODO Retrieve new Song object
  22. utils.emitToRoom('admin.queue', 'event:song.updated', { songId });
  23. });
  24. module.exports = {
  25. index: hooks.adminRequired((session, cb) => {
  26. db.models.queueSong.find({}, (err, songs) => {
  27. if (err) throw err;
  28. cb(songs);
  29. });
  30. }),
  31. update: hooks.adminRequired((session, _id, updatedSong, cb) => {
  32. //TODO Check if id and updatedSong is valid
  33. db.models.queueSong.findOne({ _id }, (err, currentSong) => {
  34. if (err) console.error(err);
  35. // TODO Check if new id, if any, is already in use in queue or on rotation
  36. let updated = false;
  37. for (let prop in updatedSong) if (updatedSong[prop] !== currentSong[prop]) currentSong[prop] = updatedSong[prop]; updated = true;
  38. if (!updated) return cb({ status: 'error', message: 'No properties changed' });
  39. else {
  40. currentSong.save(err => {
  41. if (err) console.error(err);
  42. return cb({ status: 'success', message: 'Successfully updated the queued song' });
  43. });
  44. }
  45. });
  46. }),
  47. remove: hooks.adminRequired((session, songId, cb) => {
  48. db.models.queueSong.remove({ _id: songId }, (err, res) => {
  49. if (err) return cb({ status: 'failure', message: err.message });
  50. cache.pub('queue.removedSong', songId);
  51. cb({ status: 'success', message: 'Song was removed successfully' });
  52. });
  53. }),
  54. add: hooks.loginRequired((session, songId, cb, userId) => {
  55. //TODO Check if id is valid
  56. let requestedAt = Date.now();
  57. async.waterfall([
  58. (next) => {
  59. db.models.queueSong.findOne({_id: songId}, (err, song) => {
  60. if (err) return next('Something went wrong while getting the song from the Database.');
  61. if (song) return next('This song is already in the queue.');
  62. next();
  63. });
  64. },
  65. (next) => {
  66. db.models.song.findOne({_id: songId}, (err, song) => {
  67. if (err) return next('Something went wrong while getting the song from the Database.');
  68. if (song) return next('This song has already been added.');
  69. next();
  70. });
  71. },
  72. // Get YouTube data from id
  73. (next) => {
  74. utils.getSongFromYouTube(songId, (song) => {
  75. song.artists = [];
  76. song.genres = [];
  77. song.skipDuration = 0;
  78. song.thumbnail = 'empty';
  79. song.explicit = false;
  80. song.requestedBy = userId;
  81. song.requestedAt = requestedAt;
  82. next(null, song);
  83. });
  84. },
  85. (newSong, next) => {
  86. utils.getSongFromSpotify(newSong, (song) => {
  87. next(null, song);
  88. });
  89. },
  90. (newSong, next) => {
  91. const song = new db.models.queueSong(newSong);
  92. // check if song already exists
  93. song.save(err => {
  94. if (err) {
  95. console.error(err);
  96. return next('Failed to add song to database');
  97. }
  98. //stations.getStation(station).playlist.push(newSong);
  99. next(null, newSong);
  100. });
  101. }
  102. ],
  103. (err, newSong) => {
  104. if (err) return cb({ status: 'error', message: err });
  105. cache.pub('queue.newSong', newSong._id);
  106. return cb({ status: 'success', message: 'Successfully added that song to the queue' });
  107. });
  108. })
  109. };