Multi-Agent Collaboration for Team and Friends
TypeScript agent-automationagent-orchestrationagent-workflowai-agentsai-collaborationai-memoryai-workforceautonomous-agentsclaude-codeclaude-code-skillscodexcoding-agentmulti-agentmultiple-agentone-person-businessone-person-companyone-person-teamopencodesolopreneur
fix(community): restore last-opened forum post / thread on server re-entry
Generf1e702c673aa
diff --git a/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx b/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsxindex b10f42b47..65f3aa243 100644--- a/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx+++ b/src/web/src/app/c/channels/[serverId]/[channelId]/page.tsx@@ -20,7 +20,7 @@ import { canManageServer } from "@/components/community/_types"import type { MentionType } from "@alook/shared"import { isForum as isForumType, deriveThreadName, USE_SERVER_DEFAULT } from "@alook/shared"import { resolveRowPresence } from "@/lib/community/presence"-import { setLastChannel } from "@/lib/community/last-channel"+import { setLastChannel, getLastChannel, clearLastChannel } from "@/lib/community/last-channel"import { makeUserNameResolver } from "@/lib/community/display-name"import { avatarInitial } from "@/lib/community/avatar"import {@@ -482,11 +482,22 @@ function ChannelView() {.catch((e) => {useCommunityStore.getState().setCurrentChannelMeta(null)// A channel not in the server tree is either a real thread/forum-post- // (meta fetch succeeds above) or one that was just deleted out from- // under us (meta 404s here). On the delete case, don't strand the user- // on a dead URL with a misleading "thread" error — bounce to the server- // root, which forwards to the first remaining channel.- if (e instanceof ApiError && e.status === 404) {+ // (meta fetch succeeds above) or one we can't open: deleted (404) or+ // no-longer-accessible — gone private / kicked (403, the threads/:id+ // 404-vs-403 contract). Both mean "can't land here" — bounce to the+ // server root, which forwards to the first remaining channel, rather+ // than strand the user on a dead URL with a misleading "thread" error.+ // This is also what makes last-channel restore safe for a remembered+ // forum-post id (per-server memory trusts the id; validity is settled+ // here): a remembered post that was deleted/hidden degrades to default.+ if (e instanceof ApiError && (e.status === 404 || e.status === 403)) {+ // BREAK THE REDIRECT LOOP: if this dead id is the server's remembered+ // last channel, forget it FIRST — otherwise server-root re-reads it,+ // re-navigates here, bounces again… forever (this exact id was just+ // written to memory by the mount effect above). Clear only when it+ // matches, so an unrelated valid memory is never wiped. With no+ // memory, server-root falls to the default channel — one hop, no loop.+ if (getLastChannel(serverId) === channelId) clearLastChannel(serverId)router.replace(`/c/channels/${params.serverId}`)return}diff --git a/src/web/src/lib/community/last-channel.test.ts b/src/web/src/lib/community/last-channel.test.tsindex 2c6f132e4..07f40411f 100644--- a/src/web/src/lib/community/last-channel.test.ts+++ b/src/web/src/lib/community/last-channel.test.ts@@ -3,6 +3,7 @@ import {lastChannelKey,getLastChannel,setLastChannel,+ clearLastChannel,pickServerLandingChannel,} from "./last-channel"@@ -70,24 +71,56 @@ describe("last-channel", () => {expect(getLastChannel("srv_1")).toBeNull()expect(() => setLastChannel("srv_1", "ch_1")).not.toThrow()})++ it("clearLastChannel forgets the remembered id (redirect-loop breaker)", () => {+ setLastChannel("srv_1", "post_dead")+ setLastChannel("srv_2", "ch_keep")+ clearLastChannel("srv_1")+ expect(getLastChannel("srv_1")).toBeNull() // dead id gone → next landing picks default+ expect(getLastChannel("srv_2")).toBe("ch_keep") // other servers untouched+ })++ it("clearLastChannel swallows a throwing removeItem and is a no-op under SSR", () => {+ vi.stubGlobal("localStorage", {+ getItem: vi.fn(() => null),+ setItem: vi.fn(),+ removeItem: vi.fn(() => {+ throw new Error("SecurityError")+ }),+ })+ expect(() => clearLastChannel("srv_1")).not.toThrow()+ vi.stubGlobal("window", undefined)+ expect(() => clearLastChannel("srv_1")).not.toThrow()+ })})describe("pickServerLandingChannel", () => {- it("restores the remembered last channel when it still exists", () => {+ it("restores the remembered last channel (returned directly, not validated against the list)", () => {expect(pickServerLandingChannel(["ch_1", "ch_2", "ch_3"], "ch_2")).toBe("ch_2")})- it("falls back to the first channel when there is no memory", () => {+ it("restores a remembered id that is NOT in the top-level list — a forum post / thread (the bug this fixes)", () => {+ // A post/thread is a child channel: its id never appears in the top-level+ // list, so the old `includes` gate dropped it and always fell to default.+ // Now it's returned directly; the destination page renders the post opener.+ expect(pickServerLandingChannel(["ch_1", "ch_2"], "post_abc")).toBe("post_abc")+ })++ it("falls back to the first top-level channel when there is no memory", () => {expect(pickServerLandingChannel(["ch_1", "ch_2"], null)).toBe("ch_1")})- it("falls back to the first channel when the remembered id is gone (deleted / no access)", () => {- // The graceful fallback: a stale id simply isn't in the visible list.- expect(pickServerLandingChannel(["ch_1", "ch_2"], "ch_deleted")).toBe("ch_1")+ it("returns a dirty/garbage remembered id as-is — validity is the destination's job (its meta fetch 404/403 → bounce to default)", () => {+ // "Trust the id" ≠ "trust the id is valid". A localStorage value hand-edited+ // or written stale by another tab still navigates; the destination page's+ // 404||403 bounce degrades it to default (Blondie/Melly's dirty-last check).+ expect(pickServerLandingChannel(["ch_1", "ch_2"], "garbage_id")).toBe("garbage_id")})- it("returns undefined when the server has no channels", () => {- expect(pickServerLandingChannel([], "ch_1")).toBeUndefined()+ it("returns undefined only when there's no memory AND no channels; a remembered id wins even over an empty list", () => {expect(pickServerLandingChannel([], null)).toBeUndefined()+ // With memory, the id is returned even if the top-level list is empty (the+ // remembered channel may be a child channel under a forum, not top-level).+ expect(pickServerLandingChannel([], "post_abc")).toBe("post_abc")})})diff --git a/src/web/src/lib/community/last-channel.ts b/src/web/src/lib/community/last-channel.tsindex 8ef8949df..6c9e9b9ee 100644--- a/src/web/src/lib/community/last-channel.ts+++ b/src/web/src/lib/community/last-channel.ts@@ -35,19 +35,46 @@ export function setLastChannel(serverId: string, channelId: string): void {}}+/**+ * Forget the remembered channel for a server. Called when the remembered id+ * turns out to be unopenable (deleted / no-access / garbage) so the next+ * server-landing has no memory and picks the default — WITHOUT this, a+ * server-root → dead-id → bounce-to-server-root cycle re-reads the same dead id+ * forever (the redirect loop this breaks). Same SSR / privacy-mode guard as the+ * setters; a failure just means the stale id lingers, never a throw.+ */+export function clearLastChannel(serverId: string): void {+ if (typeof window === "undefined") return+ try {+ localStorage.removeItem(lastChannelKey(serverId))+ } catch {+ /* best-effort — see setLastChannel */+ }+}+/*** Pick which channel the server-landing page should redirect to: the remembered- * `last` channel when it still exists among the server's currently-visible- * `channelIds` (a deleted / no-longer-accessible id isn't in the list → falls- * through — this IS the graceful fallback, no error path needed), else the- * first channel by position. `undefined` when the server has no channels.- * Extracted pure so the selection rule is unit-tested without a page-render- * harness.+ * `last` channel when there is one, else the first top-level channel by position.+ * `undefined` when the server has no channels and there's no memory.+ *+ * `last` is trusted and returned directly — it is NOT validated against+ * `channelIds`. `channelIds` (the server's TOP-LEVEL channels) is only the+ * source for the DEFAULT when there's no memory. This is deliberate: a+ * remembered channel may be a FORUM POST or THREAD (a child channel — its id+ * lives under a forum/parent, never in the top-level list), so validating `last`+ * against the top-level list would drop every remembered post/thread and always+ * fall back to default (the bug this fixes). Instead the landing page navigates+ * to `last` and the destination `[channelId]` page validates it: a real+ * child-channel renders its opener view; a deleted / no-longer-accessible /+ * outright-garbage id fails its meta fetch and bounces back to default (see the+ * 404||403 bounce in that page). "Trust the id" ≠ "trust the id is valid" —+ * validity is the destination's job, not this selector's. Extracted pure so the+ * rule is unit-tested without a page-render harness.*/export function pickServerLandingChannel(channelIds: readonly string[],last: string | null,): string | undefined {- if (last !== null && channelIds.includes(last)) return last+ if (last !== null) return lastreturn channelIds[0]}