fix: show a next run moved by an automation's code on its page and in the Calendar (#35388)

When an automation's code moved its next run, the run happened at the new time, but the automation page kept showing the regular scheduled time and the Calendar did not list the moved run. Both now show the next run as it will really happen, including for automations that only run manually, and the Calendar leaves out the scheduled times the moved run replaces. Once the moved run is done, both go back to the regular schedule.

Fixes #35377
This commit is contained in:
Classic298 authored and GitHub committed 2026-12-01 00:11:31 +04:00
1 parent c2bbab6cf6
commit bc410a62bd
2 files changed
+40 -30

No files matched your search

+36 -23
View File
@@ -210,7 +210,31 @@ async def get_events(
# Future runs: expand RRULEs for active automations only
active_automations = await Automations.get_active_by_user(user.id)
now_ns = int(time.time_ns())
for auto in active_automations:
next_run_at = auto.next_run_at
action = auto.data['action']
next_run_event = {
'id': f'auto_{auto.id}_next',
'calendar_id': SCHEDULED_TASKS_CALENDAR_ID,
'user_id': user.id,
'title': auto.name,
'description': action.get('prompt') or action.get('code') or '',
'start_at': next_run_at,
'end_at': None,
'all_day': False,
'rrule': None,
'color': None,
'location': None,
'data': None,
'meta': {'automation_id': auto.id},
'is_cancelled': False,
'attendees': [],
'created_at': auto.created_at,
'updated_at': auto.updated_at,
'user': None,
}
scheduled_starts = set()
schedules = [t['rrule'] for t in auto.data['triggers'] if t['type'] == 'schedule']
for i, rrule_str in enumerate(schedules):
start_at = await next_run_ns(rrule_str, tz=user.timezone) or 0
@@ -222,36 +246,25 @@ async def get_events(
except ValueError:
pass
action = auto.data['action']
virtual = {
'id': f'auto_{auto.id}_{i}',
'calendar_id': SCHEDULED_TASKS_CALENDAR_ID,
'user_id': user.id,
'title': auto.name,
'description': action.get('prompt') or action.get('code') or '',
'start_at': start_at,
'end_at': None,
'all_day': False,
'rrule': rrule_str,
'color': None,
'location': None,
'data': None,
'meta': {'automation_id': auto.id},
'is_cancelled': False,
'attendees': [],
'created_at': auto.created_at,
'updated_at': auto.updated_at,
'user': None,
}
virtual = {**next_run_event, 'id': f'auto_{auto.id}_{i}', 'start_at': start_at, 'rrule': rrule_str}
# Only expand into the future — past runs are handled below
now_ns = int(time.time_ns())
rrule_start = max(start_ns, now_ns)
instances = await evaluate_recurrence(
expand_recurring_event, virtual, rrule_start, end_ns, user.timezone
)
for inst in instances:
expanded.append(CalendarEventUserResponse(**inst))
# A next run moved by code replaces the scheduled times before it
if inst['start_at'] >= (next_run_at or 0):
scheduled_starts.add(inst['start_at'])
expanded.append(CalendarEventUserResponse(**inst))
if (
next_run_at
and max(start_ns, now_ns) <= next_run_at < end_ns
and next_run_at not in scheduled_starts
):
expanded.append(CalendarEventUserResponse(**next_run_event))
# Past runs: single range query joined with automation
runs_with_auto = await AutomationRuns.get_runs_by_user_range(user.id, start_ns, end_ns)
@@ -1040,7 +1040,7 @@
</div>
</div>
{#if action_type === 'code' || automation.data.triggers.some((trigger) => trigger.type === 'schedule') || automation.last_run_at}
{#if action_type === 'code' || automation.next_run_at || automation.data.triggers.some((trigger) => trigger.type === 'schedule') || automation.last_run_at}
<div class="mt-6 mb-1 text-[0.6875rem] text-base-400 dark:text-base-500">
{$i18n.t('Runtime')}
</div>
@@ -1097,9 +1097,8 @@
<div
class={rowClass}
class:hidden={!automation.data.triggers.some(
(trigger) => trigger.type === 'schedule'
)}
class:hidden={!automation.next_run_at &&
!automation.data.triggers.some((trigger) => trigger.type === 'schedule')}
>
<span class={labelClass}>
<svg
@@ -1123,9 +1122,7 @@
class="min-w-0 flex-1 truncate text-xs {is_active
? 'text-base-700 dark:text-base-300'
: 'text-base-400 dark:text-base-500'}"
>{is_active
? formatNextRun(automation.next_runs?.[0] ?? automation.next_run_at)
: $i18n.t('Paused')}</span
>{is_active ? formatNextRun(automation.next_run_at) : $i18n.t('Paused')}</span
>
</div>