Skip to content

Commit

Permalink
Improved AccessToken usage but not using Non-Updating Session propert…
Browse files Browse the repository at this point in the history
…y and instead use TokenProvider accordingly
  • Loading branch information
surfbryce committed Feb 17, 2024
1 parent f70b922 commit 210e87e
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 29 deletions.
26 changes: 13 additions & 13 deletions dist/beautiful-lyrics.js

Large diffs are not rendered by default.

13 changes: 7 additions & 6 deletions src/Services/AutoUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {Timeout} from '../../../../Packages/Scheduler'
import {version as PackageVersion} from '../../package.json'

// Services
import {GlobalMaid, IsDevelopment, Script} from './Session'
import {GlobalMaid, IsDevelopment, Script, ShowNotification} from './Session'

// Behavior Constants
const JustUpdatedNotificationLifetime = 7.5 // Measured in Seconds
Expand Down Expand Up @@ -116,19 +116,20 @@ const CheckForUpdate = async () => {
&& ((cachedVersion.Major > 2) || ((cachedVersion.Major == 2) && (cachedVersion.Minor >= 4)))
) {
// Now send out the notifcation
Spicetify.showNotification(
ShowNotification(
`<h3>Beautiful Lyrics Updated!</h3>
<h4 style = 'margin-top: 4px; margin-bottom: 4px; font-weight: normal;'>No need to re-install - it's already running!</h4>
<span style = 'opacity: 0.75;'>Version ${ExtensionVersion.Text} -> ${cachedVersion.Text}</span>`,
(
(
(versionDistance.Major > 0) ? "success"
: (
(versionDistance.Major < 0)
|| (versionDistance.Minor < 0)
|| (versionDistance.Patch < 0)
)
|| (versionDistance.Major > 0)
) ? "warning"
: "info"
),
(JustUpdatedNotificationLifetime * 1000)
JustUpdatedNotificationLifetime
)

// Obviously we should return here
Expand Down
96 changes: 86 additions & 10 deletions src/Services/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ let SpotifyHistory: {
entries: HistoryLocation[];
} = SpotifyPlatform?.History
let SpotifyPlaybar = Spicetify.Playbar
let SpotifySession = ((SpotifyPlatform !== undefined) ? Spicetify.Platform.Session : undefined)
let SpotifySnackbar = (Spicetify as any).Snackbar
{
const WaitForSpicetify = () => {
// Update our variables
Expand All @@ -77,16 +77,16 @@ let SpotifySession = ((SpotifyPlatform !== undefined) ? Spicetify.Platform.Sessi
SpotifyPlatform = Spicetify.Platform
SpotifyHistory = SpotifyPlatform?.History
SpotifyPlaybar = Spicetify.Playbar
SpotifySession = ((SpotifyPlatform !== undefined) ? Spicetify.Platform.Session : undefined)
SpotifySnackbar = (Spicetify as any).Snackbar

// Check if we have them all yet
if (
(SpotifyPlayer === undefined)
|| (SpotifySession === undefined)
|| (SpotifyShowNotification === undefined)
|| (SpotifyPlatform === undefined)
|| (SpotifyHistory === undefined)
|| (SpotifyPlaybar === undefined)
|| (SpotifySnackbar === undefined)
) {
GlobalMaid.Give(Timeout(0, WaitForSpicetify), "WaitForSpicetify")
} else {
Expand All @@ -100,15 +100,91 @@ let SpotifySession = ((SpotifyPlatform !== undefined) ? Spicetify.Platform.Sessi
}

// Custom fetch function
type TokenProviderResponse = {accessToken: string, accessTokenExpirationTimestampMs: number}
let tokenProviderResponse: (TokenProviderResponse | undefined)
let accessTokenPromise: Promise<string> | undefined
const NeedsToRefresh = (tokenProviderResponse?: TokenProviderResponse): Promise<boolean> => {
if (tokenProviderResponse === undefined) {
return Promise.resolve(true)
}

// Otherwise, check if we have to wait at all
const timeUntilRefresh = ((tokenProviderResponse.accessTokenExpirationTimestampMs - Date.now()) / 1000)
if (timeUntilRefresh > 2) {
return Promise.resolve(false)
} else if (timeUntilRefresh > 0) {
const initialPromise = (
new Promise(resolve => GlobalMaid.Give(Timeout((timeUntilRefresh + 0.5), resolve)))
.then(_ => true)
)
accessTokenPromise = (initialPromise as any)
return initialPromise
}

// Otherwise, we need to refresh
return Promise.resolve(true)
}
export const GetAccessToken = (): Promise<string> => {
if (accessTokenPromise !== undefined) {
return accessTokenPromise
}

return (
NeedsToRefresh(tokenProviderResponse)
.then(
needsToRefresh => {
if (needsToRefresh) {
return (
SpotifyPlatform.AuthorizationAPI._tokenProvider()
.then(
(result: TokenProviderResponse) => {
tokenProviderResponse = result, accessTokenPromise = undefined
return GetAccessToken() // Re-run this to make sure we don't need to refresh again
}
)
)
}

return Promise.resolve(tokenProviderResponse!.accessToken)
}
)
)
}

export const SpotifyFetch = (url: string) => {
return fetch(
url,
{
headers: {
"Authorization": `Bearer ${SpotifySession.accessToken}`,
"Spotify-App-Version": SpotifyPlatform.version,
"App-Platform": SpotifyPlatform.PlatformData.app_platform
return (
GetAccessToken()
.then(
accessToken => fetch(
url,
{
headers: {
"Authorization": `Bearer ${accessToken}`,
"Spotify-App-Version": SpotifyPlatform.version,
"App-Platform": SpotifyPlatform.PlatformData.app_platform
}
}
)
)
)
}

// Custom notification function
export const ShowNotification = (
html: string, variant: ("info" | "success" | "warning" | "error" | "default"),
hideAfter: number
) => {
SpotifySnackbar.enqueueSnackbar(
Spicetify.React.createElement(
"div",
{
dangerouslySetInnerHTML: {
__html: html.trim()
}
}
), {
variant: variant,
autoHideDuration: (hideAfter * 1000)
}
)
}
Expand Down

0 comments on commit 210e87e

Please sign in to comment.