Files
HomesteadGateway/ws/structs.go

122 lines
3.4 KiB
Go

package ws
import (
"context"
"encoding/json"
"homestead/homestead_gateway/util/cache"
"log/slog"
"sync"
"time"
"github.com/gorilla/websocket"
)
type WebsocketGateway struct {
port int
apiKey string
bodySizeBytes int64
upgrader websocket.Upgrader
connsMu sync.Mutex
conns map[*websocket.Conn]connectionMetaData
cache cache.Cache
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(ctx context.Context, msg GatewayModMessageIn) error
}
type BotHandler interface {
Handle(ctx context.Context, msg GatewayBotMessageIn) error
}
type connectionMetaData struct {
connectionType string // "mod" or "bot"
id string // server_id or bot_id for logging
}