Fix: CardDAV sync broke when todo items didn't have a creation date. Also fixed todos without a due date not being displays and wrong calculation of warning badge states.

This commit is contained in:
2026-05-25 15:31:29 +02:00
parent 2324ad95bf
commit da3ceeaff3
5 changed files with 95 additions and 45 deletions
+23 -4
View File
@@ -16,7 +16,8 @@ class CaldavSyncCommand extends Command
public function handle(CaldavService $service)
{
// only run every 5 minutes although the task is called every minute
// Throttle execution
// only run every 5 minutes although the scheduler is run every minute
$cacheKey = 'caldav_sync_last_run';
if (\Illuminate\Support\Facades\Cache::has($cacheKey)) {
Log::info('CalDAV sync Throttled');
@@ -29,6 +30,7 @@ public function handle(CaldavService $service)
$todos = $service->getTodos();
// Update
$count = 0;
foreach ($todos as $todo) {
// Only update the fields that are present in CalDAV
@@ -74,12 +76,27 @@ public function handle(CaldavService $service)
}
}
Todo::upsert($data, 'id');
// Get the existing todo from the database
$existingTodo = Todo::find($data['id']);
if ($existingTodo) {
// Compare the etag or modification date to determine if the todo needs to be updated
if ($existingTodo->etag !== $data['etag'] || $existingTodo->last_modified < $data['last_modified']) {
// Update the existing todo with the new data
$existingTodo->update($data);
}
} else {
// Create a new todo if it doesn't exist
Todo::create($data);
}
$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.
// Collect hrefs/URLs returned by the CalDAV server so we can compare
// them to local todos and find those 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)));
@@ -98,12 +115,14 @@ public function handle(CaldavService $service)
->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.");
+6 -3
View File
@@ -19,7 +19,7 @@ public function index()
* Display a listing of the resource.
* @param Request $request
* @param string $modelType The type of the model (e.g., 'Customer', 'Invoice')
* @param string $modelId The ID of the model
* @param int $modelId The ID of the model
* @return \Illuminate\Http\JsonResponse
*/
public function todosForModel(Request $request, string $modelType, int $modelId)
@@ -27,9 +27,12 @@ public function todosForModel(Request $request, string $modelType, int $modelId)
$model = app("App\\Models\\" . $modelType)::findOrFail($modelId);
// Load all todos of the model with the user relationship
$todos = $model->todos()->with('type')->orderBy('created_at', 'desc')->get();
$todos = $model->todos()
->with('type')
->orderBy('created_at', 'desc')
->get();
// Transformiere die Daten in camelCase
// Transform data to camelCase
$notesArray = $todos->map(function ($todo) {
return ApiDataTransformer::snakeToCamel($todo->toArray());
});
+2 -2
View File
@@ -163,8 +163,8 @@ public function getTodos()
if (isset($vcalendar->VTODO->PRIORITY)) $todo->priority = $vcalendar->VTODO->PRIORITY->getValue();
if (isset($vcalendar->VTODO->STATUS)) $todo->status = $vcalendar->VTODO->STATUS->getValue();
if (isset($vcalendar->VTODO->{'RELATED-TO'})) $todo->parent = $vcalendar->VTODO->{'RELATED-TO'}->getValue();
$todo->created_at = $vcalendar->VTODO->CREATED->getDateTime();
$todo->last_modified = $vcalendar->VTODO->{'LAST-MODIFIED'}->getDateTime();
$todo->created_at = $vcalendar->VTODO->CREATED?->getDateTime();
$todo->last_modified = $vcalendar->VTODO->{'LAST-MODIFIED'}?->getDateTime();
$todos[] = $todo;
}