info('Starting CalDAV sync'); $todos = $service->getTodos(); $count = 0; foreach ($todos as $todo) { Todo::upsert($todo->attributesToArray(), 'id'); $count++; } // Collect hrefs/URLs returned by the CalDAV server so we can remove local // todos that belong to this calendar but were deleted on the server. $hrefs = array_values(array_filter(array_map(function ($t) { return $t->url ?? null; }, $todos))); // Remove local todos that have a URL in this calendar but weren't returned // by the server (i.e. they were deleted remotely). Scope deletion by // calendar prefix to avoid touching other calendars. $calendarId = $service->getCalendarId(); $username = config('caldav.username') ?? ''; $calendarPrefix = 'calendars/' . $username . '/' . $calendarId; if ($calendarId && $username) { Todo::whereNotNull('url') ->where('url', 'like', '%' . $calendarPrefix . "%") ->whereNotIn('url', $hrefs ?: ['']) ->delete(); } // Remove old todos Todo::where('status', 'COMPLETED') ->where('last_modified', '<', now()->subDays(30)) // TODO: get from settings ->where('due_date', '<', now()->subDays(30)) ->delete(); Log::info("Synced " . count($todos) . " todos."); $this->info("Synced " . count($todos) . " todos."); return 0; } }