Fix Kalender-Suche: nur zukuenftige Events cachen, nicht historische ab 2003
This commit is contained in:
@@ -62,7 +62,11 @@ router.post('/sync-calendar', authenticate, async (req, res) => {
|
||||
if (!feeds.length) return res.json({ ok: true, synced: 0, message: 'Keine Kalender-Abos vorhanden' });
|
||||
|
||||
// Alter des Caches prüfen
|
||||
if (!force) {
|
||||
// Alte Cache-Prüfung: Wenn noch historische Events drin (start_dt < 2000), force-sync
|
||||
const hasOldData = safeGet(() =>
|
||||
db.prepare("SELECT 1 FROM calendar_event_cache WHERE user_id=? AND start_dt < '20200101' LIMIT 1").get(uid)
|
||||
);
|
||||
if (!force && !hasOldData) {
|
||||
const lastSync = safeGet(() =>
|
||||
db.prepare("SELECT MAX(synced_at) as s FROM calendar_event_cache WHERE user_id=?").get(uid)?.s
|
||||
);
|
||||
@@ -80,6 +84,14 @@ router.post('/sync-calendar', authenticate, async (req, res) => {
|
||||
if (!raw) { errors.push(`${feed.name}: kein gültiger iCal-Feed`); continue; }
|
||||
const events = parseIcal(raw);
|
||||
if (!events.length) { errors.push(`${feed.name}: 0 Events geparst`); continue; }
|
||||
|
||||
// Nur zukünftige Events speichern (ab gestern rückwärts 1 Tag für Tagesevents)
|
||||
// start_dt Format: YYYYMMDD oder YYYYMMDDTHHmmssZ → string-vergleichbar
|
||||
const yesterday = new Date();
|
||||
yesterday.setDate(yesterday.getDate() - 1);
|
||||
const minDt = yesterday.toISOString().replace(/-/g,'').slice(0,8); // '20260604'
|
||||
const futureEvents = events.filter(ev => !ev.start || ev.start >= minDt);
|
||||
|
||||
// prepare INSIDE try-catch – wirft wenn Tabelle nicht existiert
|
||||
const ins = db.prepare(`
|
||||
INSERT INTO calendar_event_cache
|
||||
@@ -88,11 +100,11 @@ router.post('/sync-calendar', authenticate, async (req, res) => {
|
||||
`);
|
||||
db.transaction(() => {
|
||||
db.prepare('DELETE FROM calendar_event_cache WHERE feed_id=?').run(feed.id);
|
||||
events.slice(0, 300).forEach(ev =>
|
||||
futureEvents.slice(0, 500).forEach(ev =>
|
||||
ins.run(feed.id, uid, ev.summary||'', ev.description||'', ev.location||'', ev.start||'', ev.end||'')
|
||||
);
|
||||
})();
|
||||
totalSynced += events.length;
|
||||
totalSynced += futureEvents.length;
|
||||
} catch(e) {
|
||||
console.error(`[CalendarSync] Feed "${feed.name}" (${feed.url}): ${e.message}`);
|
||||
errors.push(`${feed.name}: ${e.message}`);
|
||||
|
||||
Reference in New Issue
Block a user