Stores the site-wide chat's messages and each user's read marker.
The WebSocket route (routes/ws.ts) writes messages and moves read markers,
then broadcasts the result. routes/chat.ts reads the history, a page at a
time.
Checks whether message id is flagged mentionsEveryone, false when there
is no such message. The WebSocket route reads it before an edit, to notify
everyone only when the edit adds @everyone.
Stores a message from userId on a chat_send, and claims the pasted images
it links to. A replyToId naming no message is dropped, and the message is
posted as no reply. The message mentionsEveryone when it says @everyone
and userId may. The caller broadcasts the result, and notifies everyone
when it is flagged.
Deletes a message on a chat_delete. canModerate lets a moderator delete
anyone's message. Replies to it keep their replyToId, without its preview.
Returns the first limit messages after after, oldest first. The chatbox
uses it to fetch what it missed while the socket was down. limit is
clamped to 1–100.
Returns how far userId has read the chat. A user who never opened it has
read up to their registration, so the history from before it isn't unread.
Returns the latest limit messages, or with before the limit messages
just older than it, oldest first either way. limit is clamped to 1–100.
Moves userId's read marker to at, a message's created_at, on a
chat_read. It never moves back, so a lagging tab can't undo a newer tab's
read. It never passes the newest message, so a timestamp from the future
can't mark messages read before they are sent.
Replaces a message's body on a chat_edit, and claims any image the new body
links to. canModerate lets a moderator edit anyone's message. The flag
mentionsEveryone is set again from the new body and the author's role, not
the editor's: a moderator's edit can't make a user's message reach everyone.
The caller broadcasts the result.
Passed by routes/chat.ts to page through the history, from the message the
client already has at one end. Messages are ordered by createdAt, then by
id, so a page boundary between two messages sent in the same millisecond
skips neither.
-
createdAt: string
An ISO timestamp, compared as a string with
created_at. -
id: string
Without it, the cursor takes in every message at
createdAt: they are all older for getRecentChatMessages and all newer for getChatMessagesAfter.
Returned to the WebSocket route by getChatReadState and
markChatRead. The route sends it in welcome and in
chat_read_update, and the client shows unread on the chat's badge.
-
readAt: string
The
created_atof the newest message the user has read. -
unread: number
Counts only the messages of other users after
readAt.
Usage
import * as mod from "api/services/chat-service.ts";