Skip to content

Commit e607882

Browse files
matt2eclaude
andcommitted
fix(links): look up a public channel the viewer left before previewing its message
After the viewer joined and then left a public channel, hovering a link to one of its messages failed. Discovery denies the old admission until a fresh lookup confirms the channel is still open, but describeReference answered `found` from its cached open metadata. lookUpReference skipped found answers, and useChannelReference only demanded a lookup while an answer wasn't found, so no lookup or read ever ran. The preview showed "Couldn't load this preview." on every hover, while a click, which resolves the channel, opened the message. Admission now decides. A channel named only by its open metadata answers `found` with `readable: false`, and `looking: true` while its lookup is queued or in flight, so a link keeps the name. lookUpReference skips a channel only when discovery admits it. useChannelReference demands a lookup until the answer is readable. A lookup that still admits nothing is kept in `withheld` for the usual five minutes, as a withheld answer is. BuzzLinkPreview keys its read on readability instead of `found`, so it reads again when the lookup admits the channel, even though the metadata is unchanged. It waits while that lookup is on the way. A channel-only link to a left public channel now runs one background lookup too. Once admitted, its hover card can show the member count, as it already does for an unjoined channel that has been resolved. The store test that expected no lookup for a left channel now covers leave, refer, the found-and-looking answer, and readable recovery. A preview test covers a left channel that the lookup admits again. Both fail on the previous commit. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent f7db65b commit e607882

6 files changed

Lines changed: 87 additions & 31 deletions

File tree

