on screen code refactor, playlet lib requires code
This commit is contained in:
1 parent
fe15ea177b
commit
9c1de5e1cf
11 files changed
+233
-102
No files matched your search
@@ -8,6 +8,13 @@ namespace DialogUtils
|
||||
' Very large messages can lead to execution timeout
|
||||
const MESSAGE_LENGTH_LIMIT = 1000
|
||||
|
||||
const ON_SCREEN_CODE_MIN_INTERVAL_MS = 2000
|
||||
|
||||
enum OnScreenCodePurpose
|
||||
ExportRegistry = "ExportRegistry"
|
||||
SetPlayletLibUrls = "SetPlayletLibUrls"
|
||||
end enum
|
||||
|
||||
function ShowDialogEx(options as object) as object
|
||||
currentDialog = m.top.getScene().dialog
|
||||
alwaysOnTop = false
|
||||
@@ -118,6 +125,29 @@ namespace DialogUtils
|
||||
return dialog
|
||||
end function
|
||||
|
||||
function ShowOnScreenCodeDialog(purpose as DialogUtils.OnScreenCodePurpose, title as string) as object
|
||||
if m._onScreenCodeShownTimer = invalid
|
||||
m._onScreenCodeShownTimer = CreateObject("roTimespan")
|
||||
else if m._onScreenCodeShownTimer.TotalMilliseconds() < ON_SCREEN_CODE_MIN_INTERVAL_MS
|
||||
return invalid
|
||||
end if
|
||||
m._onScreenCodeShownTimer.Mark()
|
||||
|
||||
dialog = CreateObject("roSGNode", "OnScreenCodeDialog")
|
||||
dialog.purpose = purpose
|
||||
dialog.title = title
|
||||
m.top.getScene().dialog = dialog
|
||||
return dialog
|
||||
end function
|
||||
|
||||
function ConsumeOnScreenCode(purpose as DialogUtils.OnScreenCodePurpose, code as dynamic) as boolean
|
||||
dialog = m.top.getScene().dialog
|
||||
if dialog = invalid or dialog.subtype() <> "OnScreenCodeDialog"
|
||||
return false
|
||||
end if
|
||||
return dialog@.Consume(purpose, code)
|
||||
end function
|
||||
|
||||
function ShowDialogOnce(options as object) as object
|
||||
messageId = ValidString(options.messageId)
|
||||
hasMessageId = messageId <> ""
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import "pkg:/source/utils/Locale.bs"
|
||||
import "pkg:/source/utils/Types.bs"
|
||||
|
||||
' Proves the caller can see the TV: a LAN peer can press remote keys over
|
||||
' ECP, but cannot read the screen.
|
||||
|
||||
const CODE_LIFETIME_MINUTES = 2
|
||||
|
||||
function Init()
|
||||
code = Rnd(999999).ToStr()
|
||||
if code.Len() < 6
|
||||
code = string(6 - code.Len(), "0") + code
|
||||
end if
|
||||
m.code = code
|
||||
|
||||
' TODO:P2 localization
|
||||
m.top.message = [
|
||||
`The code is: ${code.Left(3)} ${code.Right(3)}.`
|
||||
`This code will expire in ${CODE_LIFETIME_MINUTES} minutes.`
|
||||
"If you did not request this, please close this dialog."
|
||||
]
|
||||
m.top.buttons = [Tr(Locale.Buttons.OK)]
|
||||
m.top.alwaysOnTop = true
|
||||
|
||||
m.expiryTimer = CreateObject("roSGNode", "Timer")
|
||||
m.expiryTimer.duration = CODE_LIFETIME_MINUTES * 60
|
||||
m.expiryTimer.observeFieldScoped("fire", FuncName(CloseDialog))
|
||||
m.expiryTimer.control = "start"
|
||||
|
||||
m.top.observeField("wasClosed", FuncName(OnDialogClosed))
|
||||
end function
|
||||
|
||||
function OnDialogClosed()
|
||||
m.expiryTimer.control = "stop"
|
||||
m.expiryTimer.unobserveFieldScoped("fire")
|
||||
end function
|
||||
|
||||
function Consume(purpose as string, code as dynamic) as boolean
|
||||
expectedCode = m.code
|
||||
m.code = invalid
|
||||
CloseDialog()
|
||||
|
||||
return expectedCode <> invalid and purpose = m.top.purpose and IsString(code) and code.Replace(" ", "") = expectedCode
|
||||
end function
|
||||
@@ -0,0 +1,6 @@
|
||||
<component name="OnScreenCodeDialog" extends="SimpleDialog">
|
||||
<interface>
|
||||
<field id="purpose" type="string" />
|
||||
<function name="Consume" />
|
||||
</interface>
|
||||
</component>
|
||||
@@ -1,3 +1,4 @@
|
||||
import "pkg:/components/Dialog/DialogUtils.bs"
|
||||
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
@@ -29,11 +30,31 @@ namespace Http
|
||||
return true
|
||||
end function
|
||||
|
||||
@post("/api/playlet-lib-urls/code")
|
||||
function ShowSetPlayletLibUrlsCode(context as object) as boolean
|
||||
response = context.response
|
||||
|
||||
' TODO:P2 localization
|
||||
dialog = DialogUtils.ShowOnScreenCodeDialog(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, "Change Playlet library")
|
||||
if dialog = invalid
|
||||
response.Default(429, "Too Many Requests")
|
||||
return true
|
||||
end if
|
||||
|
||||
response.Default(204, "OK")
|
||||
return true
|
||||
end function
|
||||
|
||||
@post("/api/playlet-lib-urls")
|
||||
function SetPlayletLibUrls(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
if not DialogUtils.ConsumeOnScreenCode(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, request.query.code)
|
||||
response.Default(400, "Invalid code")
|
||||
return true
|
||||
end if
|
||||
|
||||
payload = request.BodyJson()
|
||||
if payload = invalid or not IsArray(payload)
|
||||
response.Default(400, "Invalid payload")
|
||||
|
||||
@@ -1,100 +1,38 @@
|
||||
import "pkg:/components/Dialog/DialogUtils.bs"
|
||||
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
|
||||
import "pkg:/source/utils/RegistryUtils.bs"
|
||||
import "pkg:/source/utils/StringUtils.bs"
|
||||
import "pkg:/source/utils/TimeUtils.bs"
|
||||
|
||||
const REGISTRY_EXPORT_VERSION = 1
|
||||
|
||||
namespace Http
|
||||
|
||||
' TODO:P2 localization
|
||||
class RegistryRouter extends HttpRouter
|
||||
|
||||
function new(server as object)
|
||||
function new()
|
||||
super()
|
||||
|
||||
m.task = server.task
|
||||
m.lastRequestCodeTimeSeconds = 0
|
||||
m.requestCode = invalid
|
||||
end function
|
||||
|
||||
@get("/api/registry/export/code")
|
||||
@post("/api/registry/export/code")
|
||||
function ShowExportRegistryCode(context as object) as boolean
|
||||
response = context.response
|
||||
|
||||
requestCodeTimeSeconds = TimeUtils.Now().AsSeconds()
|
||||
|
||||
m.requestCode = invalid
|
||||
scene = m.task.getScene()
|
||||
dialog = scene.dialog
|
||||
if dialog <> invalid and dialog.hasField("isExportRegistryCodeDialog")
|
||||
dialog.close = true
|
||||
end if
|
||||
|
||||
if requestCodeTimeSeconds - m.lastRequestCodeTimeSeconds < 2
|
||||
' TODO:P2 localization
|
||||
dialog = DialogUtils.ShowOnScreenCodeDialog(DialogUtils.OnScreenCodePurpose.ExportRegistry, "Export registry")
|
||||
if dialog = invalid
|
||||
response.Default(429, "Too Many Requests")
|
||||
return true
|
||||
end if
|
||||
|
||||
requestCode = rnd(999999).ToStr()
|
||||
if requestCode.Len() < 6
|
||||
requestCode = string(6 - requestCode.Len(), "0") + requestCode
|
||||
end if
|
||||
|
||||
m.requestCode = requestCode
|
||||
m.lastRequestCodeTimeSeconds = requestCodeTimeSeconds
|
||||
requestCode = requestCode.Left(3) + " " + requestCode.Right(3)
|
||||
|
||||
message = `The code to export the registry data is: ${requestCode}.
|
||||
This code will expire in 2 minutes.
|
||||
If you did not request to export the registry, please close this dialog.`
|
||||
dialog = DialogUtils.ShowDialogEx({
|
||||
message: message
|
||||
title: "Export registry"
|
||||
alwaysOnTop: true
|
||||
})
|
||||
if dialog <> invalid
|
||||
dialog.addField("isExportRegistryCodeDialog", "boolean", false)
|
||||
end if
|
||||
|
||||
response.Default(204, "OK")
|
||||
return true
|
||||
end function
|
||||
|
||||
@get("/api/registry/export")
|
||||
@post("/api/registry/export")
|
||||
function ExportRegistry(context as object) as boolean
|
||||
request = context.request
|
||||
response = context.response
|
||||
|
||||
requestCode = m.requestCode
|
||||
m.requestCode = invalid
|
||||
dialog = m.task.getScene().dialog
|
||||
if dialog = invalid or not dialog.hasField("isExportRegistryCodeDialog")
|
||||
response.Default(400, "Invalid code")
|
||||
return true
|
||||
end if
|
||||
dialog.close = true
|
||||
|
||||
requestTimeSeconds = TimeUtils.Now().AsSeconds()
|
||||
if requestTimeSeconds - m.lastRequestCodeTimeSeconds > 120
|
||||
response.Default(400, "Invalid code")
|
||||
return true
|
||||
end if
|
||||
|
||||
if requestCode = invalid
|
||||
response.Default(400, "Invalid code")
|
||||
return true
|
||||
end if
|
||||
|
||||
code = request.query.code
|
||||
if StringUtils.IsNullOrEmpty(code)
|
||||
response.Default(400, "Invalid code")
|
||||
return true
|
||||
end if
|
||||
|
||||
code = code.Replace(" ", "")
|
||||
if code <> requestCode
|
||||
if not DialogUtils.ConsumeOnScreenCode(DialogUtils.OnScreenCodePurpose.ExportRegistry, request.query.code)
|
||||
response.Default(400, "Invalid code")
|
||||
return true
|
||||
end if
|
||||
|
||||
@@ -84,7 +84,7 @@ function SetupRoutes(server as object)
|
||||
server.UseRouter(new Http.PlayletLibUrlsRouter(server))
|
||||
server.UseRouter(new Http.DialRouter(server))
|
||||
server.UseRouter(new Http.CacheRouter(server))
|
||||
server.UseRouter(new Http.RegistryRouter(server))
|
||||
server.UseRouter(new Http.RegistryRouter())
|
||||
server.UseRouter(new Http.PlayletInvidiousBackendRouter(server))
|
||||
server.UseRouter(new Http.InnertubeRouter(server))
|
||||
|
||||
|
||||
Reference in new issue
Block a user