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.

1.7 KB
Raw Blame
import { and as siteAnd, eq as siteEq } from 'drizzle-orm'
import { DEFAULT_SITE } from '@chronoframe/core'
import type { PublicSettingsResponse } from '@chronoframe/contracts/settings'
import { useDB, tables, eq, or, and } from '~~/server/utils/db'

/**
 * 获取所有公开设置
 * 仅返回 isPublic=true 的设置,按命名空间分组
 * 特例:始终返回 system:firstLaunch 以便前端判断是否需要初始化
 */
export default eventHandler(async () => {
  const db = useDB()

  // 查询所有公开设置
  const allSettings = db
    .select()
    .from(tables.settings)
    .where(
      siteAnd(
        or(
          and(
            eq(tables.settings.isPublic, true),
            eq(tables.settings.isSecret, false),
          ),
          and(
            eq(tables.settings.namespace, 'system'),
            eq(tables.settings.key, 'firstLaunch'),
          ),
        ),
        siteEq(tables.settings.siteId, DEFAULT_SITE.id),
      ),
    )
    .all()

  // 按命名空间分组
  const grouped: Record<string, Record<string, any>> = {}

  for (const setting of allSettings) {
    if (!grouped[setting.namespace]) {
      grouped[setting.namespace] = {}
    }

    // 解析值
    let value: any = setting.value
    try {
      if (setting.type === 'json') {
        value = setting.value ? JSON.parse(setting.value) : null
      } else if (setting.type === 'number') {
        value = setting.value ? Number(setting.value) : null
      } else if (setting.type === 'boolean') {
        value = setting.value === 'true' || setting.value === '1'
      }
    } catch {
      // 保持原始值
    }

    grouped[setting.namespace][setting.key] = value
  }

  return {
    timestamp: Date.now(),
    data: grouped,
  } satisfies PublicSettingsResponse
})