settings.js

3.9 KB v1.0
// 用户设置读写(外观 / 同步 / 编辑器 / 备份)
import { Router } from 'express';
import { db } from '../db.js';
import { encrypt } from '../crypto.js';
import {
  getRawSettings,
  getClientSettings,
  saveSettings,
  deepMerge,
} from '../settings.js';
import { requireAuth } from '../auth.js';

const router = Router();
router.use(requireAuth);

function clamp(value, min, max, fallback) {
  const num = Number(value);
  if (!Number.isFinite(num)) return fallback;
  return Math.min(max, Math.max(min, num));
}

router.get('/', (req, res) => {
  res.json(getClientSettings(req.user.id));
});

router.put('/', (req, res) => {
  const current = getRawSettings(req.user.id);
  const body = req.body && typeof req.body === 'object' ? structuredClone(req.body) : {};

  // 单独处理 WebDAV 密码:明文仅用于服务端加密,不进入设置 JSON
  let newWebdavPassword = null;
  let clearWebdavPassword = false;
  if (body.backup && typeof body.backup === 'object') {
    newWebdavPassword = body.backup.webdavPassword;
    clearWebdavPassword = Boolean(body.backup.clearWebdavPassword);
    delete body.backup.webdavPassword;
    delete body.backup.clearWebdavPassword;
    delete body.backup.hasWebdavPassword;
    // 不允许前端直接写入加密字段
    delete body.backup.webdavPasswordEnc;
  }

  // Telegram 通知凭据:与 WebDAV 密码一样只保存加密值
  let newBotToken = null;
  let newChatId = null;
  let clearTelegram = false;
  if (body.notifications && typeof body.notifications === 'object') {
    newBotToken = body.notifications.botToken;
    newChatId = body.notifications.chatId;
    clearTelegram = Boolean(body.notifications.clearCredentials);
    delete body.notifications.botToken;
    delete body.notifications.chatId;
    delete body.notifications.clearCredentials;
    delete body.notifications.hasBotToken;
    delete body.notifications.hasChatId;
    delete body.notifications.botTokenEnc;
    delete body.notifications.chatIdEnc;
  }

  const next = deepMerge(current, body);

  if (clearWebdavPassword) next.backup.webdavPasswordEnc = '';
  if (typeof newWebdavPassword === 'string' && newWebdavPassword !== '') {
    next.backup.webdavPasswordEnc = encrypt(newWebdavPassword);
  }

  if (clearTelegram) {
    next.notifications.botTokenEnc = '';
    next.notifications.chatIdEnc = '';
  }
  if (typeof newBotToken === 'string' && newBotToken.trim()) {
    next.notifications.botTokenEnc = encrypt(newBotToken.trim());
  }
  if (typeof newChatId === 'string' && newChatId.trim()) {
    next.notifications.chatIdEnc = encrypt(newChatId.trim());
  }
  next.notifications.enabled = Boolean(next.notifications.enabled);

  // 数值范围校正
  next.appearance.fontSize = clamp(next.appearance.fontSize, 12, 22, 15);
  next.appearance.lineHeight = clamp(next.appearance.lineHeight, 1.2, 2.2, 1.6);
  next.sync.rangeDays = clamp(next.sync.rangeDays, 1, 365, 30);
  next.sync.folderLimit = clamp(next.sync.folderLimit, 20, 2000, 300);
  next.sync.intervalMinutes = clamp(next.sync.intervalMinutes, 1, 1440, 15);
  next.editor.fontSize = clamp(next.editor.fontSize, 12, 22, 15);
  next.editor.lineHeight = clamp(next.editor.lineHeight, 1.2, 2.2, 1.7);
  next.backup.intervalHours = clamp(next.backup.intervalHours, 1, 720, 24);
  next.backup.days = clamp(next.backup.days, 1, 3650, 90);
  if (!Array.isArray(next.backup.accountIds)) next.backup.accountIds = [];

  // 默认发件账号必须属于当前用户
  if (next.editor.defaultAccountId) {
    const owned = db
      .prepare('SELECT id FROM accounts WHERE id = ? AND user_id = ?')
      .get(Number(next.editor.defaultAccountId), req.user.id);
    if (!owned) next.editor.defaultAccountId = null;
    else next.editor.defaultAccountId = Number(next.editor.defaultAccountId);
  } else {
    next.editor.defaultAccountId = null;
  }

  saveSettings(req.user.id, next);
  res.json(getClientSettings(req.user.id));
});

export default router;