From 26bc707d0ec75e10744f58f18d0bc4efbb141335 Mon Sep 17 00:00:00 2001 From: GregTCLTK Date: Thu, 2 Nov 2023 03:56:15 +0100 Subject: [PATCH] wip: remove ptero --- pages/hosting/loading.ts | 80 ++++------------------------ pages/hosting/main.ts | 6 +-- pages/hosting/views/ServerDetails.ts | 64 +++++++++++----------- pages/hosting/views/menu.ts | 5 +- pages/shared/restSpec.ts | 1 + 5 files changed, 50 insertions(+), 106 deletions(-) diff --git a/pages/hosting/loading.ts b/pages/hosting/loading.ts index a34c5ad4..1fa66b90 100644 --- a/pages/hosting/loading.ts +++ b/pages/hosting/loading.ts @@ -1,14 +1,13 @@ -import { HmRequest, LoginRequest, MessageType, SyncResponse, TriggerRequest } from "https://deno.land/x/hmsys_connector@0.9.0/mod.ts"; +import { LoginRequest, MessageType, SyncResponse, TriggerRequest } from "https://deno.land/x/hmsys_connector@0.9.0/mod.ts"; import { API, ProgressTracker } from "shared"; import { Deferred, deferred } from "std/async/deferred.ts"; import { decodeBase64, encodeBase64 } from "std/encoding/base64.ts"; -import { Pointer, State, asPointer, lazyInit } from "webgen/mod.ts"; -import { Server, ServerDetails, SidecarRequest, SidecarResponse } from "../../spec/music.ts"; +import { Pointer, State, asPointer } from "webgen/mod.ts"; +import { Server, SidecarRequest, SidecarResponse } from "../../spec/music.ts"; import { activeUser, tokens } from "../_legacy/helper.ts"; import { state } from "./data.ts"; import { canWriteInFolder, currentFiles } from "./views/state.ts"; - export async function refreshState() { state.servers = State((await API.hosting.servers()).map(x => State(x))); state.meta = State(await API.hosting.meta()); @@ -64,65 +63,6 @@ export function listener() { ws.onclose = () => setTimeout(() => listener(), 1000); } -export const currentDetailsTarget = asPointer(undefined); -export const currentDetailsSource = asPointer((_data: ServerDetails) => { }); - -export const messageQueue = []; -// deno-lint-ignore require-await -export const streamingPool = lazyInit(async () => { - function connect() { - const ws = new WebSocket(API.WS_URL); - let firstTime = true; - ws.onmessage = ({ data }) => { - const json = JSON.parse(data); - - if (json.login === "require authentication" && tokens.accessToken) ws.send(JSON.stringify({ - action: MessageType.Login, - type: "client", - token: API.getToken(), - id: activeUser.id - })); - - if (json.login === true && firstTime === true) { - firstTime = false; - currentDetailsTarget.listen((id, _oldId) => { - if (id) - ws.send(JSON.stringify({ - action: MessageType.Trigger, - type: "@bbn/hosting/details", - data: { - id - }, - auth: { - token: API.getToken(), - id: activeUser.id - } - })); - else if (_oldId != undefined) { - // Else somehow unregister the oldId - // Can't current unregister the conversation. Thats why we just restart the pool. - ws.close(); - } - - }); - } - if (json.type == "sync" && json.data.type == "@bbn/hosting/details") { - currentDetailsSource.getValue()?.(json.data.data); - } - }; - ws.onerror = () => { - - }; - setInterval(() => { - if (ws.readyState != ws.OPEN || messageQueue.length == 0) return; - - ws.send(JSON.stringify(messageQueue.shift())); - }, 100); - ws.onclose = () => setTimeout(() => connect(), 1000); - } - connect(); -}); - export let messageQueueSidecar = <{ request: SidecarRequest, response: Deferred; }[]>[]; let activeSideCar: Deferred | undefined = undefined; export const isSidecarConnect = asPointer(false); @@ -137,7 +77,7 @@ export async function startSidecarConnection(id: string) { } const url = new URL(`wss://bbn.one/api/@bbn/sidecar/${id}/ws`); - url.searchParams.set("TOKEN", localStorage[ "access-token" ]); + url.searchParams.set("TOKEN", API.getToken()); const ws = new WebSocket(url.toString()); activeSideCar = deferred(); @@ -192,12 +132,12 @@ export async function startSidecarConnection(id: string) { ws.close(); } -export async function listFiles(_path: string) { +export async function listFiles(path: string) { const response = deferred(); messageQueueSidecar.push({ request: { type: "list", - path: _path + path }, response }); @@ -216,7 +156,7 @@ export function downloadFile(path: string) { messageQueueSidecar.push({ request: { type: firstTime ? "read" : "next-chunk", - path: path + path }, response: nextChunk }); @@ -235,12 +175,12 @@ export function downloadFile(path: string) { } }); } -export async function uploadFile(_path: string, file: File, progress: Pointer) { +export async function uploadFile(path: string, file: File, progress: Pointer) { const check = deferred(); messageQueueSidecar.push({ request: { type: "write", - path: _path + path }, response: check }); @@ -253,7 +193,7 @@ export async function uploadFile(_path: string, file: File, progress: Pointer s._id == serverId)) + state.servers.push(State(server)); if (!server.identifier) startSidecarConnection(serverId); if (subView === "storage") { diff --git a/pages/hosting/views/ServerDetails.ts b/pages/hosting/views/ServerDetails.ts index ce6144ec..cff60f8e 100644 --- a/pages/hosting/views/ServerDetails.ts +++ b/pages/hosting/views/ServerDetails.ts @@ -1,8 +1,8 @@ import { API, stupidErrorAlert } from "shared"; import { deferred } from "std/async/deferred.ts"; -import { Box, Button, Custom, Entry, Form, Grid, State, StateHandler, TextInput, isMobile, refMerge } from "webgen/mod.ts"; +import { Box, Button, Custom, Entry, Form, Grid, Label, State, StateHandler, TextInput, isMobile, refMerge } from "webgen/mod.ts"; import { Server, SidecarResponse } from "../../../spec/music.ts"; -import { currentDetailsTarget, isSidecarConnect, listFiles, messageQueueSidecar, sidecarDetailsSource } from "../loading.ts"; +import { isSidecarConnect, listFiles, messageQueueSidecar, sidecarDetailsSource } from "../loading.ts"; import { ServerStaticInfo } from "./ServerStaticInfo.ts"; import { editServerDialog } from "./dialogs/editServerDialog.ts"; import { auditEntry, hostingMenu } from "./menu.ts"; @@ -22,7 +22,6 @@ export function ServerDetails(server: StateHandler) { terminal.connected.listen(val => { if (val) { - currentDetailsTarget.setValue(server._id); sidecarDetailsSource.setValue((data: SidecarResponse) => { if (data.type == "log") { if (data.backlog) @@ -45,32 +44,38 @@ export function ServerDetails(server: StateHandler) { .map(({ connected, mobile }) => (() => { const items = Grid( ...ServerStaticInfo(mobile, server, input), - !server.identifier && !connected - ? DisconnectedScreen() - : Entry( - Grid( - Box(Custom(terminal).addClass("terminal-window")).removeFromLayout(), - Form(Grid( - TextInput("text", "Send a Command") - .sync(input, "message"), - Button("Send") - .setId("submit-button") - .onClick(() => { - messageQueueSidecar.push({ - request: { - type: "command", - command: input.message - }, - response: deferred() - }); - input.message = ""; - }) - ) - .setRawColumns("auto max-content") - .setGap(".5rem")) - .activeSubmitTo("#submit-button") - ).addClass("internal-grid") - ).addClass("terminal-card"), + server.identifier ? Grid( + Grid( + Label("Please migrate", "h1"), + Label("This server is still running on our legacy system. Please re-create it.", "h2") + ).setJustify("center") + ).addClass("disconnected-screen") : + connected + ? Entry( + Grid( + Box(Custom(terminal).addClass("terminal-window")).removeFromLayout(), + Form(Grid( + TextInput("text", "Send a Command") + .sync(input, "message"), + Button("Send") + .setId("submit-button") + .onClick(() => { + messageQueueSidecar.push({ + request: { + type: "command", + command: input.message + }, + response: deferred() + }); + input.message = ""; + }) + ) + .setRawColumns("auto max-content") + .setGap(".5rem")) + .activeSubmitTo("#submit-button") + ).addClass("internal-grid") + ).addClass("terminal-card") + : DisconnectedScreen(), server.identifier ? Grid( Entry({ title: "Settings", @@ -99,7 +104,6 @@ export function ServerDetails(server: StateHandler) { title: "Storage", subtitle: "Manage your persistence", }).onClick(async () => { - console.log(hostingMenu.path.getValue()); await listFiles("/"); path.setValue("/"); hostingMenu.path.setValue(`${hostingMenu.path.getValue()}storage/`); diff --git a/pages/hosting/views/menu.ts b/pages/hosting/views/menu.ts index 755ae7c1..af66d2a1 100644 --- a/pages/hosting/views/menu.ts +++ b/pages/hosting/views/menu.ts @@ -6,7 +6,7 @@ import serverTypes from "../../../data/servers.json" assert { type: "json" }; import { Addon, AuditTypes, SidecarResponse } from "../../../spec/music.ts"; import { activeUser, showProfilePicture } from "../../_legacy/helper.ts"; import { state } from "../data.ts"; -import { messageQueueSidecar, startSidecarConnection, stopSidecarConnection, streamingPool } from "../loading.ts"; +import { messageQueueSidecar, startSidecarConnection, stopSidecarConnection } from "../loading.ts"; import { profileView } from "../views/profile.ts"; import { ChangeStateButton } from "./changeStateButton.ts"; import './details.css'; @@ -52,8 +52,7 @@ export const hostingMenu = Navigation({ .setGap("1rem") .setAlign("center"), suffix: ChangeStateButton(server), - clickHandler: async () => { - await streamingPool(); + clickHandler: () => { if (!server.identifier) startSidecarConnection(server._id); // TODO wait until first data is showing to prevent blinking diff --git a/pages/shared/restSpec.ts b/pages/shared/restSpec.ts index 4dfd4fb9..70fe0123 100644 --- a/pages/shared/restSpec.ts +++ b/pages/shared/restSpec.ts @@ -54,6 +54,7 @@ function blob() { return await asExternal(rsp.blob()); }; } +} export function stupidErrorAlert(data: PromiseSettledResult): T { if (data.status === "fulfilled")