72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package ws
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
type LoggingModHandler struct {
|
|
logger *slog.Logger
|
|
}
|
|
|
|
type LoggingBotHandler struct {
|
|
logger *slog.Logger
|
|
}
|
|
|
|
func NewLoggingModHandler(logger *slog.Logger) *LoggingModHandler {
|
|
return &LoggingModHandler{logger: logger}
|
|
}
|
|
|
|
func NewLoggingBotHandler(logger *slog.Logger) *LoggingBotHandler {
|
|
return &LoggingBotHandler{logger: logger}
|
|
}
|
|
|
|
func (h *LoggingModHandler) Handle(conn *websocket.Conn, msg GatewayModMessageIn) error {
|
|
// For now, just log and pretend it's being forwarded
|
|
// TODO: Look up channel_id from database using server
|
|
// TODO: Forward to bot connection(s)
|
|
|
|
fwd := GatewayModMessageOut{
|
|
Type: "mod",
|
|
ChannelID: "TODO", // will come from database lookup
|
|
Author: msg.Author,
|
|
Content: msg.Content,
|
|
Meta: msg.Meta,
|
|
Ts: msg.Ts,
|
|
ReceivedAt: msg.ReceivedAt,
|
|
ForwardedAt: time.Now().UTC(),
|
|
}
|
|
|
|
b, _ := json.Marshal(fwd)
|
|
h.logger.Info("received mod message", "msg_id", msg.MsgID, "server", msg.Server, "Author", msg.Author.Name, "content", msg.Content)
|
|
h.logger.Debug("forwarding mod message", "msg_id", msg.MsgID, "server", msg.Server, "payload", string(b))
|
|
|
|
return nil
|
|
}
|
|
|
|
func (h *LoggingBotHandler) Handle(conn *websocket.Conn, msg GatewayBotMessageIn) error {
|
|
// For now, just log and pretend it's being forwarded
|
|
// TODO: Look up server_id from database using channel_id
|
|
// TODO: Forward to mod connection(s)
|
|
|
|
fwd := GatewayBotMessageOut{
|
|
Type: "bot",
|
|
ChannelID: msg.ChannelID,
|
|
Author: msg.Author,
|
|
Content: msg.Content,
|
|
Meta: msg.Meta,
|
|
Ts: msg.Ts,
|
|
ReceivedAt: msg.ReceivedAt,
|
|
ForwardedAt: time.Now().UTC(),
|
|
}
|
|
|
|
b, _ := json.Marshal(fwd)
|
|
h.logger.Info("received bot message", "msg_id", msg.MsgID, "channel", msg.ChannelID, "author", msg.Author, "content", msg.Content)
|
|
h.logger.Debug("forwarding bot message", "msg_id", msg.MsgID, "channel", msg.ChannelID, "payload", string(b))
|
|
|
|
return nil
|
|
}
|