re-formatted, better structure

This commit is contained in:
2025-12-01 18:09:10 +01:00
parent 4a668493c4
commit 46aef47e21
9 changed files with 328 additions and 235 deletions

View File

@@ -3,6 +3,7 @@ package ws
import (
"context"
"encoding/json"
"homestead/homestead_gateway/util/cache"
"log/slog"
"sync"
"time"
@@ -17,48 +18,58 @@ type WebsocketGateway struct {
upgrader websocket.Upgrader
connsMu sync.Mutex
conns map[*websocket.Conn]connMetadata
conns map[*websocket.Conn]connectionMetaData
cache cache.Cache
modHandler ModHandler
botHandler BotHandler
logger *slog.Logger
closefn func() error
closeFn func() error
}
type User struct {
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"`
User User `json:"user"`
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)
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 string `json:"author"`
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 time.Time `json:"-"` // ReceivedAt is populated by gateway (not from bot)
}
// ForwardedModMessage : Gateway<GatewayModMessageIn> -> Bot
type ForwardedModMessage struct {
// GatewayModMessageOut : Gateway<GatewayModMessageIn> -> Bot
type GatewayModMessageOut struct {
Type string `json:"type"` // "mod"
ServerID string `json:"server_id"`
ChannelID string `json:"channel_id"`
User User `json:"user"`
Author MinecraftUser `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `json:"ts,omitempty"`
@@ -66,12 +77,11 @@ type ForwardedModMessage struct {
ForwardedAt time.Time `json:"forwarded_at"`
}
// ForwardedBotMessage : Gateway<GatewayBotMessageIn> -> Mod
type ForwardedBotMessage struct {
// GatewayBotMessageOut : Gateway<GatewayBotMessageIn> -> Mod
type GatewayBotMessageOut struct {
Type string `json:"type"` // "bot"
ServerID string `json:"server_id"`
ChannelID string `json:"channel_id"`
Author string `json:"author"`
Author DiscordUser `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `json:"ts,omitempty"`
@@ -79,25 +89,16 @@ type ForwardedBotMessage struct {
ForwardedAt time.Time `json:"forwarded_at"`
}
type BotAck struct {
Type string `json:"type"` // "acknowledge"
MsgID string `json:"msg_id"`
Status string `json:"status,omitempty"` // "queued"/"sent"
type GatewayAck struct {
Status string `json:"status"`
Type string `json:"type"`
}
type MessageEnvelope struct {
type Handshake struct {
Type string `json:"type"` // "mod" or "bot"
Data json.RawMessage `json:"data"`
}
type ModHandler interface {
Handle(ctx context.Context, msg GatewayModMessageIn) error
}
type BotHandler interface {
Handle(ctx context.Context, msg GatewayBotMessageIn) error
}
type ModHandshake struct {
ServerID string `json:"server_id"`
}
@@ -106,7 +107,15 @@ type BotHandshake struct {
BotID string `json:"bot_id"`
}
type connMetadata struct {
connType string // "mod" or "bot"
id string // server_id or bot_id for logging
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
}