Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(onboarding): persist Site Info preferences (#510)

Ahmet Ömer223e1ca7d0f2
diff --git a/apps/self-hosted/server/api/wizard/site.post.ts b/apps/self-hosted/server/api/wizard/site.post.ts
index c218f1f0..1089c463 100644
--- a/apps/self-hosted/server/api/wizard/site.post.ts
+++ b/apps/self-hosted/server/api/wizard/site.post.ts
@@ -1,23 +1,13 @@
import { defineWizardHandler } from '../../utils/wizard-handler'
-import { z } from 'zod'
-import { settingsManager } from '~~/server/services/settings/settingsManager'
+import {
+ persistWizardSiteSettings,
+ wizardSiteSchema,
+} from '~~/server/services/wizard/site-settings'
export default defineWizardHandler(async (event) => {
- const body = await readValidatedBody(
- event,
- z.object({
- title: z.string().min(1),
- slogan: z.string().optional(),
- avatarUrl: z.string().optional(),
- author: z.string().optional(),
- }).parse,
- )
+ const body = await readValidatedBody(event, wizardSiteSchema.parse)
- await settingsManager.set('app', 'title', body.title)
- if (body.slogan) await settingsManager.set('app', 'slogan', body.slogan)
- if (body.avatarUrl)
- await settingsManager.set('app', 'avatarUrl', body.avatarUrl)
- if (body.author) await settingsManager.set('app', 'author', body.author)
+ await persistWizardSiteSettings(body)
return { success: true }
})
diff --git a/apps/self-hosted/server/api/wizard/submit.post.ts b/apps/self-hosted/server/api/wizard/submit.post.ts
index 363a36bb..36f8c13b 100644
--- a/apps/self-hosted/server/api/wizard/submit.post.ts
+++ b/apps/self-hosted/server/api/wizard/submit.post.ts
@@ -2,6 +2,10 @@ import { toUserSummary } from '~~/server/utils/dto'
import { defineWizardHandler } from '../../utils/wizard-handler'
import { z } from 'zod'
import { settingsManager } from '~~/server/services/settings/settingsManager'
+import {
+ persistWizardSiteSettings,
+ wizardSiteSchema,
+} from '~~/server/services/wizard/site-settings'
import { storageConfigSchema } from '#shared/types/storage'
import { useDB, tables, eq } from '~~/server/utils/db'
@@ -14,12 +18,7 @@ export default defineWizardHandler(async (event) => {
password: z.string().min(6),
username: z.string().min(2).default('admin'),
}),
- site: z.object({
- title: z.string().min(1),
- slogan: z.string().optional(),
- avatarUrl: z.string().optional(),
- author: z.string().optional(),
- }),
+ site: wizardSiteSchema,
storage: z.object({
name: z.string().min(1),
config: storageConfigSchema,
@@ -78,13 +77,7 @@ export default defineWizardHandler(async (event) => {
}
// 2. Handle Site Settings
- await settingsManager.set('app', 'title', body.site.title)
- if (body.site.slogan)
- await settingsManager.set('app', 'slogan', body.site.slogan)
- if (body.site.avatarUrl)
- await settingsManager.set('app', 'avatarUrl', body.site.avatarUrl)
- if (body.site.author)
- await settingsManager.set('app', 'author', body.site.author)
+ await persistWizardSiteSettings(body.site)
// 3. Handle Storage Settings
// Check if provider already exists to avoid duplicates if re-running?
diff --git a/apps/self-hosted/server/services/wizard/site-settings.ts b/apps/self-hosted/server/services/wizard/site-settings.ts
new file mode 100644
index 00000000..a8e74463
--- /dev/null
+++ b/apps/self-hosted/server/services/wizard/site-settings.ts
@@ -0,0 +1,38 @@
+import { z } from 'zod'
+import { settingsManager } from '../settings/settingsManager'
+
+export const wizardSiteSchema = z.object({
+ title: z.string().min(1),
+ slogan: z.string().optional(),
+ avatarUrl: z.string().optional(),
+ author: z.string().optional(),
+ homeView: z.enum(['photos', 'albums']).optional(),
+ 'appearance.theme': z.enum(['light', 'dark', 'system']).optional(),
+ 'appearance.homeHeaderLayout': z.enum(['embedded', 'fixed']).optional(),
+})
+
+export type WizardSiteSettings = z.infer<typeof wizardSiteSchema>
+
+export async function persistWizardSiteSettings(
+ site: WizardSiteSettings,
+): Promise<void> {
+ await settingsManager.set('app', 'title', site.title)
+ if (site.slogan) await settingsManager.set('app', 'slogan', site.slogan)
+ if (site.avatarUrl)
+ await settingsManager.set('app', 'avatarUrl', site.avatarUrl)
+ if (site.author) await settingsManager.set('app', 'author', site.author)
+ if (site.homeView !== undefined)
+ await settingsManager.set('app', 'homeView', site.homeView)
+ if (site['appearance.theme'] !== undefined)
+ await settingsManager.set(
+ 'app',
+ 'appearance.theme',
+ site['appearance.theme'],
+ )
+ if (site['appearance.homeHeaderLayout'] !== undefined)
+ await settingsManager.set(
+ 'app',
+ 'appearance.homeHeaderLayout',
+ site['appearance.homeHeaderLayout'],
+ )
+}
diff --git a/tests/wizard-site-persistence.test.ts b/tests/wizard-site-persistence.test.ts
new file mode 100644
index 00000000..7925bb17
--- /dev/null
+++ b/tests/wizard-site-persistence.test.ts
@@ -0,0 +1,188 @@
+import assert from 'node:assert/strict'
+import { readFileSync } from 'node:fs'
+import { createRequire } from 'node:module'
+import test from 'node:test'
+import vm from 'node:vm'
+import ts from 'typescript'
+import { z } from 'zod'
+
+const require = createRequire(import.meta.url)
+
+function wizardPayload(
+ site: Record<string, unknown> = {
+ title: 'Example gallery',
+ slogan: 'Example slogan',
+ avatarUrl: 'https://example.com/avatar.jpg',
+ author: 'Example author',
+ homeView: 'albums',
+ 'appearance.theme': 'dark',
+ 'appearance.homeHeaderLayout': 'fixed',
+ },
+) {
+ return {
+ admin: {
+ email: '[email protected]',
+ password: 'test-password-123',
+ username: 'admin',
+ },
+ site,
+ storage: {
+ name: 'Default storage',
+ config: { provider: 'local' },
+ },
+ map: { provider: 'maplibre', token: '' },
+ }
+}
+
+function loadWizardRoute(
+ payload: unknown,
+ file = 'apps/self-hosted/server/api/wizard/submit.post.ts',
+) {
+ const settingWrites: Array<[string, string, unknown]> = []
+ const existingAdmin = {
+ id: 1,
+ email: '[email protected]',
+ username: 'admin',
+ isAdmin: 1,
+ }
+ const db = {
+ select: () => ({
+ from: () => ({
+ limit: () => ({ get: () => existingAdmin }),
+ where: () => ({ get: () => existingAdmin }),
+ }),
+ }),
+ update: () => ({
+ set: () => ({
+ where: () => ({ run: async () => undefined }),
+ }),
+ }),
+ }
+ function loadTypeScriptModule(file: string) {
+ const code = ts.transpileModule(readFileSync(file, 'utf8'), {
+ compilerOptions: {
+ module: ts.ModuleKind.CommonJS,
+ target: ts.ScriptTarget.ES2022,
+ esModuleInterop: true,
+ },
+ }).outputText
+ const exports: Record<string, unknown> = {}
+
+ vm.runInNewContext(
+ code,
+ {
+ exports,
+ require: mockedRequire,
+ readValidatedBody: async (
+ _event: unknown,
+ parse: (value: unknown) => unknown,
+ ) => parse(payload),
+ hashPassword: async () => 'hashed-password',
+ setUserSession: async () => undefined,
+ createError: (options: object) =>
+ Object.assign(new Error('HTTP error'), options),
+ },
+ { filename: file },
+ )
+
+ return exports
+ }
+
+ function mockedRequire(name: string) {
+ if (name === '~~/server/utils/dto')
+ return { toUserSummary: (user: unknown) => user }
+ if (name === '../../utils/wizard-handler')
+ return { defineWizardHandler: (handler: unknown) => handler }
+ if (name === '~~/server/services/settings/settingsManager') {
+ return {
+ settingsManager: {
+ set: async (namespace: string, key: string, value: unknown) => {
+ settingWrites.push([namespace, key, value])
+ },
+ storage: { addProvider: async () => 1 },
+ },
+ }
+ }
+ if (name === '~~/server/services/wizard/site-settings') {
+ return loadTypeScriptModule(
+ 'apps/self-hosted/server/services/wizard/site-settings.ts',
+ )
+ }
+ if (name === '../settings/settingsManager')
+ return mockedRequire('~~/server/services/settings/settingsManager')
+ if (name === '#shared/types/storage') {
+ return {
+ storageConfigSchema: z.object({ provider: z.literal('local') }),
+ }
+ }
+ if (name === '~~/server/utils/db') {
+ return {
+ useDB: () => db,
+ tables: { users: { id: 'id', email: 'email' } },
+ eq: () => ({}),
+ }
+ }
+ return require(name)
+ }
+
+ const exports = loadTypeScriptModule(file)
+
+ return {
+ route: exports.default as (event: unknown) => Promise<unknown>,
+ settingWrites,
+ }
+}
+
+test('wizard submit persists non-default Site Info preferences', async () => {
+ const { route, settingWrites } = loadWizardRoute(wizardPayload())
+
+ await route({})
+
+ assert.deepEqual(
+ settingWrites.filter(([namespace]) => namespace === 'app'),
+ [
+ ['app', 'title', 'Example gallery'],
+ ['app', 'slogan', 'Example slogan'],
+ ['app', 'avatarUrl', 'https://example.com/avatar.jpg'],
+ ['app', 'author', 'Example author'],
+ ['app', 'homeView', 'albums'],
+ ['app', 'appearance.theme', 'dark'],
+ ['app', 'appearance.homeHeaderLayout', 'fixed'],
+ ],
+ )
+})
+
+test('wizard submit rejects unsupported Site Info preferences before writing settings', async () => {
+ const { route, settingWrites } = loadWizardRoute(
+ wizardPayload({
+ title: 'Example gallery',
+ homeView: 'timeline',
+ 'appearance.theme': 'neon',
+ 'appearance.homeHeaderLayout': 'floating',
+ }),
+ )
+
+ await assert.rejects(route({}))
+ assert.deepEqual(settingWrites, [])
+})
+
+test('wizard Site Info endpoint shares preference persistence behavior', async () => {
+ const { route, settingWrites } = loadWizardRoute(
+ {
+ title: 'Example gallery',
+ homeView: 'albums',
+ 'appearance.theme': 'dark',
+ 'appearance.homeHeaderLayout': 'fixed',
+ },
+ 'apps/self-hosted/server/api/wizard/site.post.ts',
+ )
+
+ await route({})
+
+ assert.deepEqual(settingWrites, [
+ ['app', 'title', 'Example gallery'],
+ ['app', 'homeView', 'albums'],
+ ['app', 'appearance.theme', 'dark'],
+ ['app', 'appearance.homeHeaderLayout', 'fixed'],
+ ])
+})