2024-05-24 23:08:24 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package clientservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-08-01 06:27:46 +02:00
|
|
|
"log"
|
2024-05-24 23:08:24 +02:00
|
|
|
"time"
|
|
|
|
|
2024-08-01 06:27:46 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/eventbus"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/service/objectservice"
|
2024-06-20 04:10:53 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/util/utilfn"
|
2024-08-20 23:56:48 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/waveobj"
|
2024-05-24 23:08:24 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-01 09:52:15 +02:00
|
|
|
return wstore.CreateWindow(ctx, 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-01 06:27:46 +02:00
|
|
|
cs.BootstrapStarterLayout(ctx)
|
2024-08-20 23:56:48 +02:00
|
|
|
return waveobj.ContextGetUpdatesRtn(ctx), nil
|
2024-07-30 20:44:19 +02:00
|
|
|
}
|
2024-08-01 06:27:46 +02:00
|
|
|
|
|
|
|
type PortableLayout []struct {
|
|
|
|
IndexArr []int
|
|
|
|
Size uint
|
2024-08-20 23:56:48 +02:00
|
|
|
BlockDef *waveobj.BlockDef
|
2024-08-01 06:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cs *ClientService) BootstrapStarterLayout(ctx context.Context) error {
|
|
|
|
ctx, cancelFn := context.WithTimeout(ctx, 2*time.Second)
|
|
|
|
defer cancelFn()
|
2024-08-20 23:56:48 +02:00
|
|
|
client, err := wstore.DBGetSingleton[*waveobj.Client](ctx)
|
2024-08-01 06:27:46 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to find client: %v\n", err)
|
|
|
|
return fmt.Errorf("unable to find client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(client.WindowIds) < 1 {
|
|
|
|
return fmt.Errorf("error bootstrapping layout, no windows exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
windowId := client.WindowIds[0]
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
window, err := wstore.DBMustGet[*waveobj.Window](ctx, windowId)
|
2024-08-01 06:27:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting window: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tabId := window.ActiveTabId
|
|
|
|
|
|
|
|
starterLayout := PortableLayout{
|
2024-08-20 23:56:48 +02:00
|
|
|
{IndexArr: []int{0}, BlockDef: &waveobj.BlockDef{
|
|
|
|
Meta: waveobj.MetaMapType{
|
|
|
|
waveobj.MetaKey_View: "term",
|
|
|
|
waveobj.MetaKey_Controller: "shell",
|
2024-08-01 06:27:46 +02:00
|
|
|
},
|
|
|
|
}},
|
2024-08-20 23:56:48 +02:00
|
|
|
{IndexArr: []int{1}, BlockDef: &waveobj.BlockDef{
|
|
|
|
Meta: waveobj.MetaMapType{
|
|
|
|
waveobj.MetaKey_View: "cpuplot",
|
2024-08-01 06:27:46 +02:00
|
|
|
},
|
|
|
|
}},
|
2024-08-20 23:56:48 +02:00
|
|
|
{IndexArr: []int{1, 1}, BlockDef: &waveobj.BlockDef{
|
|
|
|
Meta: waveobj.MetaMapType{
|
|
|
|
waveobj.MetaKey_View: "web",
|
|
|
|
waveobj.MetaKey_Url: "https://github.com/wavetermdev/waveterm",
|
2024-08-01 06:27:46 +02:00
|
|
|
},
|
|
|
|
}},
|
2024-08-20 23:56:48 +02:00
|
|
|
{IndexArr: []int{1, 2}, BlockDef: &waveobj.BlockDef{
|
|
|
|
Meta: waveobj.MetaMapType{
|
|
|
|
waveobj.MetaKey_View: "preview",
|
|
|
|
waveobj.MetaKey_File: "~",
|
2024-08-01 06:27:46 +02:00
|
|
|
},
|
|
|
|
}},
|
2024-08-20 23:56:48 +02:00
|
|
|
{IndexArr: []int{2}, BlockDef: &waveobj.BlockDef{
|
|
|
|
Meta: waveobj.MetaMapType{
|
|
|
|
waveobj.MetaKey_View: "help",
|
2024-08-01 06:27:46 +02:00
|
|
|
},
|
|
|
|
}},
|
2024-08-20 23:56:48 +02:00
|
|
|
{IndexArr: []int{2, 1}, BlockDef: &waveobj.BlockDef{
|
|
|
|
Meta: waveobj.MetaMapType{
|
|
|
|
waveobj.MetaKey_View: "waveai",
|
2024-08-01 06:27:46 +02:00
|
|
|
},
|
|
|
|
}},
|
2024-08-01 09:52:15 +02:00
|
|
|
// {IndexArr: []int{2, 2}, BlockDef: &wstore.BlockDef{
|
|
|
|
// Meta: wstore.MetaMapType{
|
2024-08-20 23:56:48 +02:00
|
|
|
// waveobj.MetaKey_View: "web",
|
|
|
|
// waveobj.MetaKey_Url: "https://www.youtube.com/embed/cKqsw_sAsU8",
|
2024-08-01 09:52:15 +02:00
|
|
|
// },
|
|
|
|
// }},
|
2024-08-01 06:27:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
objsvc := &objectservice.ObjectService{}
|
|
|
|
|
|
|
|
for i := 0; i < len(starterLayout); i++ {
|
|
|
|
layoutAction := starterLayout[i]
|
|
|
|
|
2024-08-20 23:56:48 +02:00
|
|
|
blockData, err := objsvc.CreateBlock_NoUI(ctx, tabId, layoutAction.BlockDef, &waveobj.RuntimeOpts{})
|
2024-08-01 06:27:46 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create block for starter layout: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
eventbus.SendEventToWindow(windowId, eventbus.WSEventType{
|
|
|
|
EventType: eventbus.WSEvent_LayoutAction,
|
|
|
|
Data: &eventbus.WSLayoutActionData{
|
|
|
|
ActionType: "insertatindex",
|
|
|
|
TabId: tabId,
|
|
|
|
BlockId: blockData.OID,
|
|
|
|
IndexArr: layoutAction.IndexArr,
|
|
|
|
NodeSize: layoutAction.Size,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|