Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290import {unregisterPushToken} from '#/lib/notifications/notifications'import {logger} from '#/lib/notifications/util'import {wrapSessionReducerForLogging} from './logging'import {createPublicSessionBundle} from './session-core'import {type AtpSessionEvent, type SessionAccount} from './types'import {createTemporaryClientsAndResume} from './util'
// Keep session internals outside the reducer's static view of a bundle.type OpaqueSessionBundle = { readonly service: URL}
type BundleState = { readonly bundle: OpaqueSessionBundle readonly did: string | undefined}
export type State = { readonly accounts: SessionAccount[] readonly currentBundleState: BundleState needsPersist: boolean // Cleared after persistence is scheduled.}
export type Action = | { type: 'received-session-event' bundle: OpaqueSessionBundle accountDid: string refreshedAccount: SessionAccount | undefined sessionEvent: AtpSessionEvent } | { type: 'switched-to-account' newBundle: OpaqueSessionBundle newAccount: SessionAccount } | { // Replace an immutable session from synced or rescued tokens without rebroadcasting. type: 'replaced-current-bundle' newBundle: OpaqueSessionBundle newAccount: SessionAccount } | { type: 'removed-account' accountDid: string } | { type: 'logged-out-current-account' } | { type: 'logged-out-every-account' } | { type: 'oauth-session-terminated' accountDid: string } | { type: 'synced-accounts' syncedAccounts: SessionAccount[] syncedCurrentDid: string | undefined } | { type: 'partial-refresh-session' accountDid: string patch: Pick< SessionAccount, 'emailConfirmed' | 'emailAuthFactor' | 'active' | 'status' > & Partial<Pick<SessionAccount, 'handle' | 'email'>> }
function createPublicBundleState(): BundleState { return { bundle: createPublicSessionBundle(), did: undefined, }}
export function getInitialState(persistedAccounts: SessionAccount[]): State { return { accounts: persistedAccounts, currentBundleState: createPublicBundleState(), needsPersist: false, }}
let reducer = (state: State, action: Action): State => { switch (action.type) { case 'received-session-event': { const {bundle, accountDid, refreshedAccount, sessionEvent} = action if (bundle !== state.currentBundleState.bundle) { /* * Stale bundles must neither log out the current account nor restore * tokens after logout or an account switch. */ return state } if (sessionEvent === 'network-error') { // Assume it's transient. return state } const existingAccount = state.accounts.find(a => a.did === accountDid) if ( !existingAccount || JSON.stringify(existingAccount) === JSON.stringify(refreshedAccount) ) { return state } return { accounts: state.accounts.map(a => { if (a.did === accountDid) { if (refreshedAccount) { return refreshedAccount } else { return { ...a, // If we didn't receive a refreshed account, clear out the tokens. accessJwt: undefined, refreshJwt: undefined, } } } else { return a } }), currentBundleState: refreshedAccount ? state.currentBundleState : createPublicBundleState(), // Log out if expired. needsPersist: true, } } case 'switched-to-account': { const {newAccount, newBundle} = action return { accounts: [ newAccount, ...state.accounts.filter(a => a.did !== newAccount.did), ], currentBundleState: { did: newAccount.did, bundle: newBundle, }, needsPersist: true, } } case 'replaced-current-bundle': { const {newBundle, newAccount} = action return { ...state, currentBundleState: { did: state.currentBundleState.did, bundle: newBundle, }, accounts: state.accounts.map(a => a.did === newAccount.did ? newAccount : a, ), needsPersist: false, // Synced from another tab. Don't persist to avoid cycles. } } case 'removed-account': { const {accountDid} = action
// side effect const account = state.accounts.find(a => a.did === accountDid) if (account && !account.isOauthSession) { createTemporaryClientsAndResume([account]) .then(clients => unregisterPushToken(clients)) .then(() => logger.debug('Push token unregistered', {did: accountDid}), ) .catch(err => { logger.error('Failed to unregister push token', { did: accountDid, error: err, }) }) }
return { accounts: state.accounts.filter(a => a.did !== accountDid), currentBundleState: state.currentBundleState.did === accountDid ? createPublicBundleState() // Log out if removing the current one. : state.currentBundleState, needsPersist: true, } } case 'logged-out-current-account': { const {currentBundleState} = state const accountDid = currentBundleState.did // side effect const account = state.accounts.find(a => a.did === accountDid) if (account && accountDid && !account.isOauthSession) { createTemporaryClientsAndResume([account]) .then(clients => unregisterPushToken(clients)) .then(() => logger.debug('Push token unregistered', {did: accountDid}), ) .catch(err => { logger.error('Failed to unregister push token', { did: accountDid, error: err, }) }) }
return { accounts: state.accounts.map(a => a.did === accountDid ? { ...a, refreshJwt: undefined, accessJwt: undefined, } : a, ), currentBundleState: createPublicBundleState(), needsPersist: true, } } case 'logged-out-every-account': { createTemporaryClientsAndResume( state.accounts.filter(account => !account.isOauthSession), ) .then(clients => unregisterPushToken(clients)) .then(() => logger.debug('Push token unregistered')) .catch(err => { logger.error('Failed to unregister push token', { error: err, }) })
return { accounts: state.accounts.map(a => ({ ...a, // Clear tokens for *every* account (this is a hard logout). refreshJwt: undefined, accessJwt: undefined, })), currentBundleState: createPublicBundleState(), needsPersist: true, } } case 'oauth-session-terminated': { if (state.currentBundleState.did !== action.accountDid) return state return { accounts: state.accounts, currentBundleState: createPublicBundleState(), needsPersist: true, } } case 'synced-accounts': { const {syncedAccounts, syncedCurrentDid} = action return { accounts: syncedAccounts, currentBundleState: syncedCurrentDid === state.currentBundleState.did ? state.currentBundleState : createPublicBundleState(), // Log out if different user. needsPersist: false, // Synced from another tab. Don't persist to avoid cycles. } } case 'partial-refresh-session': { const {accountDid, patch} = action
// PasswordSession has no setter; consumers read these fields from the account. return { ...state, accounts: state.accounts.map(a => { 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, status: patch.status, } } return a }), needsPersist: true, } } }}reducer = wrapSessionReducerForLogging(reducer)export {reducer}