import "pkg:/components/Dialog/DialogUtils.bs" import "pkg:/components/Dialog/YouTubeLoginAccountItemUtils.bs" import "pkg:/components/Services/Innertube/InnertubeService.bs" import "pkg:/source/utils/CancellationUtils.bs" import "pkg:/source/utils/Logging.bs" @asynctask function YouTubeLoginTask(input as object) as object profilesNode = input.profilesService dialogNode = input.dialog cancellation = m.top.cancellation ' TODO:P2 cache client identity authCode = InnertubeService.AuthGetCode(cancellation) if CancellationUtils.IsCancelled(cancellation) return invalid end if if authCode.error <> invalid return { success: false error: authCode.error } end if url = InnertubeService.AuthGetActivationUrl(authCode) LogInfo("Login url:", url) dialogNode.url = url dialogNode.code = authCode.userCode accessToken = InnertubeService.AuthPollForAccessToken(authCode, cancellation) if CancellationUtils.IsCancelled(cancellation) return invalid end if if accessToken.error <> invalid return { success: false error: accessToken.error } end if accounts = InnertubeService.AuthListAccounts(accessToken.accessToken, cancellation) if CancellationUtils.IsCancelled(cancellation) return invalid end if if IsAssociativeArray(accounts) and accounts.error <> invalid return { success: false error: accounts.error } end if if not IsArray(accounts) or accounts.Count() = 0 return { success: false error: Tr(Locale.Dialogs.NoValidAccountsFound) } end if ' If there is only one account, we can skip the selection screen ' and just create a profile if accounts.Count() = 1 profile = YouTubeLoginAccountItemUtils.CreateProfileContentNode(accounts[0], accessToken) profilesNode@.LoginWithProfile(profile) return { success: true } end if return { success: true accounts: accounts accessToken: accessToken } end function