queueSongs.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. const spotifyParams = [
  82. `q=${encodeURIComponent(newSong.title)}`,
  83. `type=track`
  84. ].join('&');
  85. request(`https://api.spotify.com/v1/search?${spotifyParams}`, (err, res, body) => {
  86. if (err) {
  87. console.error(err);
  88. return next('Failed to find song from Spotify');
  89. }
  90. body = JSON.parse(body);
  91. durationArtistLoop:
  92. for (let i in body) {
  93. let items = body[i].items;
  94. for (let j in items) {
  95. let item = items[j];
  96. let hasArtist = false;
  97. for (let k = 0; k < item.artists.length; k++) {
  98. let artist = item.artists[k];
  99. if (newSong.title.indexOf(artist.name) !== -1) {
  100. hasArtist = true;
  101. }
  102. }
  103. if (hasArtist && newSong.title.indexOf(item.name) !== -1) {
  104. newSong.duration = item.duration_ms / 1000;
  105. newSong.artists = item.artists.map(artist => {
  106. return artist.name;
  107. });
  108. newSong.title = item.name;
  109. newSong.explicit = item.explicit;
  110. newSong.thumbnail = item.album.images[1].url;
  111. break durationArtistLoop;
  112. }
  113. }
  114. }
  115. next(null, newSong);
  116. });
  117. },
  118. (newSong, next) => {
  119. const song = new db.models.queueSong(newSong);
  120. // check if song already exists
  121. song.save(err => {
  122. if (err) {
  123. console.error(err);
  124. return next('Failed to add song to database');
  125. }
  126. //stations.getStation(station).playlist.push(newSong);
  127. next(null, newSong);
  128. });
  129. }
  130. ],
  131. (err, newSong) => {
  132. if (err) return cb({ status: 'error', message: err });
  133. cache.pub('queue.newSong', newSong._id);
  134. return cb({ status: 'success', message: 'Successfully added that song to the queue' });
  135. });
  136. })
  137. };