Multiple profiles (#312)
* Navbar user profile * profiles stuff * fix profile * Lint fix * Some ui experiments * More ui work * progress * Lint fix * migration, web api, wip * profile view and web app * web app and apis * Invidious and profiles * Lint fix * bug fix * Remove ActionContentNode * Profile icon button, profile color * Lint fix * reset server url * href that doesn't work * remove activate button on active profile * TODO * Fin. * Final touches --------- Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
1 parent
8e03ca0ea6
commit
394dd23999
60 files changed
+1725
-311
No files matched your search
@@ -5,7 +5,6 @@ import "pkg:/source/AsyncTask/Tasks.bs"
|
||||
function Init()
|
||||
m.top.apiDefinitions = ParseJson(ReadAsciiFile("libpkg:/config/invidious_video_api.yaml"))
|
||||
m.service = new Invidious.InvidiousService(m.top)
|
||||
m.top.authToken = m.service.GetAuthToken()
|
||||
end function
|
||||
|
||||
function GetCurrentInstance(_unused as dynamic) as string
|
||||
@@ -16,10 +15,6 @@ function GetAuthorizeTokenLink(_unused as dynamic) as dynamic
|
||||
return m.service.GetAuthorizeTokenLink()
|
||||
end function
|
||||
|
||||
function SetAuthToken(token as string, instance as string, username as dynamic)
|
||||
m.service.SetAuthToken(token, instance, username)
|
||||
end function
|
||||
|
||||
function OnAuthTokenChange() as void
|
||||
m.top.refreshSubscribtions = true
|
||||
end function
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
<field id="applicationInfo" type="node" />
|
||||
<field id="preferences" type="node" />
|
||||
<field id="webServer" type="node" />
|
||||
<field id="profiles" type="node" />
|
||||
|
||||
<field id="authToken" type="assocarray" onChange="OnAuthTokenChange" />
|
||||
<field id="apiDefinitions" type="assocarray" />
|
||||
@@ -11,6 +12,5 @@
|
||||
|
||||
<function name="GetCurrentInstance" />
|
||||
<function name="GetAuthorizeTokenLink" />
|
||||
<function name="SetAuthToken" />
|
||||
</interface>
|
||||
</component>
|
||||
@@ -37,15 +37,15 @@ namespace Invidious
|
||||
const ERROR_NOT_AUTHENTICATED = "Not authenticated"
|
||||
|
||||
class InvidiousService
|
||||
public node as object
|
||||
public invidiousNode as object
|
||||
|
||||
function new(node as object)
|
||||
m.node = node
|
||||
function new(invidiousNode as object)
|
||||
m.invidiousNode = invidiousNode
|
||||
end function
|
||||
|
||||
function GetUserRegion() as string
|
||||
if m._userRegion = invalid
|
||||
m._userRegion = m.node.applicationInfo@.GetUserCountryCode()
|
||||
m._userRegion = m.invidiousNode.applicationInfo@.GetUserCountryCode()
|
||||
end if
|
||||
return m._userRegion
|
||||
end function
|
||||
@@ -58,7 +58,7 @@ namespace Invidious
|
||||
|
||||
function GetInstance() as string
|
||||
if m.preferences = invalid
|
||||
m.preferences = m.node.preferences
|
||||
m.preferences = m.invidiousNode.preferences
|
||||
end if
|
||||
instance = m.preferences["invidious.instance"]
|
||||
if not StringUtils.IsNullOrEmpty(instance)
|
||||
@@ -73,7 +73,7 @@ namespace Invidious
|
||||
return
|
||||
end if
|
||||
|
||||
authToken = m.node.authToken
|
||||
authToken = m.invidiousNode.authToken
|
||||
if authToken = invalid
|
||||
return
|
||||
end if
|
||||
@@ -149,7 +149,7 @@ namespace Invidious
|
||||
end function
|
||||
|
||||
function GetPlaylist(playlistId as string, index = 0 as integer, cancellation = invalid as object) as object
|
||||
authToken = m.node.authToken
|
||||
authToken = m.invidiousNode.authToken
|
||||
if authToken = invalid
|
||||
instance = m.GetInstance()
|
||||
url = `${instance}${Invidious.PLAYLISTS_ENDPOINT}/${playlistId}`
|
||||
@@ -194,7 +194,7 @@ namespace Invidious
|
||||
feedSource = feedSources[feedSourcesIndex]
|
||||
|
||||
if m.endpoints = invalid
|
||||
m.endpoints = m.node.apiDefinitions
|
||||
m.endpoints = m.invidiousNode.apiDefinitions
|
||||
end if
|
||||
|
||||
endpoint = m.endpoints[feedSource.endpoint]
|
||||
@@ -231,7 +231,7 @@ namespace Invidious
|
||||
|
||||
function MakeRequest(feedSource as object, additionalQueryParams = invalid as object, cancellation = invalid as object) as object
|
||||
if m.endpoints = invalid
|
||||
m.endpoints = m.node.apiDefinitions
|
||||
m.endpoints = m.invidiousNode.apiDefinitions
|
||||
end if
|
||||
|
||||
endpoint = m.endpoints[feedSource.endpoint]
|
||||
@@ -246,8 +246,10 @@ namespace Invidious
|
||||
|
||||
request = HttpClient.Get(instance + endpoint.url)
|
||||
|
||||
authToken = invalid
|
||||
|
||||
if endpoint.authenticated = true
|
||||
authToken = m.node.authToken
|
||||
authToken = m.invidiousNode.authToken
|
||||
if authToken = invalid
|
||||
return {
|
||||
success: false
|
||||
@@ -264,7 +266,7 @@ namespace Invidious
|
||||
if queryParam.default <> invalid
|
||||
if IsString(queryParam.type) and queryParam.type = "#ISO3166"
|
||||
if queryParam.default = "GetUserCountryCode"
|
||||
request.QueryParam(queryParamKey, m.node.applicationInfo@.GetUserCountryCode())
|
||||
request.QueryParam(queryParamKey, m.GetUserRegion())
|
||||
else
|
||||
request.QueryParam(queryParamKey, queryParam.default)
|
||||
end if
|
||||
@@ -305,23 +307,18 @@ namespace Invidious
|
||||
|
||||
response = request.Await()
|
||||
|
||||
if m.responseHandlers = invalid
|
||||
m.responseHandlers = {
|
||||
DefaultHandler: m.DefaultHandler
|
||||
AuthFeedHandler: m.AuthFeedHandler
|
||||
PlaylistHandler: m.PlaylistHandler
|
||||
VideoInfoHandler: m.VideoInfoHandler
|
||||
PlaylistInfoHandler: m.PlaylistInfoHandler
|
||||
ChannelInfoHandler: m.ChannelInfoHandler
|
||||
ChannelVideosHandler: m.ChannelVideosHandler
|
||||
ChannelPlaylistsHandler: m.ChannelPlaylistsHandler
|
||||
WatchHistoryHandler: m.WatchHistoryHandler
|
||||
}
|
||||
responseHandler = invalid
|
||||
if IsString(endpoint.responseHandler) and m.DoesExist(endpoint.responseHandler)
|
||||
responseHandler = endpoint.responseHandler
|
||||
else
|
||||
responseHandler = "DefaultHandler"
|
||||
end if
|
||||
|
||||
responseHandler = endpoint.responseHandler <> invalid ? m.responseHandlers[endpoint.responseHandler] : m.responseHandlers["DefaultHandler"]
|
||||
|
||||
result = responseHandler(m, feedSource, response)
|
||||
result = m[responseHandler]({
|
||||
feedSource: feedSource
|
||||
response: response
|
||||
authToken: authToken
|
||||
})
|
||||
|
||||
if response.IsSuccess() and result <> invalid
|
||||
return {
|
||||
@@ -336,7 +333,9 @@ namespace Invidious
|
||||
end if
|
||||
end function
|
||||
|
||||
function DefaultHandler(m as object, feedSource as object, response as object) as object
|
||||
function DefaultHandler(context as object) as object
|
||||
response = context.response
|
||||
|
||||
if response.StatusCode() = 200
|
||||
json = response.Json()
|
||||
return {
|
||||
@@ -346,7 +345,9 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function PlaylistHandler(m as object, feedSource as object, response as object) as object
|
||||
function PlaylistHandler(context as object) as object
|
||||
response = context.response
|
||||
|
||||
if response.StatusCode() = 200
|
||||
json = response.Json()
|
||||
return {
|
||||
@@ -356,7 +357,9 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function ChannelVideosHandler(m as object, feedSource as object, response as object) as object
|
||||
function ChannelVideosHandler(context as object) as object
|
||||
response = context.response
|
||||
|
||||
if response.StatusCode() = 200
|
||||
json = response.Json()
|
||||
return {
|
||||
@@ -367,7 +370,9 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function ChannelPlaylistsHandler(m as object, feedSource as object, response as object) as object
|
||||
function ChannelPlaylistsHandler(context as object) as object
|
||||
response = context.response
|
||||
|
||||
if response.StatusCode() = 200
|
||||
json = response.Json()
|
||||
return {
|
||||
@@ -378,7 +383,10 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function VideoInfoHandler(m as object, feedSource as object, response as object) as object
|
||||
function VideoInfoHandler(context as object) as object
|
||||
response = context.response
|
||||
feedSource = context.feedSource
|
||||
|
||||
statusCode = response.StatusCode()
|
||||
if statusCode >= 500
|
||||
' In some cases, such as a video became private, or a live stream ended,
|
||||
@@ -406,7 +414,9 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function PlaylistInfoHandler(m as object, feedSource as object, response as object) as object
|
||||
function PlaylistInfoHandler(context as object) as object
|
||||
response = context.response
|
||||
|
||||
if response.StatusCode() = 200
|
||||
json = response.Json()
|
||||
json.type = "playlist"
|
||||
@@ -417,7 +427,9 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function ChannelInfoHandler(m as object, feedSource as object, response as object) as object
|
||||
function ChannelInfoHandler(context as object) as object
|
||||
response = context.response
|
||||
|
||||
if response.StatusCode() = 200
|
||||
json = response.Json()
|
||||
json.type = "channel"
|
||||
@@ -428,8 +440,11 @@ namespace Invidious
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function AuthFeedHandler(m as object, feedSource as object, response as object) as object
|
||||
m.DeleteExpiredToken(response)
|
||||
function AuthFeedHandler(context as object) as object
|
||||
response = context.response
|
||||
authToken = context.authToken
|
||||
|
||||
m.DeleteExpiredToken(response, authToken)
|
||||
|
||||
if response.StatusCode() <> 200
|
||||
return invalid
|
||||
@@ -455,8 +470,11 @@ namespace Invidious
|
||||
}
|
||||
end function
|
||||
|
||||
function WatchHistoryHandler(m as object, feedSource as object, response as object) as object
|
||||
m.DeleteExpiredToken(response)
|
||||
function WatchHistoryHandler(context as object) as object
|
||||
response = context.response
|
||||
authToken = context.authToken
|
||||
|
||||
m.DeleteExpiredToken(response, authToken)
|
||||
|
||||
if response.StatusCode() <> 200
|
||||
return invalid
|
||||
@@ -480,53 +498,19 @@ namespace Invidious
|
||||
}
|
||||
end function
|
||||
|
||||
function DeleteExpiredToken(response as object)
|
||||
if response.StatusCode() = 403
|
||||
LogWarn(`Invidious instance responded with 403: ${response.Text()}`)
|
||||
LogWarn("Deleting Invidious token...")
|
||||
m.DeleteAuthToken()
|
||||
function DeleteExpiredToken(response as object, authToken as object) as void
|
||||
if response.StatusCode() <> 403
|
||||
return
|
||||
end if
|
||||
end function
|
||||
|
||||
function GetAuthToken() as object
|
||||
tokenPayload = RegistryUtils.Read(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
if tokenPayload = invalid
|
||||
return invalid
|
||||
LogWarn(`Invidious instance responded with 403: ${response.Text()}`)
|
||||
if not IsAssociativeArray(authToken) or not IsString(authToken.token)
|
||||
LogWarn("No valid token to delete")
|
||||
return
|
||||
end if
|
||||
authToken = ParseJson(tokenPayload)
|
||||
if authToken = invalid or authToken.instance = invalid or authToken.token = invalid
|
||||
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
return invalid
|
||||
end if
|
||||
if not m.VerifyTokenScope(authToken.token)
|
||||
' Right now we're probably in a UI thread, and we can't unregister the token
|
||||
' Without a Task
|
||||
AsyncTask.Start(Tasks.InvidiousUnregisterTokenTask, {
|
||||
invidious: m.node
|
||||
authToken: authToken
|
||||
})
|
||||
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
message = `It looks like you are logged in to Invidious, but your access token does not have the required permissions.
|
||||
For that reason, we have logged you out. Please log in again.
|
||||
This can happen when Playlet is updated with new features.
|
||||
We apologize for the inconvenience.`
|
||||
DialogUtils.ShowDialog(message, "User logged out")
|
||||
return invalid
|
||||
end if
|
||||
return authToken
|
||||
end function
|
||||
|
||||
function SetAuthToken(token as string, instance as string, username as dynamic)
|
||||
obj = {
|
||||
token: token
|
||||
instance: instance
|
||||
username: username
|
||||
}
|
||||
|
||||
json = FormatJson(obj)
|
||||
RegistryUtils.Write(RegistryUtils.INVIDIOUS_TOKEN, json)
|
||||
|
||||
m.node.authToken = obj
|
||||
LogInfo("Deleting Invidious token...")
|
||||
m.invidiousNode.profiles@.LogoutWithAccessToken(authToken.token)
|
||||
end function
|
||||
|
||||
function VerifyTokenScope(token as string) as boolean
|
||||
@@ -550,30 +534,14 @@ We apologize for the inconvenience.`
|
||||
return validScopes
|
||||
end function
|
||||
|
||||
function DeleteAuthToken()
|
||||
m.node.authToken = invalid
|
||||
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
end function
|
||||
|
||||
function Logout() as boolean
|
||||
authToken = m.node.authToken
|
||||
if authToken = invalid
|
||||
return false
|
||||
end if
|
||||
|
||||
m.UnregisterToken(authToken)
|
||||
m.DeleteAuthToken()
|
||||
return true
|
||||
end function
|
||||
|
||||
function UnregisterToken(authToken as object) as void
|
||||
function UnregisterToken(authToken as object) as object
|
||||
url = `${authToken.instance}${Invidious.AUTH_TOKENS_UNREGISTER}`
|
||||
|
||||
request = HttpClient.Post(url, authToken.token)
|
||||
request.Headers(m.GetAuthenticationHeaders(authToken.token))
|
||||
request.Header("Content-Type", "application/json")
|
||||
|
||||
request.Await()
|
||||
return request.Await()
|
||||
end function
|
||||
|
||||
function GetAuthorizeTokenLink() as dynamic
|
||||
@@ -581,7 +549,7 @@ We apologize for the inconvenience.`
|
||||
' Make sure the host is not on https, because https cannot redirect to http, which is our local server
|
||||
invidiousHost = instance.Replace("https://", "http://")
|
||||
|
||||
address = m.node.webServer@.GetServerAddress()
|
||||
address = m.invidiousNode.webServer@.GetServerAddress()
|
||||
if address = invalid
|
||||
return invalid
|
||||
end if
|
||||
@@ -594,7 +562,7 @@ We apologize for the inconvenience.`
|
||||
end function
|
||||
|
||||
function GetSubscriptions(cancellation = invalid as object) as object
|
||||
authToken = m.node.authToken
|
||||
authToken = m.invidiousNode.authToken
|
||||
if authToken = invalid
|
||||
return invalid
|
||||
end if
|
||||
@@ -608,7 +576,7 @@ We apologize for the inconvenience.`
|
||||
end function
|
||||
|
||||
function Subscribe(channelId as string, cancellation = invalid as object) as object
|
||||
authToken = m.node.authToken
|
||||
authToken = m.invidiousNode.authToken
|
||||
if authToken = invalid
|
||||
return invalid
|
||||
end if
|
||||
@@ -622,7 +590,7 @@ We apologize for the inconvenience.`
|
||||
end function
|
||||
|
||||
function Unsubscribe(channelId as string, cancellation = invalid as object) as object
|
||||
authToken = m.node.authToken
|
||||
authToken = m.invidiousNode.authToken
|
||||
if authToken = invalid
|
||||
return invalid
|
||||
end if
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
import "InvidiousService.bs"
|
||||
|
||||
@asynctask
|
||||
function InvidiousUnregisterTokenTask(input as object) as object
|
||||
invidiousNode = input.invidious
|
||||
authToken = input.authToken
|
||||
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
service.UnregisterToken(authToken)
|
||||
|
||||
return invalid
|
||||
end function
|
||||
@@ -0,0 +1,331 @@
|
||||
import "pkg:/source/AsyncTask/AsyncTask.bs"
|
||||
import "pkg:/source/AsyncTask/Tasks.bs"
|
||||
import "pkg:/source/utils/ArrayUtils.bs"
|
||||
import "pkg:/source/utils/ColorUtils.bs"
|
||||
import "pkg:/source/utils/CryptoUtils.bs"
|
||||
import "pkg:/source/utils/Logging.bs"
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
|
||||
' TODO:P1 add the ability to change color of profiles
|
||||
function Init()
|
||||
m.disableSavingToRegistry = false
|
||||
content = m.top.findNode("ProfilesContent")
|
||||
m.top.content = content
|
||||
m.profilesContent = content.getChild(0)
|
||||
end function
|
||||
|
||||
function OnNodeReady()
|
||||
LoadProfilesFromRegistry()
|
||||
end function
|
||||
|
||||
function LoadProfilesFromRegistry()
|
||||
LogInfo("Loading profiles from registry...")
|
||||
|
||||
' Step 1: Load user profiles from registry
|
||||
userProfiles = RegistryUtils.Read(RegistryUtils.USER_PROFILES)
|
||||
|
||||
if userProfiles = invalid
|
||||
userProfiles = {
|
||||
profiles: []
|
||||
currentProfile: invalid
|
||||
}
|
||||
else
|
||||
userProfiles = ParseJson(userProfiles)
|
||||
end if
|
||||
|
||||
' Step 2: Migrate existing profiles to new format (if needed)
|
||||
isDirty = MigrateExistingProfiles(userProfiles)
|
||||
|
||||
' Step 3: Save user preferences (if needed)
|
||||
if isDirty
|
||||
SaveUserProfilesToRegistry()
|
||||
end if
|
||||
|
||||
' Step 4: Load user profiles into content nodes
|
||||
profileNodes = []
|
||||
for each profile in userProfiles.profiles
|
||||
profileNode = CreateObject("roSGNode", "ProfileContentNode")
|
||||
profileNode.id = profile.id
|
||||
profileNode.type = profile.type
|
||||
profileNode.username = profile.username
|
||||
profileNode.serverUrl = profile.serverUrl
|
||||
profileNode.accessToken = profile.accessToken
|
||||
profileNode.color = profile.color
|
||||
|
||||
profileNodes.Push(profileNode)
|
||||
end for
|
||||
|
||||
m.profilesContent.insertChildren(profileNodes, 0)
|
||||
|
||||
if not StringUtils.IsNullOrEmpty(userProfiles.currentProfile)
|
||||
for each profileNode in profileNodes
|
||||
if profileNode.id = userProfiles.currentProfile
|
||||
' Setting current profile will also save the profiles to registry
|
||||
' but we just saved the profiles
|
||||
m.disableSavingToRegistry = true
|
||||
SetCurrentProfile(profileNode)
|
||||
m.disableSavingToRegistry = false
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
end if
|
||||
|
||||
if profileNodes.Count() > 0
|
||||
AsyncTask.Start(Tasks.ProfilesVerifyTokensTask, {
|
||||
profiles: m.top
|
||||
invidious: m.invidious
|
||||
profileNodes: profileNodes
|
||||
})
|
||||
end if
|
||||
end function
|
||||
|
||||
function MigrateExistingProfiles(userProfiles as object) as boolean
|
||||
tokenPayload = RegistryUtils.Read(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
if tokenPayload = invalid
|
||||
return false
|
||||
end if
|
||||
|
||||
authToken = ParseJson(tokenPayload)
|
||||
RegistryUtils.Delete(RegistryUtils.INVIDIOUS_TOKEN)
|
||||
|
||||
if authToken = invalid or authToken.instance = invalid or authToken.token = invalid or authToken.username = invalid
|
||||
return false
|
||||
end if
|
||||
|
||||
id = CreateProfileId(authToken.username, authToken.instance)
|
||||
for each profile in userProfiles.profiles
|
||||
if profile.id = id
|
||||
return false
|
||||
end if
|
||||
end for
|
||||
|
||||
profile = {
|
||||
"id": id
|
||||
"type": "invidious"
|
||||
"username": authToken.username
|
||||
"serverUrl": authToken.instance
|
||||
"accessToken": authToken.token
|
||||
"color": ColorUtils.RandomSoftColorHex()
|
||||
}
|
||||
|
||||
userProfiles.profiles.Unshift(profile)
|
||||
if StringUtils.IsNullOrEmpty(userProfiles["currentProfile"])
|
||||
userProfiles["currentProfile"] = profile.id
|
||||
end if
|
||||
|
||||
return true
|
||||
end function
|
||||
|
||||
function SaveUserProfilesToRegistry() as void
|
||||
if m.disableSavingToRegistry
|
||||
return
|
||||
end if
|
||||
|
||||
userProfiles = GetProfilesDto(true)
|
||||
|
||||
userProfiles.__version = m.top.__version
|
||||
RegistryUtils.Write(RegistryUtils.USER_PROFILES, FormatJson(userProfiles))
|
||||
end function
|
||||
|
||||
function GetProfilesDto(includeAccessToken as boolean) as object
|
||||
userProfiles = {
|
||||
"profiles": []
|
||||
"currentProfile": invalid
|
||||
}
|
||||
|
||||
profileNodes = m.profilesContent.getChildren(-1, 0)
|
||||
|
||||
for each profileNode in profileNodes
|
||||
if profileNode.type = "login"
|
||||
continue for
|
||||
end if
|
||||
|
||||
color = profileNode.color
|
||||
if IsInt(color)
|
||||
' Use hex color for web app
|
||||
color = ColorUtils.IntToHex(color)
|
||||
end if
|
||||
|
||||
profile = {
|
||||
"id": profileNode.id
|
||||
"type": profileNode.type
|
||||
"username": profileNode.username
|
||||
"serverUrl": profileNode.serverUrl
|
||||
"color": color
|
||||
}
|
||||
|
||||
if includeAccessToken
|
||||
profile["accessToken"] = profileNode.accessToken
|
||||
end if
|
||||
|
||||
userProfiles.profiles.Push(profile)
|
||||
end for
|
||||
|
||||
currentProfile = m.top.currentProfile
|
||||
if currentProfile <> invalid
|
||||
userProfiles["currentProfile"] = currentProfile.id
|
||||
end if
|
||||
|
||||
return userProfiles
|
||||
end function
|
||||
|
||||
function LoginWithProfile(newProfile as object)
|
||||
isNewProfile = true
|
||||
if StringUtils.IsNullOrEmpty(newProfile.id)
|
||||
newProfile.id = CreateProfileId(newProfile.username, newProfile.serverUrl)
|
||||
end if
|
||||
|
||||
existingProfile = invalid
|
||||
profiles = m.profilesContent.getChildren(-1, 0)
|
||||
for each profile in profiles
|
||||
if profile.id = newProfile.id
|
||||
existingProfile = profile
|
||||
isNewProfile = false
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
|
||||
if not isNewProfile
|
||||
' Refresh token of existing profile
|
||||
existingProfile.accessToken = newProfile.accessToken
|
||||
newProfile = existingProfile
|
||||
end if
|
||||
|
||||
if isNewProfile
|
||||
newProfile.color = ColorUtils.RandomSoftColorHex()
|
||||
m.profilesContent.insertChild(newProfile, 0)
|
||||
end if
|
||||
SetCurrentProfile(newProfile)
|
||||
end function
|
||||
|
||||
function LogoutWithProfile(profile as object) as void
|
||||
if profile = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
profile = m.profilesContent.findNode(profile.id)
|
||||
if profile = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
m.profilesContent.removeChild(profile)
|
||||
currentProfile = m.top.currentProfile
|
||||
if currentProfile <> invalid and currentProfile.id = profile.id
|
||||
SetCurrentProfile(invalid)
|
||||
else
|
||||
RefreshActiveProfileState(currentProfile)
|
||||
end if
|
||||
|
||||
m.top.onProfileLogout = true
|
||||
|
||||
AsyncTask.Start(Tasks.ProfilesInvidiousUnregisterTokenTask, {
|
||||
profiles: m.top
|
||||
invidious: m.invidious
|
||||
profileNodes: [profile]
|
||||
})
|
||||
end function
|
||||
|
||||
function LogoutWithProfiles(profiles as object) as void
|
||||
if profiles = invalid or profiles.Count() = 0
|
||||
return
|
||||
end if
|
||||
|
||||
currentProfile = m.top.currentProfile
|
||||
logoutFromCurrentProfile = false
|
||||
onProfileLogout = false
|
||||
for each profile in profiles
|
||||
profileNode = m.profilesContent.findNode(profile.id)
|
||||
if profileNode = invalid
|
||||
continue for
|
||||
end if
|
||||
|
||||
m.profilesContent.removeChild(profileNode)
|
||||
onProfileLogout = true
|
||||
if currentProfile <> invalid and currentProfile.id = profile.id
|
||||
logoutFromCurrentProfile = true
|
||||
end if
|
||||
end for
|
||||
|
||||
if logoutFromCurrentProfile
|
||||
SetCurrentProfile(invalid)
|
||||
else
|
||||
RefreshActiveProfileState(currentProfile)
|
||||
end if
|
||||
|
||||
if onProfileLogout
|
||||
m.top.onProfileLogout = true
|
||||
end if
|
||||
|
||||
AsyncTask.Start(Tasks.ProfilesInvidiousUnregisterTokenTask, {
|
||||
profiles: m.top
|
||||
invidious: m.invidious
|
||||
profileNodes: profiles
|
||||
})
|
||||
end function
|
||||
|
||||
function LogoutWithAccessToken(accessToken as dynamic) as void
|
||||
if StringUtils.IsNullOrEmpty(accessToken)
|
||||
return
|
||||
end if
|
||||
|
||||
' We are not going to invalidate the token on the server
|
||||
' because we are likely dealing with an invalid token
|
||||
' in the first place. All we need to do is remove the
|
||||
' profile from the list of profiles.
|
||||
|
||||
allProfiles = m.profilesContent.getChildren(-1, 0)
|
||||
for each profile in allProfiles
|
||||
if profile.accessToken = accessToken
|
||||
m.profilesContent.removeChild(profile)
|
||||
currentProfile = m.top.currentProfile
|
||||
if currentProfile <> invalid and currentProfile.id = profile.id
|
||||
SetCurrentProfile(invalid)
|
||||
else
|
||||
RefreshActiveProfileState(currentProfile)
|
||||
end if
|
||||
m.top.onProfileLogout = true
|
||||
exit for
|
||||
end if
|
||||
end for
|
||||
end function
|
||||
|
||||
function SetCurrentProfile(newActiveProfile as object) as void
|
||||
if newActiveProfile = invalid
|
||||
RefreshActiveProfileState(invalid)
|
||||
m.top.currentProfile = invalid
|
||||
m.invidious.authToken = invalid
|
||||
LogInfo("Current profile set to <invalid>")
|
||||
SaveUserProfilesToRegistry()
|
||||
return
|
||||
end if
|
||||
|
||||
RefreshActiveProfileState(newActiveProfile)
|
||||
|
||||
m.top.currentProfile = newActiveProfile
|
||||
m.invidious.authToken = {
|
||||
token: newActiveProfile.accessToken
|
||||
instance: newActiveProfile.serverUrl
|
||||
username: newActiveProfile.username
|
||||
}
|
||||
LogInfo("Current profile set to", newActiveProfile.username, "@", newActiveProfile.serverUrl)
|
||||
SaveUserProfilesToRegistry()
|
||||
end function
|
||||
|
||||
function RefreshActiveProfileState(activeProfile as object) as void
|
||||
allProfiles = m.profilesContent.getChildren(-1, 0)
|
||||
if activeProfile = invalid
|
||||
for each profile in allProfiles
|
||||
profile.isSelected = false
|
||||
end for
|
||||
return
|
||||
end if
|
||||
|
||||
for each profile in allProfiles
|
||||
profile.isSelected = profile.id = activeProfile.id
|
||||
end for
|
||||
end function
|
||||
|
||||
function CreateProfileId(username as string, serverUrl as string) as string
|
||||
return "v1:" + CryptoUtils.GetMd5(username + serverUrl)
|
||||
end function
|
||||
@@ -0,0 +1,31 @@
|
||||
<component name="Profiles" extends="Node" includes="AutoBind">
|
||||
<interface>
|
||||
<!--
|
||||
The version of the profiles. This is used to determine if the
|
||||
profiles saved need to be migrated. If the version is less than the
|
||||
current version, a migration is needed.
|
||||
-->
|
||||
<field id="__version" type="integer" value="1" />
|
||||
<field id="currentProfile" type="node" />
|
||||
<field id="onProfileLogout" type="boolean" alwaysNotify="true" />
|
||||
<field id="content" type="node" />
|
||||
|
||||
<field id="invidious" type="node" />
|
||||
|
||||
<function name="GetProfilesDto" />
|
||||
<function name="LoginWithProfile" />
|
||||
<function name="LogoutWithProfile" />
|
||||
<function name="LogoutWithProfiles" />
|
||||
<function name="LogoutWithAccessToken" />
|
||||
<function name="SetCurrentProfile" />
|
||||
</interface>
|
||||
<children>
|
||||
<!-- Note: two content nodes because we're rendering profiles in a RowList on the ProfilesScreen -->
|
||||
<ContentNode id="ProfilesContent">
|
||||
<ContentNode>
|
||||
<ProfileContentNode type="login">
|
||||
</ProfileContentNode>
|
||||
</ContentNode>
|
||||
</ContentNode>
|
||||
</children>
|
||||
</component>
|
||||
@@ -0,0 +1,22 @@
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
|
||||
@asynctask
|
||||
function ProfilesInvidiousUnregisterTokenTask(input as object) as object
|
||||
profileContentNodes = input.profileNodes
|
||||
invidiousNode = input.invidious
|
||||
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
|
||||
for each profileNode in profileContentNodes
|
||||
authToken = {
|
||||
instance: profileNode.serverUrl
|
||||
token: profileNode.accessToken
|
||||
}
|
||||
response = service.UnregisterToken(authToken)
|
||||
if not response.IsSuccess()
|
||||
LogWarn(`Failed to unregister token: ${response.ErrorMessage()}`)
|
||||
end if
|
||||
end for
|
||||
|
||||
return invalid
|
||||
end function
|
||||
@@ -0,0 +1,38 @@
|
||||
import "pkg:/components/Services/Invidious/InvidiousService.bs"
|
||||
|
||||
@asynctask
|
||||
function ProfilesVerifyTokensTask(input as object) as object
|
||||
profilesNode = input.profiles
|
||||
invidiousNode = input.invidious
|
||||
profileContentNodes = input.profileNodes
|
||||
|
||||
service = new Invidious.InvidiousService(invidiousNode)
|
||||
invalidProfiles = []
|
||||
|
||||
for each profileNode in profileContentNodes
|
||||
if not service.VerifyTokenScope(profileNode.accessToken)
|
||||
invalidProfiles.push(profileNode)
|
||||
end if
|
||||
end for
|
||||
|
||||
if invalidProfiles.Count() = 0
|
||||
return invalid
|
||||
end if
|
||||
|
||||
profilesNode@.LogoutWithProfiles(invalidProfiles)
|
||||
|
||||
profileNames = []
|
||||
for each profileNode in invalidProfiles
|
||||
profileNames.push(profileNode.username)
|
||||
end for
|
||||
profileNames = profileNames.join(", ")
|
||||
|
||||
message = `It looks like you are logged in to Invidious, but do not have the required permissions.
|
||||
For that reason, we have logged you out from the following profiles: ${profileNames}
|
||||
Please log in again.
|
||||
This can happen when Playlet is updated with new features.
|
||||
We apologize for the inconvenience.`
|
||||
DialogUtils.ShowDialog(message, "User logged out")
|
||||
|
||||
return invalid
|
||||
end function
|
||||
Reference in new issue
Block a user