spotify.js 3.0 KB

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