// Copyright 2024, Command Line Inc. // SPDX-License-Identifier: Apache-2.0 package clientservice import ( "context" "fmt" "time" "github.com/wavetermdev/waveterm/pkg/remote/conncontroller" "github.com/wavetermdev/waveterm/pkg/util/utilfn" "github.com/wavetermdev/waveterm/pkg/waveobj" "github.com/wavetermdev/waveterm/pkg/wcore" "github.com/wavetermdev/waveterm/pkg/wlayout" "github.com/wavetermdev/waveterm/pkg/wshrpc" "github.com/wavetermdev/waveterm/pkg/wstore" ) type ClientService struct{} const DefaultTimeout = 2 * time.Second func (cs *ClientService) GetClientData() (*waveobj.Client, error) { ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout) defer cancelFn() clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx) if err != nil { return nil, fmt.Errorf("error getting client data: %w", err) } return clientData, nil } func (cs *ClientService) GetWorkspace(workspaceId string) (*waveobj.Workspace, error) { ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout) defer cancelFn() ws, err := wstore.DBGet[*waveobj.Workspace](ctx, workspaceId) if err != nil { return nil, fmt.Errorf("error getting workspace: %w", err) } return ws, nil } func (cs *ClientService) GetTab(tabId string) (*waveobj.Tab, error) { ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout) defer cancelFn() tab, err := wstore.DBGet[*waveobj.Tab](ctx, tabId) if err != nil { return nil, fmt.Errorf("error getting tab: %w", err) } return tab, nil } func (cs *ClientService) GetWindow(windowId string) (*waveobj.Window, error) { ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout) defer cancelFn() window, err := wstore.DBGet[*waveobj.Window](ctx, windowId) if err != nil { return nil, fmt.Errorf("error getting window: %w", err) } return window, nil } func (cs *ClientService) MakeWindow(ctx context.Context) (*waveobj.Window, error) { window, err := wcore.CreateWindow(ctx, nil) if err != nil { return nil, err } err = wlayout.BootstrapNewWindowLayout(ctx, window) if err != nil { return window, err } return window, nil } func (cs *ClientService) GetAllConnStatus(ctx context.Context) ([]wshrpc.ConnStatus, error) { return conncontroller.GetAllConnStatus(), nil } // moves the window to the front of the windowId stack func (cs *ClientService) FocusWindow(ctx context.Context, windowId string) error { client, err := cs.GetClientData() if err != nil { return err } winIdx := utilfn.SliceIdx(client.WindowIds, windowId) if winIdx == -1 { return nil } client.WindowIds = utilfn.MoveSliceIdxToFront(client.WindowIds, winIdx) return wstore.DBUpdate(ctx, client) } func (cs *ClientService) AgreeTos(ctx context.Context) (waveobj.UpdatesRtnType, error) { ctx = waveobj.ContextWithUpdates(ctx) clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx) if err != nil { return nil, fmt.Errorf("error getting client data: %w", err) } timestamp := time.Now().UnixMilli() clientData.TosAgreed = timestamp err = wstore.DBUpdate(ctx, clientData) if err != nil { return nil, fmt.Errorf("error updating client data: %w", err) } wlayout.BootstrapStarterLayout(ctx) return waveobj.ContextGetUpdatesRtn(ctx), nil }