Documentation
¶
Overview ¶
Package protocol hand-ports the NG Chat wire types from the server's Zod schemas (ngchat repo, src/shared/protocol). The server validates client messages with strict schemas — unknown fields are rejected — so the structs in this file must stay exactly in sync with the server. Server messages are decoded tolerantly instead (see server.go), so server-side additions never break this client.
Index ¶
- func Decode(data []byte) any
- func FrameName(data []byte) string
- type Authenticate
- type Authenticated
- type Away
- type ChannelID
- type ChannelUser
- type ChatMessage
- type Error
- type GetChannelID
- type IdleTimeout
- type Kicked
- type Message
- type Notification
- type Ping
- type PingData
- type Pong
- type Reauthenticate
- type Revalidate
- type Revalidated
- type Subscribe
- type Subscribed
- type Typing
- type TypingEvent
- type Unauthorized
- type Unknown
- type Unsubscribe
- type Unsubscribed
- type UserJoined
- type UserLeft
- type UserUpdated
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Authenticate ¶
Authenticate must be the first message sent on a new socket. Only Authenticate and Ping are accepted pre-auth; the server closes unauthenticated sockets after 15s.
func NewAuthenticate ¶
func NewAuthenticate(token string) Authenticate
NewAuthenticate builds an authenticate message for the given chat JWT.
type Authenticated ¶
type Authenticated struct {
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsSiteMod bool `json:"isSiteMod"`
MOTD json.RawMessage `json:"motd"`
ServerTime int64 `json:"serverTime"`
UserID int `json:"userID"`
Username string `json:"username"`
}
Authenticated confirms a successful authenticate and describes the user. Upstream's clientHash (a bundle-staleness marker for the web SPA, optional since 18.7.0 and slated for removal) is deliberately absent: a CLI has no bundle to compare it against.
func (Authenticated) MOTDText ¶
func (a Authenticated) MOTDText() string
MOTDText returns the message of the day when it is a plain string, otherwise "".
type Away ¶
type Away struct {
ChannelID int `json:"channelID"`
UserID int `json:"userID"`
Username string `json:"username"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsAway bool `json:"isAway"`
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
ServerTime int64 `json:"serverTime"`
}
Away reports a user going away (IsAway true, with a message) or coming back (IsAway false). It patches the roster entry; it also appears in the backfill buffer, where the DB copy omits isAway entirely, so backfilled ones are ambiguous and are not replayed.
type ChannelUser ¶
type ChannelUser struct {
UserID int `json:"userID"`
Username string `json:"username"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsAway bool `json:"isAway"`
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
}
ChannelUser is one roster entry. AwayMessage is server-rendered HTML, like message bodies; AwayMessageRaw is the text the user typed.
type ChatMessage ¶
type ChatMessage struct {
Name string `json:"name"`
ChannelID int `json:"channelID"`
Message string `json:"message"`
}
ChatMessage sends a chat line (1..5000 chars) to a subscribed channel. Slash commands (/me, /dm, ...) are plain message text parsed server-side.
func NewChatMessage ¶
func NewChatMessage(channelID int, text string) ChatMessage
NewChatMessage builds a message send for a channel.
type Error ¶
type Error struct {
Message string `json:"message"`
RedirectChannel string `json:"redirectChannel"`
}
Error is a generic request failure; RedirectChannel is set when a channel lookup failed and the server suggests another channel.
type GetChannelID ¶
GetChannelID resolves a channel name to its numeric ID. Unknown names come back as an Error with a RedirectChannel.
func NewGetChannelID ¶
func NewGetChannelID(channelName string) GetChannelID
NewGetChannelID builds a channel-name lookup. Names are lowercase on the server; callers should lowercase before sending.
type IdleTimeout ¶
IdleTimeout precedes a disconnect for 24h without chatting; the close reason "idle timeout" must not trigger auto-reconnect.
type Kicked ¶
type Kicked struct {
ChannelID int `json:"channelID"`
KickedByUserID int `json:"kickedByUserID"`
Reason string `json:"reason"`
ServerTime int64 `json:"serverTime"`
}
Kicked precedes a server-initiated disconnect of this user. Reason is optional on the wire; KickedByUserID is 0 for a system kick.
type Message ¶
type Message struct {
Name string `json:"name"`
ChannelID int `json:"channelID"`
ID *int64 `json:"id"`
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
Mentions []string `json:"mentions"`
IsSpoiler bool `json:"isSpoiler"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
UserIcon string `json:"userIcon"`
UserID int `json:"userID"`
Username string `json:"username"`
UserPageURL string `json:"userPageURL"`
ServerTime int64 `json:"serverTime"`
}
Message is a chat line. It also covers the meMessage, slapMessage, serverMessage and directMessage variants, which share its shape; check Name to tell them apart. Message is server-rendered HTML; MessageRaw is the sender's original text.
type Notification ¶
type Notification struct {
Message string `json:"message"`
MessageRaw string `json:"messageRaw"`
MessageType string `json:"messageType"`
ServerTime int64 `json:"serverTime"`
UserID int `json:"userID"`
Username string `json:"username"`
}
Notification is one inbox row from Subscribed: a mention or DM that reached the user while away. MessageType is the server's notification category, not the frame name: "message", "meMessage" (which also covers a mention inside an away message), "directMessage", "modDirectMessage" or "serverMessage". Message is rendered HTML.
type Ping ¶
Ping is the application-level heartbeat. The server terminates any socket with no inbound traffic for 15 seconds, so clients must ping continuously (the browser client uses a 1-5s cadence).
type PingData ¶
type PingData struct {
Time int64 `json:"time"`
}
PingData carries the client timestamp echoed back in Pong.
type Pong ¶
type Pong struct {
Data PingData `json:"data"`
}
Pong answers Ping, echoing the client timestamp.
type Reauthenticate ¶
Reauthenticate answers a server Revalidate with a freshly minted chat JWT. It is deliberately not Authenticate: the server replaces the socket's claim snapshot in place instead of re-running the join/subscribe/motd pipeline, and Authenticate's already-authenticated guard stays. The server refuses a token for a different account or one whose expiry is not strictly later than the token in force, and caps attempts per token (~5), so send at most one per Revalidate.
func NewReauthenticate ¶
func NewReauthenticate(token string) Reauthenticate
NewReauthenticate builds the in-place renewal message.
type Revalidate ¶
type Revalidate struct {
ServerTime int64 `json:"serverTime"`
}
Revalidate is the server's nudge shortly before the chat JWT's hard close deadline. Answering with Reauthenticate carrying a fresh token swaps the socket's claims in place; ignoring it leaves the unchanged close-and-reconnect path, since the deadline never moves.
type Revalidated ¶
type Revalidated struct {
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsSiteMod bool `json:"isSiteMod"`
ServerTime int64 `json:"serverTime"`
}
Revalidated acknowledges a successful Reauthenticate. The privilege flags come from the new token and replace those from Authenticated, so a mod demoted on the site loses the flags without reconnecting.
type Subscribe ¶
Subscribe joins a channel by ID; the reply is Subscribed with the backfill buffer and user list.
func NewSubscribe ¶
NewSubscribe builds a channel subscription.
type Subscribed ¶
type Subscribed struct {
ChannelID int `json:"channelID"`
ChannelName string `json:"channelName"`
MessageBuffer []json.RawMessage `json:"messageBuffer"`
UserList []ChannelUser `json:"userList"`
Notifications []Notification `json:"notifications"`
ServerTime int64 `json:"serverTime"`
}
Subscribed answers Subscribe. MessageBuffer holds up to 25 recent events (same envelopes as live ones — decode each with Decode); it is the only history a client ever receives. UserList is the full roster at subscribe time, the baseline that UserJoined, UserLeft, UserUpdated and Away then patch. Notifications are the server's persistent inbox of mentions and DMs received while away, newest first, up to 100; the server never clears it, so every subscribe carries the same rows again.
type TypingEvent ¶
type TypingEvent struct {
ChannelID int `json:"channelID"`
UserID int `json:"userID"`
Username string `json:"username"`
}
TypingEvent reports another user composing in a channel.
type Unauthorized ¶
type Unauthorized struct {
}
Unauthorized reports an auth failure. "jwt expired"/"jwt malformed" are soft failures: re-mint and re-send Authenticate on the same socket. ChannelID is set only when a subscribe was refused (channel ban, alt of a banned account); the socket stays open in that case, so the client must decide for itself that there is nothing left to do.
type Unknown ¶
type Unknown struct {
Name string
}
Unknown is any frame this client does not (yet) understand, including known names whose payload failed to decode. Callers must treat it as ignorable — that tolerance is what lets old binaries survive protocol additions (ADR 0002).
type Unsubscribe ¶
Unsubscribe leaves a channel.
func NewUnsubscribe ¶
func NewUnsubscribe(channelID int) Unsubscribe
NewUnsubscribe builds a channel unsubscription.
type Unsubscribed ¶
type Unsubscribed struct {
ChannelID int `json:"channelID"`
}
Unsubscribed answers Unsubscribe.
type UserJoined ¶
type UserJoined struct {
ChannelID int `json:"channelID"`
UserID int `json:"userID"`
Username string `json:"username"`
IsAdmin bool `json:"isAdmin"`
IsChatMod bool `json:"isChatMod"`
IsAway bool `json:"isAway"`
AwayMessage string `json:"awayMessage"`
AwayMessageRaw string `json:"awayMessageRaw"`
UserIcon string `json:"userIcon"`
UserPageURL string `json:"userPageURL"`
ServerTime int64 `json:"serverTime"`
}
UserJoined reports a user entering a channel.
type UserLeft ¶
type UserLeft struct {
ChannelID int `json:"channelID"`
UserID int `json:"userID"`
Username string `json:"username"`
ServerTime int64 `json:"serverTime"`
}
UserLeft reports a user leaving a channel.
type UserUpdated ¶
type UserUpdated UserJoined
UserUpdated is a silent roster patch: the same shape as UserJoined but for a user already in the channel (a mid-session role or away change, typically published by an in-place token renewal). Consumers must not announce it as a join; the UI's user list applies it as a patch.