‎src/features/conversation/BuzzLinkPreview.test.tsx‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ function setup(
7070
let described = reference;
7171
const listListeners = new Set<() => void>();
7272
// As in the store, a demanded lookup is on the way at once, and its batch
73-
// tells list subscribers from a microtask.
73+
// tells list subscribers from a microtask. A found channel keeps its name.
7474
const refer = vi.fn(() => {
75-
described = { state: "looking" };
75+
described =
76+
described?.state === "found"
77+
? { ...described, looking: true }
78+
: { state: "looking" };
7679
queueMicrotask(() => {
7780
for (const listener of listListeners) listener();
7881
});
@@ -272,6 +275,26 @@ it("waits for the lookup a ready list starts, then reads again", async () => {
272275
h.views[1]?.publish({ ...base, status: "ready", root });
273276
expect(h.tree.textContent).toContain("reconciled body");
274277
});
278+
it("looks up a channel the viewer left before trusting a refused read, then reads again", async () => {
279+
// Discovery stopped admitting it, though its open metadata still names it.
280+
const h = setup([purged, base], root.id, undefined, {
281+
...(open as Extract<ChannelReference, { state: "found" }>),
282+
readable: false,
283+
});
284+
await act(async () => {});
285+
expect(h.refer).toHaveBeenCalledOnce();
286+
expect(text(h.tree)).toBe("Loading message…");
287+
expect(h.thread).toHaveBeenCalledOnce();
288+
// The lookup admits it again with the same metadata.
289+
h.answer(open);
290+
expect(h.thread).toHaveBeenCalledTimes(2);
291+
expect(h.views[0]?.view.dispose).toHaveBeenCalledOnce();
292+
expect(h.views[1]?.view.refresh).toHaveBeenCalledOnce();
293+
expect(text(h.tree)).toBe("Loading message…");
294+
h.views[1]?.publish({ ...base, status: "ready", root });
295+
expect(h.tree.textContent).toContain("reconciled body");
296+
expect(h.refer).toHaveBeenCalledOnce();
297+
});
275298
it("keeps loading while a read is genuinely in flight", () => {
276299
const loading = setup({
277300
...base,

‎src/features/conversation/BuzzLinkPreview.tsx‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const loading = <span role="status">Loading message…</span>;
3333

3434
/** Mounted only while a preview is open. The session owns access, edits and
3535
* deletion. Like a click, the card looks the channel up before trusting a
36-
* refused read: an unjoined public channel is readable once found. */
36+
* refused read: an unjoined public channel is readable once found, and one
37+
* the viewer left once a lookup confirms it is still open. */
3738
export function BuzzLinkPreview({
3839
session,
3940
channelId,
@@ -49,28 +50,28 @@ export function BuzzLinkPreview({
4950
() => session.channels.list().status === "loading",
5051
() => false,
5152
);
52-
const found = reference.state === "found";
53+
const readable = reference.state === "found" && reference.readable !== false;
5354
const withheld = reference.state === "withheld";
54-
// Finding the channel starts a fresh read, and a read from before it is
55-
// never shown; a withheld channel is never read.
55+
// A channel becoming readable starts a fresh read, and a read from before
56+
// it is never shown; a withheld channel is never read.
5657
const [read, setRead] = useState<{
5758
view: ThreadView | undefined;
58-
found: boolean;
59+
readable: boolean;
5960
}>();
6061
useEffect(() => {
6162
if (withheld) return;
6263
try {
6364
const owned = session.thread(channelId, messageId, { exact: true });
64-
setRead({ view: owned, found });
65+
setRead({ view: owned, readable });
6566
void owned.refresh();
6667
return () => owned.dispose();
6768
} catch {
68-
setRead({ view: undefined, found });
69+
setRead({ view: undefined, readable });
6970
}
70-
}, [session, channelId, messageId, found, withheld]);
71+
}, [session, channelId, messageId, readable, withheld]);
7172
const destination = namedChannel(reference);
7273
if (withheld) return <PreviewFailure reason="withheld" />;
73-
if (read?.found !== found) return loading;
74+
if (read?.readable !== readable) return loading;
7475
if (!read.view)
7576
return <PreviewFailure reason="unavailable" channel={destination} />;
7677
return (
@@ -83,7 +84,11 @@ export function BuzzLinkPreview({
8384
// discovery is still loading the list, which then either looks the
8485
// channel up or errors. Otherwise (the list errored, a failed lookup is
8586
// backing off, or the store is cached-only), the read's answer stands.
86-
looking={reference.state === "looking" || listLoading}
87+
looking={
88+
reference.state === "looking" ||
89+
(reference.state === "found" && !!reference.looking) ||
90+
listLoading
91+
}
8792
/>
8893
);
8994
}

‎src/features/conversation/channel-reference.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ const noop = () => () => {};
77
/**
88
* What a link can show about a channel the reader may not have joined. The
99
* store owns the lookup, its batching, expiry and retry; this hook only
10-
* demands one while the answer isn't `found` and the list is ready, because
11-
* discovery owns access.
10+
* demands one while the list is ready and the answer isn't a readable
11+
* `found`, because discovery owns access.
1212
*/
1313
export function useChannelReference(
1414
queries: ChannelQueries | undefined,
@@ -32,12 +32,12 @@ export function useChannelReference(
3232
);
3333
// Mounted links hold the demand, so the store rechecks them when an answer
3434
// expires or a failure's backoff ends. Moving between answers that aren't
35-
// `found` keeps the same demand.
36-
const found = reference.state === "found";
35+
// readable keeps the same demand.
36+
const readable = reference.state === "found" && reference.readable !== false;
3737
useEffect(() => {
38-
if (active && channelId && ready && !found)
38+
if (active && channelId && ready && !readable)
3939
return active.refer?.(channelId);
40-
}, [active, channelId, ready, found]);
40+
}, [active, channelId, ready, readable]);
4141
return reference;
4242
}
4343

‎src/features/relay/channel-references.test.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ it("never reports a failed lookup as withheld, and backs off before retrying", a
167167
expect(h.channels.describe?.(SECRET)).toEqual({ state: "withheld" });
168168
});
169169

170-
it("describes a public channel the viewer left as public and not joined", async () => {
170+
it("names a public channel the viewer left, and looks it up until it is readable again", async () => {
171171
const h = await setup();
172172
h.emit([roster(relay, MINE, [], 1_700_000_001)]);
173173
await vi.waitFor(() => expect(h.channels.get?.(MINE)).toBeUndefined());
@@ -176,9 +176,26 @@ it("describes a public channel the viewer left as public and not joined", async
176176
name: "mine",
177177
private: false,
178178
joined: false,
179+
readable: false,
179180
});
181+
// Its open metadata still names it, but discovery no longer admits reading
182+
// it, so a mounted link demands a lookup and keeps the name meanwhile.
183+
const link = renderHook(() => useChannelReference(h.channels, MINE));
184+
await flush();
185+
expect(link.result.current).toMatchObject({
186+
state: "found",
187+
name: "mine",
188+
readable: false,
189+
looking: true,
190+
});
191+
await h.lookup([MINE], [open(MINE, "mine")]);
192+
expect(h.channels.get?.(MINE)).toMatchObject({ readOnly: true });
193+
expect(link.result.current).toMatchObject({ state: "found", name: "mine" });
194+
expect(link.result.current).not.toHaveProperty("readable");
195+
expect(link.result.current).not.toHaveProperty("looking");
196+
// Readable again, it is never looked up for another link.
180197
h.channels.refer?.(MINE);
181-
await new Promise((resolve) => setTimeout(resolve, 0));
198+
await flush();
182199
expect(h.pending).toHaveLength(0);
183200
});
184201

‎src/features/relay/contracts.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ export type ChannelReference =
232232
joined: boolean;
233233
/** Only when the viewer may see the roster. */
234234
members?: number;
235+
/** A public channel the viewer left is named by its open metadata, but
236+
* its messages stay unreadable until a lookup confirms it is still
237+
* open; `looking` while that lookup is queued or in flight. */
238+
readable?: false;
239+
looking?: true;
235240
}>;
236241
/** Reads are side-effect-free; snapshots retain identity until their value changes.
237242
* Commands are idempotent requests; the store decides whether network work is needed. */
@@ -279,8 +284,8 @@ export interface ChannelQueries {
279284
>[];
280285
/** Current reference state from local evidence; never reads. */
281286
describe?(channelId: string): ChannelReference;
282-
/** Demand a background lookup for a reference that isn't `found`, until the
283-
* returned release. The store coalesces requests into bounded reads, keeps
287+
/** Demand a background lookup for a reference that isn't `found`, or is
288+
* found but not `readable`, until the returned release. The store coalesces requests into bounded reads, keeps
284289
* a `withheld` answer for a few minutes, backs off after failures,
285290
* rechecks demanded references when either ends, and notifies list
286291
* subscribers once per batch as a lookup starts (unless it only rechecks

‎src/features/relay/store.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,8 +2557,10 @@ export function createChannelStore(
25572557
warming = false;
25582558
}
25592559
}
2560-
/** Channel-link lookups. `withheld` answers are kept for a while (and
2561-
* shown while rechecked); failures back off. Both reset with the epoch.
2560+
/** Channel-link lookups. `withheld` keeps a lookup that admitted nothing (a
2561+
* withheld answer, or a left channel still unreadable) for a while, and its
2562+
* answer is shown while rechecked; failures back off. Both reset with the
2563+
* epoch.
25622564
* `demand` counts mounted links per channel; a timer rechecks them when
25632565
* their answer expires or their backoff ends. */
25642566
const references = {
@@ -2612,8 +2614,11 @@ export function createChannelStore(
26122614
...(channel.members?.length ? { members: channel.members.length } : {}),
26132615
};
26142616
}
2617+
const state = currentReferences();
2618+
const looking = state.queue.has(id) || state.pending.has(id);
26152619
// A public channel the viewer has left: its signed open metadata stays
2616-
// public even while discovery denies reading it as a member.
2620+
// public even while discovery denies reading it as a member, until a
2621+
// lookup confirms it is still open.
26172622
const event = discovery?.metadataVersion(id);
26182623
const name = event && openMetadata(event) && metadataName(event);
26192624
if (event && name) {
@@ -2628,12 +2633,13 @@ export function createChannelStore(
26282633
hidden: false,
26292634
archived: hasTag(event, "archived", "true"),
26302635
joined: false,
2636+
readable: false,
2637+
...(looking ? { looking: true as const } : {}),
26312638
};
26322639
}
2633-
const state = currentReferences();
26342640
return state.withheld.has(id)
26352641
? { state: "withheld" }
2636-
: state.queue.has(id) || state.pending.has(id)
2642+
: looking
26372643
? { state: "looking" }
26382644
: { state: "unknown" };
26392645
}
@@ -2670,8 +2676,9 @@ export function createChannelStore(
26702676
}
26712677
function lookUpReference(id: string) {
26722678
if (disposed || !transport || !discovery || options.cachedOnly) return;
2673-
if (list.status !== "ready" || describeReference(id).state === "found")
2674-
return;
2679+
// Discovery's admission, not a found name, decides: a left channel's
2680+
// messages need a fresh lookup.
2681+
if (list.status !== "ready" || discovery.get(id)) return;
26752682
const state = currentReferences();
26762683
if (
26772684
state.queue.has(id) ||
@@ -2699,8 +2706,7 @@ export function createChannelStore(
26992706
for (const id of batch) {
27002707
state.pending.delete(id);
27012708
state.retryAt.delete(id);
2702-
if (describeReference(id).state === "found")
2703-
state.withheld.delete(id);
2709+
if (discovery?.get(id)) state.withheld.delete(id);
27042710
else state.withheld.set(id, now());
27052711
}
27062712
setList(list, true);

0 commit comments

Comments
 (0)