Compare commits
No commits in common. "2876469e915d4dce3b77af6bccb7d6705b29506a" and "9677d1e71a5e911526711c89f65f98230b456aa8" have entirely different histories.
2876469e91
...
9677d1e71a
27 changed files with 153 additions and 930 deletions
103
backend/cache.py
103
backend/cache.py
|
|
@ -1,103 +0,0 @@
|
||||||
"""BAN YARO — In-Memory TTL-Cache für statische DB-Daten.
|
|
||||||
|
|
||||||
Hintergrund:
|
|
||||||
Routes wie /api/training/exercises, /api/help, /api/wiki/stats laden bei
|
|
||||||
jedem Request statische Daten aus der DB. Das ist verschwendete Energie.
|
|
||||||
Diese Daten ändern sich nur durch Admin-Aktionen.
|
|
||||||
|
|
||||||
Verwendung:
|
|
||||||
from cache import ttl_cache
|
|
||||||
|
|
||||||
@ttl_cache(ttl=3600)
|
|
||||||
def my_func(arg1, arg2):
|
|
||||||
...
|
|
||||||
|
|
||||||
API:
|
|
||||||
@ttl_cache(ttl=3600) Decorator – cached pro Argumenten-Signatur
|
|
||||||
my_func.cache_clear() Komplett leeren (z.B. nach Admin-Update)
|
|
||||||
|
|
||||||
Hinweis:
|
|
||||||
Diese Implementierung ist absichtlich klein und ohne externe Dependency
|
|
||||||
(kein cachetools nötig). Thread-safe via Lock. Reicht für Read-Only-
|
|
||||||
Listen, die sich selten ändern. Niemals für user-spezifische Daten
|
|
||||||
verwenden!
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import functools
|
|
||||||
import threading
|
|
||||||
import time
|
|
||||||
from typing import Any, Callable
|
|
||||||
|
|
||||||
|
|
||||||
def ttl_cache(ttl: int = 3600, maxsize: int = 128) -> Callable:
|
|
||||||
"""Decorator: cached Rückgabewert pro Argumenten-Signatur für `ttl` Sek.
|
|
||||||
|
|
||||||
- ttl: Time-to-live in Sekunden (Default: 1 Stunde)
|
|
||||||
- maxsize: max. Anzahl Einträge im Cache (FIFO-Eviction bei Überlauf)
|
|
||||||
|
|
||||||
Die dekorierte Funktion bekommt zusätzlich:
|
|
||||||
.cache_clear() – leert den gesamten Cache
|
|
||||||
.cache_info() – {hits, misses, size, ttl, maxsize}
|
|
||||||
"""
|
|
||||||
def decorator(func: Callable) -> Callable:
|
|
||||||
store: dict[tuple, tuple[float, Any]] = {}
|
|
||||||
lock = threading.Lock()
|
|
||||||
stats = {"hits": 0, "misses": 0}
|
|
||||||
|
|
||||||
def _make_key(args: tuple, kwargs: dict) -> tuple:
|
|
||||||
# kwargs als sortiertes Tuple in den Key packen
|
|
||||||
if kwargs:
|
|
||||||
return args + tuple(sorted(kwargs.items()))
|
|
||||||
return args
|
|
||||||
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
key = _make_key(args, kwargs)
|
|
||||||
now = time.monotonic()
|
|
||||||
with lock:
|
|
||||||
cached = store.get(key)
|
|
||||||
if cached is not None:
|
|
||||||
expires_at, value = cached
|
|
||||||
if expires_at > now:
|
|
||||||
stats["hits"] += 1
|
|
||||||
return value
|
|
||||||
# abgelaufen → raus
|
|
||||||
del store[key]
|
|
||||||
stats["misses"] += 1
|
|
||||||
|
|
||||||
# Außerhalb des Locks ausführen (kann DB-Calls machen)
|
|
||||||
value = func(*args, **kwargs)
|
|
||||||
|
|
||||||
with lock:
|
|
||||||
# FIFO-Eviction, wenn maxsize überschritten
|
|
||||||
if len(store) >= maxsize:
|
|
||||||
try:
|
|
||||||
oldest_key = next(iter(store))
|
|
||||||
del store[oldest_key]
|
|
||||||
except StopIteration:
|
|
||||||
pass
|
|
||||||
store[key] = (now + ttl, value)
|
|
||||||
return value
|
|
||||||
|
|
||||||
def cache_clear() -> None:
|
|
||||||
with lock:
|
|
||||||
store.clear()
|
|
||||||
stats["hits"] = 0
|
|
||||||
stats["misses"] = 0
|
|
||||||
|
|
||||||
def cache_info() -> dict:
|
|
||||||
with lock:
|
|
||||||
return {
|
|
||||||
"hits": stats["hits"],
|
|
||||||
"misses": stats["misses"],
|
|
||||||
"size": len(store),
|
|
||||||
"ttl": ttl,
|
|
||||||
"maxsize": maxsize,
|
|
||||||
}
|
|
||||||
|
|
||||||
wrapper.cache_clear = cache_clear # type: ignore[attr-defined]
|
|
||||||
wrapper.cache_info = cache_info # type: ignore[attr-defined]
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
return decorator
|
|
||||||
|
|
@ -180,8 +180,6 @@ def init_db():
|
||||||
anz_bewertungen INTEGER DEFAULT 0,
|
anz_bewertungen INTEGER DEFAULT 0,
|
||||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS idx_routes_user ON routes(user_id, created_at DESC);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS route_walks (
|
CREATE TABLE IF NOT EXISTS route_walks (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
|
@ -274,8 +272,6 @@ def init_db():
|
||||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||||
edited_at TEXT
|
edited_at TEXT
|
||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS idx_forum_posts_thread ON forum_posts(thread_id, created_at ASC);
|
|
||||||
CREATE INDEX IF NOT EXISTS idx_forum_posts_user ON forum_posts(user_id, created_at DESC);
|
|
||||||
|
|
||||||
-- PUSH SUBSCRIPTIONS (alternativ zu users.push_sub für mehrere Geräte)
|
-- PUSH SUBSCRIPTIONS (alternativ zu users.push_sub für mehrere Geräte)
|
||||||
CREATE TABLE IF NOT EXISTS push_subscriptions (
|
CREATE TABLE IF NOT EXISTS push_subscriptions (
|
||||||
|
|
|
||||||
|
|
@ -410,7 +410,7 @@ async def serve_media(path: str, request: _Request):
|
||||||
raise _HE(404, "Nicht gefunden.")
|
raise _HE(404, "Nicht gefunden.")
|
||||||
return _media_response(filepath)
|
return _media_response(filepath)
|
||||||
|
|
||||||
APP_VER = "1090" # muss mit APP_VER in app.js übereinstimmen
|
APP_VER = "1070" # muss mit APP_VER in app.js übereinstimmen
|
||||||
|
|
||||||
@app.get("/.well-known/assetlinks.json")
|
@app.get("/.well-known/assetlinks.json")
|
||||||
async def assetlinks():
|
async def assetlinks():
|
||||||
|
|
|
||||||
|
|
@ -359,7 +359,7 @@ async def list_users(
|
||||||
SELECT u.id, u.name, {_email_col}, u.rolle, u.is_premium,
|
SELECT u.id, u.name, {_email_col}, u.rolle, u.is_premium,
|
||||||
u.is_moderator, u.is_banned, u.ban_reason,
|
u.is_moderator, u.is_banned, u.ban_reason,
|
||||||
u.is_founder, u.is_partner, u.founder_number,
|
u.is_founder, u.is_partner, u.founder_number,
|
||||||
u.created_at, u.last_login, u.last_seen, u.subscription_tier,
|
u.created_at, u.last_login, u.subscription_tier,
|
||||||
(SELECT COUNT(*) FROM dogs d WHERE d.user_id=u.id) AS dog_count,
|
(SELECT COUNT(*) FROM dogs d WHERE d.user_id=u.id) AS dog_count,
|
||||||
(SELECT COUNT(*) FROM forum_threads t WHERE t.user_id=u.id AND t.is_deleted=0) AS thread_count,
|
(SELECT COUNT(*) FROM forum_threads t WHERE t.user_id=u.id AND t.is_deleted=0) AS thread_count,
|
||||||
ROUND(COALESCE((SELECT SUM(r.distanz_km) FROM routes r WHERE r.user_id=u.id), 0), 1) AS total_km,
|
ROUND(COALESCE((SELECT SUM(r.distanz_km) FROM routes r WHERE r.user_id=u.id), 0), 1) AS total_km,
|
||||||
|
|
@ -1139,16 +1139,7 @@ async def list_upgrade_requests(user=Depends(require_admin)):
|
||||||
SELECT r.id, r.user_id, r.tier, r.message, r.created_at, r.fulfilled_at,
|
SELECT r.id, r.user_id, r.tier, r.message, r.created_at, r.fulfilled_at,
|
||||||
u.name, u.email, u.billing_address,
|
u.name, u.email, u.billing_address,
|
||||||
u.is_founder, u.is_founder_pending, u.referred_by,
|
u.is_founder, u.is_founder_pending, u.referred_by,
|
||||||
COALESCE((SELECT COUNT(*) FROM users WHERE referred_by=u.id), 0) AS referral_count,
|
COALESCE((SELECT COUNT(*) FROM users WHERE referred_by=u.id), 0) AS referral_count
|
||||||
(SELECT id FROM invoices i WHERE i.user_id=r.user_id
|
|
||||||
AND i.status IN ('draft','sent')
|
|
||||||
ORDER BY i.created_at DESC LIMIT 1) AS existing_invoice_id,
|
|
||||||
(SELECT invoice_number FROM invoices i WHERE i.user_id=r.user_id
|
|
||||||
AND i.status IN ('draft','sent')
|
|
||||||
ORDER BY i.created_at DESC LIMIT 1) AS existing_invoice_number,
|
|
||||||
(SELECT status FROM invoices i WHERE i.user_id=r.user_id
|
|
||||||
AND i.status IN ('draft','sent')
|
|
||||||
ORDER BY i.created_at DESC LIMIT 1) AS existing_invoice_status
|
|
||||||
FROM upgrade_requests r
|
FROM upgrade_requests r
|
||||||
JOIN users u ON u.id = r.user_id
|
JOIN users u ON u.id = r.user_id
|
||||||
ORDER BY r.fulfilled_at IS NOT NULL, r.created_at DESC
|
ORDER BY r.fulfilled_at IS NOT NULL, r.created_at DESC
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,6 @@ from auth import get_current_user_optional as get_optional_user
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
_RADIUS_M = 20_000 # 20 km
|
_RADIUS_M = 20_000 # 20 km
|
||||||
_RADIUS_KM = _RADIUS_M / 1000.0
|
|
||||||
|
|
||||||
|
|
||||||
def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
||||||
|
|
@ -21,36 +20,15 @@ def _haversine(lat1: float, lon1: float, lat2: float, lon2: float) -> float:
|
||||||
return 2 * R * math.asin(math.sqrt(a))
|
return 2 * R * math.asin(math.sqrt(a))
|
||||||
|
|
||||||
|
|
||||||
def _bbox(lat: float, lon: float, radius_km: float) -> tuple[float, float, float, float]:
|
|
||||||
"""Bounding-Box-Approximation für lat/lon innerhalb radius_km."""
|
|
||||||
lat_delta = radius_km / 111.0
|
|
||||||
# cos darf bei Polen nicht 0 werden → mit kleinem Minimum absichern
|
|
||||||
cos_lat = max(abs(math.cos(math.radians(lat))), 0.01)
|
|
||||||
lon_delta = radius_km / (111.0 * cos_lat)
|
|
||||||
return (lat - lat_delta, lat + lat_delta, lon - lon_delta, lon + lon_delta)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("")
|
@router.get("")
|
||||||
async def nearby_alerts(lat: float, lon: float, user=Depends(get_optional_user)):
|
async def nearby_alerts(lat: float, lon: float, user=Depends(get_optional_user)):
|
||||||
now = datetime.utcnow().isoformat()
|
now = datetime.utcnow().isoformat()
|
||||||
lat_min, lat_max, lon_min, lon_max = _bbox(lat, lon, _RADIUS_KM)
|
|
||||||
with db() as conn:
|
with db() as conn:
|
||||||
# Bounding-Box-Vorfilter per SQL (billig) → reduziert die Kandidaten
|
|
||||||
# auf ~10 Einträge statt "alle". Die exakte Haversine-Prüfung passiert
|
|
||||||
# anschließend in Python.
|
|
||||||
poisons = conn.execute(
|
poisons = conn.execute(
|
||||||
"""SELECT lat, lon FROM poison
|
"SELECT lat, lon FROM poison WHERE geloest=0 AND expires_at > ?", (now,)
|
||||||
WHERE geloest=0 AND expires_at > ?
|
|
||||||
AND lat BETWEEN ? AND ?
|
|
||||||
AND lon BETWEEN ? AND ?""",
|
|
||||||
(now, lat_min, lat_max, lon_min, lon_max)
|
|
||||||
).fetchall()
|
).fetchall()
|
||||||
lost = conn.execute(
|
lost = conn.execute(
|
||||||
"""SELECT lat, lon FROM lost_dogs
|
"SELECT lat, lon FROM lost_dogs WHERE is_active=1"
|
||||||
WHERE is_active=1
|
|
||||||
AND lat BETWEEN ? AND ?
|
|
||||||
AND lon BETWEEN ? AND ?""",
|
|
||||||
(lat_min, lat_max, lon_min, lon_max)
|
|
||||||
).fetchall()
|
).fetchall()
|
||||||
# Letzten Standort des Users für geo-basierte Push-Filter speichern
|
# Letzten Standort des Users für geo-basierte Push-Filter speichern
|
||||||
if user:
|
if user:
|
||||||
|
|
|
||||||
|
|
@ -479,10 +479,3 @@ async def select_primary_dog(body: dict, user=Depends(get_current_user)):
|
||||||
"UPDATE users SET needs_dog_selection=0 WHERE id=?", (user["id"],)
|
"UPDATE users SET needs_dog_selection=0 WHERE id=?", (user["id"],)
|
||||||
)
|
)
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/heartbeat")
|
|
||||||
async def heartbeat(user=Depends(get_current_user)):
|
|
||||||
with db() as conn:
|
|
||||||
conn.execute("UPDATE users SET last_seen=datetime('now') WHERE id=?", (user["id"],))
|
|
||||||
return {"ok": True}
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Form
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
import os, logging, asyncio
|
import os, logging
|
||||||
from database import db
|
from database import db
|
||||||
from auth import get_current_user, get_current_user_optional
|
from auth import get_current_user, get_current_user_optional
|
||||||
from media_utils import validate_upload, generate_preview
|
from media_utils import validate_upload, generate_preview
|
||||||
|
|
@ -112,37 +112,27 @@ async def upload_photo(
|
||||||
file_uuid = str(uuid.uuid4())
|
file_uuid = str(uuid.uuid4())
|
||||||
file_path = os.path.join(save_dir, f"{file_uuid}.webp")
|
file_path = os.path.join(save_dir, f"{file_uuid}.webp")
|
||||||
|
|
||||||
# Blockierende Bildverarbeitung in Threadpool auslagern,
|
|
||||||
# damit der Event-Loop für andere Requests frei bleibt.
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
|
|
||||||
def _write_bytes(p: str, data: bytes) -> None:
|
|
||||||
with open(p, "wb") as f:
|
|
||||||
f.write(data)
|
|
||||||
|
|
||||||
# Thumbnail erzeugen
|
# Thumbnail erzeugen
|
||||||
thumb_bytes = await loop.run_in_executor(
|
thumb_bytes = generate_preview(raw_data, ext)
|
||||||
None, lambda: generate_preview(raw_data, ext)
|
|
||||||
)
|
|
||||||
thumb_path = None
|
thumb_path = None
|
||||||
if thumb_bytes:
|
if thumb_bytes:
|
||||||
thumb_path = os.path.join(save_dir, f"{file_uuid}_thumb.webp")
|
thumb_path = os.path.join(save_dir, f"{file_uuid}_thumb.webp")
|
||||||
await loop.run_in_executor(None, lambda: _write_bytes(thumb_path, thumb_bytes))
|
with open(thumb_path, "wb") as f:
|
||||||
|
f.write(thumb_bytes)
|
||||||
|
|
||||||
# Originalbild konvertieren und speichern (Pillow direkt — WebP-Qualität 85)
|
# Originalbild konvertieren und speichern
|
||||||
def _save_original():
|
# generate_preview liefert WebP, für das Original nehmen wir Pillow direkt
|
||||||
try:
|
try:
|
||||||
import io
|
import io
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
img = Image.open(io.BytesIO(raw_data))
|
img = Image.open(io.BytesIO(raw_data))
|
||||||
img = ImageOps.exif_transpose(img)
|
img = ImageOps.exif_transpose(img)
|
||||||
img = img.convert("RGB")
|
img = img.convert("RGB")
|
||||||
img.save(file_path, format="WEBP", quality=85)
|
img.save(file_path, format="WEBP", quality=85)
|
||||||
except Exception:
|
except Exception:
|
||||||
# Fallback: Rohdaten speichern
|
# Fallback: Rohdaten speichern
|
||||||
_write_bytes(file_path, raw_data)
|
with open(file_path, "wb") as f:
|
||||||
|
f.write(raw_data)
|
||||||
await loop.run_in_executor(None, _save_original)
|
|
||||||
|
|
||||||
# Relative Pfade für DB (relativ zu MEDIA_DIR)
|
# Relative Pfade für DB (relativ zu MEDIA_DIR)
|
||||||
rel_file = os.path.relpath(file_path, MEDIA_DIR)
|
rel_file = os.path.relpath(file_path, MEDIA_DIR)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
"""BAN YARO — Tagebuch Routes"""
|
"""BAN YARO — Tagebuch Routes"""
|
||||||
|
|
||||||
import os, uuid, json, math, logging, asyncio
|
import os, uuid, json, math, logging
|
||||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
@ -684,13 +684,7 @@ async def upload_media(dog_id: int, entry_id: int,
|
||||||
validate_upload(raw_data, file.filename or "")
|
validate_upload(raw_data, file.filename or "")
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise HTTPException(415, str(e))
|
raise HTTPException(415, str(e))
|
||||||
|
raw_data, ext = convert_media(raw_data, file.filename or "")
|
||||||
# Blockierende Bild-/Video-Konvertierung in Threadpool auslagern,
|
|
||||||
# damit der Event-Loop für andere Requests frei bleibt.
|
|
||||||
loop = asyncio.get_event_loop()
|
|
||||||
raw_data, ext = await loop.run_in_executor(
|
|
||||||
None, lambda: convert_media(raw_data, file.filename or "")
|
|
||||||
)
|
|
||||||
if not ext:
|
if not ext:
|
||||||
ext = ".jpg"
|
ext = ".jpg"
|
||||||
filename = f"diary_{entry_id}_{uuid.uuid4().hex[:8]}{ext}"
|
filename = f"diary_{entry_id}_{uuid.uuid4().hex[:8]}{ext}"
|
||||||
|
|
@ -698,21 +692,17 @@ async def upload_media(dog_id: int, entry_id: int,
|
||||||
media_type = _guess_media_type(ct, file.filename or "")
|
media_type = _guess_media_type(ct, file.filename or "")
|
||||||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||||||
|
|
||||||
def _write_bytes(p: str, data: bytes) -> None:
|
with open(path, "wb") as f:
|
||||||
with open(p, "wb") as f:
|
f.write(raw_data)
|
||||||
f.write(data)
|
|
||||||
|
|
||||||
await loop.run_in_executor(None, lambda: _write_bytes(path, raw_data))
|
|
||||||
|
|
||||||
if media_type == "video":
|
if media_type == "video":
|
||||||
await loop.run_in_executor(None, lambda: extract_video_thumb(path))
|
extract_video_thumb(path)
|
||||||
elif media_type == "image":
|
elif media_type == "image":
|
||||||
preview_bytes = await loop.run_in_executor(
|
preview_bytes = generate_preview(raw_data, ext)
|
||||||
None, lambda: generate_preview(raw_data, ext)
|
|
||||||
)
|
|
||||||
if preview_bytes:
|
if preview_bytes:
|
||||||
preview_path = os.path.splitext(path)[0] + "_preview.webp"
|
preview_path = os.path.splitext(path)[0] + "_preview.webp"
|
||||||
await loop.run_in_executor(None, lambda: _write_bytes(preview_path, preview_bytes))
|
with open(preview_path, "wb") as f:
|
||||||
|
f.write(preview_bytes)
|
||||||
|
|
||||||
media_url = f"/media/diary/{filename}"
|
media_url = f"/media/diary/{filename}"
|
||||||
|
|
||||||
|
|
@ -720,8 +710,8 @@ async def upload_media(dog_id: int, entry_id: int,
|
||||||
exif_gps = None
|
exif_gps = None
|
||||||
img_size = None
|
img_size = None
|
||||||
if media_type == "image":
|
if media_type == "image":
|
||||||
exif_gps = await loop.run_in_executor(None, lambda: extract_gps_from_exif(raw_data))
|
exif_gps = extract_gps_from_exif(raw_data)
|
||||||
img_size = await loop.run_in_executor(None, lambda: get_image_size(raw_data))
|
img_size = get_image_size(raw_data)
|
||||||
|
|
||||||
with db() as conn:
|
with db() as conn:
|
||||||
# sort_order = nächste freie Position
|
# sort_order = nächste freie Position
|
||||||
|
|
|
||||||
|
|
@ -9,20 +9,6 @@ from database import db
|
||||||
from auth import get_current_user, has_pro_access
|
from auth import get_current_user, has_pro_access
|
||||||
from routes.push import send_push_to_user
|
from routes.push import send_push_to_user
|
||||||
from media_utils import safe_media_path, preview_url_from
|
from media_utils import safe_media_path, preview_url_from
|
||||||
from cache import ttl_cache
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# Pflege-Tipps sind statische Stamm-Daten → 1h TTL-Cache
|
|
||||||
# (Filterung pro Hund passiert weiter unten in-memory, NICHT gecached)
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
@ttl_cache(ttl=3600)
|
|
||||||
def _load_all_pflege_tipps() -> list[dict]:
|
|
||||||
with db() as conn:
|
|
||||||
rows = conn.execute(
|
|
||||||
"SELECT * FROM pflege_tipps ORDER BY kategorie, titel"
|
|
||||||
).fetchall()
|
|
||||||
return [dict(r) for r in rows]
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
|
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
|
||||||
|
|
@ -1109,8 +1095,10 @@ async def get_pflege_tipps(dog_id: int, user=Depends(get_current_user)):
|
||||||
elif any(w in beschr for w in ["schneid", "geschoren", "schere", "clipper"]):
|
elif any(w in beschr for w in ["schneid", "geschoren", "schere", "clipper"]):
|
||||||
fell_pflege_art_filter = "schneiden"
|
fell_pflege_art_filter = "schneiden"
|
||||||
|
|
||||||
# Statische Tipps aus Cache (1h TTL) – Filterung passiert in-memory
|
with db() as conn:
|
||||||
alle_tipps = _load_all_pflege_tipps()
|
alle_tipps = conn.execute(
|
||||||
|
"SELECT * FROM pflege_tipps ORDER BY kategorie, titel"
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
# Relevante Tipps: kein Fell-Filter oder passend
|
# Relevante Tipps: kein Fell-Filter oder passend
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
|
||||||
|
|
@ -5,28 +5,10 @@ from pydantic import BaseModel
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from database import db
|
from database import db
|
||||||
from auth import get_current_user_optional, require_admin
|
from auth import get_current_user_optional, require_admin
|
||||||
from cache import ttl_cache
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# Öffentliche, aktive FAQ-Liste – statisch, 1h TTL-Cache.
|
|
||||||
# Admin-Pfad (?all=1) wird NICHT gecached.
|
|
||||||
# Wird bei jedem schreibenden Admin-Endpoint unten invalidiert.
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
@ttl_cache(ttl=3600)
|
|
||||||
def _load_active_help_articles() -> list[dict]:
|
|
||||||
with db() as conn:
|
|
||||||
rows = conn.execute(
|
|
||||||
"SELECT id, kategorie, frage, antwort, sort_order, aktiv "
|
|
||||||
"FROM help_articles "
|
|
||||||
"WHERE aktiv = 1 "
|
|
||||||
"ORDER BY kategorie, sort_order, id"
|
|
||||||
).fetchall()
|
|
||||||
return [dict(r) for r in rows]
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# Schemas
|
# Schemas
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
@ -57,17 +39,22 @@ def get_help(
|
||||||
is_admin = user and user.get("rolle") == "admin"
|
is_admin = user and user.get("rolle") == "admin"
|
||||||
show_all = all == 1 and is_admin
|
show_all = all == 1 and is_admin
|
||||||
|
|
||||||
if show_all:
|
with db() as conn:
|
||||||
with db() as conn:
|
if show_all:
|
||||||
rows = conn.execute(
|
rows = conn.execute(
|
||||||
"SELECT id, kategorie, frage, antwort, sort_order, aktiv "
|
"SELECT id, kategorie, frage, antwort, sort_order, aktiv "
|
||||||
"FROM help_articles "
|
"FROM help_articles "
|
||||||
"ORDER BY kategorie, sort_order, id"
|
"ORDER BY kategorie, sort_order, id"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
return [dict(r) for r in rows]
|
else:
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT id, kategorie, frage, antwort, sort_order, aktiv "
|
||||||
|
"FROM help_articles "
|
||||||
|
"WHERE aktiv = 1 "
|
||||||
|
"ORDER BY kategorie, sort_order, id"
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
# Öffentliche, aktive Artikel kommen aus dem Cache
|
return [dict(r) for r in rows]
|
||||||
return _load_active_help_articles()
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
@ -81,7 +68,6 @@ def create_article(body: ArticleCreate, admin=Depends(require_admin)):
|
||||||
"VALUES (?, ?, ?, ?, ?)",
|
"VALUES (?, ?, ?, ?, ?)",
|
||||||
(body.kategorie, body.frage, body.antwort, body.sort_order, body.aktiv),
|
(body.kategorie, body.frage, body.antwort, body.sort_order, body.aktiv),
|
||||||
)
|
)
|
||||||
_load_active_help_articles.cache_clear()
|
|
||||||
return {"ok": True, "id": cur.lastrowid}
|
return {"ok": True, "id": cur.lastrowid}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -99,7 +85,6 @@ def update_article(article_id: int, body: ArticleUpdate, admin=Depends(require_a
|
||||||
f"UPDATE help_articles SET {set_clause} WHERE id=?",
|
f"UPDATE help_articles SET {set_clause} WHERE id=?",
|
||||||
(*updates.values(), article_id),
|
(*updates.values(), article_id),
|
||||||
)
|
)
|
||||||
_load_active_help_articles.cache_clear()
|
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -110,5 +95,4 @@ def update_article(article_id: int, body: ArticleUpdate, admin=Depends(require_a
|
||||||
def delete_article(article_id: int, admin=Depends(require_admin)):
|
def delete_article(article_id: int, admin=Depends(require_admin)):
|
||||||
with db() as conn:
|
with db() as conn:
|
||||||
conn.execute("DELETE FROM help_articles WHERE id=?", (article_id,))
|
conn.execute("DELETE FROM help_articles WHERE id=?", (article_id,))
|
||||||
_load_active_help_articles.cache_clear()
|
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
|
|
|
||||||
|
|
@ -1278,26 +1278,21 @@ except Exception:
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@router.post("/training-tip")
|
@router.post("/training-tip")
|
||||||
async def training_tip(user=Depends(require_social_media)):
|
async def training_tip(user=Depends(require_social_media)):
|
||||||
# Übung wählen die noch nicht als Social-Post verwendet wurde.
|
# Übung wählen die noch nicht als Social-Post verwendet wurde
|
||||||
# Per SQL: zuerst eine unbenutzte zufällig wählen, sonst Reset (irgendeine).
|
|
||||||
with db() as conn:
|
with db() as conn:
|
||||||
row = conn.execute(
|
used = {r["exercise_id"] for r in conn.execute(
|
||||||
"""SELECT * FROM training_exercises
|
"SELECT exercise_id FROM social_content WHERE exercise_id IS NOT NULL"
|
||||||
WHERE exercise_id NOT IN (
|
).fetchall()}
|
||||||
SELECT exercise_id FROM social_content WHERE exercise_id IS NOT NULL
|
all_ex = conn.execute(
|
||||||
)
|
"SELECT * FROM training_exercises ORDER BY RANDOM()"
|
||||||
ORDER BY RANDOM() LIMIT 1"""
|
).fetchall()
|
||||||
).fetchone()
|
|
||||||
if not row:
|
|
||||||
# Alle durch — Reset: irgendeine zufällige nehmen
|
|
||||||
row = conn.execute(
|
|
||||||
"SELECT * FROM training_exercises ORDER BY RANDOM() LIMIT 1"
|
|
||||||
).fetchone()
|
|
||||||
|
|
||||||
if not row:
|
unused = [e for e in all_ex if e["exercise_id"] not in used]
|
||||||
|
pool = unused if unused else list(all_ex) # Reset wenn alle durch
|
||||||
|
if not pool:
|
||||||
raise HTTPException(404, "Keine Übungen gefunden.")
|
raise HTTPException(404, "Keine Übungen gefunden.")
|
||||||
|
|
||||||
ex = dict(row)
|
ex = dict(pool[0])
|
||||||
stil = random.choice(_TRAINING_STILE)
|
stil = random.choice(_TRAINING_STILE)
|
||||||
schritte_list = json.loads(ex["schritte"] or "[]")
|
schritte_list = json.loads(ex["schritte"] or "[]")
|
||||||
schritte_text = "\n".join(f" {i+1}. {s}" for i, s in enumerate(schritte_list[:4]))
|
schritte_text = "\n".join(f" {i+1}. {s}" for i, s in enumerate(schritte_list[:4]))
|
||||||
|
|
@ -1568,10 +1563,8 @@ async def pflege_tipp(breed_id: Optional[int] = None, user=Depends(require_socia
|
||||||
(breed_id,),
|
(breed_id,),
|
||||||
).fetchone()
|
).fetchone()
|
||||||
|
|
||||||
# LIMIT 100 deckelt das Result-Set ab (Tabelle hat aktuell ~43 Einträge);
|
|
||||||
# der Python-Filter unten braucht mehrere Kandidaten für Fell-Typ-Auswahl.
|
|
||||||
tipps = conn.execute(
|
tipps = conn.execute(
|
||||||
"SELECT * FROM pflege_tipps ORDER BY RANDOM() LIMIT 100"
|
"SELECT * FROM pflege_tipps ORDER BY RANDOM()"
|
||||||
).fetchall()
|
).fetchall()
|
||||||
|
|
||||||
# Noch nicht verwendete bevorzugen
|
# Noch nicht verwendete bevorzugen
|
||||||
|
|
|
||||||
|
|
@ -7,16 +7,15 @@ import datetime
|
||||||
import ki
|
import ki
|
||||||
from database import db
|
from database import db
|
||||||
from auth import get_current_user, require_admin
|
from auth import get_current_user, require_admin
|
||||||
from cache import ttl_cache
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# Alle Übungen aus DB (öffentlich, kein Auth)
|
# Alle Übungen aus DB (öffentlich, kein Auth)
|
||||||
# Statische Daten → 1h TTL-Cache. Wird in update_exercise() invalidiert.
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@ttl_cache(ttl=3600)
|
@router.get("/exercises")
|
||||||
def _load_exercises_by_tab() -> dict:
|
async def get_exercises():
|
||||||
|
"""Alle Übungen aus der DB, gruppiert nach Tab-ID."""
|
||||||
import json as _json
|
import json as _json
|
||||||
CAT_TO_TAB = {
|
CAT_TO_TAB = {
|
||||||
'Grundkommando': 'grundkommandos',
|
'Grundkommando': 'grundkommandos',
|
||||||
|
|
@ -34,7 +33,7 @@ def _load_exercises_by_tab() -> dict:
|
||||||
dauer, beschreibung, schritte, tipp
|
dauer, beschreibung, schritte, tipp
|
||||||
FROM training_exercises ORDER BY kategorie, name
|
FROM training_exercises ORDER BY kategorie, name
|
||||||
""").fetchall()
|
""").fetchall()
|
||||||
by_tab: dict = {}
|
by_tab = {}
|
||||||
for r in rows:
|
for r in rows:
|
||||||
tab = CAT_TO_TAB.get(r['kategorie'], r['kategorie'].lower().replace(' ', '-'))
|
tab = CAT_TO_TAB.get(r['kategorie'], r['kategorie'].lower().replace(' ', '-'))
|
||||||
by_tab.setdefault(tab, []).append({
|
by_tab.setdefault(tab, []).append({
|
||||||
|
|
@ -51,12 +50,6 @@ def _load_exercises_by_tab() -> dict:
|
||||||
})
|
})
|
||||||
return by_tab
|
return by_tab
|
||||||
|
|
||||||
|
|
||||||
@router.get("/exercises")
|
|
||||||
async def get_exercises():
|
|
||||||
"""Alle Übungen aus der DB, gruppiert nach Tab-ID (1h-Cache)."""
|
|
||||||
return _load_exercises_by_tab()
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# Admin: Übung bearbeiten (beschreibung / schritte / tipp)
|
# Admin: Übung bearbeiten (beschreibung / schritte / tipp)
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
@ -85,8 +78,6 @@ async def update_exercise(exercise_id: int, body: ExerciseUpdate, _=Depends(requ
|
||||||
return {"ok": True, "updated": 0}
|
return {"ok": True, "updated": 0}
|
||||||
vals.append(exercise_id)
|
vals.append(exercise_id)
|
||||||
conn.execute(f"UPDATE training_exercises SET {', '.join(fields)} WHERE id=?", vals)
|
conn.execute(f"UPDATE training_exercises SET {', '.join(fields)} WHERE id=?", vals)
|
||||||
# Cache invalidieren, damit der Admin-Edit sofort sichtbar wird
|
|
||||||
_load_exercises_by_tab.cache_clear()
|
|
||||||
return {"ok": True, "updated": len(fields)}
|
return {"ok": True, "updated": len(fields)}
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ from pydantic import BaseModel
|
||||||
from database import db
|
from database import db
|
||||||
from auth import get_current_user, get_current_user_optional
|
from auth import get_current_user, get_current_user_optional
|
||||||
from ratelimit import check as rl_check, block_ip
|
from ratelimit import check as rl_check, block_ip
|
||||||
from cache import ttl_cache
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
|
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
|
||||||
|
|
@ -82,34 +81,16 @@ def _quiz_score(rasse: dict, params: dict) -> int:
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# GET /api/wiki/stats — Seed-Status (1h TTL-Cache, statische Anzahl)
|
# GET /api/wiki/stats — Seed-Status
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
@ttl_cache(ttl=3600)
|
@router.get("/stats")
|
||||||
def _wiki_stats() -> dict:
|
async def get_stats():
|
||||||
with db() as conn:
|
with db() as conn:
|
||||||
row = conn.execute("SELECT COUNT(*) as total FROM wiki_rassen").fetchone()
|
row = conn.execute("SELECT COUNT(*) as total FROM wiki_rassen").fetchone()
|
||||||
total = row["total"] if row else 0
|
total = row["total"] if row else 0
|
||||||
return {"total_breeds": total, "seeded": total > 0}
|
return {"total_breeds": total, "seeded": total > 0}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/stats")
|
|
||||||
async def get_stats():
|
|
||||||
return _wiki_stats()
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# Gruppen-Liste für Filter-Dropdown – statisch, 1h TTL-Cache
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
@ttl_cache(ttl=3600)
|
|
||||||
def _wiki_gruppen() -> list[str]:
|
|
||||||
with db() as conn:
|
|
||||||
rows = conn.execute(
|
|
||||||
"SELECT DISTINCT gruppe FROM wiki_rassen "
|
|
||||||
"WHERE gruppe IS NOT NULL ORDER BY gruppe"
|
|
||||||
).fetchall()
|
|
||||||
return [r["gruppe"] for r in rows]
|
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
# GET /api/wiki/rassen — alle Rassen (Übersicht, paginiert)
|
# GET /api/wiki/rassen — alle Rassen (Übersicht, paginiert)
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
|
@ -153,13 +134,15 @@ async def get_rassen(
|
||||||
SELECT COUNT(*) as total FROM wiki_rassen {where}
|
SELECT COUNT(*) as total FROM wiki_rassen {where}
|
||||||
""", args).fetchone()
|
""", args).fetchone()
|
||||||
|
|
||||||
# Alle Gruppen für Filter-Dropdown (gecached, 1h TTL)
|
# Alle Gruppen für Filter-Dropdown
|
||||||
gruppen = _wiki_gruppen()
|
gruppen_rows = conn.execute(
|
||||||
|
"SELECT DISTINCT gruppe FROM wiki_rassen WHERE gruppe IS NOT NULL ORDER BY gruppe"
|
||||||
|
).fetchall()
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"breeds": [dict(r) for r in rows],
|
"breeds": [dict(r) for r in rows],
|
||||||
"total": count_row["total"] if count_row else 0,
|
"total": count_row["total"] if count_row else 0,
|
||||||
"gruppen": gruppen,
|
"gruppen": [r["gruppe"] for r in gruppen_rows],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,19 +24,12 @@ _job_log: dict = {}
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
# ------------------------------------------------------------------
|
|
||||||
# Job-Staffelung in 5-Minuten-Intervallen — verhindert gleichzeitige
|
|
||||||
# Last-Spitzen (mehrere Jobs zur selben Sekunde 08:00 Uhr).
|
|
||||||
# coalesce=True: bei verpassten Läufen nur ein Lauf nachholen.
|
|
||||||
# misfire_grace_time: Mindestwert 300s, höher wo Job lange dauern kann.
|
|
||||||
# ------------------------------------------------------------------
|
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_health_reminders,
|
_job_health_reminders,
|
||||||
CronTrigger(hour=8, minute=0), # täglich 08:00 Uhr
|
CronTrigger(hour=8, minute=0), # täglich 08:00 Uhr
|
||||||
id="health_reminders",
|
id="health_reminders",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_poison_archive,
|
_job_poison_archive,
|
||||||
|
|
@ -44,7 +37,6 @@ def start():
|
||||||
id="poison_archive",
|
id="poison_archive",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_weather_alert,
|
_job_weather_alert,
|
||||||
|
|
@ -52,7 +44,6 @@ def start():
|
||||||
id="weather_alert",
|
id="weather_alert",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_milestone_check,
|
_job_milestone_check,
|
||||||
|
|
@ -60,7 +51,6 @@ def start():
|
||||||
id="milestone_check",
|
id="milestone_check",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_import_events,
|
_job_import_events,
|
||||||
|
|
@ -68,7 +58,6 @@ def start():
|
||||||
id="import_events",
|
id="import_events",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=7200,
|
misfire_grace_time=7200,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Einmalig beim Start (nach 10s Verzögerung) für sofortige Befüllung
|
# Einmalig beim Start (nach 10s Verzögerung) für sofortige Befüllung
|
||||||
|
|
@ -79,32 +68,29 @@ def start():
|
||||||
id="import_events_startup",
|
id="import_events_startup",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
)
|
)
|
||||||
# 1. des Monats 03:00 — Rassen aus TheDogAPI aktualisieren
|
# Alle 4 Wochen Di 03:00 — Rassen aus TheDogAPI aktualisieren
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_seed_breeds,
|
_job_seed_breeds,
|
||||||
CronTrigger(day=1, hour=3, minute=0), # 1. jedes Monats
|
CronTrigger(day=1, hour=3, minute=0), # 1. jedes Monats
|
||||||
id="seed_breeds",
|
id="seed_breeds",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# 1. des Monats 04:00 — fehlende Rassen aus Wikidata ergänzen
|
# Alle 4 Wochen Di 04:00 — fehlende Rassen aus Wikidata ergänzen
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_seed_wikidata_breeds,
|
_job_seed_wikidata_breeds,
|
||||||
CronTrigger(day=1, hour=4, minute=0), # 1. jedes Monats
|
CronTrigger(day=1, hour=4, minute=0), # 1. jedes Monats
|
||||||
id="seed_wikidata",
|
id="seed_wikidata",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Jeden Montag 09:05 — Wöchentlicher Fortschritts-Lober (staggered)
|
# Jeden Montag 09:00 — Wöchentlicher Fortschritts-Lober
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_weekly_praise,
|
_job_weekly_praise,
|
||||||
CronTrigger(day_of_week='mon', hour=9, minute=5),
|
CronTrigger(day_of_week='mon', hour=9, minute=0),
|
||||||
id="weekly_praise",
|
id="weekly_praise",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 06:00 Uhr Status-Report per Mail
|
# Täglich 06:00 Uhr Status-Report per Mail
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -113,7 +99,6 @@ def start():
|
||||||
id="status_report",
|
id="status_report",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=1800,
|
misfire_grace_time=1800,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 12:00 — Moderation-Overdue-Check
|
# Täglich 12:00 — Moderation-Overdue-Check
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -122,25 +107,22 @@ def start():
|
||||||
id="moderation_overdue",
|
id="moderation_overdue",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=1800,
|
misfire_grace_time=1800,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# 1. Feb / Mai / Aug / Nov 07:10 — Quartalsbericht (staggered weg von 07:00)
|
# 1. Feb / Mai / Aug / Nov 07:00 — Quartalsbericht
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_quarterly_report,
|
_job_quarterly_report,
|
||||||
CronTrigger(month="2,5,8,11", day=1, hour=7, minute=10),
|
CronTrigger(month="2,5,8,11", day=1, hour=7, minute=0),
|
||||||
id="quarterly_report",
|
id="quarterly_report",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=7200,
|
misfire_grace_time=7200,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Jeden Montag 07:05 — KI-Gesundheitsberichte (staggered weg von 07:00)
|
# Jeden Montag 07:00 — KI-Gesundheitsberichte (alle 2 Wochen)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_ki_health_report,
|
_job_ki_health_report,
|
||||||
CronTrigger(day_of_week='mon', hour=7, minute=5),
|
CronTrigger(day_of_week='mon', hour=7, minute=0),
|
||||||
id="ki_health_report",
|
id="ki_health_report",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 06:30 — Wiederkehrende Ausgaben anlegen
|
# Täglich 06:30 — Wiederkehrende Ausgaben anlegen
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -149,7 +131,6 @@ def start():
|
||||||
id="recurring_expenses",
|
id="recurring_expenses",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# 1. des Monats 00:05 — Hund des Monats Sieger festlegen
|
# 1. des Monats 00:05 — Hund des Monats Sieger festlegen
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -158,7 +139,6 @@ def start():
|
||||||
id="hdm_winner",
|
id="hdm_winner",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 19:00 Uhr — Streak-Erinnerung
|
# Täglich 19:00 Uhr — Streak-Erinnerung
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -167,25 +147,22 @@ def start():
|
||||||
id="streak_reminder",
|
id="streak_reminder",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 08:05 Uhr — Tierfutter-Rückrufe prüfen (RASFF) (staggered weg von 08:00)
|
# Täglich 08:00 Uhr — Tierfutter-Rückrufe prüfen (RASFF)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_recall_check,
|
_job_recall_check,
|
||||||
CronTrigger(hour=8, minute=5),
|
CronTrigger(hour=8, minute=0),
|
||||||
id="recall_check",
|
id="recall_check",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Jeden Montag 08:10 Uhr — Neue Foto-Challenge anlegen (staggered weg von 08:00)
|
# Jeden Montag 08:00 Uhr — Neue Foto-Challenge anlegen
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_new_foto_challenge,
|
_job_new_foto_challenge,
|
||||||
CronTrigger(day_of_week='mon', hour=8, minute=10),
|
CronTrigger(day_of_week='mon', hour=8, minute=0),
|
||||||
id="new_foto_challenge",
|
id="new_foto_challenge",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 07:00 Uhr — Goldene Gassi-Stunde
|
# Täglich 07:00 Uhr — Goldene Gassi-Stunde
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -194,7 +171,6 @@ def start():
|
||||||
id="golden_gassi_hour",
|
id="golden_gassi_hour",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 09:00 Uhr — Jahrestags-Erinnerungen (Tagebuch-Einträge von heute vor X Jahren)
|
# Täglich 09:00 Uhr — Jahrestags-Erinnerungen (Tagebuch-Einträge von heute vor X Jahren)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -203,7 +179,6 @@ def start():
|
||||||
id="anniversary_reminders",
|
id="anniversary_reminders",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# 1. des Monats 10:00 — Monatlicher Rückblick per Push
|
# 1. des Monats 10:00 — Monatlicher Rückblick per Push
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
|
|
@ -212,16 +187,13 @@ def start():
|
||||||
id="monthly_recap",
|
id="monthly_recap",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
# Täglich 03:15 — Abo-Ablauf prüfen (staggered weg von 03:00 poison_archive)
|
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_subscription_check,
|
_job_subscription_check,
|
||||||
CronTrigger(hour=3, minute=15),
|
CronTrigger(hour=3, minute=0),
|
||||||
id="subscription_check",
|
id="subscription_check",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
_scheduler.add_job(
|
_scheduler.add_job(
|
||||||
_job_invoice_reminder,
|
_job_invoice_reminder,
|
||||||
|
|
@ -229,10 +201,9 @@ def start():
|
||||||
id="invoice_reminder",
|
id="invoice_reminder",
|
||||||
replace_existing=True,
|
replace_existing=True,
|
||||||
misfire_grace_time=3600,
|
misfire_grace_time=3600,
|
||||||
coalesce=True,
|
|
||||||
)
|
)
|
||||||
_scheduler.start()
|
_scheduler.start()
|
||||||
logger.info("Scheduler gestartet (gestaffelt) — Health-Reminder 08:00, Giftköder-Archiv 03:00, Wetter-Alert 07:30, Meilenstein-Check 00:05, Event-Import 1.+2./4./7./10. 02:00, Rassen-Seed 1. 03:00, Wikidata-Seed 1. 04:00, Status-Report 06:00, Moderation-Overdue 12:00, Quartalsbericht 1. Feb/Mai/Aug/Nov 07:10, KI-Gesundheitsbericht Mo 07:05, Streak-Reminder 19:00, Rückruf-Check 08:05, Goldene-Gassi-Stunde 07:00, Jahrestags-Erinnerungen 09:00, Monatlicher-Rückblick 1. 10:00, Foto-Challenge Mo 08:10, Weekly-Praise Mo 09:05, Abo-Check 03:15, Invoice-Reminder 08:30. OSM-Cache: on-demand (kein Prewarm).")
|
logger.info("Scheduler gestartet — Health-Reminder 08:00, Giftköder-Archiv 03:00, Wetter-Alert 07:30, Meilenstein-Check 00:05, Event-Import So 02:00, Rassen-Seed monatlich 1. des Monats, Status-Report täglich 06:00, Moderation-Overdue 12:00, Quartalsbericht 1. Feb/Mai/Aug/Nov 07:00, Streak-Reminder 19:00, Rückruf-Check 08:00, Goldene-Gassi-Stunde 07:00, Jahrestags-Erinnerungen 09:00, Monatlicher-Rückblick 1. des Monats 10:00, Foto-Challenge Mo 08:00, Abo-Check 03:00. OSM-Cache: on-demand (kein Prewarm).")
|
||||||
|
|
||||||
|
|
||||||
def stop():
|
def stop():
|
||||||
|
|
|
||||||
|
|
@ -8865,43 +8865,3 @@ svg.empty-state-icon {
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ============================================================
|
|
||||||
Offline-Bereitschafts-Anzeige IM Welten-FAB
|
|
||||||
Die 5 Pfoten-Pfade werden je nach Score grün gefärbt
|
|
||||||
(Default = weiß auf orange, filled = grün auf orange)
|
|
||||||
============================================================ */
|
|
||||||
#worlds-fab .offline-paw .paw-elem {
|
|
||||||
color: #fff; /* stroke via currentColor — fill bleibt 'none' aus HTML */
|
|
||||||
transition: stroke 0.4s ease;
|
|
||||||
}
|
|
||||||
#worlds-fab .offline-paw .paw-elem.filled {
|
|
||||||
color: #5C3517; /* dunkles Ban Yaro Braun — klar auf orangem FAB */
|
|
||||||
}
|
|
||||||
|
|
||||||
.offline-status-row {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: var(--space-3);
|
|
||||||
padding: var(--space-2) var(--space-3);
|
|
||||||
border-radius: var(--radius-md);
|
|
||||||
border: 1px solid var(--c-border-light);
|
|
||||||
font-size: var(--text-sm);
|
|
||||||
margin-bottom: var(--space-2);
|
|
||||||
}
|
|
||||||
.offline-status-row .osr-check {
|
|
||||||
width: 24px;
|
|
||||||
height: 24px;
|
|
||||||
border-radius: 50%;
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
flex-shrink: 0;
|
|
||||||
font-size: 14px;
|
|
||||||
font-weight: 700;
|
|
||||||
}
|
|
||||||
.offline-status-row.ok .osr-check { background: var(--c-success); color: #fff; }
|
|
||||||
.offline-status-row.miss .osr-check { background: var(--c-surface-2); color: var(--c-text-muted); border: 1px dashed var(--c-border); }
|
|
||||||
.offline-status-row .osr-text { flex: 1; min-width: 0; }
|
|
||||||
.offline-status-row .osr-title { font-weight: 600; }
|
|
||||||
.offline-status-row .osr-detail { font-size: var(--text-xs); color: var(--c-text-muted); margin-top: 2px; }
|
|
||||||
|
|
|
||||||
|
|
@ -101,9 +101,9 @@
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- CSS: Reihenfolge ist wichtig — ?v= zwingt Browser zur Neuladung -->
|
<!-- CSS: Reihenfolge ist wichtig — ?v= zwingt Browser zur Neuladung -->
|
||||||
<link rel="stylesheet" href="/css/design-system.css?v=1090">
|
<link rel="stylesheet" href="/css/design-system.css?v=1070">
|
||||||
<link rel="stylesheet" href="/css/layout.css?v=1090">
|
<link rel="stylesheet" href="/css/layout.css?v=1070">
|
||||||
<link rel="stylesheet" href="/css/components.css?v=1090">
|
<link rel="stylesheet" href="/css/components.css?v=1070">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
|
|
@ -602,16 +602,7 @@
|
||||||
<div class="world-panel" id="wp-welt"><div id="ww-content"></div></div>
|
<div class="world-panel" id="wp-welt"><div id="ww-content"></div></div>
|
||||||
</div>
|
</div>
|
||||||
<button id="worlds-fab" aria-label="Hinzufügen">
|
<button id="worlds-fab" aria-label="Hinzufügen">
|
||||||
<svg class="offline-paw" viewBox="0 0 256 256" aria-hidden="true" style="width:24px;height:24px">
|
<svg class="ph-icon" style="width:22px;height:22px"><use href="/icons/phosphor.svg#paw-print"></use></svg>
|
||||||
<!-- 5 Sub-Pfade einzeln einfärbbar via .paw-elem; Default: weiß auf orange -->
|
|
||||||
<path class="paw-elem" data-step="1"
|
|
||||||
d="M128,104A36,36,0,0,0,93.43,130a43.49,43.49,0,0,1-20.67,25.9,32,32,0,0,0,27.73,57.62,72.49,72.49,0,0,1,55,0,32,32,0,0,0,27.73-57.62A43.46,43.46,0,0,1,162.57,130,36,36,0,0,0,128,104Z"
|
|
||||||
fill="none" stroke="currentColor" stroke-width="16" stroke-linecap="round" stroke-linejoin="round"/>
|
|
||||||
<circle class="paw-elem" data-step="2" cx="44" cy="108" r="20" fill="none" stroke="currentColor" stroke-width="16"/>
|
|
||||||
<circle class="paw-elem" data-step="3" cx="92" cy="60" r="20" fill="none" stroke="currentColor" stroke-width="16"/>
|
|
||||||
<circle class="paw-elem" data-step="4" cx="164" cy="60" r="20" fill="none" stroke="currentColor" stroke-width="16"/>
|
|
||||||
<circle class="paw-elem" data-step="5" cx="212" cy="108" r="20" fill="none" stroke="currentColor" stroke-width="16"/>
|
|
||||||
</svg>
|
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="worlds-back" aria-label="Zurück zur Welten-Navigation">
|
<div id="worlds-back" aria-label="Zurück zur Welten-Navigation">
|
||||||
|
|
@ -625,11 +616,10 @@
|
||||||
<div id="modal-container"></div>
|
<div id="modal-container"></div>
|
||||||
|
|
||||||
<!-- JS: Reihenfolge ist wichtig — erst Basis, dann Features -->
|
<!-- JS: Reihenfolge ist wichtig — erst Basis, dann Features -->
|
||||||
<script src="/js/api.js?v=1090"></script>
|
<script src="/js/api.js?v=1070"></script>
|
||||||
<script src="/js/ui.js?v=1090"></script>
|
<script src="/js/ui.js?v=1070"></script>
|
||||||
<script src="/js/app.js?v=1090"></script>
|
<script src="/js/app.js?v=1070"></script>
|
||||||
<script src="/js/worlds.js?v=1090"></script>
|
<script src="/js/worlds.js?v=1070"></script>
|
||||||
<script src="/js/offline-indicator.js?v=1090"></script>
|
|
||||||
|
|
||||||
<!-- Feature-Seiten werden lazy geladen -->
|
<!-- Feature-Seiten werden lazy geladen -->
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -814,57 +814,9 @@ const API = (() => {
|
||||||
} catch {}
|
} catch {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------
|
|
||||||
// BILD-KOMPRESSION (Client-Side vor Upload)
|
|
||||||
// ----------------------------------------------------------
|
|
||||||
// iPhone-Fotos sind 4-12 MB — vor Upload auf max. 2000px / JPEG q=0.85
|
|
||||||
// skalieren. HEIC/HEIF unverändert lassen (Browser-Canvas kann sie nicht
|
|
||||||
// decoden, Backend hat eigene HEIC-Konvertierung). Nicht-Bilder unverändert.
|
|
||||||
async function compressImage(file, maxSize = 2000, quality = 0.85) {
|
|
||||||
try {
|
|
||||||
if (!file || !(file instanceof File || file instanceof Blob)) return file;
|
|
||||||
const type = (file.type || '').toLowerCase();
|
|
||||||
if (!type.startsWith('image/')) return file;
|
|
||||||
if (type === 'image/heic' || type === 'image/heif') return file;
|
|
||||||
if (type === 'image/gif') return file; // GIF-Animation nicht kaputt skalieren
|
|
||||||
if (file.size < 500_000) return file; // <500KB: lohnt sich nicht
|
|
||||||
|
|
||||||
const img = await createImageBitmap(file);
|
|
||||||
try {
|
|
||||||
const longest = Math.max(img.width, img.height);
|
|
||||||
const scale = Math.min(1, maxSize / longest);
|
|
||||||
// Nur skalieren wenn Bild wirklich größer ist; bei scale=1 trotzdem als JPEG
|
|
||||||
// neu kodieren — spart bei iPhone-Originalen oft trotzdem viel (EXIF, weniger Qualität).
|
|
||||||
const w = Math.round(img.width * scale);
|
|
||||||
const h = Math.round(img.height * scale);
|
|
||||||
|
|
||||||
const canvas = document.createElement('canvas');
|
|
||||||
canvas.width = w; canvas.height = h;
|
|
||||||
const ctx = canvas.getContext('2d');
|
|
||||||
if (!ctx) return file;
|
|
||||||
ctx.drawImage(img, 0, 0, w, h);
|
|
||||||
|
|
||||||
const blob = await new Promise(res => canvas.toBlob(res, 'image/jpeg', quality));
|
|
||||||
if (!blob || blob.size >= file.size) return file; // Kompression hat nichts gebracht
|
|
||||||
|
|
||||||
const newName = (file.name || 'photo.jpg').replace(/\.(heic|heif|png|webp|jpeg|jpg)$/i, '.jpg');
|
|
||||||
const finalName = /\.jpe?g$/i.test(newName) ? newName : (newName + '.jpg');
|
|
||||||
return new File([blob], finalName, { type: 'image/jpeg', lastModified: Date.now() });
|
|
||||||
} finally {
|
|
||||||
// ImageBitmap-Ressourcen freigeben (wo unterstützt)
|
|
||||||
if (typeof img.close === 'function') img.close();
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Bei jedem Fehler (z.B. createImageBitmap auf HEIC) — original zurück
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Auch global verfügbar, damit Seiten-Module ihn direkt nutzen können
|
|
||||||
if (typeof window !== 'undefined') window.compressImage = compressImage;
|
|
||||||
|
|
||||||
// Öffentliche API
|
// Öffentliche API
|
||||||
return {
|
return {
|
||||||
get, post, put, patch, del, upload, swCacheDelete, compressImage,
|
get, post, put, patch, del, upload, swCacheDelete,
|
||||||
auth, dogs, diary, health, tieraerzte, healthDocs, poison,
|
auth, dogs, diary, health, tieraerzte, healthDocs, poison,
|
||||||
places, routes, walks, events, sitting, forum, lost, knigge, weather, push,
|
places, routes, walks, events, sitting, forum, lost, knigge, weather, push,
|
||||||
friends, chat, webcal, importData, sharing, widget, notifications, services, ratings, sittingAccess, training, notes,
|
friends, chat, webcal, importData, sharing, widget, notifications, services, ratings, sittingAccess, training, notes,
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,8 @@
|
||||||
Router, State-Management, Navigation, Initialisierung.
|
Router, State-Management, Navigation, Initialisierung.
|
||||||
============================================================ */
|
============================================================ */
|
||||||
|
|
||||||
const APP_VER = '1090'; // ← bei jedem Deploy mit Frontend-Änderungen erhöhen
|
const APP_VER = '1070'; // ← bei jedem Deploy mit Frontend-Änderungen erhöhen
|
||||||
const APP_VERSION = '1.6.0'; // ← semantische Version, wird bei make release gesetzt
|
const APP_VERSION = '1.6.0'; // ← semantische Version, wird bei make release gesetzt
|
||||||
window.APP_VER = APP_VER; // global verfügbar für andere Module (z.B. offline-indicator)
|
|
||||||
window.APP_VERSION = APP_VERSION;
|
|
||||||
const IS_STAGING = location.hostname === 'staging.banyaro.app';
|
const IS_STAGING = location.hostname === 'staging.banyaro.app';
|
||||||
// Cache-Bust-Parameter nach Update-Reload sofort entfernen.
|
// Cache-Bust-Parameter nach Update-Reload sofort entfernen.
|
||||||
// Flag MUSS vor replaceState gesetzt werden — index.html liest es danach.
|
// Flag MUSS vor replaceState gesetzt werden — index.html liest es danach.
|
||||||
|
|
|
||||||
|
|
@ -1,263 +0,0 @@
|
||||||
/* ============================================================
|
|
||||||
BAN YARO — Offline-Bereitschafts-Anzeige IM Welten-FAB
|
|
||||||
Färbt die 5 Pfoten-Pfade je nach Cache-Stand grün:
|
|
||||||
1 = App-Shell · 2 = Wichtige Seiten · 3 = Hund-/Tagebuchdaten
|
|
||||||
4 = Karten-Tiles · 5 = Training & Wissen
|
|
||||||
============================================================ */
|
|
||||||
|
|
||||||
window.OfflineIndicator = (() => {
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
// Cache-Namen dynamisch finden — robust gegen Versions-Updates
|
|
||||||
const CACHE_TILES = 'ban-yaro-tiles-v1';
|
|
||||||
const CACHE_API = 'ban-yaro-api-v1';
|
|
||||||
const TILE_MIN = 50;
|
|
||||||
|
|
||||||
async function _staticCache() {
|
|
||||||
const names = await caches.keys();
|
|
||||||
const found = names.find(n => /^by-v\d+-static$/.test(n));
|
|
||||||
return found ? await caches.open(found) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const CHECKS = [
|
|
||||||
{ step: 1, title: 'App-Grundgerüst',
|
|
||||||
detail: 'CSS, Layout und Hauptmodule — die Basis',
|
|
||||||
probe: async () => {
|
|
||||||
const c = await _staticCache();
|
|
||||||
if (!c) return false;
|
|
||||||
const urls = (await c.keys()).map(r => r.url);
|
|
||||||
return urls.some(u => u.includes('/css/design-system.css'))
|
|
||||||
&& urls.some(u => u.includes('/js/app.js'));
|
|
||||||
} },
|
|
||||||
|
|
||||||
{ step: 2, title: 'Wichtige Seiten',
|
|
||||||
detail: 'Tagebuch, Gesundheit, Karte, Gassi, Erste Hilfe, Notizen, Ausgaben, Routen',
|
|
||||||
probe: async () => {
|
|
||||||
const c = await _staticCache();
|
|
||||||
if (!c) return false;
|
|
||||||
const want = ['diary.js','health.js','map.js','walks.js','erste-hilfe.js',
|
|
||||||
'notes.js','expenses.js','routes.js'];
|
|
||||||
const urls = (await c.keys()).map(r => r.url);
|
|
||||||
const have = want.filter(name => urls.some(u => u.includes('/js/pages/' + name)));
|
|
||||||
return have.length >= want.length - 1; // 1 Toleranz (falls einzelner Fetch fehlschlug)
|
|
||||||
} },
|
|
||||||
|
|
||||||
{ step: 3, title: 'Hund-Daten',
|
|
||||||
detail: 'Profil, Tagebuch und Gesundheit',
|
|
||||||
probe: async () => {
|
|
||||||
const c = await caches.open(CACHE_API).catch(() => null);
|
|
||||||
if (!c) return false;
|
|
||||||
const urls = (await c.keys()).map(r => r.url);
|
|
||||||
const hasProfile = urls.some(u => /\/api\/dogs\/\d+(\?|$)/.test(u))
|
|
||||||
|| urls.some(u => /\/api\/dogs\/\d+\/welcome-dashboard/.test(u));
|
|
||||||
const hasDiary = urls.some(u => /\/api\/dogs\/\d+\/diary/.test(u));
|
|
||||||
const hasHealth = urls.some(u => /\/api\/dogs\/\d+\/health/.test(u));
|
|
||||||
// Profil + mindestens eine Datenquelle (Tagebuch oder Gesundheit)
|
|
||||||
return hasProfile && (hasDiary || hasHealth);
|
|
||||||
} },
|
|
||||||
|
|
||||||
{ step: 4, title: 'Weitere Listen',
|
|
||||||
detail: 'Ausgaben, Routen, Notizen',
|
|
||||||
probe: async () => {
|
|
||||||
const c = await caches.open(CACHE_API).catch(() => null);
|
|
||||||
if (!c) return false;
|
|
||||||
const urls = (await c.keys()).map(r => r.url);
|
|
||||||
return urls.some(u => u.includes('/api/expenses'))
|
|
||||||
&& urls.some(u => u.includes('/api/routes'))
|
|
||||||
&& urls.some(u => u.includes('/api/notes'));
|
|
||||||
} },
|
|
||||||
|
|
||||||
{ step: 5, title: 'Karten-Kacheln',
|
|
||||||
detail: `Mindestens ${TILE_MIN} OSM-Tiles im Umkreis`,
|
|
||||||
probe: async () => {
|
|
||||||
const c = await caches.open(CACHE_TILES).catch(() => null);
|
|
||||||
if (!c) return false;
|
|
||||||
return (await c.keys()).length >= TILE_MIN;
|
|
||||||
} },
|
|
||||||
];
|
|
||||||
|
|
||||||
// Tile-Prefetch-Konfiguration
|
|
||||||
const TILE_PREFETCH = [
|
|
||||||
{ zoom: 14, radius: 3 }, // 7x7 = 49 Tiles im Nahbereich
|
|
||||||
{ zoom: 13, radius: 1 }, // 3x3 = 9 Tiles für Übersicht
|
|
||||||
];
|
|
||||||
|
|
||||||
let _fab = null;
|
|
||||||
|
|
||||||
async function refresh() {
|
|
||||||
_fab = document.getElementById('worlds-fab');
|
|
||||||
if (!_fab || !('caches' in window)) return null;
|
|
||||||
|
|
||||||
const results = await Promise.all(CHECKS.map(async c => {
|
|
||||||
try { return { ...c, ok: await c.probe() }; }
|
|
||||||
catch { return { ...c, ok: false }; }
|
|
||||||
}));
|
|
||||||
|
|
||||||
_fab.querySelectorAll('.paw-elem').forEach(el => {
|
|
||||||
const step = Number(el.dataset.step);
|
|
||||||
const isOk = results.find(r => r.step === step)?.ok;
|
|
||||||
el.classList.toggle('filled', !!isOk);
|
|
||||||
});
|
|
||||||
|
|
||||||
const score = results.filter(r => r.ok).length;
|
|
||||||
_fab.setAttribute('data-offline-score', `${score}/5`);
|
|
||||||
return { score, results };
|
|
||||||
}
|
|
||||||
|
|
||||||
// Optional aufrufbar: zeigt das Status-Modal mit Nachlade-Button
|
|
||||||
async function openStatus() {
|
|
||||||
const data = await refresh();
|
|
||||||
if (!data) return;
|
|
||||||
const { score, results } = data;
|
|
||||||
const missing = results.filter(r => !r.ok);
|
|
||||||
const rows = results.map(r => `
|
|
||||||
<div class="offline-status-row ${r.ok ? 'ok' : 'miss'}">
|
|
||||||
<div class="osr-check">${r.ok ? '✓' : '○'}</div>
|
|
||||||
<div class="osr-text">
|
|
||||||
<div class="osr-title">${r.title}</div>
|
|
||||||
<div class="osr-detail">${r.detail}</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`).join('');
|
|
||||||
|
|
||||||
UI.modal.open({
|
|
||||||
title: `🐾 Offline-Bereitschaft ${score}/5`,
|
|
||||||
body: `
|
|
||||||
<p style="font-size:var(--text-sm);color:var(--c-text-secondary);margin:0 0 var(--space-3)">
|
|
||||||
${missing.length === 0
|
|
||||||
? 'Voll offline-fähig — Tagebuch, Karte und Daten funktionieren auch ohne Internet.'
|
|
||||||
: 'Je grüner deine Pfote im FAB, desto mehr klappt offline. Fehlende Inhalte werden beim nächsten Online-Aufruf automatisch geladen.'}
|
|
||||||
</p>
|
|
||||||
${rows}
|
|
||||||
`,
|
|
||||||
footer: `
|
|
||||||
<div style="display:flex;flex-direction:column;gap:var(--space-2);width:100%">
|
|
||||||
${missing.length ? `<button class="btn btn-primary" id="offline-fill-btn" style="width:100%">
|
|
||||||
${UI.icon('cloud-arrow-down')} Fehlende jetzt nachladen
|
|
||||||
</button>` : ''}
|
|
||||||
<button class="btn btn-secondary" data-modal-close style="width:100%">Schließen</button>
|
|
||||||
</div>
|
|
||||||
`,
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('offline-fill-btn')?.addEventListener('click', async () => {
|
|
||||||
const btn = document.getElementById('offline-fill-btn');
|
|
||||||
btn.disabled = true; btn.textContent = 'Lade …';
|
|
||||||
await _fetchMissing(missing);
|
|
||||||
UI.modal.close();
|
|
||||||
UI.toast.success('Offline-Inhalte aktualisiert.');
|
|
||||||
refresh();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _fetchMissing(missing) {
|
|
||||||
const tasks = [];
|
|
||||||
for (const m of missing) {
|
|
||||||
if (m.step === 2) {
|
|
||||||
['diary.js','map.js','walks.js','erste-hilfe.js','notes.js','expenses.js','routes.js'].forEach(p =>
|
|
||||||
tasks.push(fetch(`/js/pages/${p}?v=${window.APP_VER}`).catch(() => {})));
|
|
||||||
} else if (m.step === 3) {
|
|
||||||
const dogId = window._appState?.activeDog?.id;
|
|
||||||
if (dogId) {
|
|
||||||
tasks.push(fetch(`/api/dogs/${dogId}`).catch(() => {}));
|
|
||||||
tasks.push(fetch(`/api/dogs/${dogId}/diary?limit=20`).catch(() => {}));
|
|
||||||
tasks.push(fetch(`/api/dogs/${dogId}/health`).catch(() => {}));
|
|
||||||
}
|
|
||||||
} else if (m.step === 4) {
|
|
||||||
tasks.push(fetch('/api/expenses').catch(() => {}));
|
|
||||||
tasks.push(fetch('/api/routes').catch(() => {}));
|
|
||||||
tasks.push(fetch('/api/notes').catch(() => {}));
|
|
||||||
} else if (m.step === 5) {
|
|
||||||
await _prefetchTiles();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await Promise.all(tasks);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ----------------------------------------------------------
|
|
||||||
// Tile-URL-Berechnung (OSM, Subdomain 'a')
|
|
||||||
// ----------------------------------------------------------
|
|
||||||
function _tile(lat, lon, z) {
|
|
||||||
const n = Math.pow(2, z);
|
|
||||||
const x = Math.floor((lon + 180) / 360 * n);
|
|
||||||
const latRad = lat * Math.PI / 180;
|
|
||||||
const y = Math.floor((1 - Math.log(Math.tan(latRad) + 1/Math.cos(latRad)) / Math.PI) / 2 * n);
|
|
||||||
return { x, y };
|
|
||||||
}
|
|
||||||
function _tileUrls(lat, lon, zoom, radius) {
|
|
||||||
const center = _tile(lat, lon, zoom);
|
|
||||||
const out = [];
|
|
||||||
for (let dx = -radius; dx <= radius; dx++) {
|
|
||||||
for (let dy = -radius; dy <= radius; dy++) {
|
|
||||||
out.push(`https://a.tile.openstreetmap.org/${zoom}/${center.x + dx}/${center.y + dy}.png`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Tile-Prefetch im Umkreis der aktuellen GPS-Position (nur wenn Permission schon da)
|
|
||||||
async function _prefetchTiles() {
|
|
||||||
if (!navigator.serviceWorker?.controller) return;
|
|
||||||
if (!navigator.permissions || !navigator.geolocation) return;
|
|
||||||
try {
|
|
||||||
const perm = await navigator.permissions.query({ name: 'geolocation' });
|
|
||||||
if (perm.state !== 'granted') return; // kein Popup wenn nicht schon erlaubt
|
|
||||||
const pos = await new Promise(res =>
|
|
||||||
navigator.geolocation.getCurrentPosition(p => res(p), () => res(null), { timeout: 5000 }));
|
|
||||||
if (!pos) return;
|
|
||||||
const urls = [];
|
|
||||||
TILE_PREFETCH.forEach(({ zoom, radius }) =>
|
|
||||||
urls.push(..._tileUrls(pos.coords.latitude, pos.coords.longitude, zoom, radius)));
|
|
||||||
navigator.serviceWorker.controller.postMessage({ type: 'CACHE_TILES', urls });
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Page-Module proaktiv fetchen — falls SW-Install sie noch nicht alle hatte
|
|
||||||
function _prefetchPages() {
|
|
||||||
['diary','health','map','walks','erste-hilfe','notes','expenses','routes','poison','lost']
|
|
||||||
.forEach(p => fetch(`/js/pages/${p}.js?v=${window.APP_VER}`).catch(() => {}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Daten-Prefetch: Listen die offline brauchbar sein müssen,
|
|
||||||
// auch wenn der User die Seiten noch nie geöffnet hat
|
|
||||||
function _prefetchData() {
|
|
||||||
fetch('/api/expenses').catch(() => {});
|
|
||||||
fetch('/api/routes').catch(() => {});
|
|
||||||
fetch('/api/notes').catch(() => {});
|
|
||||||
const dogId = window._appState?.activeDog?.id;
|
|
||||||
if (dogId) {
|
|
||||||
fetch(`/api/dogs/${dogId}`).catch(() => {});
|
|
||||||
fetch(`/api/dogs/${dogId}/health`).catch(() => {});
|
|
||||||
fetch(`/api/dogs/${dogId}/diary?limit=20`).catch(() => {});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function init() {
|
|
||||||
refresh();
|
|
||||||
_prefetchPages();
|
|
||||||
_prefetchTiles();
|
|
||||||
_prefetchData();
|
|
||||||
|
|
||||||
// Mehrere Retries für hund-spezifische Daten — _appState.activeDog wird oft
|
|
||||||
// erst nach Login/Hunde-Load gesetzt, manchmal mehrere Sekunden nach Init
|
|
||||||
[2_000, 5_000, 10_000, 20_000].forEach(delay => {
|
|
||||||
setTimeout(() => { _prefetchData(); refresh(); }, delay);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (navigator.serviceWorker) {
|
|
||||||
navigator.serviceWorker.addEventListener('message', e => {
|
|
||||||
if (e?.data?.type === 'CACHE_UPDATE') refresh();
|
|
||||||
if (e?.data?.type === 'CACHE_TILES_PROGRESS') refresh();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
setInterval(refresh, 60_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
return { init, refresh, openStatus };
|
|
||||||
})();
|
|
||||||
|
|
||||||
if (document.readyState !== 'loading') {
|
|
||||||
window.OfflineIndicator.init();
|
|
||||||
} else {
|
|
||||||
document.addEventListener('DOMContentLoaded', () => window.OfflineIndicator.init());
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,6 @@ window.Page_admin = (() => {
|
||||||
let _container = null;
|
let _container = null;
|
||||||
let _appState = null;
|
let _appState = null;
|
||||||
let _tab = 'uebersicht';
|
let _tab = 'uebersicht';
|
||||||
let _delegationAttached = null; // WeakRef-Ersatz: zuletzt mit Delegation versehener Container
|
|
||||||
|
|
||||||
const TABS = [
|
const TABS = [
|
||||||
{ id: 'uebersicht', label: 'Übersicht', icon: 'house-line' },
|
{ id: 'uebersicht', label: 'Übersicht', icon: 'house-line' },
|
||||||
|
|
@ -72,43 +71,20 @@ window.Page_admin = (() => {
|
||||||
|
|
||||||
_container.querySelector('#adm-tabs')
|
_container.querySelector('#adm-tabs')
|
||||||
?.style.setProperty('--adm-tab-cols', Math.ceil(TABS.length / 2));
|
?.style.setProperty('--adm-tab-cols', Math.ceil(TABS.length / 2));
|
||||||
|
_container.querySelectorAll('#adm-tabs .by-tab').forEach(btn => {
|
||||||
// Event-Delegation: Listener EINMAL pro Container-Instanz setzen
|
btn.addEventListener('click', () => {
|
||||||
// (innerHTML überschreibt zwar das DOM, aber bei jedem init() würden ohne
|
_tab = btn.dataset.tab;
|
||||||
// Flag erneut N=18 Tab-Listener akkumuliert werden)
|
_container.querySelectorAll('#adm-tabs .by-tab').forEach(b =>
|
||||||
if (_delegationAttached !== _container) {
|
b.classList.toggle('active', b.dataset.tab === _tab)
|
||||||
_container.addEventListener('click', _onContainerClick);
|
);
|
||||||
_delegationAttached = _container;
|
_renderTab();
|
||||||
}
|
});
|
||||||
|
});
|
||||||
|
|
||||||
_renderActionItems();
|
_renderActionItems();
|
||||||
_renderTab();
|
_renderTab();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delegation-Handler für Tab-Buttons + Action-Item-Buttons
|
|
||||||
function _onContainerClick(e) {
|
|
||||||
// Tab-Button (#adm-tabs .by-tab)
|
|
||||||
const tabBtn = e.target.closest('#adm-tabs .by-tab');
|
|
||||||
if (tabBtn && _container.contains(tabBtn)) {
|
|
||||||
_tab = tabBtn.dataset.tab;
|
|
||||||
_container.querySelectorAll('#adm-tabs .by-tab').forEach(b =>
|
|
||||||
b.classList.toggle('active', b.dataset.tab === _tab)
|
|
||||||
);
|
|
||||||
_renderTab();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Action-Item-Button ([data-action-tab])
|
|
||||||
const actionBtn = e.target.closest('[data-action-tab]');
|
|
||||||
if (actionBtn && _container.contains(actionBtn)) {
|
|
||||||
_tab = actionBtn.dataset.actionTab;
|
|
||||||
_container.querySelectorAll('#adm-tabs .by-tab').forEach(b =>
|
|
||||||
b.classList.toggle('active', b.dataset.tab === _tab)
|
|
||||||
);
|
|
||||||
_renderTab();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function _renderActionItems() {
|
async function _renderActionItems() {
|
||||||
const el = _container.querySelector('#adm-action-items');
|
const el = _container.querySelector('#adm-action-items');
|
||||||
if (!el) return;
|
if (!el) return;
|
||||||
|
|
@ -158,7 +134,15 @@ window.Page_admin = (() => {
|
||||||
</span>
|
</span>
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
// Klicks auf [data-action-tab] werden zentral via _onContainerClick (Delegation) behandelt
|
el.querySelectorAll('[data-action-tab]').forEach(btn => {
|
||||||
|
btn.addEventListener('click', () => {
|
||||||
|
_tab = btn.dataset.actionTab;
|
||||||
|
_container.querySelectorAll('#adm-tabs .by-tab').forEach(b =>
|
||||||
|
b.classList.toggle('active', b.dataset.tab === _tab)
|
||||||
|
);
|
||||||
|
_renderTab();
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async function _renderTab() {
|
async function _renderTab() {
|
||||||
|
|
@ -836,13 +820,7 @@ window.Page_admin = (() => {
|
||||||
<div style="font-size:var(--text-xs);color:var(--c-text-muted);margin-top:2px">
|
<div style="font-size:var(--text-xs);color:var(--c-text-muted);margin-top:2px">
|
||||||
🗺 ${u.route_count} Routen · ${u.total_km} km
|
🗺 ${u.route_count} Routen · ${u.total_km} km
|
||||||
· 📍 ${u.poi_count} POIs
|
· 📍 ${u.poi_count} POIs
|
||||||
</div>
|
${u.last_route ? '· zuletzt ' + new Date(u.last_route).toLocaleDateString('de-DE') : ''}
|
||||||
<div style="font-size:var(--text-xs);color:var(--c-text-muted);margin-top:2px">
|
|
||||||
${u.last_seen
|
|
||||||
? '🟢 zuletzt aktiv ' + new Date(u.last_seen).toLocaleString('de-DE', {day:'2-digit',month:'2-digit',year:'numeric',hour:'2-digit',minute:'2-digit'})
|
|
||||||
: u.last_login
|
|
||||||
? '🔵 zuletzt eingeloggt ' + new Date(u.last_login).toLocaleString('de-DE', {day:'2-digit',month:'2-digit',year:'numeric',hour:'2-digit',minute:'2-digit'})
|
|
||||||
: '⚪ nie aktiv'}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -3569,15 +3547,6 @@ window.Page_admin = (() => {
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div style="display:grid;grid-template-columns:1fr 1fr;gap:var(--space-2);margin-top:var(--space-3)">
|
<div style="display:grid;grid-template-columns:1fr 1fr;gap:var(--space-2);margin-top:var(--space-3)">
|
||||||
${r.existing_invoice_id ? `
|
|
||||||
<button class="btn adm-invoice-edit-btn"
|
|
||||||
data-invoice-id="${r.existing_invoice_id}"
|
|
||||||
title="Rechnung ${_esc(r.existing_invoice_number)} (${_esc(r.existing_invoice_status)}) bearbeiten"
|
|
||||||
style="background:#eab308;color:#1a1a1a;border:none;
|
|
||||||
padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
|
||||||
cursor:pointer;font-size:var(--text-sm);font-weight:600">
|
|
||||||
${UI.icon('receipt')} Rechnung bearbeiten
|
|
||||||
</button>` : `
|
|
||||||
<button class="btn adm-invoice-btn"
|
<button class="btn adm-invoice-btn"
|
||||||
data-name="${_esc(r.name)}" data-email="${_esc(r.email)}"
|
data-name="${_esc(r.name)}" data-email="${_esc(r.email)}"
|
||||||
data-tier="${r.tier}" data-address="${_esc(r.billing_address || '')}"
|
data-tier="${r.tier}" data-address="${_esc(r.billing_address || '')}"
|
||||||
|
|
@ -3588,7 +3557,7 @@ window.Page_admin = (() => {
|
||||||
padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
||||||
cursor:pointer;font-size:var(--text-sm);font-weight:600">
|
cursor:pointer;font-size:var(--text-sm);font-weight:600">
|
||||||
${UI.icon('receipt')} Rechnung erstellen
|
${UI.icon('receipt')} Rechnung erstellen
|
||||||
</button>`}
|
</button>
|
||||||
<button class="btn adm-fulfill-btn" data-id="${r.id}" data-name="${_esc(r.name)}" data-tier="${r.tier}"
|
<button class="btn adm-fulfill-btn" data-id="${r.id}" data-name="${_esc(r.name)}" data-tier="${r.tier}"
|
||||||
style="background:#16a34a;color:#fff;border:none;
|
style="background:#16a34a;color:#fff;border:none;
|
||||||
padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
||||||
|
|
@ -3708,29 +3677,6 @@ window.Page_admin = (() => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// "Rechnung bearbeiten" — lädt existierenden Entwurf/Sent-Rechnung im Edit-Modus
|
|
||||||
el.querySelectorAll('.adm-invoice-edit-btn').forEach(btn => {
|
|
||||||
btn.addEventListener('click', async () => {
|
|
||||||
try {
|
|
||||||
const inv = await API.get(`/admin/invoices/${btn.dataset.invoiceId}`);
|
|
||||||
_openNeueRechnungModal(() => {
|
|
||||||
// Nach Speichern/Stornieren: zurück auf Upgrades-Tab, damit der Button neu rendert
|
|
||||||
_renderTab();
|
|
||||||
}, {
|
|
||||||
recipient_name: inv.recipient_name,
|
|
||||||
recipient_email: inv.recipient_email,
|
|
||||||
recipient_address: inv.recipient_address || '',
|
|
||||||
service_period: inv.service_period || '',
|
|
||||||
discount_pct: inv.discount_pct || 0,
|
|
||||||
notes: inv.notes || '',
|
|
||||||
items: inv.items.map(it => ({ description: it.description, quantity: it.quantity, unit_price: it.unit_price })),
|
|
||||||
}, inv.id, inv.status, inv.invoice_number);
|
|
||||||
} catch (e) {
|
|
||||||
UI.toast.error(e.message || 'Rechnung konnte nicht geladen werden.');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------
|
// ------------------------------------------------------------------
|
||||||
|
|
@ -3923,7 +3869,7 @@ window.Page_admin = (() => {
|
||||||
discount_pct: inv.discount_pct || 0,
|
discount_pct: inv.discount_pct || 0,
|
||||||
notes: inv.notes || '',
|
notes: inv.notes || '',
|
||||||
items: inv.items.map(it => ({ description: it.description, quantity: it.quantity, unit_price: it.unit_price })),
|
items: inv.items.map(it => ({ description: it.description, quantity: it.quantity, unit_price: it.unit_price })),
|
||||||
}, inv.id, inv.status, inv.invoice_number);
|
}, inv.id);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -3943,31 +3889,16 @@ window.Page_admin = (() => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function _openNeueRechnungModal(reload, prefill = null, invoiceId = null, status = null, invoiceNumber = null) {
|
function _openNeueRechnungModal(reload, prefill = null, invoiceId = null) {
|
||||||
const id = `inv-new-${Date.now()}`;
|
const id = `inv-new-${Date.now()}`;
|
||||||
const p = prefill || {};
|
const p = prefill || {};
|
||||||
const isEdit = !!invoiceId;
|
const isEdit = !!invoiceId;
|
||||||
const isLocked = isEdit && status && status !== 'draft'; // sent / paid / cancelled → nicht änderbar
|
|
||||||
const canCancel = isEdit && status !== 'cancelled'; // alles außer schon storniert
|
|
||||||
const lockedBanner = isLocked ? (() => {
|
|
||||||
const map = {
|
|
||||||
sent: ['#fff8f0', '#f0a060', '#c05000', 'Diese Rechnung wurde bereits versendet — Inhalt nicht mehr änderbar. Korrekturen nur per Stornierung.'],
|
|
||||||
paid: ['#f0fdf4', '#86efac', '#15803d', 'Diese Rechnung ist bezahlt — Inhalt nicht änderbar.'],
|
|
||||||
cancelled: ['#fef2f2', '#fca5a5', '#b91c1c', 'Diese Rechnung wurde storniert — Inhalt nicht änderbar.'],
|
|
||||||
};
|
|
||||||
const [bg, br, fg, msg] = map[status] || ['#f5f5f5', '#ccc', '#444', `Status: ${status}`];
|
|
||||||
return `<div style="padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
|
||||||
background:${bg};border:1px solid ${br};
|
|
||||||
font-size:var(--text-xs);color:${fg};line-height:1.6">${msg}</div>`;
|
|
||||||
})() : '';
|
|
||||||
|
|
||||||
UI.modal.open({
|
UI.modal.open({
|
||||||
title: `${UI.icon('receipt')} ${isEdit ? (isLocked ? 'Rechnung ansehen' : 'Rechnung bearbeiten') : 'Neue Rechnung erstellen'}`,
|
title: `${UI.icon('receipt')} ${isEdit ? 'Rechnung bearbeiten' : 'Neue Rechnung erstellen'}`,
|
||||||
body: `
|
body: `
|
||||||
<form id="${id}" style="display:flex;flex-direction:column;gap:var(--space-3)">
|
<form id="${id}" style="display:flex;flex-direction:column;gap:var(--space-3)">
|
||||||
|
|
||||||
${lockedBanner}
|
|
||||||
|
|
||||||
${!isEdit && !p.recipient_name ? `
|
${!isEdit && !p.recipient_name ? `
|
||||||
<div style="padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
<div style="padding:var(--space-2) var(--space-3);border-radius:var(--radius-md);
|
||||||
background:#fff8f0;border:1px solid #f0a060;
|
background:#fff8f0;border:1px solid #f0a060;
|
||||||
|
|
@ -4046,43 +3977,11 @@ window.Page_admin = (() => {
|
||||||
</form>
|
</form>
|
||||||
`,
|
`,
|
||||||
footer: `
|
footer: `
|
||||||
<div style="display:flex;flex-direction:column;gap:var(--space-2);width:100%">
|
<button class="btn btn-secondary" data-modal-close>Abbrechen</button>
|
||||||
${isLocked
|
<button class="btn btn-primary" form="${id}" type="submit">${UI.icon('receipt')} ${isEdit ? 'Änderungen speichern' : 'Rechnung erstellen'}</button>
|
||||||
? `<button class="btn btn-secondary" data-modal-close style="width:100%">Schließen</button>`
|
|
||||||
: `<div style="display:grid;grid-template-columns:1fr 1fr;gap:var(--space-2)">
|
|
||||||
<button class="btn btn-secondary" data-modal-close>Abbrechen</button>
|
|
||||||
<button class="btn btn-primary" form="${id}" type="submit" style="min-width:0">
|
|
||||||
${UI.icon('receipt')} ${isEdit ? 'Speichern' : 'Erstellen'}
|
|
||||||
</button>
|
|
||||||
</div>`}
|
|
||||||
${canCancel ? `
|
|
||||||
<button class="btn btn-ghost" id="${id}-cancel-invoice"
|
|
||||||
style="width:100%;color:var(--c-danger);border:1px solid var(--c-danger)">
|
|
||||||
${UI.icon('x-circle')} Rechnung stornieren
|
|
||||||
</button>` : ''}
|
|
||||||
</div>
|
|
||||||
`,
|
`,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Bei gesperrten Rechnungen (sent/paid/cancelled) alle Eingaben & Action-Buttons readonly
|
|
||||||
if (isLocked) {
|
|
||||||
setTimeout(() => {
|
|
||||||
const form = document.getElementById(id);
|
|
||||||
if (!form) return;
|
|
||||||
form.querySelectorAll('input, textarea').forEach(inp => { inp.disabled = true; });
|
|
||||||
// "+ Position hinzufügen" und Item-Lösch-Buttons verstecken
|
|
||||||
const addBtn = document.getElementById(`${id}-add-item`);
|
|
||||||
if (addBtn) addBtn.style.display = 'none';
|
|
||||||
form.querySelectorAll('.inv-item-remove').forEach(b => { b.style.display = 'none'; });
|
|
||||||
}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stornieren — schließt dieses Modal, öffnet den Storno-Dialog
|
|
||||||
document.getElementById(`${id}-cancel-invoice`)?.addEventListener('click', () => {
|
|
||||||
// _openStornoModal ruft intern UI.modal.open() → schließt dieses Modal automatisch
|
|
||||||
_openStornoModal(invoiceId, invoiceNumber || `#${invoiceId}`, reload);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Items-Container und Hilfsfunktionen
|
// Items-Container und Hilfsfunktionen
|
||||||
const itemsContainer = document.getElementById(`${id}-items`);
|
const itemsContainer = document.getElementById(`${id}-items`);
|
||||||
const previewEl = document.getElementById(`${id}-preview`);
|
const previewEl = document.getElementById(`${id}-preview`);
|
||||||
|
|
@ -4147,7 +4046,6 @@ window.Page_admin = (() => {
|
||||||
// Form Submit
|
// Form Submit
|
||||||
document.getElementById(id)?.addEventListener('submit', async e => {
|
document.getElementById(id)?.addEventListener('submit', async e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (isLocked) return; // gesperrte Rechnung — Submit ignorieren (Button ist eh ausgeblendet)
|
|
||||||
const fd = new FormData(e.target);
|
const fd = new FormData(e.target);
|
||||||
const items = [];
|
const items = [];
|
||||||
itemsContainer.querySelectorAll('.adm-inv-item-row').forEach(row => {
|
itemsContainer.querySelectorAll('.adm-inv-item-row').forEach(row => {
|
||||||
|
|
|
||||||
|
|
@ -1437,33 +1437,25 @@ window.Page_diary = (() => {
|
||||||
wrap.innerHTML = grid;
|
wrap.innerHTML = grid;
|
||||||
wrap.querySelectorAll('.diary-media-thumb-del').forEach(btn => {
|
wrap.querySelectorAll('.diary-media-thumb-del').forEach(btn => {
|
||||||
btn.addEventListener('click', async () => {
|
btn.addEventListener('click', async () => {
|
||||||
const wrap2 = btn.closest('.diary-media-thumb-wrap') || btn.parentElement;
|
const wrap2 = btn.closest('.diary-media-thumb-wrap');
|
||||||
const mediaId = btn.dataset.mediaId ? parseInt(btn.dataset.mediaId) : null;
|
const mediaId = btn.dataset.mediaId ? parseInt(btn.dataset.mediaId) : null;
|
||||||
const isLegacy = !!btn.dataset.legacy;
|
const isLegacy = !!btn.dataset.legacy;
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
let alreadyGone = false;
|
|
||||||
try {
|
try {
|
||||||
if (mediaId != null) {
|
if (mediaId != null) {
|
||||||
await API.diary.deleteMediaItem(_appState.activeDog.id, entry.id, mediaId);
|
await API.diary.deleteMediaItem(_appState.activeDog.id, entry.id, mediaId);
|
||||||
|
// aus entry.media_items entfernen
|
||||||
|
if (entry.media_items) entry.media_items = entry.media_items.filter(m => m.id !== mediaId);
|
||||||
} else if (isLegacy) {
|
} else if (isLegacy) {
|
||||||
await API.diary.deleteMedia(_appState.activeDog.id, entry.id);
|
await API.diary.deleteMedia(_appState.activeDog.id, entry.id);
|
||||||
|
entry.media_url = null;
|
||||||
}
|
}
|
||||||
|
wrap2.remove();
|
||||||
|
UI.toast.success('Medium entfernt.');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e?.status === 404) {
|
btn.disabled = false;
|
||||||
alreadyGone = true; // serverseitig schon weg → trotzdem lokal aufräumen
|
UI.toast.error(e.message || 'Fehler beim Löschen.');
|
||||||
} else {
|
|
||||||
btn.disabled = false;
|
|
||||||
UI.toast.error(e.message || 'Fehler beim Löschen.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (mediaId != null && entry.media_items) {
|
|
||||||
entry.media_items = entry.media_items.filter(m => m.id !== mediaId);
|
|
||||||
} else if (isLegacy) {
|
|
||||||
entry.media_url = null;
|
|
||||||
}
|
|
||||||
if (wrap2) wrap2.remove();
|
|
||||||
UI.toast.success(alreadyGone ? 'Verwaisten Eintrag aufgeräumt.' : 'Medium entfernt.');
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// Stern-Buttons im Edit-Formular
|
// Stern-Buttons im Edit-Formular
|
||||||
|
|
@ -1730,37 +1722,29 @@ window.Page_diary = (() => {
|
||||||
};
|
};
|
||||||
|
|
||||||
async function _uploadNewFiles(entryId) {
|
async function _uploadNewFiles(entryId) {
|
||||||
const total = _newFiles.length;
|
let failCount = 0;
|
||||||
const saveBtn = document.querySelector('button[form="diary-form"]');
|
const uploaded = [];
|
||||||
let done = 0;
|
let exifGps = null;
|
||||||
if (saveBtn) saveBtn.textContent = `0 von ${total} hochgeladen…`;
|
for (const file of _newFiles) {
|
||||||
|
|
||||||
const results = await Promise.all(_newFiles.map(async file => {
|
|
||||||
// Bild-Kompression vor Upload (HEIC/Video/<500KB werden unverändert durchgereicht)
|
|
||||||
const toUpload = await API.compressImage(file);
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append('file', toUpload);
|
|
||||||
try {
|
try {
|
||||||
|
const formData = new FormData();
|
||||||
|
formData.append('file', file);
|
||||||
const m = await API.diary.uploadMedia(_appState.activeDog.id, entryId, formData);
|
const m = await API.diary.uploadMedia(_appState.activeDog.id, entryId, formData);
|
||||||
if (saveBtn) saveBtn.textContent = `${++done} von ${total} hochgeladen…`;
|
uploaded.push(m);
|
||||||
return { ok: true, m };
|
if (m.exif_lat != null && m.exif_lon != null && !exifGps) {
|
||||||
|
exifGps = { lat: m.exif_lat, lon: m.exif_lon };
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
if (saveBtn) saveBtn.textContent = `${++done} von ${total} hochgeladen…`;
|
failCount++;
|
||||||
return { ok: false };
|
|
||||||
}
|
}
|
||||||
}));
|
}
|
||||||
|
|
||||||
const uploaded = results.filter(r => r.ok).map(r => r.m);
|
|
||||||
const failCount = results.filter(r => !r.ok).length;
|
|
||||||
const exifGps = results.find(r => r.ok && r.m.exif_lat != null)?.m;
|
|
||||||
|
|
||||||
if (failCount > 0) {
|
if (failCount > 0) {
|
||||||
UI.toast.warning(`${failCount} Medium${failCount > 1 ? 'en' : ''} konnte${failCount > 1 ? 'n' : ''} nicht hochgeladen werden.`);
|
UI.toast.warning(`${failCount} Medium${failCount > 1 ? 'en' : ''} konnte${failCount > 1 ? 'n' : ''} nicht hochgeladen werden.`);
|
||||||
}
|
}
|
||||||
if (exifGps) {
|
if (exifGps) {
|
||||||
UI.toast.success(`📍 Standort aus Foto-GPS übernommen`);
|
UI.toast.success(`📍 Standort aus Foto-GPS übernommen`);
|
||||||
}
|
}
|
||||||
return { uploaded, exifGps: exifGps ? { lat: exifGps.exif_lat, lon: exifGps.exif_lon } : null };
|
return { uploaded, exifGps };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isEdit) {
|
if (isEdit) {
|
||||||
|
|
|
||||||
|
|
@ -762,10 +762,8 @@ window.Page_dog_profile = (() => {
|
||||||
const file = e.target.files[0];
|
const file = e.target.files[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
try {
|
try {
|
||||||
// Client-Side-Kompression vor Upload (HEIC bleibt unverändert)
|
|
||||||
const toUpload = await API.compressImage(file);
|
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('file', toUpload);
|
fd.append('file', file);
|
||||||
const result = await API.dogs.uploadPhoto(dog.id, fd);
|
const result = await API.dogs.uploadPhoto(dog.id, fd);
|
||||||
// Position zurücksetzen
|
// Position zurücksetzen
|
||||||
await API.dogs.updatePhotoPosition(dog.id, 1.0, 0.0, 0.0);
|
await API.dogs.updatePhotoPosition(dog.id, 1.0, 0.0, 0.0);
|
||||||
|
|
@ -1386,10 +1384,8 @@ window.Page_dog_profile = (() => {
|
||||||
// Foto hochladen wenn gewählt
|
// Foto hochladen wenn gewählt
|
||||||
if (fotoFile) {
|
if (fotoFile) {
|
||||||
try {
|
try {
|
||||||
// Client-Side-Kompression vor Upload
|
|
||||||
const toUpload = await API.compressImage(fotoFile);
|
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('file', toUpload);
|
fd.append('file', fotoFile);
|
||||||
const result = await API.dogs.uploadPhoto(saved.id, fd);
|
const result = await API.dogs.uploadPhoto(saved.id, fd);
|
||||||
saved.foto_url = result.foto_url;
|
saved.foto_url = result.foto_url;
|
||||||
_appState.activeDog = { ...saved };
|
_appState.activeDog = { ...saved };
|
||||||
|
|
|
||||||
|
|
@ -1263,9 +1263,8 @@ window.Page_health = (() => {
|
||||||
if (!saved.media_items) saved.media_items = [];
|
if (!saved.media_items) saved.media_items = [];
|
||||||
for (const f of files) {
|
for (const f of files) {
|
||||||
try {
|
try {
|
||||||
const toUpload = await API.compressImage(f);
|
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
fd.append('file', toUpload);
|
fd.append('file', f);
|
||||||
const res = await API.health.uploadMedia(dogId, saved.id, fd);
|
const res = await API.health.uploadMedia(dogId, saved.id, fd);
|
||||||
saved.media_items.push({ id: res.id, url: res.url, media_type: res.media_type });
|
saved.media_items.push({ id: res.id, url: res.url, media_type: res.media_type });
|
||||||
// Rückwärtskompatibilität: erste Datei auch als datei_url sichern
|
// Rückwärtskompatibilität: erste Datei auch als datei_url sichern
|
||||||
|
|
@ -2740,8 +2739,6 @@ window.Page_health = (() => {
|
||||||
}
|
}
|
||||||
|
|
||||||
await UI.asyncButton(btn, async () => {
|
await UI.asyncButton(btn, async () => {
|
||||||
// Client-Side-Kompression nur wenn Bild (PDFs etc. unverändert durchgereicht)
|
|
||||||
const toUpload = await API.compressImage(file);
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('dog_id', String(dog.id));
|
formData.append('dog_id', String(dog.id));
|
||||||
formData.append('typ', fd.typ);
|
formData.append('typ', fd.typ);
|
||||||
|
|
@ -2749,7 +2746,7 @@ window.Page_health = (() => {
|
||||||
formData.append('beschreibung', fd.beschreibung || '');
|
formData.append('beschreibung', fd.beschreibung || '');
|
||||||
formData.append('datum', fd.datum || '');
|
formData.append('datum', fd.datum || '');
|
||||||
if (fd.vet_id) formData.append('vet_id', fd.vet_id);
|
if (fd.vet_id) formData.append('vet_id', fd.vet_id);
|
||||||
formData.append('file', toUpload);
|
formData.append('file', file);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const doc = await API.healthDocs.upload(formData);
|
const doc = await API.healthDocs.upload(formData);
|
||||||
|
|
|
||||||
|
|
@ -772,9 +772,8 @@ window.Page_lost = (() => {
|
||||||
// Foto hochladen
|
// Foto hochladen
|
||||||
if (photoInput?.files[0]) {
|
if (photoInput?.files[0]) {
|
||||||
try {
|
try {
|
||||||
const toUpload = await API.compressImage(photoInput.files[0]);
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', toUpload);
|
formData.append('file', photoInput.files[0]);
|
||||||
const media = await API.lost.uploadFoto(created.id, formData);
|
const media = await API.lost.uploadFoto(created.id, formData);
|
||||||
created.foto_url = media.foto_url;
|
created.foto_url = media.foto_url;
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
||||||
|
|
@ -552,9 +552,8 @@ window.Page_poison = (() => {
|
||||||
// Foto hochladen
|
// Foto hochladen
|
||||||
if (photoInput?.files[0]) {
|
if (photoInput?.files[0]) {
|
||||||
try {
|
try {
|
||||||
const toUpload = await API.compressImage(photoInput.files[0]);
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', toUpload);
|
formData.append('file', photoInput.files[0]);
|
||||||
const media = await API.poison.uploadPhoto(created.id, formData);
|
const media = await API.poison.uploadPhoto(created.id, formData);
|
||||||
created.foto_url = media.foto_url;
|
created.foto_url = media.foto_url;
|
||||||
} catch {
|
} catch {
|
||||||
|
|
|
||||||
|
|
@ -628,10 +628,8 @@ window.Page_walks = (() => {
|
||||||
document.getElementById('wd-photo-input')?.addEventListener('change', async function() {
|
document.getElementById('wd-photo-input')?.addEventListener('change', async function() {
|
||||||
if (!this.files.length) return;
|
if (!this.files.length) return;
|
||||||
const file = this.files[0];
|
const file = this.files[0];
|
||||||
// Client-Side-Kompression vor Upload (HEIC bleibt unverändert)
|
|
||||||
const toUpload = await API.compressImage(file);
|
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.append('file', toUpload);
|
formData.append('file', file);
|
||||||
try {
|
try {
|
||||||
const photo = await API.walks.uploadPhoto(walk.id, formData);
|
const photo = await API.walks.uploadPhoto(walk.id, formData);
|
||||||
const grid = document.getElementById('wd-photos-grid');
|
const grid = document.getElementById('wd-photos-grid');
|
||||||
|
|
|
||||||
|
|
@ -4,22 +4,19 @@
|
||||||
============================================================ */
|
============================================================ */
|
||||||
|
|
||||||
// ← EINZIGE Stelle für die Version — STATIC_ASSETS und CACHE_VERSION leiten sich ab
|
// ← EINZIGE Stelle für die Version — STATIC_ASSETS und CACHE_VERSION leiten sich ab
|
||||||
const VER = '1090';
|
const VER = '1070';
|
||||||
const CACHE_VERSION = `by-v${VER}`;
|
const CACHE_VERSION = `by-v${VER}`;
|
||||||
const CACHE_STATIC = `${CACHE_VERSION}-static`;
|
const CACHE_STATIC = `${CACHE_VERSION}-static`;
|
||||||
const CACHE_TILES = 'ban-yaro-tiles-v1'; // bleibt über SW-Updates erhalten
|
const CACHE_TILES = 'ban-yaro-tiles-v1'; // bleibt über SW-Updates erhalten
|
||||||
const CACHE_API = 'ban-yaro-api-v1'; // API-Response-Cache
|
const CACHE_API = 'ban-yaro-api-v1'; // API-Response-Cache
|
||||||
|
|
||||||
// Prioritäts-Seiten: werden nach Install im Hintergrund gecacht (nicht blockierend)
|
// Prioritäts-Seiten: werden nach Install im Hintergrund gecacht (nicht blockierend)
|
||||||
// Diese Seiten MÜSSEN offline funktionieren — auch wenn der User sie noch nie geöffnet hat.
|
|
||||||
const PRIORITY_PAGES = [
|
const PRIORITY_PAGES = [
|
||||||
|
'/js/pages/admin.js',
|
||||||
|
'/js/pages/erste-hilfe.js',
|
||||||
'/js/pages/diary.js',
|
'/js/pages/diary.js',
|
||||||
'/js/pages/health.js',
|
|
||||||
'/js/pages/map.js',
|
'/js/pages/map.js',
|
||||||
'/js/pages/walks.js',
|
'/js/pages/walks.js',
|
||||||
'/js/pages/erste-hilfe.js',
|
|
||||||
'/js/pages/notes.js',
|
|
||||||
'/js/pages/expenses.js',
|
|
||||||
'/js/pages/routes.js',
|
'/js/pages/routes.js',
|
||||||
'/js/pages/poison.js',
|
'/js/pages/poison.js',
|
||||||
'/js/pages/lost.js',
|
'/js/pages/lost.js',
|
||||||
|
|
@ -35,7 +32,6 @@ const STATIC_ASSETS = [
|
||||||
`/js/ui.js?v=${VER}`,
|
`/js/ui.js?v=${VER}`,
|
||||||
`/js/app.js?v=${VER}`,
|
`/js/app.js?v=${VER}`,
|
||||||
`/js/worlds.js?v=${VER}`,
|
`/js/worlds.js?v=${VER}`,
|
||||||
`/js/offline-indicator.js?v=${VER}`,
|
|
||||||
'/js/leaflet.markercluster.js',
|
'/js/leaflet.markercluster.js',
|
||||||
'/css/MarkerCluster.css',
|
'/css/MarkerCluster.css',
|
||||||
'/css/MarkerCluster.Default.css',
|
'/css/MarkerCluster.Default.css',
|
||||||
|
|
@ -136,26 +132,6 @@ function _isMediaUpload(request) {
|
||||||
return (request.headers.get('Content-Type') || '').includes('multipart');
|
return (request.headers.get('Content-Type') || '').includes('multipart');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tile-Cache LRU: Eviction wenn zu viele Tiles drin sind
|
|
||||||
// cache.keys() liefert Insertion-Order — daher löschen wir vom Anfang.
|
|
||||||
// Bei jedem Tile-Add: trimTileCache() im Hintergrund (kein await vor respondWith).
|
|
||||||
const _TILE_MAX_ENTRIES = 500;
|
|
||||||
let _tileTrimRunning = false;
|
|
||||||
async function trimTileCache(maxEntries = _TILE_MAX_ENTRIES) {
|
|
||||||
if (_tileTrimRunning) return; // gleichzeitige Trims verhindern
|
|
||||||
_tileTrimRunning = true;
|
|
||||||
try {
|
|
||||||
const cache = await caches.open(CACHE_TILES);
|
|
||||||
const keys = await cache.keys();
|
|
||||||
if (keys.length > maxEntries) {
|
|
||||||
const toDelete = keys.slice(0, keys.length - maxEntries);
|
|
||||||
await Promise.all(toDelete.map(k => cache.delete(k)));
|
|
||||||
}
|
|
||||||
} catch {} finally {
|
|
||||||
_tileTrimRunning = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Welche GET-API-Endpoints sollen gecacht werden?
|
// Welche GET-API-Endpoints sollen gecacht werden?
|
||||||
const _CACHEABLE_GET = [
|
const _CACHEABLE_GET = [
|
||||||
/^\/api\/dogs(\/\d+)?$/,
|
/^\/api\/dogs(\/\d+)?$/,
|
||||||
|
|
@ -175,7 +151,6 @@ const _CACHEABLE_GET = [
|
||||||
/^\/api\/walks/,
|
/^\/api\/walks/,
|
||||||
/^\/api\/lost/,
|
/^\/api\/lost/,
|
||||||
/^\/api\/expenses/,
|
/^\/api\/expenses/,
|
||||||
/^\/api\/notes/,
|
|
||||||
// Drei Welten — offline-fähig
|
// Drei Welten — offline-fähig
|
||||||
/^\/api\/streak\/\d+/,
|
/^\/api\/streak\/\d+/,
|
||||||
/^\/api\/forum\/threads/,
|
/^\/api\/forum\/threads/,
|
||||||
|
|
@ -340,18 +315,14 @@ self.addEventListener('fetch', event => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// OSM-Kartenkacheln: eigener persistenter Cache (Cache-First mit LRU-Eviction)
|
// OSM-Kartenkacheln: eigener persistenter Cache
|
||||||
if (url.hostname.endsWith('tile.openstreetmap.org')) {
|
if (url.hostname.endsWith('tile.openstreetmap.org')) {
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.open(CACHE_TILES).then(cache =>
|
caches.open(CACHE_TILES).then(cache =>
|
||||||
cache.match(event.request).then(cached => {
|
cache.match(event.request).then(cached => {
|
||||||
if (cached) return cached;
|
if (cached) return cached;
|
||||||
return fetch(event.request).then(response => {
|
return fetch(event.request).then(response => {
|
||||||
if (response.ok) {
|
if (response.ok) cache.put(event.request, response.clone());
|
||||||
cache.put(event.request, response.clone())
|
|
||||||
.then(() => trimTileCache()) // im Hintergrund — blockiert respondWith nicht
|
|
||||||
.catch(() => {});
|
|
||||||
}
|
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|
@ -464,7 +435,6 @@ self.addEventListener('message', event => {
|
||||||
function fetchBatch() {
|
function fetchBatch() {
|
||||||
if (queue.length === 0) {
|
if (queue.length === 0) {
|
||||||
source?.postMessage({ type: 'CACHE_TILES_PROGRESS', done: total, total });
|
source?.postMessage({ type: 'CACHE_TILES_PROGRESS', done: total, total });
|
||||||
trimTileCache(); // nach Bulk-Vorausladen einmal trimmen
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const batch = queue.splice(0, 8);
|
const batch = queue.splice(0, 8);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue