diff --git a/playlet-lib/src/components/ContentNode/ProfileContentNode.bs b/playlet-lib/src/components/ContentNode/ProfileContentNode.bs
new file mode 100644
index 00000000..4ae24911
--- /dev/null
+++ b/playlet-lib/src/components/ContentNode/ProfileContentNode.bs
@@ -0,0 +1,56 @@
+import "pkg:/source/AsyncTask/AsyncTask.bs"
+import "pkg:/source/AsyncTask/Tasks.bs"
+import "pkg:/source/utils/Logging.bs"
+import "pkg:/source/utils/StringUtils.bs"
+import "pkg:/source/utils/TimeUtils.bs"
+
+function HasValidAccessToken(_unused as dynamic) as boolean
+ serverType = m.top.type
+ accessToken = m.top.accessToken
+ if StringUtils.IsNullOrEmpty(accessToken)
+ return false
+ end if
+
+ if serverType = "invidious"
+ return true
+ end if
+
+ nowSeconds = TimeUtils.Now().AsSeconds()
+ expiresTimestamp = m.top.expiresTimestamp
+
+ ' We should refresh the token if it's about to expire in 30 minutes
+ ' Although the current token might be valid, we can let current request
+ ' wait for the new token
+ shouldRefreshToken = expiresTimestamp < nowSeconds + 60 * 30
+
+ if shouldRefreshToken
+ RefreshAccessToken()
+ end if
+
+ return not shouldRefreshToken
+end function
+
+function RefreshAccessToken() as void
+ if m.refreshTokenTask <> invalid
+ return
+ end if
+
+ if m.profilesService = invalid
+ m.profilesService = m.top.getScene().findNode("ProfilesService")
+ end if
+
+ LogInfo("Refreshing access token for profile:", m.top.username)
+ m.refreshTokenTask = AsyncTask.Start(Tasks.ProfileRefreshTokenTask, {
+ profile: m.top
+ profilesService: m.profilesService
+ accessTokenObject: {
+ clientId: m.top.clientId
+ clientSecret: m.top.clientSecret
+ refreshToken: m.top.refreshToken
+ }
+ }, OnProfileRefreshTokenTaskResult)
+end function
+
+function OnProfileRefreshTokenTaskResult(output as object) as void
+ m.refreshTokenTask = invalid
+end function
diff --git a/playlet-lib/src/components/ContentNode/ProfileContentNode.xml b/playlet-lib/src/components/ContentNode/ProfileContentNode.xml
index f073fd76..65d8cca0 100644
--- a/playlet-lib/src/components/ContentNode/ProfileContentNode.xml
+++ b/playlet-lib/src/components/ContentNode/ProfileContentNode.xml
@@ -18,5 +18,7 @@
+
+
\ No newline at end of file
diff --git a/playlet-lib/src/components/ContentNode/ProfileRefreshTokenTask.bs b/playlet-lib/src/components/ContentNode/ProfileRefreshTokenTask.bs
new file mode 100644
index 00000000..e0683886
--- /dev/null
+++ b/playlet-lib/src/components/ContentNode/ProfileRefreshTokenTask.bs
@@ -0,0 +1,25 @@
+import "pkg:/components/Services/Innertube/InnertubeService.bs"
+import "pkg:/source/utils/ErrorUtils.bs"
+
+@asynctask
+function ProfileRefreshTokenTask(input as object) as object
+ profileNode = input.profile
+ accessTokenObject = input.accessTokenObject
+ profilesService = input.profilesService
+ try
+ if InnertubeService.AuthRefreshAccessToken(accessTokenObject)
+ profileNode.expiresIn = accessTokenObject.expiresIn
+ profileNode.expiresTimestamp = accessTokenObject.expiresTimestamp
+ profileNode.accessToken = accessTokenObject.accessToken
+ profilesService@.SaveUserProfilesToRegistry()
+ else
+ LogError("Failed to refresh access token")
+ profileNode.accessToken = invalid
+ end if
+ catch error
+ LogError("Failed to refresh access token:", ErrorUtils.Format(error))
+ profileNode.accessToken = invalid
+ end try
+
+ return invalid
+end function
diff --git a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs
index 291e3d6b..db3f04c9 100644
--- a/playlet-lib/src/components/Services/Innertube/InnertubeService.bs
+++ b/playlet-lib/src/components/Services/Innertube/InnertubeService.bs
@@ -524,24 +524,19 @@ namespace InnertubeService
return Innertube.PollForAccessToken(code, code, cancellation)
end function
- function AuthRefreshAccessTokenIfNeeded(accessToken as object, cancellation = invalid as object) as boolean
+ function AuthRefreshAccessToken(accessToken as object, cancellation = invalid as object) as boolean
if not IsAssociativeArray(accessToken)
return false
end if
- nowSeconds = TimeUtils.Now().AsSeconds()
- expiresTimestamp = accessToken["expires_timestamp"]
+ refreshed = Innertube.RefreshAccessToken(accessToken, cancellation)
- if expiresTimestamp = invalid or expiresTimestamp < nowSeconds
- refreshed = Innertube.RefreshAccessToken(accessToken, cancellation)
-
- if IsAssociativeArray(refreshed) and refreshed.DoesExist("accessToken")
- accessToken.Append(refreshed)
- if accessToken.DoesExist("expiresIn")
- accessToken["expiresTimestamp"] = TimeUtils.Now().AsSeconds() + accessToken["expiresIn"]
- end if
- return true
+ if IsAssociativeArray(refreshed) and refreshed.DoesExist("accessToken")
+ accessToken.Append(refreshed)
+ if accessToken.DoesExist("expiresIn")
+ accessToken["expiresTimestamp"] = TimeUtils.Now().AsSeconds() + accessToken["expiresIn"]
end if
+ return true
end if
return false
diff --git a/playlet-lib/src/components/Services/Innertube/NodesParser.bs b/playlet-lib/src/components/Services/Innertube/NodesParser.bs
index c0764ffc..bb31aff5 100644
--- a/playlet-lib/src/components/Services/Innertube/NodesParser.bs
+++ b/playlet-lib/src/components/Services/Innertube/NodesParser.bs
@@ -82,6 +82,8 @@ namespace Innertube
"gridContinuation": ParseGridContinuation
"tvSurfaceContentContinuation": ParseTvSurfaceContentContinuation
' Ignored nodes:
+ ' A playlist at the end of a video. We don't need that for now.
+ "endScreenPlaylistRenderer": ParseNotImplemented
' Posts not yet supported
"postRenderer": ParseNotImplemented
' a "View all posts" button
diff --git a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs
index d377bb0f..29a89b96 100644
--- a/playlet-lib/src/components/Services/Invidious/InvidiousService.bs
+++ b/playlet-lib/src/components/Services/Invidious/InvidiousService.bs
@@ -6,6 +6,7 @@ import "pkg:/source/AsyncTask/Tasks.bs"
import "pkg:/source/services/HttpClient.bs"
import "pkg:/source/utils/CryptoUtils.bs"
import "pkg:/source/utils/Logging.bs"
+import "pkg:/source/utils/MessagePortUtils.bs"
import "pkg:/source/utils/OptionUtils.bs"
import "pkg:/source/utils/RegistryUtils.bs"
import "pkg:/source/utils/StringUtils.bs"
@@ -294,6 +295,20 @@ namespace Invidious
error: ERROR_NOT_AUTHENTICATED
}
end if
+ profileNode = authToken.profileNode
+ ' This is not the right place for a token refresh,
+ ' but it needs a refactor to be done properly.
+ if not profileNode@.HasValidAccessToken()
+ messagePort = CreateObject("roMessagePort")
+ MessagePortUtils.ObserveFieldScopedExSafe(profileNode, "accessToken", messagePort)
+ msg = wait(10000, messagePort)
+ profileNode.unobserveFieldScoped("accessToken")
+ if msg <> invalid
+ authToken.token = profileNode.accessToken
+ m.invidiousNode.authToken = authToken
+ end if
+ end if
+
request.Url(authToken.instance + endpoint.url)
request.Headers(m.GetAuthenticationHeaders(authToken.token))
end if
@@ -367,6 +382,7 @@ namespace Invidious
return {
success: false
error: response.ErrorMessage()
+ cancelled: response.IsCancelled()
}
end if
end function
diff --git a/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs b/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs
index 6f873ad2..8443b029 100644
--- a/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs
+++ b/playlet-lib/src/components/Services/ProfilesService/ProfilesService.bs
@@ -39,7 +39,7 @@ function LoadProfilesFromRegistry()
' Step 3: Save user preferences (if needed)
if isDirty
- SaveUserProfilesToRegistry()
+ SaveUserProfilesToRegistry(invalid)
end if
' Step 4: Load user profiles into content nodes
@@ -127,7 +127,7 @@ function MigrateExistingProfiles(userProfiles as object) as boolean
return true
end function
-function SaveUserProfilesToRegistry() as void
+function SaveUserProfilesToRegistry(_unused as dynamic) as void
if m.disableSavingToRegistry
return
end if
@@ -322,7 +322,7 @@ function SetCurrentProfile(newActiveProfile as object) as void
m.top.currentProfile = invalid
m.invidious.authToken = invalid
LogInfo("Current profile set to ")
- SaveUserProfilesToRegistry()
+ SaveUserProfilesToRegistry(invalid)
return
end if
@@ -333,9 +333,10 @@ function SetCurrentProfile(newActiveProfile as object) as void
token: newActiveProfile.accessToken
instance: newActiveProfile.serverUrl
username: newActiveProfile.username
+ profileNode: newActiveProfile
}
LogInfo("Current profile set to", newActiveProfile.username, "@", newActiveProfile.serverUrl)
- SaveUserProfilesToRegistry()
+ SaveUserProfilesToRegistry(invalid)
end function
function RefreshActiveProfileState(activeProfile as object) as void
diff --git a/playlet-lib/src/components/Services/ProfilesService/ProfilesService.xml b/playlet-lib/src/components/Services/ProfilesService/ProfilesService.xml
index 0546a9a7..a210ea8e 100644
--- a/playlet-lib/src/components/Services/ProfilesService/ProfilesService.xml
+++ b/playlet-lib/src/components/Services/ProfilesService/ProfilesService.xml
@@ -18,6 +18,7 @@
+
diff --git a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs
index 83c8699f..a6e11781 100644
--- a/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs
+++ b/playlet-lib/src/components/VideoFeed/VideoRowListRowContentTask.bs
@@ -96,7 +96,8 @@ function VideoRowListRowContentTask(input as object) as object
feedContentNode.title += " (Failed to load)"
end if
- HandleFeedLoadErrorDialog(response.error, rowList)
+ isCancelled = response.cancelled = true
+ HandleFeedLoadErrorDialog(response.error, isCancelled, rowList)
continue while
end if
@@ -151,7 +152,7 @@ function VideoRowListRowContentTask(input as object) as object
end while
catch error
- HandleFeedLoadErrorDialog(error, rowList)
+ HandleFeedLoadErrorDialog(error, false, rowList)
return {
success: false
error: error
@@ -186,13 +187,16 @@ function RemovePlaceHolderItems(contentNode as object)
end if
end function
-function HandleFeedLoadErrorDialog(error as object, rowList as object) as void
+function HandleFeedLoadErrorDialog(error as object, isCancelled as boolean, rowList as object) as void
error = ErrorUtils.Format(error)
LogError(error)
rowList.onError = error
if rowList.hasField("dispose") and rowList.dispose
return
end if
+ if isCancelled
+ return
+ end if
DialogUtils.ShowDialogEx({
message: error
title: "Failed to load feed"