temp push

This commit is contained in:
2025-12-01 13:40:58 +01:00
parent aac6db39c2
commit 4a668493c4
11 changed files with 559 additions and 116 deletions

View File

@@ -1,6 +1,8 @@
package ws
import (
"context"
"encoding/json"
"log/slog"
"sync"
"time"
@@ -13,13 +15,12 @@ type WebsocketGateway struct {
apiKey string
bodySizeBytes int64
upgrader websocket.Upgrader
modConnsMu sync.Mutex
modConns map[*websocket.Conn]struct{}
upgrader websocket.Upgrader
connsMu sync.Mutex
conns map[*websocket.Conn]connMetadata
botConnsMu sync.Mutex
botConns map[*websocket.Conn]*sync.Mutex
outgoingCh chan GatewayMessageOut
modHandler ModHandler
botHandler BotHandler
logger *slog.Logger
closefn func() error
@@ -30,11 +31,8 @@ type User struct {
Name string `json:"name"`
}
type Destination struct {
Channel string `json:"channel"`
}
type GatewayMessageIn struct {
// GatewayModMessageIn : Mod -> Gateway -> Bot
type GatewayModMessageIn struct {
MsgID string `json:"msg_id"`
Server string `json:"server"`
User User `json:"user"`
@@ -44,12 +42,41 @@ type GatewayMessageIn struct {
ReceivedAt time.Time `json:"-"` // ReceivedAt is populated by gateway (not from mod)
}
type GatewayMessageOut struct {
Type string `json:"type"` // "message"
Payload GatewayMessageIn `json:"payload"`
Destination Destination `json:"destination"`
ForwardedBy string `json:"forwarded_by"` // "ws"/"http"
ForwardedAt time.Time `json:"forwarded_at"`
// GatewayBotMessageIn : Bot -> Gateway -> Mod
type GatewayBotMessageIn struct {
MsgID string `json:"msg_id"`
ChannelID string `json:"channel_id"`
Author string `json:"author"`
Content string `json:"content"`
Meta map[string]interface{} `json:"meta,omitempty"`
Ts string `json:"ts,omitempty"`
ReceivedAt time.Time `json:"-"`
}
// ForwardedModMessage : Gateway<GatewayModMessageIn> -> Bot
type ForwardedModMessage struct {
Type string `json:"type"` // "mod"
ServerID string `json:"server_id"`
ChannelID string `json:"channel_id"`
User User `json:"user"`
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"`
}
// ForwardedBotMessage : Gateway<GatewayBotMessageIn> -> Mod
type ForwardedBotMessage struct {
Type string `json:"type"` // "bot"
ServerID string `json:"server_id"`
ChannelID string `json:"channel_id"`
Author string `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 BotAck struct {
@@ -57,3 +84,29 @@ type BotAck struct {
MsgID string `json:"msg_id"`
Status string `json:"status,omitempty"` // "queued"/"sent"
}
type MessageEnvelope 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"`
}
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
}