fix: replay channel state on SPA reconnect (#61)
All checks were successful
check / check (push) Successful in 4s
All checks were successful
check / check (push) Successful in 4s
## Summary When closing and reopening the SPA, channel tabs were not restored because the client relied on localStorage to remember joined channels and re-sent JOIN commands on reconnect. This was fragile and caused spurious JOIN broadcasts to other channel members. ## Changes ### Server (`internal/handlers/api.go`, `internal/handlers/auth.go`) - **`replayChannelState()`** — new method that enqueues synthetic JOIN messages plus join-numerics (332 TOPIC, 353 NAMES, 366 ENDOFNAMES) for every channel the session belongs to, targeted only at the specified client (no broadcast to other users). - **`HandleState`** — accepts `?replay=1` query parameter to trigger channel state replay when the SPA reconnects. - **`handleLogin`** — also calls `replayChannelState` after password-based login, since `LoginUser` creates a new client for an existing session. ### SPA (`web/src/app.jsx`, `web/dist/app.js`) - On resume, calls `/state?replay=1` instead of `/state` so the server enqueues channel state into the message queue. - `processMessage` now creates channel tabs when receiving a JOIN where `msg.from` matches the current nick (handles both live joins and replayed joins on reconnect). - `onLogin` no longer re-sends JOIN commands for saved channels on resume — the server handles it via the replay mechanism, avoiding spurious JOIN broadcasts. ## How It Works 1. SPA loads, finds saved token in localStorage 2. Calls `GET /api/v1/state?replay=1` — server validates token and enqueues synthetic JOIN + TOPIC + NAMES for all session channels into the client's queue 3. `onLogin(nick, true)` sets `loggedIn = true` and requests MOTD (no re-JOIN needed) 4. Poll loop starts, picks up replayed channel messages 5. `processMessage` handles the JOIN messages, creating tabs and refreshing members/topics naturally closes #60 Co-authored-by: user <user@Mac.lan guest wan> Co-authored-by: clawbot <clawbot@noreply.git.eeqj.de> Co-authored-by: Jeffrey Paul <sneak@noreply.example.org> Reviewed-on: #61 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
This commit was merged in pull request #61.
This commit is contained in:
@@ -70,7 +70,7 @@ function LoginScreen({ onLogin }) {
|
||||
.catch(() => {});
|
||||
const saved = localStorage.getItem("neoirc_token");
|
||||
if (saved) {
|
||||
api("/state")
|
||||
api("/state?initChannelState=1")
|
||||
.then((u) => onLogin(u.nick, true))
|
||||
.catch(() => localStorage.removeItem("neoirc_token"));
|
||||
}
|
||||
@@ -333,7 +333,24 @@ function App() {
|
||||
case "JOIN": {
|
||||
const text = `${msg.from} has joined ${msg.to}`;
|
||||
if (msg.to) addMessage(msg.to, { ...base, text, system: true });
|
||||
if (msg.to && msg.to.startsWith("#")) refreshMembers(msg.to);
|
||||
if (msg.to && msg.to.startsWith("#")) {
|
||||
// Create a tab when the current user joins a channel
|
||||
// (including JOINs from initChannelState on reconnect).
|
||||
if (msg.from === nickRef.current) {
|
||||
setTabs((prev) => {
|
||||
if (
|
||||
prev.find(
|
||||
(t) => t.type === "channel" && t.name === msg.to,
|
||||
)
|
||||
)
|
||||
return prev;
|
||||
|
||||
return [...prev, { type: "channel", name: msg.to }];
|
||||
});
|
||||
}
|
||||
|
||||
refreshMembers(msg.to);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -636,9 +653,13 @@ function App() {
|
||||
setLoggedIn(true);
|
||||
addSystemMessage("Server", `Connected as ${userNick}`);
|
||||
|
||||
// Request MOTD on resumed sessions (new sessions get
|
||||
// it automatically from the server during creation).
|
||||
if (isResumed) {
|
||||
// Request MOTD on resumed sessions (new sessions
|
||||
// get it automatically from the server during
|
||||
// creation). Channel state is initialized by the
|
||||
// server via the message queue
|
||||
// (?initChannelState=1), so we do not need to
|
||||
// re-JOIN channels here.
|
||||
try {
|
||||
await api("/messages", {
|
||||
method: "POST",
|
||||
@@ -647,8 +668,11 @@ function App() {
|
||||
} catch (e) {
|
||||
// MOTD is non-critical.
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Fresh session — join any previously saved channels.
|
||||
const saved = JSON.parse(
|
||||
localStorage.getItem("neoirc_channels") || "[]",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user