on screen code refactor, playlet lib requires code

This commit is contained in:
Brahim Hadriche committed 2026-10-08 17:35:22 -04:00
1 parent fe15ea177b
commit 9c1de5e1cf
11 files changed
+215 -84

No files matched your search

+3 -1
View File
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Nothing worth mentioning yet.
### Changed
- Changing Playlet library url (e.g. to test a Canary version) now requires entering a verification code
## [0.48.8] - 2026-10-07
+1 -1
View File
@@ -6,5 +6,5 @@ To test this release:
- Open Playlet web app settings in a browser (`http://ROKU_IP_ADDRESS:8888/index.html#settings`)
- Scroll down to `Developer settings` and set `Playlet Library version` to `canary` and click `Apply`
- Restart Playlet
- Enter the code shown on your TV
- You can verify that you're testing the `canary` release in the `Info` tab in Playlet or the web app
@@ -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))
@@ -0,0 +1,48 @@
import "pkg:/components/Dialog/DialogUtils.bs"
namespace Tests
@suite
class OnScreenCodeDialogTests extends Rooibos.BaseTestSuite
protected override function beforeEach()
m.dialog = CreateObject("roSGNode", "OnScreenCodeDialog")
m.dialog.purpose = DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls
match = CreateObject("roRegex", "(\d{3}) (\d{3})", "").Match(m.dialog.message[0])
m.code = match[1] + match[2]
end function
@describe("OnScreenCodeDialog.Consume")
@it("accepts the code shown, and closes")
function _()
displayedCode = `${m.code.Left(3)} ${m.code.Right(3)}`
m.assertTrue(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, displayedCode))
m.assertTrue(m.dialog.close)
end function
@it("is single use")
function _()
m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code)
m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code))
end function
@it("burns the code on a wrong guess")
function _()
m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, "wrong")
m.assertTrue(m.dialog.close)
m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code))
end function
@it("rejects the code for another purpose")
function _()
m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.ExportRegistry, m.code))
end function
@it("rejects a code that is not a string")
function _()
m.assertFalse(m.dialog@.Consume(DialogUtils.OnScreenCodePurpose.SetPlayletLibUrls, m.code.ToInt()))
end function
end class
end namespace
+24 -7
View File
@@ -193,11 +193,11 @@ export class PlayletApi {
}
static async showExportRegistryCode() {
await fetch(`${PlayletApi.host()}/api/registry/export/code`);
await PlayletApi.send("POST", `${PlayletApi.host()}/api/registry/export/code`);
}
static async exportRegistry(code: string) {
const response = await fetch(`${PlayletApi.host()}/api/registry/export?code=${code}`);
const response = await PlayletApi.send("POST", `${PlayletApi.host()}/api/registry/export?code=${encodeURIComponent(code)}`);
if (!response.ok) {
const error = `Error from /api/registry/export: ${response.statusText}`;
console.error(error);
@@ -218,8 +218,7 @@ export class PlayletApi {
return { filename, content };
}
static async setPlayletLibVersion(tag) {
if (tag !== "") {
static playletLibUrlsForTag(tag: string) {
const urls = [{
link: `https://github.com/iBicha/playlet/releases/download/${tag}/playlet-lib.squashfs.pkg`,
type: 'custom'
@@ -238,9 +237,27 @@ export class PlayletApi {
type: 'custom'
})
}
await PlayletApi.send("POST", `${PlayletApi.host()}/api/playlet-lib-urls`, urls);
} else {
return await PlayletApi.send("DELETE", `${PlayletApi.host()}/api/playlet-lib-urls`);
return urls;
}
static async showSetPlayletLibUrlsCode() {
const response = await PlayletApi.send("POST", `${PlayletApi.host()}/api/playlet-lib-urls/code`);
if (!response.ok) {
throw new Error(`Error from /api/playlet-lib-urls/code: ${response.status} ${await response.text()}`);
}
}
static async setPlayletLibUrls(urls: { link: string, type: string }[], code: string) {
const response = await PlayletApi.send("POST", `${PlayletApi.host()}/api/playlet-lib-urls?code=${encodeURIComponent(code)}`, urls);
if (!response.ok) {
throw new Error(`Error from /api/playlet-lib-urls: ${response.status} ${await response.text()}`);
}
}
static async resetPlayletLibUrls() {
const response = await PlayletApi.send("DELETE", `${PlayletApi.host()}/api/playlet-lib-urls`);
if (!response.ok) {
throw new Error(`Error from /api/playlet-lib-urls: ${response.status} ${await response.text()}`);
}
}
@@ -97,19 +97,44 @@
const trFn = get(translate);
if (
confirm(
!confirm(
trFn(
"Are you sure you want to change the Playlet Library version to %1?"
).replace("%1", version)
) +
).replace("%1", version) +
`\n` +
trFn("This will restart Playlet.")
)
) {
await PlayletApi.setPlayletLibVersion(selectedRelease);
return;
}
try {
if (selectedRelease === "") {
await PlayletApi.resetPlayletLibUrls();
} else {
await PlayletApi.showSetPlayletLibUrlsCode();
const code = prompt(trFn("Enter the code you see on your Roku device"));
if (!code) {
return;
}
await PlayletApi.setPlayletLibUrls(
PlayletApi.playletLibUrlsForTag(selectedRelease),
code
);
}
} catch (error) {
console.error(error);
alert(
trFn(
"Failed to change the Playlet Library version. Please try again. See console for details."
)
);
return;
}
alert(trFn("Playlet Library version changed. Playlet will now restart."));
await ExternalControlProtocol.restartApp(appId);
}
}
</script>
<div class="m-5">