From 75bc30f191cc7e2f65268e6ffe9507e15782444f Mon Sep 17 00:00:00 2001 From: Rudy Fraser Date: Tue, 10 Nov 2026 20:11:24 -0500 Subject: [PATCH] fix(merge): stop strict reads and partial refreshes from dropping account data Threadgate and postgate records are matched leniently before they are rewritten, so a legacy datetime no longer makes an existing gate look absent. Creating a profile asserts the record is still missing. An OAuth session refresh carries the account's current handle and email. --- src/lib/api/__tests__/upsert-profile.test.ts | 4 +- src/lib/api/upsert-profile.ts | 3 +- .../queries/__tests__/gate-records.test.ts | 59 +++++++++++++++++++ src/state/queries/postgate/index.ts | 9 ++- src/state/queries/threadgate/index.ts | 9 ++- src/state/session/__tests__/session-test.ts | 42 +++++++++++++ src/state/session/index.tsx | 2 + src/state/session/reducer.ts | 5 +- 8 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 src/state/queries/__tests__/gate-records.test.ts diff --git a/src/lib/api/__tests__/upsert-profile.test.ts b/src/lib/api/__tests__/upsert-profile.test.ts index 1e8650c51..8662102ee 100644 --- a/src/lib/api/__tests__/upsert-profile.test.ts +++ b/src/lib/api/__tests__/upsert-profile.test.ts @@ -38,7 +38,7 @@ function mockClient() { ([, init]) => JSON.parse(init.body as string) as { record: Record - swapRecord?: string + swapRecord?: string | null }, ) return {client, fetchHandler, puts} @@ -91,7 +91,7 @@ describe('upsertProfile', () => { $type: 'app.bsky.actor.profile', displayName: 'New', }) - expect(puts()[0].swapRecord).toBeUndefined() + expect(puts()[0].swapRecord).toBeNull() }) it('writes nothing when the profile cannot be read', async () => { diff --git a/src/lib/api/upsert-profile.ts b/src/lib/api/upsert-profile.ts index e1ba71973..920d9f28c 100644 --- a/src/lib/api/upsert-profile.ts +++ b/src/lib/api/upsert-profile.ts @@ -51,7 +51,8 @@ export const upsertProfile: Action = async ( collection: COLLECTION, rkey: 'self', record: {...updated, $type: COLLECTION}, - swapRecord: existing?.cid, + // `null` asserts the record is still absent, so two first writes cannot both land. + swapRecord: existing ? existing.cid : null, }) return } catch (e) { diff --git a/src/state/queries/__tests__/gate-records.test.ts b/src/state/queries/__tests__/gate-records.test.ts new file mode 100644 index 000000000..c52b0aa67 --- /dev/null +++ b/src/state/queries/__tests__/gate-records.test.ts @@ -0,0 +1,59 @@ +import {type Client} from '@atproto/lex' + +import {getPostgateRecord} from '../postgate' +import {getThreadgateRecord} from '../threadgate' + +jest.mock('#/state/session', () => ({ + useAppviewClient: jest.fn(), + usePdsClient: jest.fn(), + useSession: jest.fn(), +})) + +const POST = 'at://did:plc:me/app.bsky.feed.post/3kpost' +// No timezone: valid to the pre-lex validators, rejected by a strict match. +const LEGACY_DATETIME = '2023-08-01T12:00:00' + +function clientReturning(value: unknown) { + return { + call: jest.fn().mockResolvedValue({uri: POST, value}), + } as unknown as Client +} + +describe('gate record readers', () => { + it('returns a threadgate whose datetime predates strict formatting', async () => { + const record = { + $type: 'app.bsky.feed.threadgate', + post: POST, + createdAt: LEGACY_DATETIME, + allow: [{$type: 'app.bsky.feed.threadgate#mentionRule'}], + hiddenReplies: ['at://did:plc:them/app.bsky.feed.post/3kreply'], + } + + await expect( + getThreadgateRecord({pdsClient: clientReturning(record), postUri: POST}), + ).resolves.toEqual(record) + }) + + it('returns a postgate whose datetime predates strict formatting', async () => { + const record = { + $type: 'app.bsky.feed.postgate', + post: POST, + createdAt: LEGACY_DATETIME, + embeddingRules: [{$type: 'app.bsky.feed.postgate#disableRule'}], + detachedEmbeddingUris: ['at://did:plc:them/app.bsky.feed.post/3kquote'], + } + + await expect( + getPostgateRecord({pdsClient: clientReturning(record), postUri: POST}), + ).resolves.toEqual(record) + }) + + it('still treats a record of the wrong shape as absent', async () => { + await expect( + getThreadgateRecord({ + pdsClient: clientReturning({$type: 'app.bsky.feed.threadgate'}), + postUri: POST, + }), + ).resolves.toBeNull() + }) +}) diff --git a/src/state/queries/postgate/index.ts b/src/state/queries/postgate/index.ts index 08dff1a45..233549dcd 100644 --- a/src/state/queries/postgate/index.ts +++ b/src/state/queries/postgate/index.ts @@ -57,7 +57,14 @@ export async function getPostgateRecord({ }), ) - if (data.value && bsky.matches(app.bsky.feed.postgate, data.value)) { + /* + * Lenient, because a record this reads as missing is then overwritten: a + * strict failure on a legacy datetime would drop its existing rules. + */ + if ( + data.value && + bsky.matches(app.bsky.feed.postgate, data.value, {strict: false}) + ) { return data.value } else { return undefined diff --git a/src/state/queries/threadgate/index.ts b/src/state/queries/threadgate/index.ts index c9b1740c0..384bec8f6 100644 --- a/src/state/queries/threadgate/index.ts +++ b/src/state/queries/threadgate/index.ts @@ -120,7 +120,14 @@ export async function getThreadgateRecord({ }), ) - if (data.value && bsky.matches(app.bsky.feed.threadgate, data.value)) { + /* + * Lenient, because a record this reads as missing is then overwritten: a + * strict failure on a legacy datetime would drop its existing rules. + */ + if ( + data.value && + bsky.matches(app.bsky.feed.threadgate, data.value, {strict: false}) + ) { return data.value } else { return null diff --git a/src/state/session/__tests__/session-test.ts b/src/state/session/__tests__/session-test.ts index 73d83de23..eee624148 100644 --- a/src/state/session/__tests__/session-test.ts +++ b/src/state/session/__tests__/session-test.ts @@ -1831,6 +1831,48 @@ describe('session', () => { expect(state.currentBundleState.did).toBe('alice-did') expect(state.needsPersist).toBe(true) }) + + it('patches a changed handle and email, and keeps them when the patch has none', () => { + let state = getInitialState([]) + state = run(state, [ + { + type: 'switched-to-account', + newBundle: makeBundle('https://alice.com'), + newAccount: makeAccount('https://alice.com', { + active: true, + did: 'alice-did', + handle: 'alice.test', + accessJwt: 'alice-access-jwt-1', + refreshJwt: 'alice-refresh-jwt-1', + email: 'alice@foo.bar', + emailAuthFactor: false, + emailConfirmed: false, + }), + }, + { + type: 'partial-refresh-session', + accountDid: 'alice-did', + patch: { + handle: 'alice.example', + email: 'alice@new.example', + emailConfirmed: false, + emailAuthFactor: false, + }, + }, + ]) + expect(state.accounts[0].handle).toBe('alice.example') + expect(state.accounts[0].email).toBe('alice@new.example') + + state = run(state, [ + { + type: 'partial-refresh-session', + accountDid: 'alice-did', + patch: {emailConfirmed: true, emailAuthFactor: false}, + }, + ]) + expect(state.accounts[0].handle).toBe('alice.example') + expect(state.accounts[0].email).toBe('alice@new.example') + }) }) function run(initialState: State, actions: Action[]): State { diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx index 4d5a9c795..b8b96439c 100644 --- a/src/state/session/index.tsx +++ b/src/state/session/index.tsx @@ -563,6 +563,8 @@ export function Provider({children}: React.PropsWithChildren<{}>) { type: 'partial-refresh-session', accountDid: did, patch: { + handle: data.handle, + email: data.email, emailConfirmed: data.emailConfirmed, emailAuthFactor: data.emailAuthFactor, active: data.active, diff --git a/src/state/session/reducer.ts b/src/state/session/reducer.ts index ee9e9dc27..2a2e92e6e 100644 --- a/src/state/session/reducer.ts +++ b/src/state/session/reducer.ts @@ -65,7 +65,8 @@ export type Action = patch: Pick< SessionAccount, 'emailConfirmed' | 'emailAuthFactor' | 'active' | 'status' - > + > & + Partial> } function createPublicBundleState(): BundleState { @@ -269,6 +270,8 @@ let reducer = (state: State, action: Action): State => { if (a.did === accountDid) { return { ...a, + handle: patch.handle ?? a.handle, + email: patch.email ?? a.email, emailConfirmed: patch.emailConfirmed ?? a.emailConfirmed, emailAuthFactor: patch.emailAuthFactor ?? a.emailAuthFactor, active: patch.active, -- knotmirror 0.0.1