2024-05-24 23:08:24 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package clientservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2024-09-05 23:25:45 +02:00
|
|
|
"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"
|
2024-05-24 23:08:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type ClientService struct{}
|
|
|
|
|
|
|
|
const DefaultTimeout = 2 * time.Second
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (cs *ClientService) GetClientData() (*waveobj.Client, error) {
|
2024-05-24 23:08:24 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting client data: %w", err)
|
|
|
|
}
|
|
|
|
return clientData, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (cs *ClientService) GetWorkspace(workspaceId string) (*waveobj.Workspace, error) {
|
2024-05-24 23:08:24 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
ws, err := wstore.DBGet[*waveobj.Workspace](ctx, workspaceId)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting workspace: %w", err)
|
|
|
|
}
|
|
|
|
return ws, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (cs *ClientService) GetTab(tabId string) (*waveobj.Tab, error) {
|
2024-05-24 23:08:24 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
tab, err := wstore.DBGet[*waveobj.Tab](ctx, tabId)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting tab: %w", err)
|
|
|
|
}
|
|
|
|
return tab, nil
|
|
|
|
}
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (cs *ClientService) GetWindow(windowId string) (*waveobj.Window, error) {
|
2024-05-24 23:08:24 +02:00
|
|
|
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
window, err := wstore.DBGet[*waveobj.Window](ctx, windowId)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting window: %w", err)
|
|
|
|
}
|
|
|
|
return window, nil
|
|
|
|
}
|
2024-06-20 04:10:53 +02:00
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (cs *ClientService) MakeWindow(ctx context.Context) (*waveobj.Window, error) {
|
2024-08-27 00:17:37 +02:00
|
|
|
return wcore.CreateWindow(ctx, nil)
|
2024-06-20 04:10:53 +02:00
|
|
|
}
|
|
|
|
|
2024-08-24 03:12:40 +02:00
|
|
|
func (cs *ClientService) GetAllConnStatus(ctx context.Context) ([]wshrpc.ConnStatus, error) {
|
|
|
|
return conncontroller.GetAllConnStatus(), nil
|
|
|
|
}
|
|
|
|
|
2024-06-20 04:10:53 +02:00
|
|
|
// 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)
|
|
|
|
}
|
2024-07-30 20:44:19 +02:00
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
func (cs *ClientService) AgreeTos(ctx context.Context) (waveobj.UpdatesRtnType, error) {
|
|
|
|
ctx = waveobj.ContextWithUpdates(ctx)
|
|
|
|
clientData, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
2024-07-30 20:44:19 +02:00
|
|
|
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)
|
|
|
|
}
|
2024-08-28 03:38:57 +02:00
|
|
|
wlayout.BootstrapStarterLayout(ctx)
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-07-30 20:44:19 +02:00
|
|
|
}
|