Repository navigation
fix(mentions): stop per-keystroke member scans in large channels #1828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
84a4999
fix(mentions): stop per-keystroke member scans in large channels
loganj 835b4fb
Keep mention caches across draft, team and empty-page rerenders
loganj 75172f1
Restore real timers after each team mention test
loganj eb88f67
Rebuild mention choices only for directory people that change them
loganj e7313ea
Intern directory changes regardless of their order
loganj e089a86
Bound the mention name index for long profile names
loganj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { expect, it } from "vitest"; | ||
| import { directoryChanges, mentionCandidates } from "./mention-candidates"; | ||
| import type { ComposerSession } from "../../features/messages/composer-session"; | ||
|
|
||
| const key = (n: number) => n.toString(16).padStart(64, "0"); | ||
| const members = [1, 2, 3, 4, 5].map(key); | ||
| // 1: person; 2: unnamed; 3: agent member; 4: person; 5: archived. | ||
| const profiles = new Map<string, { name?: string; isAgent?: boolean }>([ | ||
| [key(1), { name: "Ada" }], | ||
| [key(3), { name: "Bot", isAgent: true }], | ||
| [key(4), { name: "Cy" }], | ||
| [key(5), { name: "Gone" }], | ||
| ]); | ||
| const agents = [ | ||
| { pubkey: key(3), name: "Bot" }, | ||
| { pubkey: key(6), name: "Helper", managed: true }, | ||
| ]; | ||
| const elsewhere = [ | ||
| { pubkey: key(7), name: "Far", relay: "wss://other.example" }, | ||
| ]; | ||
|
|
||
| // A new session each time, so no cached rows answer. | ||
| function rows( | ||
| directory?: Parameters<typeof mentionCandidates>[4], | ||
| session = fresh(), | ||
| ) { | ||
| return [ | ||
| ...mentionCandidates(session, "c", false, undefined, directory, elsewhere), | ||
| ].sort((a, b) => a.recipient.pubkey.localeCompare(b.recipient.pubkey)); | ||
| } | ||
| function fresh() { | ||
| return { | ||
| viewer: key(1), | ||
| channels: { | ||
| list: () => ({ | ||
| status: "ready", | ||
| channels: [{ id: "c", name: "C", channelType: "stream", members }], | ||
| }), | ||
| }, | ||
| agentChoices: { snapshot: () => ({ identities: agents }) }, | ||
| profiles: { snapshot: () => profiles }, | ||
| archives: { | ||
| snapshot: () => ({}), | ||
| state: (pubkey: string) => | ||
| pubkey === key(5) ? "archived" : "not-archived", | ||
| }, | ||
| outbox: { supports: () => true }, | ||
| } as unknown as ComposerSession; | ||
| } | ||
|
|
||
| it("reads only the session stores its cache compares", () => { | ||
| // mentionCandidates caches rows on these stores' snapshots (and the viewer, | ||
| // fixed per session). A new read here, including in the host's admission | ||
| // rules, must join that cache key, or rows go stale. | ||
| const read = new Set<string | symbol>(); | ||
| const session = new Proxy(fresh(), { | ||
| get(target, key, receiver) { | ||
| read.add(key); | ||
| return Reflect.get(target, key, receiver); | ||
| }, | ||
| }); | ||
| rows([{ pubkey: key(8), name: "Dee" }], session); | ||
| expect([...read].sort()).toEqual([ | ||
| "agentChoices", | ||
| "archives", | ||
| "channels", | ||
| "outbox", | ||
| "profiles", | ||
| "viewer", | ||
| ]); | ||
| }); | ||
|
|
||
| it("directory people that change nothing can be left out", () => { | ||
| const directory = [ | ||
| { pubkey: key(1), name: "Ada Lovelace" }, // named member: no change | ||
| { pubkey: key(3), name: "Bot", isAgent: true as const }, // agent member: no change | ||
| { pubkey: key(2), name: "Bea" }, // unnamed member: names its row | ||
| { pubkey: key(4), name: "Cy", isAgent: true as const }, // marks a member an agent | ||
| { pubkey: key(5), name: "Gone" }, // archived member | ||
| { pubkey: key(6), name: "Helper Two" }, // renames an outside agent | ||
| { pubkey: key(7), name: "Near" }, // takes another community's agent row | ||
| { pubkey: key(8), name: "Dee" }, // outsider | ||
| ]; | ||
| const local = new Map(rows().map((row) => [row.recipient.pubkey, row])); | ||
| const changes = directoryChanges( | ||
| local, | ||
| directory, | ||
| profiles, | ||
| (pubkey) => !!profiles.get(pubkey)?.name, | ||
| ); | ||
| expect(changes.map((person) => person.pubkey)).toEqual( | ||
| [2, 4, 5, 6, 7, 8].map(key), | ||
| ); | ||
| expect(rows(changes)).toEqual(rows(directory)); | ||
| // Each other kept person does change the rows. The archived member has no | ||
| // row to compare, so it is kept, which is safe. | ||
| for (const person of changes.filter((p) => p.pubkey !== key(5))) | ||
| expect(rows(changes.filter((other) => other !== person))).not.toEqual( | ||
| rows(changes), | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.