fix: stable event hashing, HEAD fallback, caldav str/bytes compat

This commit is contained in:
2026-06-11 23:06:47 +02:00
parent 0f390ff1e1
commit d628f03a82
3 changed files with 52 additions and 9 deletions
+14 -3
View File
@@ -5,6 +5,19 @@ from icalendar import Calendar
logger = logging.getLogger(__name__)
def _stable_event_key(component) -> str:
event_bytes = component.to_ical()
text = event_bytes.decode("utf-8", errors="replace")
stable_lines = []
for line in text.split("\r\n"):
key = line.split(":")[0].upper() if ":" in line else ""
if key in ("DTSTAMP", "LAST-MODIFIED"):
continue
stable_lines.append(line)
stable_text = "\r\n".join(stable_lines)
return hashlib.sha256(stable_text.encode("utf-8")).hexdigest()
def parse_ics_events(ics_text: str) -> dict[str, str]:
cal = Calendar.from_ical(ics_text.encode() if isinstance(ics_text, str) else ics_text)
result = {}
@@ -17,9 +30,7 @@ def parse_ics_events(ics_text: str) -> dict[str, str]:
continue
uid_str = str(uid)
try:
event_bytes = component.to_ical()
file_hash = hashlib.sha256(event_bytes).hexdigest()
result[uid_str] = file_hash
result[uid_str] = _stable_event_key(component)
except Exception as e:
logger.warning("Failed to process event %s: %s", uid_str, e)
return result