Files
HomesteadGateway/ws/structs.go
2025-12-02 10:34:45 +01:00

116 lines
3.3 KiB
Go

package ws
import (
"encoding/json"
"homestead/homestead_gateway/util/cache"
"log/slog"
"time"
"github.com/gorilla/websocket"
)
type WebsocketGateway struct {
port int
apiKey string
bodySizeBytes int64
upgrader websocket.Upgrader
cache *cache.Cache
bot *websocket.Conn
conns *cache.ConnectionCache
modHandler ModHandler
botHandler BotHandler
logger *slog.Logger
closeFn func() error
}
type MinecraftUser struct {
ID string `json:"id"`
Name string `json:"name"`
}
type DiscordUser struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Destination struct {
ChannelID string `json:"channel_id"`
}
// GatewayModMessageIn : Mod -> Gateway -> Bot
type GatewayModMessageIn struct {
MsgID string `json:"msg_id"`
Server string `json:"server"`
Destination Destination `json:"destination"`
Author MinecraftUser `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `json:"ts,omitempty"`
ReceivedAt time.Time `json:"-"` // ReceivedAt is populated by gateway (not from mod)
}
// GatewayBotMessageIn : Bot -> Gateway -> Mod
type GatewayBotMessageIn struct {
MsgID string `json:"msg_id"`
ChannelID string `json:"channel_id"`
Author DiscordUser `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `json:"ts,omitempty"`
ReceivedAt time.Time `json:"-"` // ReceivedAt is populated by gateway (not from bot)
}
// GatewayModMessageOut : Gateway<GatewayModMessageIn> -> Bot
type GatewayModMessageOut struct {
Type string `json:"type"` // "mod"
ChannelID string `json:"channel_id"`
Author MinecraftUser `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `json:"ts,omitempty"`
ReceivedAt time.Time `json:"received_at"`
ForwardedAt time.Time `json:"forwarded_at"`
}
// GatewayBotMessageOut : Gateway<GatewayBotMessageIn> -> Mod
type GatewayBotMessageOut struct {
Type string `json:"type"` // "bot"
ChannelID string `json:"channel_id"`
Author DiscordUser `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `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"`
}
type BotHandshake struct {
BotID string `json:"bot_id"`
}
type ModHandler interface {
Handle(conn *websocket.Conn, msg GatewayModMessageIn) error
}
type BotHandler interface {
Handle(conn *websocket.Conn, msg GatewayBotMessageIn) error
}