waveterm/pkg/pcloud/pclouddata.go

162 lines
4.8 KiB
Go
Raw Normal View History

2023-03-26 22:21:58 +02:00
package pcloud
import (
2023-03-27 03:48:43 +02:00
"context"
2023-03-26 22:21:58 +02:00
"fmt"
"github.com/scripthaus-dev/mshell/pkg/packet"
2023-03-27 03:48:43 +02:00
"github.com/scripthaus-dev/sh2-server/pkg/remote"
"github.com/scripthaus-dev/sh2-server/pkg/rtnstate"
2023-03-26 22:21:58 +02:00
"github.com/scripthaus-dev/sh2-server/pkg/sstore"
)
type NoTelemetryInputType struct {
ClientId string `json:"clientid"`
Value bool `json:"value"`
}
type TelemetryInputType struct {
UserId string `json:"userid"`
ClientId string `json:"clientid"`
CurDay string `json:"curday"`
Activity []*sstore.ActivityType `json:"activity"`
}
type WebShareUpdateType struct {
ScreenId string `json:"screenid"`
LineId string `json:"lineid"`
UpdateId int64 `json:"updateid"`
2023-03-26 22:21:58 +02:00
UpdateType string `json:"updatetype"`
2023-03-28 10:05:01 +02:00
UpdateTs int64 `json:"updatets"`
2023-03-26 22:21:58 +02:00
Screen *WebShareScreenType `json:"screen,omitempty"`
Line *WebShareLineType `json:"line,omitempty"`
Cmd *WebShareCmdType `json:"cmd,omitempty"`
PtyData *WebSharePtyData `json:"ptydata,omitempty"`
SVal string `json:"sval,omitempty"`
BVal bool `json:"bval,omitempty"`
DoneInfo *sstore.CmdDoneInfo `json:"doneinfo,omitempty"`
2023-03-27 03:48:43 +02:00
TermOpts *sstore.TermOpts `json:"termopts,omitempty"`
2023-03-26 22:21:58 +02:00
}
type WebShareUpdateResponseType struct {
UpdateId int64 `json:"updateid"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
func (ur *WebShareUpdateResponseType) GetSimpleKey() int64 {
return ur.UpdateId
}
2023-03-26 22:21:58 +02:00
type WebShareRemotePtr struct {
2023-03-29 07:53:59 +02:00
Alias string `json:"alias,omitempty"`
CanonicalName string `json:"canonicalname"`
2023-03-26 22:21:58 +02:00
Name string `json:"name,omitempty"`
}
2023-03-27 03:48:43 +02:00
func webRemoteFromRemotePtr(rptr sstore.RemotePtrType) *WebShareRemotePtr {
if rptr.RemoteId == "" {
return nil
}
rcopy := remote.GetRemoteById(rptr.RemoteId).GetRemoteCopy()
return &WebShareRemotePtr{
Alias: rcopy.RemoteAlias,
CanonicalName: rcopy.RemoteCanonicalName,
Name: rptr.Name,
}
}
2023-03-26 22:21:58 +02:00
type WebShareScreenType struct {
ScreenId string `json:"screenid"`
ShareName string `json:"sharename"`
ViewKey string `json:"viewkey"`
}
func webScreenFromScreen(s *sstore.ScreenType) (*WebShareScreenType, error) {
if s == nil || s.ScreenId == "" {
return nil, fmt.Errorf("invalid nil screen")
}
if s.WebShareOpts == nil {
return nil, fmt.Errorf("invalid screen, no WebShareOpts")
}
if s.WebShareOpts.ViewKey == "" {
return nil, fmt.Errorf("invalid screen, no ViewKey")
}
var shareName string
if s.WebShareOpts.ShareName != "" {
shareName = s.WebShareOpts.ShareName
} else {
shareName = s.Name
2023-03-26 22:21:58 +02:00
}
return &WebShareScreenType{ScreenId: s.ScreenId, ShareName: shareName, ViewKey: s.WebShareOpts.ViewKey}, nil
2023-03-26 22:21:58 +02:00
}
type WebShareLineType struct {
LineId string `json:"lineid"`
Ts int64 `json:"ts"`
LineNum int64 `json:"linenum"`
LineType string `json:"linetype"`
Renderer string `json:"renderer,omitempty"`
Text string `json:"text,omitempty"`
CmdId string `json:"cmdid,omitempty"`
Archived bool `json:"archived,omitempty"`
}
func webLineFromLine(line *sstore.LineType) (*WebShareLineType, error) {
2023-03-27 03:48:43 +02:00
rtn := &WebShareLineType{
LineId: line.LineId,
Ts: line.Ts,
LineNum: line.LineNum,
LineType: line.LineType,
Renderer: line.Renderer,
Text: line.Text,
CmdId: line.CmdId,
Archived: line.Archived,
}
return rtn, nil
2023-03-26 22:21:58 +02:00
}
type WebShareCmdType struct {
LineId string `json:"lineid"`
CmdStr string `json:"cmdstr"`
RawCmdStr string `json:"rawcmdstr"`
2023-03-27 03:48:43 +02:00
Remote *WebShareRemotePtr `json:"remote"`
2023-03-26 22:21:58 +02:00
FeState sstore.FeStateType `json:"festate"`
TermOpts sstore.TermOpts `json:"termopts"`
Status string `json:"status"`
StartPk *packet.CmdStartPacketType `json:"startpk,omitempty"`
DoneInfo *sstore.CmdDoneInfo `json:"doneinfo,omitempty"`
RtnState bool `json:"rtnstate,omitempty"`
RtnStateStr string `json:"rtnstatestr,omitempty"`
}
2023-03-27 03:48:43 +02:00
func webCmdFromCmd(lineId string, cmd *sstore.CmdType) (*WebShareCmdType, error) {
rtn := &WebShareCmdType{
LineId: lineId,
CmdStr: cmd.CmdStr,
RawCmdStr: cmd.RawCmdStr,
Remote: webRemoteFromRemotePtr(cmd.Remote),
FeState: cmd.FeState,
TermOpts: cmd.TermOpts,
Status: cmd.Status,
StartPk: cmd.StartPk,
DoneInfo: cmd.DoneInfo,
RtnState: cmd.RtnState,
}
if cmd.RtnState {
barr, err := rtnstate.GetRtnStateDiff(context.Background(), cmd.ScreenId, cmd.CmdId)
if err != nil {
return nil, fmt.Errorf("error creating rtnstate diff for cmd:%s: %v", cmd.CmdId, err)
}
rtn.RtnStateStr = string(barr)
}
return rtn, nil
2023-03-26 22:21:58 +02:00
}
type WebSharePtyData struct {
PtyPos int64 `json:"ptypos,omitempty"`
Data []byte `json:"data,omitempty"`
Eof bool `json:"-"` // internal use
2023-03-26 22:21:58 +02:00
}