queueSongs.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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) console.error(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) console.error(err);
  38. return cb({ status: 'success', message: 'Successfully updated the queued song' });
  39. });
  40. }
  41. });
  42. }),
  43. remove: hooks.adminRequired((session, songId, cb) => {
  44. db.models.queueSong.remove({ _id: songId }, (err, res) => {
  45. if (err) return cb({ status: 'failure', message: err.message });
  46. //TODO Pub/sub for (queue)songs on admin pages.
  47. cb({ status: 'success', message: 'Song was removed successfully' });
  48. });
  49. }),
  50. add: hooks.loginRequired((session, songId, cb, userId) => {
  51. //TODO Check if id is valid
  52. let requestedAt = Date.now();
  53. async.waterfall([
  54. (next) => {
  55. db.models.queueSong.findOne({_id: songId}, (err, song) => {
  56. if (err) return next('Something went wrong while getting the song from the Database.');
  57. if (song) return next('This song is already in the queue.');
  58. next();
  59. });
  60. },
  61. (next) => {
  62. db.models.song.findOne({_id: songId}, (err, song) => {
  63. if (err) return next('Something went wrong while getting the song from the Database.');
  64. if (song) return next('This song has already been added.');
  65. next();
  66. });
  67. },
  68. // Get YouTube data from id
  69. (next) => {
  70. utils.getSongFromYouTube(songId, (song) => {
  71. song.artists = [];
  72. song.genres = [];
  73. song.skipDuration = 0;
  74. song.thumbnail = 'empty';
  75. song.explicit = false;
  76. song.requestedBy = userId;
  77. song.requestedAt = requestedAt;
  78. next(null, song);
  79. });
  80. },
  81. (newSong, next) => {
  82. utils.getSongFromSpotify(newSong, (song) => {
  83. next(null, song);
  84. });
  85. },
  86. (newSong, next) => {
  87. const song = new db.models.queueSong(newSong);
  88. // check if song already exists
  89. song.save(err => {
  90. if (err) {
  91. console.error(err);
  92. return next('Failed to add song to database');
  93. }
  94. //stations.getStation(station).playlist.push(newSong);
  95. next(null, newSong);
  96. });
  97. }
  98. ],
  99. (err, newSong) => {
  100. if (err) return cb({ status: 'error', message: err });
  101. cache.pub('queue.newSong', newSong._id);
  102. return cb({ status: 'success', message: 'Successfully added that song to the queue' });
  103. });
  104. })
  105. };