Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276import {type AtpAgent, type AtpSessionEvent} from '@atproto/api'
import {unregisterPushToken} from '#/lib/notifications/notifications'import {logger} from '#/lib/notifications/util'import {createPublicAgent} from './agent'import {wrapSessionReducerForLogging} from './logging'import {type SessionAccount} from './types'import {createTemporaryAgentsAndResume} from './util'
// A hack so that the reducer can't read anything from the agent.// From the reducer's point of view, it should be a completely opaque object.type OpaqueBskyAgent = { readonly service?: URL | undefined readonly api: unknown readonly app: unknown readonly com: unknown}
type AgentState = { readonly agent: OpaqueBskyAgent readonly did: string | undefined}
export type State = { readonly accounts: SessionAccount[] readonly currentAgentState: AgentState needsPersist: boolean // Mutated in an effect.}
export type Action = | { type: 'received-agent-event' agent: OpaqueBskyAgent accountDid: string refreshedAccount: SessionAccount | undefined sessionEvent: AtpSessionEvent } | { type: 'switched-to-account' newAgent: OpaqueBskyAgent newAccount: SessionAccount } | { type: 'removed-account' accountDid: string } | { type: 'logged-out-current-account' } | { type: 'logged-out-every-account' } | { type: 'synced-accounts' syncedAccounts: SessionAccount[] syncedCurrentDid: string | undefined } | { type: 'partial-refresh-session' accountDid: string patch: Pick< SessionAccount, 'emailConfirmed' | 'emailAuthFactor' | 'active' | 'status' > }
function createPublicAgentState(): AgentState { return { agent: createPublicAgent(), did: undefined, }}
export function getInitialState(persistedAccounts: SessionAccount[]): State { return { accounts: persistedAccounts, currentAgentState: createPublicAgentState(), needsPersist: false, }}
let reducer = (state: State, action: Action): State => { switch (action.type) { case 'received-agent-event': { const {agent, accountDid, refreshedAccount, sessionEvent} = action if ( refreshedAccount === undefined && agent !== state.currentAgentState.agent ) { // If the session got cleared out (e.g. due to expiry or network error) but // this account isn't the active one, don't clear it out at this time. // This way, if the problem is transient, it'll work on next resume. 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) ) { // Fast path without a state update. 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 } }), currentAgentState: refreshedAccount ? state.currentAgentState : createPublicAgentState(), // Log out if expired. needsPersist: true, } } case 'switched-to-account': { const {newAccount, newAgent} = action return { accounts: [ newAccount, ...state.accounts.filter(a => a.did !== newAccount.did), ], currentAgentState: { did: newAccount.did, agent: newAgent, }, needsPersist: true, } } case 'removed-account': { const {accountDid} = action
// side effect const account = state.accounts.find(a => a.did === accountDid) if (account) { createTemporaryAgentsAndResume([account]) .then(agents => unregisterPushToken(agents)) .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), currentAgentState: state.currentAgentState.did === accountDid ? createPublicAgentState() // Log out if removing the current one. : state.currentAgentState, needsPersist: true, } } case 'logged-out-current-account': { const {currentAgentState} = state const accountDid = currentAgentState.did // side effect const account = state.accounts.find(a => a.did === accountDid) if (account && accountDid) { createTemporaryAgentsAndResume([account]) .then(agents => unregisterPushToken(agents)) .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, ), currentAgentState: createPublicAgentState(), needsPersist: true, } } case 'logged-out-every-account': { createTemporaryAgentsAndResume(state.accounts) .then(agents => unregisterPushToken(agents)) .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, })), currentAgentState: createPublicAgentState(), needsPersist: true, } } case 'synced-accounts': { const {syncedAccounts, syncedCurrentDid} = action return { accounts: syncedAccounts, currentAgentState: syncedCurrentDid === state.currentAgentState.did ? state.currentAgentState : createPublicAgentState(), // 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 const agent = state.currentAgentState.agent as AtpAgent
/* * Only mutating values that are safe. Be very careful with this. */ if (agent.session) { agent.session.emailConfirmed = patch.emailConfirmed ?? agent.session.emailConfirmed agent.session.emailAuthFactor = patch.emailAuthFactor ?? agent.session.emailAuthFactor }
return { ...state, currentAgentState: { ...state.currentAgentState, agent, }, accounts: state.accounts.map(a => { if (a.did === accountDid) { return { ...a, 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}