spotify.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { useHasPermission } from "../hooks/hasPermission";
  2. // eslint-disable-next-line
  3. import moduleManager from "../../index";
  4. const UtilsModule = moduleManager.modules.utils;
  5. const SpotifyModule = moduleManager.modules.spotify;
  6. export default {
  7. /**
  8. * Fetches tracks from media sources
  9. * @param {object} session - the session object automatically added by the websocket
  10. * @param {Array} mediaSources - the media sources to get tracks for
  11. * @returns {{status: string, data: object}}
  12. */
  13. getTracksFromMediaSources: useHasPermission(
  14. "spotify.getTracksFromMediaSources",
  15. function getTracksFromMediaSources(session, mediaSources, cb) {
  16. SpotifyModule.runJob("GET_TRACKS_FROM_MEDIA_SOURCES", { mediaSources }, this)
  17. .then(response => {
  18. this.log(
  19. "SUCCESS",
  20. "SPOTIFY_GET_TRACKS_FROM_MEDIA_SOURCES",
  21. `Getting tracks from media sources was successful.`
  22. );
  23. return cb({ status: "success", data: { tracks: response.tracks } });
  24. })
  25. .catch(async err => {
  26. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  27. this.log(
  28. "ERROR",
  29. "SPOTIFY_GET_TRACKS_FROM_MEDIA_SOURCES",
  30. `Getting tracks from media sources failed. "${err}"`
  31. );
  32. return cb({ status: "error", message: err });
  33. });
  34. }
  35. ),
  36. /**
  37. * Fetches albums from ids
  38. * @param {object} session - the session object automatically added by the websocket
  39. * @param {Array} albumIds - the ids of the Spotify albums to get
  40. * @returns {{status: string, data: object}}
  41. */
  42. getAlbumsFromIds: useHasPermission("spotify.getAlbumsFromIds", function getAlbumsFromIds(session, albumIds, cb) {
  43. SpotifyModule.runJob("GET_ALBUMS_FROM_IDS", { albumIds }, this)
  44. .then(albums => {
  45. this.log("SUCCESS", "SPOTIFY_GET_ALBUMS_FROM_IDS", `Getting albums from ids was successful.`);
  46. return cb({ status: "success", data: { albums } });
  47. })
  48. .catch(async err => {
  49. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  50. this.log("ERROR", "SPOTIFY_GET_ALBUMS_FROM_IDS", `Getting albums from ids failed. "${err}"`);
  51. return cb({ status: "error", message: err });
  52. });
  53. }),
  54. /**
  55. * Fetches artists from ids
  56. * @param {object} session - the session object automatically added by the websocket
  57. * @param {Array} artistIds - the ids of the Spotify artists to get
  58. * @returns {{status: string, data: object}}
  59. */
  60. getArtistsFromIds: useHasPermission(
  61. "spotify.getArtistsFromIds",
  62. function getArtistsFromIds(session, artistIds, cb) {
  63. SpotifyModule.runJob("GET_ARTISTS_FROM_IDS", { artistIds }, this)
  64. .then(artists => {
  65. this.log("SUCCESS", "SPOTIFY_GET_ARTISTS_FROM_IDS", `Getting artists from ids was successful.`);
  66. return cb({ status: "success", data: { artists } });
  67. })
  68. .catch(async err => {
  69. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  70. this.log("ERROR", "SPOTIFY_GET_ARTISTS_FROM_IDS", `Getting artists from ids failed. "${err}"`);
  71. return cb({ status: "error", message: err });
  72. });
  73. }
  74. )
  75. };