queueSongs.js 3.5 KB

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