[skip ci] more job system work
This commit is contained in:
1 parent
2fa06604a7
commit
9d7b792f42
14 files changed
+126
-95
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import "pkg:/source/AsyncTask/AsyncTask.bs"
|
import "pkg:/components/JobSystem/Jobs.bs"
|
||||||
import "pkg:/source/AsyncTask/Tasks.bs"
|
import "pkg:/components/JobSystem/JobSystem.bs"
|
||||||
import "pkg:/source/utils/Logging.bs"
|
import "pkg:/source/utils/Logging.bs"
|
||||||
import "pkg:/source/utils/StringUtils.bs"
|
import "pkg:/source/utils/StringUtils.bs"
|
||||||
import "pkg:/source/utils/TimeUtils.bs"
|
import "pkg:/source/utils/TimeUtils.bs"
|
||||||
@@ -31,16 +31,23 @@ function HasValidAccessToken(_unused as dynamic) as boolean
|
|||||||
end function
|
end function
|
||||||
|
|
||||||
function RefreshAccessToken() as void
|
function RefreshAccessToken() as void
|
||||||
if m.refreshTokenTask <> invalid
|
if m.profileRefreshTokenJob <> invalid
|
||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
|
if m.scene = invalid
|
||||||
|
m.scene = m.top.getScene()
|
||||||
|
end if
|
||||||
if m.profilesService = invalid
|
if m.profilesService = invalid
|
||||||
m.profilesService = m.top.getScene().findNode("ProfilesService")
|
m.profilesService = m.scene.findNode("ProfilesService")
|
||||||
|
end if
|
||||||
|
if m.jobQueue = invalid
|
||||||
|
m.jobQueue = m.scene.findNode("JobQueue")
|
||||||
end if
|
end if
|
||||||
|
|
||||||
LogInfo("Refreshing access token for profile:", m.top.username)
|
LogInfo("Refreshing access token for profile:", m.top.username)
|
||||||
m.refreshTokenTask = AsyncTask.Start(Tasks.ProfileRefreshTokenTask, {
|
|
||||||
|
input = {
|
||||||
profile: m.top
|
profile: m.top
|
||||||
profilesService: m.profilesService
|
profilesService: m.profilesService
|
||||||
accessTokenObject: {
|
accessTokenObject: {
|
||||||
@@ -48,9 +55,14 @@ function RefreshAccessToken() as void
|
|||||||
clientSecret: m.top.clientSecret
|
clientSecret: m.top.clientSecret
|
||||||
refreshToken: m.top.refreshToken
|
refreshToken: m.top.refreshToken
|
||||||
}
|
}
|
||||||
}, OnProfileRefreshTokenTaskResult)
|
}
|
||||||
|
|
||||||
|
callback = JobSystem.CreateCallback(OnProfileRefreshTokenJobDone)
|
||||||
|
JobSystem.QueueJob(m.jobQueue, Jobs.ProfileRefreshTokenJob, input, callback)
|
||||||
|
m.profileRefreshTokenJob = callback
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function OnProfileRefreshTokenTaskResult(_output as object) as void
|
function OnProfileRefreshTokenJobDone(event as object) as void
|
||||||
m.refreshTokenTask = invalid
|
JobSystem.UnobserveCallback(event.getRoSGNode())
|
||||||
|
m.profileRefreshTokenJob = invalid
|
||||||
end function
|
end function
|
||||||
+7
-7
@@ -1,25 +1,25 @@
|
|||||||
import "pkg:/components/Services/Innertube/InnertubeService.bs"
|
import "pkg:/components/Services/Innertube/InnertubeService.bs"
|
||||||
import "pkg:/source/utils/ErrorUtils.bs"
|
import "pkg:/source/utils/ErrorUtils.bs"
|
||||||
|
|
||||||
@asynctask
|
@job("ProfileRefreshTokenJob")
|
||||||
function ProfileRefreshTokenTask(input as object) as object
|
function ExecuteJob() as void
|
||||||
|
input = JobGetInput()
|
||||||
profileNode = input.profile
|
profileNode = input.profile
|
||||||
accessTokenObject = input.accessTokenObject
|
accessTokenObject = input.accessTokenObject
|
||||||
profilesService = input.profilesService
|
profilesService = input.profilesService
|
||||||
try
|
try
|
||||||
if InnertubeService.AuthRefreshAccessToken(accessTokenObject)
|
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
|
||||||
|
if InnertubeService.AuthRefreshAccessToken(accessTokenObject, cancellation)
|
||||||
profileNode.expiresIn = accessTokenObject.expiresIn
|
profileNode.expiresIn = accessTokenObject.expiresIn
|
||||||
profileNode.expiresTimestamp = accessTokenObject.expiresTimestamp
|
profileNode.expiresTimestamp = accessTokenObject.expiresTimestamp
|
||||||
profileNode.accessToken = accessTokenObject.accessToken
|
profileNode.accessToken = accessTokenObject.accessToken
|
||||||
profilesService@.SaveUserProfilesToRegistry()
|
profilesService@.SaveUserProfilesToRegistry()
|
||||||
else
|
else
|
||||||
LogError("Failed to refresh access token")
|
JobError("Failed to refresh access token")
|
||||||
profileNode.accessToken = invalid
|
profileNode.accessToken = invalid
|
||||||
end if
|
end if
|
||||||
catch error
|
catch error
|
||||||
LogError("Failed to refresh access token:", ErrorUtils.Format(error))
|
JobError("Failed to refresh access token: " + ErrorUtils.Format(error))
|
||||||
profileNode.accessToken = invalid
|
profileNode.accessToken = invalid
|
||||||
end try
|
end try
|
||||||
|
|
||||||
return invalid
|
|
||||||
end function
|
end function
|
||||||
@@ -32,6 +32,17 @@ function JobSuccess() as void
|
|||||||
m.jobStateSet = true
|
m.jobStateSet = true
|
||||||
end function
|
end function
|
||||||
|
|
||||||
|
function JobSuccessData(data as dynamic) as void
|
||||||
|
callbackNode = JobGetCallbackNode()
|
||||||
|
if callbackNode = invalid
|
||||||
|
return
|
||||||
|
end if
|
||||||
|
callbackNode.data = data
|
||||||
|
callbackNode.success = true
|
||||||
|
callbackNode.done = true
|
||||||
|
m.jobStateSet = true
|
||||||
|
end function
|
||||||
|
|
||||||
function JobError(error as dynamic) as void
|
function JobError(error as dynamic) as void
|
||||||
error = ErrorUtils.Format(error)
|
error = ErrorUtils.Format(error)
|
||||||
LogError(error)
|
LogError(error)
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
<field id="done" type="boolean" />
|
<field id="done" type="boolean" />
|
||||||
<field id="success" type="boolean" />
|
<field id="success" type="boolean" />
|
||||||
<field id="error" type="string" />
|
<field id="error" type="string" />
|
||||||
|
<field id="data" type="assocarray" />
|
||||||
<!-- read/write field, can be used to cancel a job, and to check for cancellation. -->
|
<!-- read/write field, can be used to cancel a job, and to check for cancellation. -->
|
||||||
<field id="cancel" type="boolean" />
|
<field id="cancel" type="boolean" />
|
||||||
</interface>
|
</interface>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace JobSystem
|
|||||||
node = CreateObject("roSGNode", "JobCallback")
|
node = CreateObject("roSGNode", "JobCallback")
|
||||||
node.id = "JobCallback-" + StringUtils.RandomId()
|
node.id = "JobCallback-" + StringUtils.RandomId()
|
||||||
if IsFunction(func)
|
if IsFunction(func)
|
||||||
node.observeFieldScoped("done", FuncName(func), ["success", "cancel", "error"])
|
node.observeFieldScoped("done", FuncName(func), ["success", "cancel", "error", "data"])
|
||||||
end if
|
end if
|
||||||
return node
|
return node
|
||||||
end function
|
end function
|
||||||
|
|||||||
@@ -73,7 +73,8 @@
|
|||||||
<ProfilesService id="ProfilesService"
|
<ProfilesService id="ProfilesService"
|
||||||
invidious="bind:../Invidious" />
|
invidious="bind:../Invidious" />
|
||||||
|
|
||||||
<Innertube id="Innertube" />
|
<Innertube id="Innertube"
|
||||||
|
jobQueue="bind:/JobQueue" />
|
||||||
<Invidious id="Invidious"
|
<Invidious id="Invidious"
|
||||||
webServer="bind:../WebServer"
|
webServer="bind:../WebServer"
|
||||||
applicationInfo="bind:../ApplicationInfo"
|
applicationInfo="bind:../ApplicationInfo"
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ function InitializeBindings()
|
|||||||
"ProfilesService": {
|
"ProfilesService": {
|
||||||
"invidious": "../Invidious"
|
"invidious": "../Invidious"
|
||||||
},
|
},
|
||||||
|
"Innertube": {
|
||||||
|
"jobQueue": "/JobQueue"
|
||||||
|
},
|
||||||
"Invidious": {
|
"Invidious": {
|
||||||
"webServer": "../WebServer",
|
"webServer": "../WebServer",
|
||||||
"applicationInfo": "../ApplicationInfo",
|
"applicationInfo": "../ApplicationInfo",
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import "pkg:/components/Dialog/DialogUtils.bs"
|
import "pkg:/components/Dialog/DialogUtils.bs"
|
||||||
|
import "pkg:/components/JobSystem/Jobs.bs"
|
||||||
|
import "pkg:/components/JobSystem/JobSystem.bs"
|
||||||
import "pkg:/components/Navigation/Navigation.bs"
|
import "pkg:/components/Navigation/Navigation.bs"
|
||||||
import "pkg:/source/AsyncTask/AsyncTask.bs"
|
|
||||||
import "pkg:/source/AsyncTask/Tasks.bs"
|
|
||||||
import "pkg:/source/utils/ErrorUtils.bs"
|
import "pkg:/source/utils/ErrorUtils.bs"
|
||||||
import "pkg:/source/utils/FocusManagement.bs"
|
import "pkg:/source/utils/FocusManagement.bs"
|
||||||
import "pkg:/source/utils/Locale.bs"
|
import "pkg:/source/utils/Locale.bs"
|
||||||
@@ -164,48 +164,48 @@ function OnLinkWithTvPanelVisibleChanged()
|
|||||||
if m.linkWithTvPanel.visible
|
if m.linkWithTvPanel.visible
|
||||||
GeneratePairingCode()
|
GeneratePairingCode()
|
||||||
else
|
else
|
||||||
if m.linkCodeTask <> invalid
|
if m.loungeGenerateManualPairingCodeJob <> invalid
|
||||||
m.linkCodeTask.cancel = true
|
m.loungeGenerateManualPairingCodeJob.cancel = true
|
||||||
m.linkCodeTask = invalid
|
m.loungeGenerateManualPairingCodeJob = invalid
|
||||||
end if
|
end if
|
||||||
m.linkCodeLabel.text = ""
|
m.linkCodeLabel.text = ""
|
||||||
end if
|
end if
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function GeneratePairingCode()
|
function GeneratePairingCode()
|
||||||
if m.linkCodeTask <> invalid
|
if m.loungeGenerateManualPairingCodeJob <> invalid
|
||||||
m.linkCodeTask.cancel = true
|
m.loungeGenerateManualPairingCodeJob.cancel = true
|
||||||
end if
|
end if
|
||||||
|
|
||||||
m.linkCodeLabel.text = Tr(Locale.RemoteScreen.Loading)
|
m.linkCodeLabel.text = Tr(Locale.RemoteScreen.Loading)
|
||||||
m.linkCodeTask = AsyncTask.Start(Tasks.LoungeGenerateManualPairingCodeTask, {
|
|
||||||
|
input = {
|
||||||
loungeService: m.loungeService
|
loungeService: m.loungeService
|
||||||
loungeToken: m.loungeService.loungeToken
|
loungeToken: m.loungeService.loungeToken
|
||||||
screenId: m.loungeService.screenId
|
screenId: m.loungeService.screenId
|
||||||
' Use deviceName as screenName
|
' Use deviceName as screenName
|
||||||
screenName: m.loungeService.deviceName
|
screenName: m.loungeService.deviceName
|
||||||
deviceId: m.loungeService.deviceId
|
deviceId: m.loungeService.deviceId
|
||||||
}, OnLoungeGenerateManualPairingCodeTaskResult)
|
}
|
||||||
|
|
||||||
|
callback = JobSystem.CreateCallback(OnLoungeGenerateManualPairingCodeJobDone)
|
||||||
|
JobSystem.QueueJob(m.jobQueue, Jobs.LoungeGenerateManualPairingCodeJob, input, callback)
|
||||||
|
m.loungeGenerateManualPairingCodeJob = callback
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function OnLoungeGenerateManualPairingCodeTaskResult(output as object) as void
|
function OnLoungeGenerateManualPairingCodeJobDone(event as object) as void
|
||||||
m.linkCodeTask = invalid
|
callback = event.getRoSGNode()
|
||||||
|
JobSystem.UnobserveCallback(callback)
|
||||||
|
m.loungeGenerateManualPairingCodeJob = invalid
|
||||||
|
|
||||||
if output.cancelled
|
info = event.GetInfo()
|
||||||
|
|
||||||
|
if info.cancel
|
||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
if not output.success or not output.result.success
|
if not info.success
|
||||||
' output.error for unhandled exception
|
message = Tr(Locale.RemoteScreen.FailedToGeneratePairingCode) + `\n${info.error}`
|
||||||
error = output.error
|
|
||||||
if error = invalid
|
|
||||||
' output.result.error for network errors
|
|
||||||
error = output.result.error
|
|
||||||
end if
|
|
||||||
error = ErrorUtils.Format(error)
|
|
||||||
LogError(error)
|
|
||||||
|
|
||||||
message = Tr(Locale.RemoteScreen.FailedToGeneratePairingCode) + `\n${error}`
|
|
||||||
DialogUtils.ShowDialogEx({
|
DialogUtils.ShowDialogEx({
|
||||||
message: message
|
message: message
|
||||||
title: Tr(Locale.RemoteScreen.TvCodeError)
|
title: Tr(Locale.RemoteScreen.TvCodeError)
|
||||||
@@ -214,18 +214,5 @@ function OnLoungeGenerateManualPairingCodeTaskResult(output as object) as void
|
|||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
manualLinkCode = output.result.code
|
m.linkCodeLabel.text = info.data.code
|
||||||
if StringUtils.IsNullOrEmpty(manualLinkCode)
|
|
||||||
m.linkCodeLabel.text = ""
|
|
||||||
return
|
|
||||||
end if
|
|
||||||
|
|
||||||
' insert a space every 3 characters.
|
|
||||||
parts = []
|
|
||||||
for i = 0 to manualLinkCode.Len() - 1 step 3
|
|
||||||
parts.Push(manualLinkCode.Mid(i, 3))
|
|
||||||
end for
|
|
||||||
linkCode = parts.Join(" ")
|
|
||||||
|
|
||||||
m.linkCodeLabel.text = linkCode
|
|
||||||
end function
|
end function
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
<field id="navBar" type="node" bind="/NavBar" />
|
<field id="navBar" type="node" bind="/NavBar" />
|
||||||
<field id="webServer" type="node" bind="/WebServer" />
|
<field id="webServer" type="node" bind="/WebServer" />
|
||||||
<field id="loungeService" type="node" bind="/LoungeService" />
|
<field id="loungeService" type="node" bind="/LoungeService" />
|
||||||
|
<field id="jobQueue" type="node" bind="/JobQueue" />
|
||||||
<field id="url" type="string" onChange="OnUrlSet" />
|
<field id="url" type="string" onChange="OnUrlSet" />
|
||||||
<field id="qrCodeUrl" type="string" onChange="OnUrlSet" />
|
<field id="qrCodeUrl" type="string" onChange="OnUrlSet" />
|
||||||
</interface>
|
</interface>
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
|
import "pkg:/components/JobSystem/Jobs.bs"
|
||||||
|
import "pkg:/components/JobSystem/JobSystem.bs"
|
||||||
import "pkg:/components/Services/Innertube/Constants.bs"
|
import "pkg:/components/Services/Innertube/Constants.bs"
|
||||||
import "pkg:/source/AsyncTask/AsyncTask.bs"
|
|
||||||
import "pkg:/source/AsyncTask/Tasks.bs"
|
|
||||||
import "pkg:/source/utils/StringUtils.bs"
|
import "pkg:/source/utils/StringUtils.bs"
|
||||||
|
|
||||||
function Init()
|
function Init()
|
||||||
|
end function
|
||||||
|
|
||||||
|
function OnNodeReady()
|
||||||
OnRequestVisitorData()
|
OnRequestVisitorData()
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function OnRequestVisitorData() as void
|
function OnRequestVisitorData() as void
|
||||||
if m.innertubeInitTask <> invalid
|
if m.innertubeSessionDataJob <> invalid
|
||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
@@ -17,11 +20,16 @@ function OnRequestVisitorData() as void
|
|||||||
return
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
m.innertubeInitTask = AsyncTask.Start(Tasks.InnertubeInitTask, {
|
input = {
|
||||||
innertube: m.top
|
innertube: m.top
|
||||||
}, OnInnertubeInitTaskResult)
|
}
|
||||||
|
|
||||||
|
callback = JobSystem.CreateCallback(OnInnertubeSessionDataJobDone)
|
||||||
|
JobSystem.QueueJob(m.top.jobQueue, Jobs.InnertubeSessionDataJob, input, callback)
|
||||||
|
m.innertubeSessionDataJob = callback
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function OnInnertubeInitTaskResult(output as object) as void
|
function OnInnertubeSessionDataJobDone(event as object) as void
|
||||||
m.innertubeInitTask = invalid
|
JobSystem.UnobserveCallback(event.getRoSGNode())
|
||||||
|
m.innertubeSessionDataJob = invalid
|
||||||
end function
|
end function
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
<component name="Innertube" extends="Node">
|
<component name="Innertube" extends="Node" includes="AutoBind">
|
||||||
<interface>
|
<interface>
|
||||||
<field id="timestamp" type="int" />
|
<field id="timestamp" type="int" />
|
||||||
<field id="visitorData" type="string" />
|
<field id="visitorData" type="string" />
|
||||||
<field id="poToken" type="string" />
|
<field id="poToken" type="string" />
|
||||||
<field id="requestVisitorData" type="boolean" alwaysNotify="true" onChange="OnRequestVisitorData" />
|
<field id="requestVisitorData" type="boolean" alwaysNotify="true" onChange="OnRequestVisitorData" />
|
||||||
|
<field id="jobQueue" type="node" />
|
||||||
</interface>
|
</interface>
|
||||||
</component>
|
</component>
|
||||||
+10
-6
@@ -1,14 +1,19 @@
|
|||||||
import "pkg:/components/Services/Innertube/SessionData.bs"
|
import "pkg:/components/Services/Innertube/SessionData.bs"
|
||||||
|
|
||||||
@asynctask
|
@job("InnertubeSessionDataJob")
|
||||||
function InnertubeInitTask(input as object) as object
|
function ExecuteJob() as void
|
||||||
|
input = JobGetInput()
|
||||||
|
|
||||||
innertubeNode = input.innertube
|
innertubeNode = input.innertube
|
||||||
|
|
||||||
sessionData = Innertube.GetSessionData()
|
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
|
||||||
|
sessionData = Innertube.GetSessionData(cancellation)
|
||||||
if sessionData = invalid
|
if sessionData = invalid
|
||||||
return invalid
|
JobError("Failed to fetch session data")
|
||||||
|
return
|
||||||
end if
|
end if
|
||||||
|
|
||||||
|
innertubeNode.queueFields(true)
|
||||||
if IsInt(sessionData.timestamp)
|
if IsInt(sessionData.timestamp)
|
||||||
innertubeNode.timestamp = sessionData.timestamp
|
innertubeNode.timestamp = sessionData.timestamp
|
||||||
end if
|
end if
|
||||||
@@ -20,6 +25,5 @@ function InnertubeInitTask(input as object) as object
|
|||||||
if not StringUtils.IsNullOrEmpty(sessionData.poToken)
|
if not StringUtils.IsNullOrEmpty(sessionData.poToken)
|
||||||
innertubeNode.poToken = sessionData.poToken
|
innertubeNode.poToken = sessionData.poToken
|
||||||
end if
|
end if
|
||||||
|
innertubeNode.queueFields(false)
|
||||||
return invalid
|
|
||||||
end function
|
end function
|
||||||
@@ -11,18 +11,18 @@ namespace Innertube
|
|||||||
const SESSION_DATA_VERSION = 1
|
const SESSION_DATA_VERSION = 1
|
||||||
const INNERTUBE_VISITOR_DATA_LIFESPAN_SECONDS = 60 * 60 * 24 * 14 ' 14 days
|
const INNERTUBE_VISITOR_DATA_LIFESPAN_SECONDS = 60 * 60 * 24 * 14 ' 14 days
|
||||||
|
|
||||||
function GetSessionData() as object
|
function GetSessionData(cancellation = invalid as object) as object
|
||||||
sessionData = ReadSessionDataFromRegistry()
|
sessionData = ReadSessionDataFromRegistry()
|
||||||
if sessionData <> invalid
|
if sessionData <> invalid
|
||||||
return sessionData
|
return sessionData
|
||||||
end if
|
end if
|
||||||
|
|
||||||
sessionData = RequestSessionData()
|
sessionData = RequestSessionData(cancellation)
|
||||||
SaveSessionDataToRegistry(sessionData)
|
SaveSessionDataToRegistry(sessionData)
|
||||||
return sessionData
|
return sessionData
|
||||||
end function
|
end function
|
||||||
|
|
||||||
function RequestSessionData() as object
|
function RequestSessionData(cancellation = invalid as object) as object
|
||||||
request = HttpClient.Get("https://www.youtube.com/sw.js_data")
|
request = HttpClient.Get("https://www.youtube.com/sw.js_data")
|
||||||
request.Headers({
|
request.Headers({
|
||||||
"Accept-Language": "en-US,en;q=0.9"
|
"Accept-Language": "en-US,en;q=0.9"
|
||||||
@@ -30,6 +30,7 @@ namespace Innertube
|
|||||||
"Accept": "*/*"
|
"Accept": "*/*"
|
||||||
"Referer": "https://www.youtube.com/sw.js"
|
"Referer": "https://www.youtube.com/sw.js"
|
||||||
})
|
})
|
||||||
|
request.Cancellation(cancellation)
|
||||||
|
|
||||||
response = request.Await()
|
response = request.Await()
|
||||||
if not response.IsSuccess()
|
if not response.IsSuccess()
|
||||||
@@ -37,13 +38,9 @@ namespace Innertube
|
|||||||
return invalid
|
return invalid
|
||||||
end if
|
end if
|
||||||
|
|
||||||
text = ValidString(response.Text())
|
text = response.Text()
|
||||||
if not text.StartsWith(")]}'")
|
text = text.Replace(`)]}'`, "")
|
||||||
LogError("Invalid session data response", text)
|
|
||||||
return invalid
|
|
||||||
end if
|
|
||||||
|
|
||||||
text = text.Replace(")]}'", "")
|
|
||||||
json = ParseJson(text)
|
json = ParseJson(text)
|
||||||
if json = invalid
|
if json = invalid
|
||||||
LogError("Failed to parse session data", text)
|
LogError("Failed to parse session data", text)
|
||||||
|
|||||||
+23
-18
@@ -3,8 +3,10 @@ import "pkg:/source/services/HttpClient.bs"
|
|||||||
import "pkg:/source/utils/MessagePortUtils.bs"
|
import "pkg:/source/utils/MessagePortUtils.bs"
|
||||||
import "pkg:/source/utils/StringUtils.bs"
|
import "pkg:/source/utils/StringUtils.bs"
|
||||||
|
|
||||||
@asynctask
|
@job("LoungeGenerateManualPairingCodeJob")
|
||||||
function LoungeGenerateManualPairingCodeTask(input as object) as object
|
function ExecuteJob() as void
|
||||||
|
input = JobGetInput()
|
||||||
|
|
||||||
loungeService = input.loungeService
|
loungeService = input.loungeService
|
||||||
|
|
||||||
if StringUtils.IsNullOrEmpty(input.loungeToken)
|
if StringUtils.IsNullOrEmpty(input.loungeToken)
|
||||||
@@ -14,10 +16,8 @@ function LoungeGenerateManualPairingCodeTask(input as object) as object
|
|||||||
message = wait(30000, messagePort)
|
message = wait(30000, messagePort)
|
||||||
loungeService.unobserveFieldScoped("loungeToken")
|
loungeService.unobserveFieldScoped("loungeToken")
|
||||||
if message = invalid or StringUtils.IsNullOrEmpty(message.GetData())
|
if message = invalid or StringUtils.IsNullOrEmpty(message.GetData())
|
||||||
return {
|
JobError("Couldn't join lounge")
|
||||||
"success": false
|
return
|
||||||
"error": "Couldn't join lounge"
|
|
||||||
}
|
|
||||||
end if
|
end if
|
||||||
|
|
||||||
input.screenId = loungeService.screenId
|
input.screenId = loungeService.screenId
|
||||||
@@ -33,30 +33,35 @@ function LoungeGenerateManualPairingCodeTask(input as object) as object
|
|||||||
|
|
||||||
for each key in form
|
for each key in form
|
||||||
if StringUtils.IsNullOrEmpty(form[key])
|
if StringUtils.IsNullOrEmpty(form[key])
|
||||||
return {
|
JobError("Required field " + key + " is empty")
|
||||||
"success": false
|
return
|
||||||
"error": key + " is empty"
|
|
||||||
}
|
|
||||||
end if
|
end if
|
||||||
end for
|
end for
|
||||||
|
|
||||||
form["access_type"] = "permanent"
|
form["access_type"] = "permanent"
|
||||||
form["app"] = "Playlet"
|
form["app"] = "Playlet"
|
||||||
|
|
||||||
|
cancellation = CancellationUtils.CreateCancellation(JobGetCallbackNode(), "cancel", true)
|
||||||
request = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/get_pairing_code?ctx=pair", "")
|
request = HttpClient.Post("https://www.youtube.com/api/lounge/pairing/get_pairing_code?ctx=pair", "")
|
||||||
request.UrlEncodedForm(form)
|
request.UrlEncodedForm(form)
|
||||||
|
request.Cancellation(cancellation)
|
||||||
|
|
||||||
response = request.Await()
|
response = request.Await()
|
||||||
if not response.IsSuccess()
|
if not response.IsSuccess()
|
||||||
return {
|
JobError(response.ErrorMessage())
|
||||||
"success": false
|
return
|
||||||
"error": response.ErrorMessage()
|
|
||||||
}
|
|
||||||
end if
|
end if
|
||||||
|
|
||||||
code = response.Text()
|
code = response.Text()
|
||||||
return {
|
|
||||||
"success": true
|
if not StringUtils.IsNullOrEmpty(code)
|
||||||
"code": code
|
' insert a space every 3 characters.
|
||||||
}
|
parts = []
|
||||||
|
for i = 0 to code.Len() - 1 step 3
|
||||||
|
parts.Push(code.Mid(i, 3))
|
||||||
|
end for
|
||||||
|
code = parts.Join(" ")
|
||||||
|
end if
|
||||||
|
|
||||||
|
JobSuccessData({ code: code })
|
||||||
end function
|
end function
|
||||||
Reference in new issue
Block a user