queueSongs.js 3.4 KB

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