aktueller Stand
This commit is contained in:
63
server.js
63
server.js
@@ -11,6 +11,7 @@ const adminConfig = require('./services/adminConfig');
|
||||
|
||||
const ONE_YEAR_MS = 365 * 24 * 60 * 60 * 1000;
|
||||
const adminEmail = (process.env.ADMIN_EMAIL || '').toLowerCase();
|
||||
const SIXTY_DAYS_MS = 60 * 24 * 60 * 60 * 1000;
|
||||
|
||||
const app = express();
|
||||
const port = process.env.PORT || 3000;
|
||||
@@ -82,6 +83,32 @@ function mergeStoresIntoConfig(config = [], stores = []) {
|
||||
return { merged: Array.from(map.values()), changed };
|
||||
}
|
||||
|
||||
async function loadStoresForSession(session, settings, { forceRefresh = false } = {}) {
|
||||
if (!session?.profile?.id) {
|
||||
return { stores: [], refreshed: false };
|
||||
}
|
||||
|
||||
const cache = session.storesCache;
|
||||
const now = Date.now();
|
||||
const isCacheValid =
|
||||
cache &&
|
||||
Array.isArray(cache.data) &&
|
||||
Number.isFinite(cache.fetchedAt) &&
|
||||
now - cache.fetchedAt <= SIXTY_DAYS_MS;
|
||||
|
||||
if (isCacheValid && !forceRefresh) {
|
||||
return { stores: cache.data, refreshed: false };
|
||||
}
|
||||
|
||||
const stores = await foodsharingClient.fetchStores(session.cookieHeader, session.profile.id, {
|
||||
delayBetweenRequestsMs: settings.storePickupCheckDelayMs
|
||||
});
|
||||
sessionStore.update(session.id, {
|
||||
storesCache: { data: stores, fetchedAt: now }
|
||||
});
|
||||
return { stores, refreshed: true };
|
||||
}
|
||||
|
||||
async function restoreSessionsFromDisk() {
|
||||
const saved = credentialStore.loadAll();
|
||||
const entries = Object.entries(saved);
|
||||
@@ -126,6 +153,9 @@ async function restoreSessionsFromDisk() {
|
||||
password: credentials.password,
|
||||
token: session.id
|
||||
});
|
||||
sessionStore.update(session.id, {
|
||||
storesCache: { data: stores, fetchedAt: Date.now() }
|
||||
});
|
||||
scheduleConfig(session.id, config, schedulerSettings);
|
||||
console.log(`[RESTORE] Session fuer Profil ${profile.id} (${profile.name}) reaktiviert.`);
|
||||
} catch (error) {
|
||||
@@ -198,6 +228,9 @@ app.post('/api/auth/login', async (req, res) => {
|
||||
}, existingToken, ONE_YEAR_MS);
|
||||
|
||||
credentialStore.save(profile.id, { email, password, token: session.id });
|
||||
sessionStore.update(session.id, {
|
||||
storesCache: { data: stores, fetchedAt: Date.now() }
|
||||
});
|
||||
scheduleConfig(session.id, config, settings);
|
||||
|
||||
return res.json({
|
||||
@@ -222,14 +255,13 @@ app.post('/api/auth/logout', requireAuth, (req, res) => {
|
||||
|
||||
app.get('/api/auth/session', requireAuth, async (req, res) => {
|
||||
const settings = adminConfig.readSettings();
|
||||
const stores = await foodsharingClient.fetchStores(req.session.cookieHeader, req.session.profile.id, {
|
||||
delayBetweenRequestsMs: settings.storePickupCheckDelayMs
|
||||
});
|
||||
let config = readConfig(req.session.profile.id);
|
||||
const { merged, changed } = mergeStoresIntoConfig(config, stores);
|
||||
if (changed) {
|
||||
config = merged;
|
||||
writeConfig(req.session.profile.id, config);
|
||||
const { stores, refreshed } = await loadStoresForSession(req.session, settings);
|
||||
if (refreshed) {
|
||||
let config = readConfig(req.session.profile.id);
|
||||
const { merged, changed } = mergeStoresIntoConfig(config, stores);
|
||||
if (changed) {
|
||||
writeConfig(req.session.profile.id, merged);
|
||||
}
|
||||
}
|
||||
res.json({
|
||||
profile: req.session.profile,
|
||||
@@ -262,14 +294,13 @@ app.post('/api/config', requireAuth, (req, res) => {
|
||||
|
||||
app.get('/api/stores', requireAuth, async (req, res) => {
|
||||
const settings = adminConfig.readSettings();
|
||||
const stores = await foodsharingClient.fetchStores(req.session.cookieHeader, req.session.profile.id, {
|
||||
delayBetweenRequestsMs: settings.storePickupCheckDelayMs
|
||||
});
|
||||
let config = readConfig(req.session.profile.id);
|
||||
const { merged, changed } = mergeStoresIntoConfig(config, stores);
|
||||
if (changed) {
|
||||
config = merged;
|
||||
writeConfig(req.session.profile.id, config);
|
||||
const { stores, refreshed } = await loadStoresForSession(req.session, settings, { forceRefresh: true });
|
||||
if (refreshed) {
|
||||
let config = readConfig(req.session.profile.id);
|
||||
const { merged, changed } = mergeStoresIntoConfig(config, stores);
|
||||
if (changed) {
|
||||
writeConfig(req.session.profile.id, merged);
|
||||
}
|
||||
}
|
||||
res.json(stores);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user