reworked settings page
This commit is contained in:
@@ -794,6 +794,15 @@ db.exec(`
|
||||
);
|
||||
`);
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS maintenance_settings (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
search_retention_days INTEGER DEFAULT ${SEARCH_POST_RETENTION_DAYS},
|
||||
auto_purge_hidden INTEGER DEFAULT 1,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`);
|
||||
|
||||
db.exec(`
|
||||
CREATE INDEX IF NOT EXISTS idx_search_seen_posts_last_seen_at
|
||||
ON search_seen_posts(last_seen_at);
|
||||
@@ -1546,9 +1555,62 @@ function getFormattedCredentialById(id) {
|
||||
return formatCredentialRow(row[0]);
|
||||
}
|
||||
|
||||
function normalizeRetentionDays(value) {
|
||||
const parsed = parseInt(value, 10);
|
||||
if (Number.isNaN(parsed) || parsed <= 0) {
|
||||
return SEARCH_POST_RETENTION_DAYS;
|
||||
}
|
||||
return Math.min(365, Math.max(1, parsed));
|
||||
}
|
||||
|
||||
function loadHiddenSettings() {
|
||||
let settings = db.prepare('SELECT * FROM maintenance_settings WHERE id = 1').get();
|
||||
if (!settings) {
|
||||
const defaults = {
|
||||
id: 1,
|
||||
search_retention_days: SEARCH_POST_RETENTION_DAYS,
|
||||
auto_purge_hidden: 1
|
||||
};
|
||||
db.prepare(`
|
||||
INSERT INTO maintenance_settings (id, search_retention_days, auto_purge_hidden, updated_at)
|
||||
VALUES (1, ?, ?, CURRENT_TIMESTAMP)
|
||||
`).run(defaults.search_retention_days, defaults.auto_purge_hidden);
|
||||
settings = defaults;
|
||||
}
|
||||
settings.search_retention_days = normalizeRetentionDays(settings.search_retention_days);
|
||||
settings.auto_purge_hidden = settings.auto_purge_hidden ? 1 : 0;
|
||||
return settings;
|
||||
}
|
||||
|
||||
function persistHiddenSettings({ retentionDays, autoPurgeEnabled }) {
|
||||
const normalizedRetention = normalizeRetentionDays(retentionDays);
|
||||
const normalizedAuto = autoPurgeEnabled ? 1 : 0;
|
||||
const existing = db.prepare('SELECT id FROM maintenance_settings WHERE id = 1').get();
|
||||
if (existing) {
|
||||
db.prepare(`
|
||||
UPDATE maintenance_settings
|
||||
SET search_retention_days = ?, auto_purge_hidden = ?, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE id = 1
|
||||
`).run(normalizedRetention, normalizedAuto);
|
||||
} else {
|
||||
db.prepare(`
|
||||
INSERT INTO maintenance_settings (id, search_retention_days, auto_purge_hidden, updated_at)
|
||||
VALUES (1, ?, ?, CURRENT_TIMESTAMP)
|
||||
`).run(normalizedRetention, normalizedAuto);
|
||||
}
|
||||
return {
|
||||
auto_purge_enabled: !!normalizedAuto,
|
||||
retention_days: normalizedRetention
|
||||
};
|
||||
}
|
||||
|
||||
function cleanupExpiredSearchPosts() {
|
||||
try {
|
||||
const threshold = `-${SEARCH_POST_RETENTION_DAYS} day`;
|
||||
const settings = loadHiddenSettings();
|
||||
if (!settings.auto_purge_hidden) {
|
||||
return;
|
||||
}
|
||||
const threshold = `-${settings.search_retention_days} day`;
|
||||
db.prepare(`
|
||||
DELETE FROM search_seen_posts
|
||||
WHERE last_seen_at < DATETIME('now', ?)
|
||||
@@ -3197,6 +3259,33 @@ app.put('/api/ai-settings', (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/hidden-settings', (req, res) => {
|
||||
try {
|
||||
const settings = loadHiddenSettings();
|
||||
res.json({
|
||||
auto_purge_enabled: !!settings.auto_purge_hidden,
|
||||
retention_days: settings.search_retention_days
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/api/hidden-settings', (req, res) => {
|
||||
try {
|
||||
const body = req.body || {};
|
||||
const retentionDays = normalizeRetentionDays(body.retention_days);
|
||||
const autoPurgeEnabled = !!body.auto_purge_enabled;
|
||||
const saved = persistHiddenSettings({ retentionDays, autoPurgeEnabled });
|
||||
if (saved.auto_purge_enabled) {
|
||||
cleanupExpiredSearchPosts();
|
||||
}
|
||||
res.json(saved);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
function sanitizeAIComment(text) {
|
||||
if (!text) {
|
||||
return '';
|
||||
|
||||
Reference in New Issue
Block a user