refactor: replace HTTP error codes with IRC numeric replies for all IRC commands
All checks were successful
check / check (push) Successful in 58s
All checks were successful
check / check (push) Successful in 58s
IRC commands (PRIVMSG, JOIN, PART, NICK, TOPIC, etc.) now respond with proper IRC numeric replies delivered through the message queue instead of HTTP status codes. HTTP error codes are now reserved exclusively for transport-level concerns: auth failures (401), malformed requests (400), and server errors (500). Changes: - Add params column to messages table for IRC-style parameters - Add Params field to IRCMessage struct and update all queries - Add respondIRCError helper for consistent IRC error delivery - Add RPL_WELCOME (001) on session creation and login - Add RPL_TOPIC/RPL_NOTOPIC (332/331), RPL_NAMREPLY (353), RPL_ENDOFNAMES (366) on JOIN - Add RPL_TOPIC (332) on TOPIC set - Replace HTTP 404 with ERR_NOSUCHCHANNEL (403) and ERR_NOSUCHNICK (401) - Replace HTTP 409 with ERR_NICKNAMEINUSE (433) - Replace HTTP 403 with ERR_NOTONCHANNEL (442) - Replace HTTP 400 with ERR_NEEDMOREPARAMS (461), ERR_ERRONEUSNICKNAME (432), and ERR_UNKNOWNCOMMAND (421) where appropriate - Change PRIVMSG/NOTICE success from HTTP 201 to HTTP 200 - Update all tests to verify IRC numerics in message queue - Add new tests for RPL_WELCOME and JOIN numerics - Update README to document new numeric reply behavior closes #54
This commit is contained in:
@@ -35,6 +35,7 @@ type IRCMessage struct {
|
||||
Command string `json:"command"`
|
||||
From string `json:"from,omitempty"`
|
||||
To string `json:"to,omitempty"`
|
||||
Params json.RawMessage `json:"params,omitempty"`
|
||||
Body json.RawMessage `json:"body,omitempty"`
|
||||
TS string `json:"ts"`
|
||||
Meta json.RawMessage `json:"meta,omitempty"`
|
||||
@@ -491,12 +492,17 @@ func (database *Database) GetSessionChannelIDs(
|
||||
func (database *Database) InsertMessage(
|
||||
ctx context.Context,
|
||||
command, from, target string,
|
||||
params json.RawMessage,
|
||||
body json.RawMessage,
|
||||
meta json.RawMessage,
|
||||
) (int64, string, error) {
|
||||
msgUUID := uuid.New().String()
|
||||
now := time.Now().UTC()
|
||||
|
||||
if params == nil {
|
||||
params = json.RawMessage("[]")
|
||||
}
|
||||
|
||||
if body == nil {
|
||||
body = json.RawMessage("[]")
|
||||
}
|
||||
@@ -508,10 +514,10 @@ func (database *Database) InsertMessage(
|
||||
res, err := database.conn.ExecContext(ctx,
|
||||
`INSERT INTO messages
|
||||
(uuid, command, msg_from, msg_to,
|
||||
body, meta, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)`,
|
||||
params, body, meta, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
msgUUID, command, from, target,
|
||||
string(body), string(meta), now)
|
||||
string(params), string(body), string(meta), now)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf(
|
||||
"insert message: %w", err,
|
||||
@@ -578,7 +584,7 @@ func (database *Database) PollMessages(
|
||||
rows, err := database.conn.QueryContext(ctx,
|
||||
`SELECT cq.id, m.uuid, m.command,
|
||||
m.msg_from, m.msg_to,
|
||||
m.body, m.meta, m.created_at
|
||||
m.params, m.body, m.meta, m.created_at
|
||||
FROM client_queues cq
|
||||
INNER JOIN messages m
|
||||
ON m.id = cq.message_id
|
||||
@@ -642,7 +648,7 @@ func (database *Database) queryHistory(
|
||||
if beforeID > 0 {
|
||||
rows, err := database.conn.QueryContext(ctx,
|
||||
`SELECT id, uuid, command, msg_from,
|
||||
msg_to, body, meta, created_at
|
||||
msg_to, params, body, meta, created_at
|
||||
FROM messages
|
||||
WHERE msg_to = ? AND id < ?
|
||||
AND command = 'PRIVMSG'
|
||||
@@ -659,7 +665,7 @@ func (database *Database) queryHistory(
|
||||
|
||||
rows, err := database.conn.QueryContext(ctx,
|
||||
`SELECT id, uuid, command, msg_from,
|
||||
msg_to, body, meta, created_at
|
||||
msg_to, params, body, meta, created_at
|
||||
FROM messages
|
||||
WHERE msg_to = ?
|
||||
AND command = 'PRIVMSG'
|
||||
@@ -684,16 +690,16 @@ func scanMessages(
|
||||
|
||||
for rows.Next() {
|
||||
var (
|
||||
msg IRCMessage
|
||||
qID int64
|
||||
body, meta string
|
||||
createdAt time.Time
|
||||
msg IRCMessage
|
||||
qID int64
|
||||
params, body, meta string
|
||||
createdAt time.Time
|
||||
)
|
||||
|
||||
err := rows.Scan(
|
||||
&qID, &msg.ID, &msg.Command,
|
||||
&msg.From, &msg.To,
|
||||
&body, &meta, &createdAt,
|
||||
¶ms, &body, &meta, &createdAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fallbackQID, fmt.Errorf(
|
||||
@@ -701,6 +707,10 @@ func scanMessages(
|
||||
)
|
||||
}
|
||||
|
||||
if params != "" && params != "[]" {
|
||||
msg.Params = json.RawMessage(params)
|
||||
}
|
||||
|
||||
msg.Body = json.RawMessage(body)
|
||||
msg.Meta = json.RawMessage(meta)
|
||||
msg.TS = createdAt.Format(time.RFC3339Nano)
|
||||
|
||||
Reference in New Issue
Block a user