almost done, queue fix next

This commit is contained in:
2025-12-07 00:48:09 +01:00
parent 7a30adc2e8
commit de03c1fe3d
10 changed files with 414 additions and 319 deletions

View File

@@ -3,6 +3,7 @@ package ws
import (
"encoding/json"
"homestead/homestead_gateway/util/cache"
"homestead/homestead_gateway/util/queue"
"log/slog"
"time"
@@ -17,73 +18,43 @@ type WebsocketGateway struct {
upgrader websocket.Upgrader
cache *cache.Cache
queue *queue.Queue[GatewayMessageOut]
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 {
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
type Destination struct {
ChannelID string `json:"channel_id"`
ID string `json:"channel_id,omitempty"`
}
// GatewayModMessageIn : Mod -> Gateway -> Bot
type GatewayModMessageIn struct {
MsgID string `json:"msg_id"`
Server string `json:"server"`
Destination Destination `json:"destination"`
Author MinecraftUser `json:"author"`
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 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"`
Ts time.Time `json:"ts,omitempty"`
ReceivedAt time.Time `json:"received_at"`
ForwardedAt time.Time `json:"forwarded_at"`
}
@@ -105,11 +76,3 @@ type ModHandshake struct {
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
}