Skip to content

Commit 6b5eaa6

Browse files
committed
fix(imessage): resolve approval controls safely
1 parent 245dc90 commit 6b5eaa6

18 files changed

Lines changed: 831 additions & 233 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import {
3+
beginIMessageApprovalControlBinding,
4+
clearIMessageApprovalControlBindingsForTest,
5+
waitForIMessageApprovalControlBinding,
6+
} from "./approval-control-binding-window.js";
7+
8+
afterEach(() => {
9+
clearIMessageApprovalControlBindingsForTest();
10+
});
11+
12+
describe("iMessage approval control binding windows", () => {
13+
it("matches an outbound handle against the richer inbound conversation", async () => {
14+
const window = beginIMessageApprovalControlBinding({
15+
accountId: "default",
16+
conversation: { handle: "+15551230000" },
17+
});
18+
const waited = waitForIMessageApprovalControlBinding({
19+
accountId: "default",
20+
conversation: {
21+
chatGuid: "iMessage;-;+15551230000",
22+
chatId: 42,
23+
handle: "+15551230000",
24+
},
25+
});
26+
27+
window.close();
28+
29+
await expect(waited).resolves.toBe(true);
30+
await expect(
31+
waitForIMessageApprovalControlBinding({
32+
accountId: "default",
33+
conversation: { handle: "+15551230000" },
34+
}),
35+
).resolves.toBe(false);
36+
});
37+
38+
it("does not wait on a different conversation", async () => {
39+
beginIMessageApprovalControlBinding({
40+
accountId: "default",
41+
conversation: { handle: "+15551230000" },
42+
});
43+
44+
await expect(
45+
waitForIMessageApprovalControlBinding({
46+
accountId: "default",
47+
conversation: { handle: "+15551239999" },
48+
}),
49+
).resolves.toBe(false);
50+
});
51+
});
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import {
2+
enumerateConversationKeyForms,
3+
type IMessageApprovalConversationKey,
4+
} from "./approval-target-keys.js";
5+
6+
type BindingWindow = {
7+
done: Promise<void>;
8+
close: () => void;
9+
};
10+
11+
const pendingByConversation = new Map<string, Set<BindingWindow>>();
12+
13+
function bindingKeys(accountId: string, conversation: IMessageApprovalConversationKey): string[] {
14+
const account = accountId.trim();
15+
return account
16+
? enumerateConversationKeyForms(conversation).map((form) => `${account}:${form}`)
17+
: [];
18+
}
19+
20+
/** Marks the send-to-binding interval during which a visible control is not yet resolvable. */
21+
export function beginIMessageApprovalControlBinding(params: {
22+
accountId: string;
23+
conversation: IMessageApprovalConversationKey;
24+
}): { close: () => void } {
25+
const keys = bindingKeys(params.accountId, params.conversation);
26+
let resolveDone = () => {};
27+
const window: BindingWindow = {
28+
done: new Promise<void>((resolve) => {
29+
resolveDone = resolve;
30+
}),
31+
close: () => {},
32+
};
33+
let closed = false;
34+
window.close = () => {
35+
if (closed) {
36+
return;
37+
}
38+
closed = true;
39+
for (const key of keys) {
40+
const windows = pendingByConversation.get(key);
41+
windows?.delete(window);
42+
if (windows?.size === 0) {
43+
pendingByConversation.delete(key);
44+
}
45+
}
46+
resolveDone();
47+
};
48+
for (const key of keys) {
49+
const windows = pendingByConversation.get(key) ?? new Set<BindingWindow>();
50+
windows.add(window);
51+
pendingByConversation.set(key, windows);
52+
}
53+
return { close: window.close };
54+
}
55+
56+
/** Waits for one matching delivery to finish binding; callers recheck until none remain. */
57+
export async function waitForIMessageApprovalControlBinding(params: {
58+
accountId: string;
59+
conversation: IMessageApprovalConversationKey;
60+
abortSignal?: AbortSignal;
61+
}): Promise<boolean> {
62+
const windows = new Set(
63+
bindingKeys(params.accountId, params.conversation).flatMap((key) => [
64+
...(pendingByConversation.get(key) ?? []),
65+
]),
66+
);
67+
if (windows.size === 0) {
68+
return false;
69+
}
70+
if (params.abortSignal?.aborted) {
71+
throw params.abortSignal.reason;
72+
}
73+
let detachAbort = () => {};
74+
const aborted = new Promise<never>((_resolve, reject) => {
75+
const onAbort = () => reject(params.abortSignal?.reason);
76+
params.abortSignal?.addEventListener("abort", onAbort, { once: true });
77+
detachAbort = () => params.abortSignal?.removeEventListener("abort", onAbort);
78+
});
79+
try {
80+
await Promise.race([Promise.race([...windows].map((window) => window.done)), aborted]);
81+
} finally {
82+
detachAbort();
83+
}
84+
return true;
85+
}
86+
87+
export function clearIMessageApprovalControlBindingsForTest(): void {
88+
for (const windows of pendingByConversation.values()) {
89+
for (const window of windows) {
90+
window.close();
91+
}
92+
}
93+
pendingByConversation.clear();
94+
}

0 commit comments

Comments
 (0)