cody-music
Version:
mac osx spotify and itunes music player controller, spotify audio features, itunes and spotify genre, and playlist control
90 lines • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserProfile = exports.SpotifyUser = void 0;
const client_1 = require("./client");
const store_1 = require("./store");
const playerstate_1 = require("./playerstate");
const models_1 = require("./models");
const musicClient = client_1.MusicClient.getInstance();
const musicPlayerCtr = playerstate_1.MusicPlayerState.getInstance();
const musicStore = store_1.MusicStore.getInstance();
class SpotifyUser {
constructor() {
this.birthdate = ""; // format YYYY-MM-DD
this.country = "";
this.display_name = "";
this.email = "";
this.uri = "";
this.id = "";
this.followers = {};
this.product = "";
this.status = 0;
// {"birthdate":"1937-06-01","country":"SE","display_name":"JM Wizzler",
// "email":"email@example.com","external_urls":{"spotify":"https://open.spotify.com/user/wizzler"},
// "followers":{"href":null,"total":3829},"href":"https://api.spotify.com/v1/users/wizzler",
// "id":"wizzler","images":[{"height":null,
// "url":"https://fbcdn-profile-a.akamaihd.net/hprofile-ak-frc3/t1.0-1/1970403_10152215092574354_1798272330_n.jpg",
//"width":null}],"product":"premium","type":"user","uri":"spotify:user:wizzler"}
}
}
exports.SpotifyUser = SpotifyUser;
class UserProfile {
constructor() {
//
}
static getInstance() {
if (!UserProfile.instance) {
UserProfile.instance = new UserProfile();
}
return UserProfile.instance;
}
async accessExpired() {
if (!musicStore.hasSpotifyAccessToken()) {
// no access token to check against
return false;
}
const api = "/v1/me";
let resp = await musicClient.spotifyApiGet(api);
if (resp.status === 401) {
// refresh the token
await musicClient.refreshSpotifyToken();
// try again
resp = await musicClient.spotifyApiGet(api);
if (resp.status === 401) {
return true;
}
}
return false;
}
async getUserProfile() {
let userProfile = new SpotifyUser();
let api = "/v1/me";
let response = await musicClient.spotifyApiGet(api);
// check if the token needs to be refreshed
if (response.status === 401) {
// refresh the token
await musicClient.refreshSpotifyToken();
// try again
response = await musicClient.spotifyApiGet(api);
}
if (response && response.status === 200 && response.data) {
userProfile = response.data;
musicStore.spotifyUserId = userProfile.id;
}
userProfile.status = response.status || 0;
return userProfile;
}
async spotifyAuthState() {
// check if they have oauth activated
const oauthActivated = musicStore.spotifyAccessToken ? true : false;
// they have a spotify access token, check devices
const devices = await musicPlayerCtr.getSpotifyDevices();
const loggedIn = oauthActivated && devices && devices.length > 0 ? true : false;
let authState = new models_1.SpotifyAuthState();
authState.loggedIn = loggedIn;
authState.oauthActivated = oauthActivated;
return authState;
}
}
exports.UserProfile = UserProfile;
//# sourceMappingURL=profile.js.map