83 lines
2.6 KiB
Plaintext
83 lines
2.6 KiB
Plaintext
import "pkg:/components/Web/WebServer/Http/HttpRouter.bs"
|
|
import "pkg:/source/utils/MessagePortUtils.bs"
|
|
import "pkg:/source/utils/Types.bs"
|
|
|
|
namespace Http
|
|
|
|
class StateApiRouter extends HttpRouter
|
|
|
|
function new(server as object)
|
|
super()
|
|
|
|
task = server.task
|
|
m.applicationInfoNode = task.applicationInfo
|
|
m.invidiousNode = task.invidious
|
|
m.preferencesNode = task.preferences
|
|
m.profilesNode = task.profilesService
|
|
end function
|
|
|
|
@get("/api/state")
|
|
function GetState(context as object) as boolean
|
|
request = context.request
|
|
response = context.response
|
|
|
|
keys = ["app", "device", "invidious", "preferences", "profiles"]
|
|
|
|
if request.query.DoesExist("key")
|
|
keys = request.query["key"]
|
|
if not IsArray(keys)
|
|
keys = [keys]
|
|
end if
|
|
end if
|
|
|
|
keysDict = {}
|
|
for each key in keys
|
|
keysDict[key] = true
|
|
end for
|
|
|
|
state = {}
|
|
|
|
if keysDict["app"] = true
|
|
appInfo = m.applicationInfoNode@.GetAppInfo()
|
|
|
|
if appInfo.lib_version_latest = "N/A"
|
|
' If the lib_version_latest field is not available, we can give it a few seconds
|
|
' To make a request to Github to get the latest version.
|
|
msgPort = CreateObject("roMessagePort")
|
|
MessagePortUtils.ObserveFieldScopedExSafe(m.applicationInfoNode, "lib_version_latest", msgPort)
|
|
wait(5000, msgPort)
|
|
m.applicationInfoNode.unobserveFieldScoped("lib_version_latest")
|
|
|
|
appInfo.lib_version_latest = m.applicationInfoNode.lib_version_latest
|
|
end if
|
|
|
|
state["app"] = appInfo
|
|
end if
|
|
|
|
if keysDict["device"] = true
|
|
state["device"] = m.applicationInfoNode@.GetDeviceInfo()
|
|
end if
|
|
|
|
if keysDict["invidious"] = true
|
|
state["invidious"] = {
|
|
current_instance: m.invidiousNode@.GetCurrentInstance()
|
|
auth_url: m.invidiousNode@.GetAuthorizeTokenLink()
|
|
}
|
|
end if
|
|
|
|
if keysDict["preferences"] = true
|
|
state["preferences"] = m.preferencesNode@.GetUserPreferences()
|
|
end if
|
|
|
|
if keysDict["profiles"] = true
|
|
state["profiles"] = m.profilesNode@.GetProfilesDto(false)
|
|
end if
|
|
|
|
response.Json(state)
|
|
return true
|
|
end function
|
|
|
|
end class
|
|
|
|
end namespace
|