Skip to content

Commit 7d3ed5a

Browse files
matt2eclaude
andcommitted
fix(mentions): keep saved team names from blocking Space on a pasted key
Review follow-up. A complete pasted key matches only its owner, so a saved team named like that npub or hex key, or like the key followed by a word, is never shown for it. teamBlocksSpace still compared team names with the query, though, so such a team cleared the owner's Space selection in the chooser and made the delayed Space resolver return the text to prose. teamBlocksSpace now returns false for an exact public key. Ordinary names keep the same-or-longer team check. Section 5 of the mentions contract says that a team name does not keep a key from selecting. New tests cover a team with the npub as its name and one with a longer name, in the chooser and in the resolver. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 5aba83c commit 7d3ed5a

4 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎src/bundled/mentions/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ set of choices when all of these are true:
218218

219219
An exact public key (Terms) is the exact name of the choice with that key, even
220220
one with no aliases, and of no other choice, and no name continues it: a key
221-
has no namesakes. The steps below then apply as for a name.
221+
has no namesakes. Nor does a saved team with the same or a longer name keep it
222+
from selecting. The steps below then apply as for a name.
222223

223224
The **local** choices are the ones known without the directory search: the
224225
channel's members and the agents the client offers for mention (section 1, items

‎src/bundled/mentions/solo-agents.test.tsx‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3610,7 +3610,7 @@ type DirectoryPage = {
36103610
function spaceFixture(
36113611
memberName: string,
36123612
others: Record<string, string | null> = {},
3613-
options: { query?: string; dm?: boolean } = {},
3613+
options: { query?: string; dm?: boolean; teams?: readonly string[] } = {},
36143614
) {
36153615
const h = setup();
36163616
h.profiles.set(h.member, { name: memberName });
@@ -3644,9 +3644,39 @@ function spaceFixture(
36443644
const publish = vi.fn();
36453645
const result = () =>
36463646
publish.mock.lastCall?.[0] as CompletionResult | undefined;
3647+
// Legacy catalog teams with these names, each of the member alone.
3648+
const catalog = {
3649+
status: "ready" as const,
3650+
entries: (options.teams ?? []).map((name, index) => ({
3651+
eventId: `team-${index}`,
3652+
createdAt: 1,
3653+
record: {
3654+
version: 1 as const,
3655+
community: "fixture",
3656+
deleted: false,
3657+
value: {
3658+
type: "team" as const,
3659+
id: `team-${index}`,
3660+
name,
3661+
agents: [h.member],
3662+
},
3663+
},
3664+
})),
3665+
};
3666+
const channelKit: NonNullable<RelaySession["channelKit"]> = {
3667+
available: true,
3668+
snapshot: () => catalog,
3669+
subscribe: () => () => {},
3670+
ensure() {},
3671+
async refresh() {},
3672+
async save() {
3673+
return "head";
3674+
},
3675+
};
36473676
const session = {
36483677
...h.session,
36493678
names,
3679+
...(options.teams ? { channelKit } : {}),
36503680
channels: { ...h.session.channels, list: () => list },
36513681
directMessages: { ...h.session.directMessages, people },
36523682
} as unknown as RelaySession;
@@ -3776,6 +3806,27 @@ it("Space selects a member by a pasted npub beside a namesake, without waiting f
37763806
h.dispose();
37773807
}
37783808
});
3809+
it.each([
3810+
{ case: "that name", suffix: "" },
3811+
{ case: "a longer name", suffix: " Crew" },
3812+
])(
3813+
"Space selects a member by a pasted npub beside a saved team with $case",
3814+
async ({ suffix }) => {
3815+
const query = npubEncode("a".repeat(64));
3816+
const h = spaceFixture("Jose", {}, { query, teams: [query + suffix] });
3817+
try {
3818+
await waitFor(() =>
3819+
expect(h.people).toHaveBeenCalledWith(query, 1, expect.anything()),
3820+
);
3821+
// A key is not a name: the team neither matches it nor declines Space.
3822+
await waitFor(() => expect(h.result()?.spaceId).toBe(h.member));
3823+
expect(h.result()?.items.map((item) => item.id)).toEqual([h.member]);
3824+
expect(h.canSpace(h.member)).toBe(true);
3825+
} finally {
3826+
h.dispose();
3827+
}
3828+
},
3829+
);
37793830
it("Space selects someone outside the channel by a pasted npub once the directory finds them", async () => {
37803831
const query = npubEncode(outsideJose);
37813832
const h = spaceFixture("Jose", {}, { query });

‎src/bundled/mentions/team-mentions.test.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
waitFor,
2323
} from "@testing-library/react";
2424
import { finalizeEvent, generateSecretKey } from "nostr-tools";
25+
import { npubEncode } from "nostr-tools/nip19";
2526
import * as communityConfig from "../../features/agents/community-config";
2627
import { SyncedTeams, teamArtifact } from "../../features/agents/team-config";
2728
import type { RelayEvent } from "../../features/relay/events";
@@ -1130,6 +1131,26 @@ it("a current team blocks a same-name person during delayed Space resolution", a
11301131
}
11311132
});
11321133

1134+
it("a team named like a pasted npub does not block its owner during delayed Space resolution", async () => {
1135+
const h = fixture();
1136+
try {
1137+
const key = npubEncode(h.agent.pubkey);
1138+
const result = await resolveSpaceMention(
1139+
h.session,
1140+
{
1141+
channelId: "parent",
1142+
selected: [],
1143+
teamNames: () => [key, `${key} Crew`],
1144+
},
1145+
key,
1146+
new AbortController().signal,
1147+
);
1148+
expect(result).toMatchObject({ pubkey: h.agent.pubkey });
1149+
} finally {
1150+
h.library.dispose();
1151+
}
1152+
});
1153+
11331154
it.each([true, false])(
11341155
"mixed-community team uses the individual chooser's addition permission: %s",
11351156
async (canAdd) => {

‎src/bundled/mentions/use-team-mentions.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ import { mentionCandidates } from "./mention-candidates";
1818
import { extendsName, normalizeName } from "../../features/search/person-match";
1919
import type { RelaySession } from "../../features/relay/session";
2020
import { mentionMatch } from "./mention-ranking";
21+
import { typedPublicKey } from "../../shared/identity/public-key";
2122

2223
const empty = { status: "unavailable", entries: [] } as const;
2324
type Catalog = Pick<
2425
ReturnType<NonNullable<RelaySession["channelKit"]>["snapshot"]>,
2526
"entries"
2627
>;
27-
/** A saved team with the same or a longer name must not auto-select a person on Space. */
28+
/**
29+
* A saved team with the same or a longer name must not auto-select a person on
30+
* Space. An exact public key is not a name: no team name equals or continues it.
31+
*/
2832
export function teamBlocksSpace(
2933
catalog: Catalog,
3034
query: string,
3135
names: readonly string[] = [],
3236
) {
37+
if (typedPublicKey(query) !== undefined) return false;
3338
const needle = normalizeName(query);
3439
const blocks = (name: string) =>
3540
normalizeName(name) === needle || extendsName(name, needle);

0 commit comments

Comments
 (0)