Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(updates-consent): preserve active receipts across email and account switches

Sebastian Bec603cd51179
diff --git a/electron/updates-consent.mjs b/electron/updates-consent.mjs
index b3936e6443..faffecdc40 100644
--- a/electron/updates-consent.mjs
+++ b/electron/updates-consent.mjs
@@ -65,10 +65,10 @@ export function createUpdatesConsent({ prefs, account, available, now = Date.now
if (!local?.pending || local.account) return;
if (local.status === "opted_in" && local.email) {
const receipt = await client().updatesConsentByEmail({ email: local.email, installId: prefs.installId(), textVersion: local.version });
- if (sameAnswer()) return save({ ...local, receipt, pending: false });
+ if (sameAnswer()) return save(withReplacedReceipts({ ...local, receipt, pending: false }, [], receipt));
const latest = prefs.updatesConsent();
if (!latest) return;
- if (!latest.account && latest.email === local.email && (latest.status === "opted_in" || !latest.receipt)) save({ ...latest, receipt });
+ if (!latest.account && latest.email === local.email && (latest.status === "opted_in" || !latest.receipt)) save(withReplacedReceipts({ ...latest, receipt }, [], receipt));
else save(withReplacedReceipts(latest, [receipt]));
} else if (local.status !== "opted_in" && local.receipt) {
await client().withdrawUpdatesConsent({ status: local.status, receipt: local.receipt });
@@ -108,7 +108,10 @@ export function createUpdatesConsent({ prefs, account, available, now = Date.now
keep({ status: remote.status, version: remote.textVersion, at: now(), account: who.id, ...(local?.email ? { email: local.email } : {}), replacedReceipts: local?.replacedReceipts });
return;
}
- if (!local || (local.account && local.account !== who.id)) return;
+ if (!local || (local.account && local.account !== who.id)) {
+ if (receipt) keep(rest);
+ return;
+ }
await client().recordUpdatesConsent(local.status, local.version);
keep({ ...rest, account: who.id, pending: false });
}
diff --git a/electron/updates-consent.node-test.mjs b/electron/updates-consent.node-test.mjs
index 24620b2355..1d10aefaf5 100644
--- a/electron/updates-consent.node-test.mjs
+++ b/electron/updates-consent.node-test.mjs
@@ -449,3 +449,78 @@ test("an account without a choice never inherits another account's answer", asyn
assert.equal(f.saved.updatesConsent.account, "account-2");
});
});
+
+test("signed out, returning to an email while its repeated opt-in is in flight never withdraws the active receipt", async t => {
+ for (const failures of [1, 0]) await t.test(`final opt-in failures=${failures}`, async t => {
+ const f = await world(t);
+ await f.consent.choose({ status: "opted_in", email: "[email protected]" });
+ const repeated = Promise.withResolvers(), final = Promise.withResolvers();
+ t.after(() => { repeated.resolve(); final.resolve(); });
+ f.gate = repeated.promise;
+ const sending = f.consent.choose({ status: "opted_in", email: "[email protected]" });
+ await until(() => f.requests.length === 2);
+ const replacing = f.consent.choose({ status: "opted_in", email: "[email protected]" });
+ await until(() => f.saved.updatesConsent.email === "[email protected]");
+ const returning = f.consent.choose({ status: "opted_in", email: "[email protected]" });
+ await until(() => f.saved.updatesConsent.email === "[email protected]");
+ f.gate = final.promise;
+ repeated.resolve();
+ await until(() => f.requests.length === 3);
+ f.failOptIns = failures;
+ f.gate = null;
+ final.resolve();
+ await Promise.all([sending, replacing, returning]);
+ assert.equal(f.emails.get("[email protected]"), "opted_in");
+ assert.deepEqual(f.requests.map(request => request.body.status), ["opted_in", "opted_in", "opted_in"]);
+ assert.deepEqual(f.saved.updatesConsent, { status: "opted_in", version: UPDATES_CONSENT_VERSION, at: f.now,
+ email: "[email protected]", receipt: RECEIPT, ...(failures ? { pending: true } : {}) });
+ f.launch();
+ await f.consent.sync();
+ assert.equal(f.emails.get("[email protected]"), "opted_in");
+ assert.equal(f.saved.updatesConsent.pending, undefined);
+ assert.equal(f.requests.some(request => request.body.status === "withdrawn"), false);
+ await f.consent.choose({ status: "withdrawn" });
+ assert.equal(f.emails.get("[email protected]"), "withdrawn");
+ });
+});
+
+test("an unanswered account withdraws another account's retained email receipt without inheriting its answer", async t => {
+ for (const status of ["opted_in", "declined", "withdrawn"]) for (const pending of [false, true])
+ await t.test(`${status}, pending=${pending}`, async t => {
+ const f = await world(t);
+ await f.consent.choose({ status: "opted_in", email: "[email protected]" });
+ const ownerAnswer = { status, version: UPDATES_CONSENT_VERSION, at: f.now, account: "account-1",
+ email: "[email protected]", ...(pending ? { pending: true } : {}) };
+ f.prefs.setUpdatesConsent({ ...ownerAnswer, receipt: receiptOf("[email protected]") });
+ f.accountId = "account-2";
+ f.accessToken = `omc_${"B".repeat(43)}`;
+ await f.signInNow();
+ const held = Promise.withResolvers();
+ t.after(() => held.resolve());
+ f.gate = held.promise;
+ const syncing = f.consent.sync().catch(error => error);
+ await until(() => f.requests.length === 2);
+ assert.equal(f.requests[1].method, "GET");
+ f.failWithdrawals = 1;
+ f.gate = null;
+ held.resolve();
+ await syncing;
+ assert.equal(f.account, null);
+ assert.equal(f.emails.get("[email protected]"), "opted_in");
+ const afterFailure = structuredClone(f.saved.updatesConsent);
+ f.launch();
+ await f.consent.sync();
+ assert.equal(f.emails.get("[email protected]"), "withdrawn");
+ assert.deepEqual(afterFailure, { ...ownerAnswer, replacedReceipts: [receiptOf("[email protected]")] });
+ assert.deepEqual(f.saved.updatesConsent, ownerAnswer);
+ const state = await f.consent.state();
+ assert.equal(state.due, true);
+ assert.equal(state.status, null);
+ assert.equal(f.account, null);
+ assert.deepEqual(f.requests.filter(request => request.path === "/api/cloud/desktop/updates-consent").map(request => request.method), ["GET", "GET", "GET"]);
+ assert.deepEqual(f.requests.filter(request => request.method === "POST").map(request => request.body), [
+ { status: "opted_in", email: "[email protected]", installId: f.saved.installId, textVersion: UPDATES_CONSENT_VERSION },
+ { status: "withdrawn", receipt: receiptOf("[email protected]") },
+ { status: "withdrawn", receipt: receiptOf("[email protected]") }]);
+ });
+});