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:
@@ -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.");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user