Files
HomesteadGateway/ws/structs.go
2025-12-07 17:11:37 +01:00

111 lines
2.6 KiB
Go

package ws
import (
"encoding/json"
"log/slog"
"sync"
"time"
"github.com/gorilla/websocket"
)
type WebsocketGateway struct {
port int
apiKey string
bodySizeBytes int64
upgrader websocket.Upgrader
registry *Registry
logger *slog.Logger
closeFn func() error
}
//
type Registry struct {
mu sync.RWMutex
entries map[string]*ChannelEntry
queueCap int
botMu sync.Mutex
bot *ConnWrapper
}
type ConnWrapper struct {
Conn *websocket.Conn
ServerID string // set for mods (the server_id)
LastSeen time.Time
}
type BoundedQueue struct {
mu sync.Mutex
buf []GatewayMessageOut
start int
length int
capacity int
}
type ChannelEntry struct {
mu sync.Mutex
Channel string
Mod *ConnWrapper
Queue *BoundedQueue
}
//
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Destination struct {
ID string `json:"channel_id,omitempty"`
}
type GatewayMessageIn struct {
Type string
ID string `json:"id"` // where am I from (channel_id or server_id)
MsgID string `json:"msg_id"` // msg id
Destination Destination `json:"destination,omitempty"` // where do I wanna go (channel_id or empty if from Bot)
Author User `json:"author"` // who sent the message
Content string `json:"content"` // message content
Meta map[string]interface{} `json:"meta,omitempty"` // additional metadata
Ts time.Time `json:"ts,omitempty"` // timestamp
ReceivedAt time.Time `json:"-"` // ReceivedAt is populated by gateway (not from mod)
}
type GatewayMessageOut struct {
Type string `json:"type"` // "mod"|"bot"
ID string `json:"channel_id,omitempty"` // message.Destination.ID
Author User `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts time.Time `json:"ts,omitempty"`
ReceivedAt time.Time `json:"received_at"`
ForwardedAt time.Time `json:"forwarded_at"`
}
type GatewayAck struct {
Status string `json:"status"`
Type string `json:"type"`
}
//
type Handshake struct {
Type string `json:"type"` // "mod" or "bot"
Data json.RawMessage `json:"data"`
}
type ModHandshake struct {
ServerID string `json:"server_id"`
ChannelID string `json:"channel_id"`
}
type BotHandshake struct {
ChannelId string `json:"channel_id"`
}