Settings work

This commit is contained in:
Brahim Hadriche committed 2022-11-18 17:11:11 -05:00
1 parent c20bdc15eb
commit 95a77f0119
16 files changed
+172 -44

No files matched your search

+25
View File
@@ -28,6 +28,8 @@ end function
function InitWebServer() function InitWebServer()
m.serverTask = createObject("roSGNode", "WebServerTask") m.serverTask = createObject("roSGNode", "WebServerTask")
m.serverTask.id = "WebServerTask"
m.serverTask.ObserveField("login", "OnWebServerLogin")
m.serverTask.control = "RUN" m.serverTask.control = "RUN"
end function end function
@@ -68,3 +70,26 @@ function OnNavIndexChange() as void
m.settingsScreen.visible = true m.settingsScreen.visible = true
end if end if
end function end function
function OnWebServerLogin()
' TODO: refresh same component instead of creating a new one
m.global.loadingIndicator.visible = true
homeIndex = GetIndexOfChild(m.top, m.homeGridScreen.id)
newHomeGridScreen = CreateObject("roSGNode", "HomeGridScreen")
m.top.replaceChild(newHomeGridScreen, homeIndex)
m.homeGridScreen = newHomeGridScreen
m.homeGridScreen.id = "HomeGridScreen"
m.homeGridScreen.ObserveField("escape", "OnHomeGridScreenEscape")
m.homeGridScreen.SetFocus(true)
end function
function GetIndexOfChild(parent as object, childId as string) as integer
count = parent.getChildCount()
for i = 0 to count - 1
child = parent.getChild(i)
if child.id = childId
return i
end if
end for
return -1
end function
@@ -1,9 +1,7 @@
import "pkg:/source/roku_modules/rokupromise/Promise.brs" import "pkg:/source/utils/General.bs"
function Init() function Init()
createTaskPromise("SettingsScreenTask", invalid).then(function(task as object) m.top.url = `http:/${GetLocalIpAddress()}:8888/index.html?tab=settings`
m.top.url = task.output.url
end function)
end function end function
function OnUrlSet() function OnUrlSet()
@@ -10,14 +10,15 @@
loadWidth='500' loadWidth='500'
loadHeight='500' loadHeight='500'
loadPadding='20' loadPadding='20'
translation='[370, 60]' /> translation='[395, 60]' />
<Label <Label
id='UrlLabel' id='UrlLabel'
height='50' height='50'
width='1000' width='1280'
font='font:MediumBoldSystemFont' font='font:MediumBoldSystemFont'
wrap='true' horizAlign="center"
translation='[140,600]' /> translation="[0,600]"
/>
</children> </children>
</component> </component>
@@ -1,11 +0,0 @@
import "pkg:/source/services/Invidious.bs"
function Init()
m.top.functionName = "TaskMain"
end function
function TaskMain()
m.top.setField("output", {
url: Invidious.GetAuthorizeTokenLink()
})
end function
@@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<component name="SettingsScreenTask" extends="Task">
<interface>
<field id="input" type="assocarray" />
<field id="output" type="assocarray" />
</interface>
</component>
+6 -1
View File
@@ -35,7 +35,12 @@ namespace Http
private function OnRequest(request as object, response as HttpResponse) as boolean private function OnRequest(request as object, response as HttpResponse) as boolean
for each route in m.routes for each route in m.routes
if m.RouteMatchesRequest(route, request) if m.RouteMatchesRequest(route, request)
if route.handler(m, request, response) = true if route.handler({
router: m,
request: request,
response: response,
server: response.server
}) = true
return true return true
end if end if
end if end if
+4 -2
View File
@@ -26,11 +26,13 @@ namespace Http
private routers private routers
private settings as Http.HttpSettings public settings as Http.HttpSettings
public task
function new(settings as HttpSettings) function new(settings as HttpSettings, task as object)
m.log = new log.Logger("HttpResponse") m.log = new log.Logger("HttpResponse")
m.settings = settings m.settings = settings
m.task = task
m.routers = [] m.routers = []
Http.InitGlobals() Http.InitGlobals()
m.Init() m.Init()
@@ -5,7 +5,11 @@ namespace Http
function new() function new()
super() super()
m.All("*", function(router as object, request as HttpRequest, response as HttpResponse) as boolean m.All("*", function(context as object) as boolean
router = context.router
request = context.request
response = context.response
method = request.method method = request.method
route = request.uri route = request.uri
@@ -13,9 +13,16 @@ namespace Http
m.root = root m.root = root
m.filesOnly = filesOnly m.filesOnly = filesOnly
m.Get("*", function(router as object, request as HttpRequest, response as HttpResponse) as boolean m.Get("*", function(context as object) as boolean
router = context.router
request = context.request
response = context.response
uri = request.uri uri = request.uri
decoded_uri = WebUtils.UrlUnescape(uri) decoded_uri = WebUtils.UrlUnescape(uri)
if decoded_uri.instr("?")
decoded_uri = decoded_uri.Tokenize("?")[0]
end if
path = CreateObject("roPath", router.root + decoded_uri) path = CreateObject("roPath", router.root + decoded_uri)
fs = CreateObject("roFilesystem") fs = CreateObject("roFilesystem")
@@ -5,17 +5,23 @@ namespace Http
function new() function new()
super() super()
m.Get("*", function(router as object, request as Http.HttpRequest, response as Http.HttpResponse) as boolean m.Get("*", function(context as object) as boolean
router = context.router
request = context.request
response = context.response
server = context.server
uri = request.uri uri = request.uri
if uri.StartsWith("/token_callback") if uri.StartsWith(Invidious.TOKEN_CALLBACK)
components = WebUtils.UrlQueryComponents(uri) components = WebUtils.UrlQueryComponents(uri)
if components["token"] <> invalid if components["token"] <> invalid
' Token is encoded twice for some reason ' Token is encoded twice for some reason
token = WebUtils.UrlUnescape(components["token"]).DecodeUriComponent() token = WebUtils.UrlUnescape(components["token"]).DecodeUriComponent()
print(`Got Token: ${token}`) print(`Got Token: ${token}`)
RegistryUtils.Write("token", token) RegistryUtils.Write("token", token)
server.task.top.login = true
end if end if
response.Redirect(Invidious.GetCurrentHost()) response.Redirect("/index.html?tab=settings")
return true return true
end if end if
return false return false
@@ -5,7 +5,11 @@ namespace Http
function new() function new()
super() super()
m.Get("/api/registry", function(router as object, request as HttpRequest, response as HttpResponse) as boolean m.Get("/api/registry", function(context as object) as boolean
router = context.router
request = context.request
response = context.response
registryValues = RegistryUtils.ReadAll() registryValues = RegistryUtils.ReadAll()
response.Json(registryValues) response.Json(registryValues)
return true return true
@@ -0,0 +1,29 @@
import "pkg:/source/services/Invidious.bs"
namespace Http
class StateApiRouter extends HttpRouter
function new()
super()
m.Get("/api/state", function(context as object) as boolean
router = context.router
request = context.request
response = context.response
state = {
invidious: {
host: Invidious.GetCurrentHost(),
auth_url: Invidious.GetAuthorizeTokenLink(),
logged_in: Invidious.GetAuthenticationHeaders() <> invalid
}
}
response.Json(state)
return true
end function)
end function
end class
end namespace
@@ -4,6 +4,7 @@ import "pkg:/components/WebServer/Middleware/HttpStaticFilesRouter.bs"
import "pkg:/components/WebServer/Middleware/HttpDefaultRouter.bs" import "pkg:/components/WebServer/Middleware/HttpDefaultRouter.bs"
import "pkg:/components/WebServer/Middleware/InvidiousRouter.bs" import "pkg:/components/WebServer/Middleware/InvidiousRouter.bs"
import "pkg:/components/WebServer/Middleware/RegistryApiRouter.bs" import "pkg:/components/WebServer/Middleware/RegistryApiRouter.bs"
import "pkg:/components/WebServer/Middleware/StateApiRouter.bs"
function Init() function Init()
m.top.functionName = "WebServerLoop" m.top.functionName = "WebServerLoop"
@@ -14,10 +15,10 @@ function WebServerLoop()
m.settings = new Http.HttpSettings(m.webServerPort) m.settings = new Http.HttpSettings(m.webServerPort)
' Root at www to get http://IP_ADDRESS:PORT/index.html ' Root at www to get http://IP_ADDRESS:PORT/index.html
' TODO: implement HttpStaticFilesRouter as middleware instead
m.settings.WwwRoot = "pkg:/www" m.settings.WwwRoot = "pkg:/www"
m.server = new Http.HttpServer(m.settings) m.server = new Http.HttpServer(m.settings, m)
m.server.UseRouter(new Http.StateApiRouter())
m.server.UseRouter(new Http.RegistryApiRouter()) m.server.UseRouter(new Http.RegistryApiRouter())
m.server.UseRouter(new Http.InvidiousRouter()) m.server.UseRouter(new Http.InvidiousRouter())
m.server.UseRouter(new Http.HttpStaticFilesRouter(m.settings.WwwRoot)) m.server.UseRouter(new Http.HttpStaticFilesRouter(m.settings.WwwRoot))
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<component name="WebServerTask" extends="Task"> <component name="WebServerTask" extends="Task">
<interface>
<field id="login" type="boolean" alwaysNotify="true" />
</interface>
</component> </component>
+3 -1
View File
@@ -8,6 +8,8 @@ import "pkg:/source/utils/WebUtils.bs"
namespace Invidious namespace Invidious
const API_URL = "https://vid.puffyan.us" const API_URL = "https://vid.puffyan.us"
const TOKEN_CALLBACK = "/invidious/token_callback"
const POPULAR_ENDPOINT = "/api/v1/popular" const POPULAR_ENDPOINT = "/api/v1/popular"
const TRENDING_ENDPOINT = "/api/v1/trending" const TRENDING_ENDPOINT = "/api/v1/trending"
@@ -148,7 +150,7 @@ namespace Invidious
webServerPort = 8888 webServerPort = 8888
expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 30 'A month from now expire = TimeUtils.Now().AsSeconds() + 60 * 60 * 24 * 30 'A month from now
callbackUrl = WebUtils.UrlEncode(`http://${localIp}:${webServerPort}/token_callback`) callbackUrl = WebUtils.UrlEncode(`http://${localIp}:${webServerPort}${Invidious.TOKEN_CALLBACK}`)
return `${invidiousHost}/authorize_token?scopes=${Invidious.AUTH_SCOPE}&callback_url=${callbackUrl}&expire=${expire}` return `${invidiousHost}/authorize_token?scopes=${Invidious.AUTH_SCOPE}&callback_url=${callbackUrl}&expire=${expire}`
end function end function
+65 -5
View File
@@ -1,11 +1,71 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head>
<head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Playlet</title> <title>Playlet</title>
</head> </head>
<body>
<div>Hello! This is an HTML file served from Playlet Roku app.</div> <body>
</body> <h2>Playlet - Youtube Player for Roku</h2>
<h4>Invidious Instance: <a id="host" target="_blank">None</a></h4>
<h4 id="logged-in">Logged In: </h4>
<button style="visibility: hidden" id="login-button" onclick="Login()">Login</button>
<button style="visibility: hidden" id="logout-button">Logout</button>
<h5>Instructions</h5>
<ul>
<li>Make sure you are logged in the Invidious instance before logging in on this page</li>
<li>If this is your first time using this invidious Instance, you might want to create an account</li>
<li>Once you are logged in to your current instance, click "Login" on this page, and authorize the token</li>
<li>Once logged in, this page should show "Logged In: true"</li>
<li>After login, the home page should refresh automatically. You should then be able to see your Subscription feed
and Playlists, if you have any.</li>
</ul>
<p>
This is a barebone page for now, stay tuned for a redesigned page!
</p>
<h5>Feedback is welcome over at: <a href="https://github.com/iBicha/playlet"
target="_blank">https://github.com/iBicha/playlet</a></h5>
<script>
function RequestGet(url, cb) {
const req = new XMLHttpRequest();
req.addEventListener("load", function () {
try {
cb(null, JSON.parse(this.responseText));
}
catch (err) {
cb(err)
}
});
req.open("GET", url);
req.send();
}
function Login() {
if (!window.appState) {
alert("Error with login, please refresh the page.")
}
window.location = window.appState.invidious.auth_url
}
RequestGet("/api/state", function (err, response) {
if (err) {
alert(err)
return
}
window.appState = response
console.log(response)
document.getElementById("logged-in").innerText = `Logged In: ${response.invidious.logged_in}`
document.getElementById("host").innerText = response.invidious.host
document.getElementById("host").href = response.invidious.host
document.getElementById("login-button").style.visibility = response.invidious.logged_in ? "hidden" : "visible";
// document.getElementById("logout-button").style.visibility = response.invidious.logged_in ? "hidden" : "visible";
})
</script>
</body>
</html> </html>