This commit is contained in:
Timothy Jaeryang Baek committed 2026-08-25 16:04:43 -04:00
1 parent b1bfc18762
commit 067114c280
1 file changed
+39 -36
+39 -36
View File
@@ -25,8 +25,7 @@ from typing import Optional
from uuid import uuid4 from uuid import uuid4
from zoneinfo import ZoneInfo from zoneinfo import ZoneInfo
from dateutil import parser as date_parser from dateutil.rrule import HOURLY, MINUTELY, SECONDLY, rruleset, rrulestr
from dateutil.rrule import rrulestr
from fastapi import Request from fastapi import Request
from fastapi.security import HTTPAuthorizationCredentials from fastapi.security import HTTPAuthorizationCredentials
from open_webui.constants import ERROR_MESSAGES from open_webui.constants import ERROR_MESSAGES
@@ -78,44 +77,48 @@ def _parse_rule(s: str, now: Optional[datetime] = None):
SECONDLY/MINUTELY/HOURLY rules use a fixed epoch DTSTART (2000-01-01 00:00) SECONDLY/MINUTELY/HOURLY rules use a fixed epoch DTSTART (2000-01-01 00:00)
so intervals snap to clock boundaries (e.g. every 5min = :00, :05, :10). so intervals snap to clock boundaries (e.g. every 5min = :00, :05, :10).
""" """
lines = s.splitlines() upper = s.upper()
rule_count = sum(1 for line in lines if line.upper().startswith('RRULE:')) if 'EXRULE' in upper:
if 'EXRULE' in s.upper():
raise ValueError('EXRULE is not supported in recurrence rules') raise ValueError('EXRULE is not supported in recurrence rules')
if rule_count > 1:
parsed = rrulestr(s, ignoretz=True)
rules = parsed._rrule if isinstance(parsed, rruleset) else [parsed]
if len(rules) > 1:
raise ValueError('only one RRULE is supported per recurrence rule') raise ValueError('only one RRULE is supported per recurrence rule')
rrule_line = next((line for line in lines if line.upper().startswith('RRULE:')), s) rule = rules[0]
raw = rrule_line.split(':', 1)[1] if rrule_line.upper().startswith('RRULE:') else rrule_line start = rule._dtstart.replace(tzinfo=None)
parts = {k.upper(): v for k, v in (p.split('=', 1) for p in raw.split(';') if '=' in p)} anchor = now or datetime.now()
freq = parts.get('FREQ', '') lines = s.splitlines()
stripped = '\n'.join(line for line in lines if not line.upper().startswith('DTSTART')) or s
has_dtstart = any(line.upper().startswith('DTSTART') for line in lines)
step = {
SECONDLY: timedelta(seconds=rule._interval),
MINUTELY: timedelta(minutes=rule._interval),
HOURLY: timedelta(hours=rule._interval),
}.get(rule._freq)
if freq in ('SECONDLY', 'MINUTELY', 'HOURLY'): if step is None:
epoch = datetime(2000, 1, 1, 0, 0, 0) if not rule._dtstart.tzinfo:
anchor = now or datetime.now() return parsed
rule = '\n'.join(line for line in lines if not line.upper().startswith('DTSTART')) or s return rrulestr(stripped, dtstart=start, ignoretz=True)
dtstart = next((line.rsplit(':', 1)[-1] for line in lines if line.upper().startswith('DTSTART')), None)
interval = int(parts.get('INTERVAL', '1')) if rule._interval < 1:
if interval < 1: raise ValueError('RRULE INTERVAL must be a positive integer')
raise ValueError('RRULE INTERVAL must be a positive integer') dtstart = None
if freq == 'SECONDLY': if has_dtstart:
step = timedelta(seconds=interval) emitted = ((anchor - start) // step) if anchor > start else 0
elif freq == 'MINUTELY': emitted *= len(rule._byminute or (0,)) * len(rule._bysecond or (0,))
step = timedelta(minutes=interval) if emitted <= 100_000:
else: if rule._dtstart.tzinfo:
step = timedelta(hours=interval) dtstart = start
if dtstart: else:
start = date_parser.parse(dtstart, ignoretz=True) return parsed
emitted = ((anchor - start) // step) if anchor > start else 0 if not has_dtstart or dtstart is None:
if 'BYMINUTE' in parts: epoch = datetime(2000, 1, 1)
emitted *= len(parts['BYMINUTE'].split(',')) dtstart = epoch + ((anchor - epoch) // step) * step
if 'BYSECOND' in parts:
emitted *= len(parts['BYSECOND'].split(',')) return rrulestr(stripped, dtstart=dtstart, ignoretz=True)
if emitted <= 100_000:
return rrulestr(s, ignoretz=True)
anchor = epoch + ((anchor - epoch) // step) * step
return rrulestr(rule, dtstart=anchor, ignoretz=True)
return rrulestr(s, ignoretz=True)
def validate_rrule(s: str, tz: str = None) -> None: def validate_rrule(s: str, tz: str = None) -> None: