Files
Caramel-CRM/app/Console/Commands/CaldavSyncCommand.php
T
2026-02-17 10:35:03 +01:00

67 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
use App\Services\CaldavService;
use App\Models\Todo;
class CaldavSyncCommand extends Command
{
protected $signature = 'caldav:sync';
protected $description = 'Sync CalDAV VTODOs into local todos table';
public function handle(CaldavService $service)
{
// only run every 5 minutes although the task is called every minute
$cacheKey = 'caldav_sync_last_run';
if (\Illuminate\Support\Facades\Cache::has($cacheKey)) {
Log::info('CalDAV sync Throttled');
return 0;
}
Cache::put($cacheKey, true, 300);
Log::info('Running CalDAV sync');
$this->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;
}
}