119 lines
2.9 KiB
Go
119 lines
2.9 KiB
Go
package settings
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
type SystemState string
|
|
|
|
const (
|
|
StateUninitialized SystemState = "uninitialized"
|
|
StateCredentialsSaved SystemState = "credentials_saved"
|
|
StateDingTalkVerified SystemState = "dingtalk_verified"
|
|
StateWaitingAdmin SystemState = "waiting_first_admin"
|
|
StateActive SystemState = "active"
|
|
StateCredentialError SystemState = "credential_error"
|
|
)
|
|
|
|
type Snapshot struct {
|
|
ClientID string
|
|
SecretConfigured bool
|
|
CredentialsVerified bool
|
|
AdministratorCount int
|
|
}
|
|
|
|
func DetermineState(s Snapshot) SystemState {
|
|
id := strings.TrimSpace(s.ClientID) != ""
|
|
if !id && !s.SecretConfigured {
|
|
return StateUninitialized
|
|
}
|
|
if !id || !s.SecretConfigured {
|
|
return StateCredentialError
|
|
}
|
|
if !s.CredentialsVerified {
|
|
return StateCredentialsSaved
|
|
}
|
|
if s.AdministratorCount < 1 {
|
|
return StateWaitingAdmin
|
|
}
|
|
return StateActive
|
|
}
|
|
|
|
type Command struct {
|
|
Name string
|
|
Args []string
|
|
}
|
|
|
|
func ParseCommand(raw string) (Command, bool) {
|
|
fields := strings.Fields(strings.TrimSpace(raw))
|
|
if len(fields) == 0 {
|
|
return Command{}, false
|
|
}
|
|
name := strings.ToLower(fields[0])
|
|
switch name {
|
|
case "/setting", "/settings", "/设置":
|
|
default:
|
|
return Command{}, false
|
|
}
|
|
return Command{Name: name, Args: fields[1:]}, true
|
|
}
|
|
|
|
type NotableLink struct{ BaseID, SheetID, ViewID string }
|
|
|
|
func ParseNotableLink(raw string) (NotableLink, error) {
|
|
if len(raw) > 4096 {
|
|
return NotableLink{}, errors.New("AI Table link is too long")
|
|
}
|
|
u, err := url.Parse(strings.TrimSpace(raw))
|
|
if err != nil || u.Scheme != "https" {
|
|
return NotableLink{}, errors.New("invalid AI Table link")
|
|
}
|
|
host := strings.ToLower(u.Hostname())
|
|
if host != "alidocs.dingtalk.com" && host != "docs.dingtalk.com" {
|
|
return NotableLink{}, errors.New("untrusted AI Table host")
|
|
}
|
|
base := path.Base(strings.TrimRight(u.Path, "/"))
|
|
q := u.Query()
|
|
result := NotableLink{BaseID: base, SheetID: q.Get("sheetId"), ViewID: q.Get("viewId")}
|
|
if result.BaseID == "" || result.BaseID == "." || result.SheetID == "" {
|
|
return NotableLink{}, errors.New("AI Table link is missing base or sheet")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
type Runtime interface {
|
|
Start(context.Context, string) error
|
|
Stop(context.Context) error
|
|
}
|
|
type Switcher struct {
|
|
runtime Runtime
|
|
active string
|
|
}
|
|
|
|
func NewSwitcher(r Runtime, active string) *Switcher { return &Switcher{runtime: r, active: active} }
|
|
func (s *Switcher) Switch(ctx context.Context, candidate string) error {
|
|
if s == nil || s.runtime == nil {
|
|
return errors.New("runtime is required")
|
|
}
|
|
candidate = strings.TrimSpace(candidate)
|
|
if candidate == "" {
|
|
return errors.New("candidate is required")
|
|
}
|
|
old := s.active
|
|
if err := s.runtime.Stop(ctx); err != nil {
|
|
return err
|
|
}
|
|
if err := s.runtime.Start(ctx, candidate); err != nil {
|
|
if old != "" {
|
|
_ = s.runtime.Start(ctx, old)
|
|
}
|
|
return err
|
|
}
|
|
s.active = candidate
|
|
return nil
|
|
}
|