fix: disambiguate device-pair notify subscribers

This commit is contained in:
Tak Hoffman
2026-04-10 19:46:06 -05:00
parent 85c7748520
commit 562025f8dc
2 changed files with 68 additions and 2 deletions

View File

@@ -85,4 +85,70 @@ describe("device-pair notify persistence", () => {
) as { subscribers: unknown[] };
expect(persisted.subscribers).toEqual([]);
});
it("does not remove a different persisted subscriber when notify fields contain pipes", async () => {
await fs.writeFile(
path.join(stateDir, "device-pair-notify.json"),
JSON.stringify(
{
subscribers: [
{
to: "chat|123",
accountId: "acct",
mode: "persistent",
addedAtMs: 1,
},
{
to: "chat",
accountId: "123|acct",
mode: "persistent",
addedAtMs: 2,
},
],
notifiedRequestIds: {},
},
null,
2,
),
"utf8",
);
const api = createTestPluginApi({
runtime: {
state: {
resolveStateDir: () => stateDir,
},
} as never,
});
await handleNotifyCommand({
api,
ctx: {
channel: "telegram",
senderId: "chat",
accountId: "123|acct",
},
action: "off",
});
const status = await handleNotifyCommand({
api,
ctx: {
channel: "telegram",
senderId: "chat",
accountId: "123|acct",
},
action: "status",
});
expect(status.text).toContain("Pair request notifications: disabled for this chat.");
const persisted = JSON.parse(
await fs.readFile(path.join(stateDir, "device-pair-notify.json"), "utf8"),
) as { subscribers: Array<{ to: string; accountId?: string }> };
expect(persisted.subscribers).toHaveLength(1);
expect(persisted.subscribers[0]).toMatchObject({
to: "chat|123",
accountId: "acct",
});
});
});

View File

@@ -154,11 +154,11 @@ function notifySubscriberKey(subscriber: {
accountId?: string;
messageThreadId?: string | number;
}): string {
return [
return JSON.stringify([
subscriber.to,
subscriber.accountId ?? "",
normalizeNotifyThreadKey(subscriber.messageThreadId),
].join("|");
]);
}
function normalizeNotifyThreadKey(messageThreadId?: string | number): string {