Skip to content

Commit

Permalink
Tweak persisted schema and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Nov 7, 2023
1 parent b6adb00 commit 9e96703
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 38 deletions.
43 changes: 28 additions & 15 deletions src/state/persisted/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,31 @@ const DEPRECATED_ROOT_STATE_STORAGE_KEY = 'root'

export function transform(legacy: LegacySchema): Schema {
return {
colorMode: legacy.shell.colorMode || defaults.colorMode,
accounts: legacy.session.accounts || defaults.accounts,
currentAccount:
legacy.session.accounts.find(a => a.did === legacy.session.data.did) ||
defaults.currentAccount,
lastEmailConfirmReminder:
legacy.reminders.lastEmailConfirm || defaults.lastEmailConfirmReminder,
primaryLanguage:
legacy.preferences.primaryLanguage || defaults.primaryLanguage,
contentLanguages:
legacy.preferences.contentLanguages || defaults.contentLanguages,
postLanguage: legacy.preferences.postLanguage || defaults.postLanguage,
postLanguageHistory:
legacy.preferences.postLanguageHistory || defaults.postLanguageHistory,
colorMode: legacy.shell?.colorMode || defaults.colorMode,
session: {
accounts: legacy.session.accounts || defaults.session.accounts,
currentAccount:
legacy.session.accounts.find(a => a.did === legacy.session.data.did) ||
defaults.session.currentAccount,
},
reminders: {
lastEmailConfirmReminder:
legacy.reminders.lastEmailConfirm ||
defaults.reminders.lastEmailConfirmReminder,
},
languagePrefs: {
primaryLanguage:
legacy.preferences.primaryLanguage ||
defaults.languagePrefs.primaryLanguage,
contentLanguages:
legacy.preferences.contentLanguages ||
defaults.languagePrefs.contentLanguages,
postLanguage:
legacy.preferences.postLanguage || defaults.languagePrefs.postLanguage,
postLanguageHistory:
legacy.preferences.postLanguageHistory ||
defaults.languagePrefs.postLanguageHistory,
},
requireAltTextEnabled:
legacy.preferences.requireAltTextEnabled ||
defaults.requireAltTextEnabled,
Expand Down Expand Up @@ -119,6 +130,8 @@ export async function migrate() {
logger.debug('persisted state: migrated legacy storage')
}
} catch (e) {
logger.error('persisted state: error migrating legacy storage', {error: e})
logger.error('persisted state: error migrating legacy storage', {
error: String(e),
})
}
}
53 changes: 30 additions & 23 deletions src/state/persisted/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ import {deviceLocales} from '#/platform/detection'
const accountSchema = z.object({
service: z.string(),
did: z.string(),
refreshJwt: z.string(),
accessJwt: z.string(),
refreshJwt: z.string().optional(),
accessJwt: z.string().optional(),
handle: z.string(),
displayName: z.string(),
aviUrl: z.string(),
})

export const schema = z.object({
colorMode: z.enum(['system', 'light', 'dark']),
accounts: z.array(accountSchema),
currentAccount: accountSchema.optional(),
lastEmailConfirmReminder: z.string().optional(),

// preferences
primaryLanguage: z.string(), // should move to server
contentLanguages: z.array(z.string()), // should move to server
postLanguage: z.string(), // should move to server
postLanguageHistory: z.array(z.string()),
session: z.object({
accounts: z.array(accountSchema),
currentAccount: accountSchema.optional(),
}),
reminders: z.object({
lastEmailConfirmReminder: z.string().optional(),
}),
languagePrefs: z.object({
primaryLanguage: z.string(), // should move to server
contentLanguages: z.array(z.string()), // should move to server
postLanguage: z.string(), // should move to server
postLanguageHistory: z.array(z.string()),
}),
requireAltTextEnabled: z.boolean(), // should move to server
mutedThreads: z.array(z.string()), // should move to server

// should move to server?
invitedUsers: z.object({
seenDids: z.array(z.string()),
copiedInvites: z.array(z.string()),
}),

onboarding: z.object({
step: z.string(),
}),
Expand All @@ -40,15 +41,21 @@ export type Schema = z.infer<typeof schema>

export const defaults: Schema = {
colorMode: 'system',
accounts: [],
currentAccount: undefined,
lastEmailConfirmReminder: undefined,
primaryLanguage: deviceLocales[0] || 'en',
contentLanguages: deviceLocales || [],
postLanguage: deviceLocales[0] || 'en',
postLanguageHistory: (deviceLocales || [])
.concat(['en', 'ja', 'pt', 'de'])
.slice(0, 6),
session: {
accounts: [],
currentAccount: undefined,
},
reminders: {
lastEmailConfirmReminder: undefined,
},
languagePrefs: {
primaryLanguage: deviceLocales[0] || 'en',
contentLanguages: deviceLocales || [],
postLanguage: deviceLocales[0] || 'en',
postLanguageHistory: (deviceLocales || [])
.concat(['en', 'ja', 'pt', 'de'])
.slice(0, 6),
},
requireAltTextEnabled: false,
mutedThreads: [],
invitedUsers: {
Expand Down
1 change: 1 addition & 0 deletions src/state/persisted/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Schema, schema} from '#/state/persisted/schema'
const BSKY_STORAGE = 'BSKY_STORAGE'

export async function write(value: Schema) {
schema.parse(value)
await AsyncStorage.setItem(BSKY_STORAGE, JSON.stringify(value))
}

Expand Down

0 comments on commit 9e96703

Please sign in to comment.