2025-12-02 17:32:52 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
2025-12-03 14:23:03 +01:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
2025-12-02 17:32:52 +01:00
|
|
|
use App\Services\CaldavService;
|
|
|
|
|
use App\Models\Todo;
|
|
|
|
|
|
|
|
|
|
class CaldavSyncCommand extends Command
|
|
|
|
|
{
|
2025-12-03 14:23:03 +01:00
|
|
|
protected $signature = 'caldav:sync';
|
2025-12-02 17:32:52 +01:00
|
|
|
protected $description = 'Sync CalDAV VTODOs into local todos table';
|
|
|
|
|
|
|
|
|
|
public function handle(CaldavService $service)
|
|
|
|
|
{
|
2025-12-03 14:23:03 +01:00
|
|
|
// only run every 5 minutes although the task is called every minute
|
|
|
|
|
$cacheKey = 'caldav_sync_last_run';
|
|
|
|
|
if (\Illuminate\Support\Facades\Cache::has($cacheKey)) {
|
2025-12-07 12:55:33 +01:00
|
|
|
Log::info('CalDAV sync Throttled');
|
2025-12-03 14:23:03 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
Cache::put($cacheKey, true, 300);
|
|
|
|
|
|
|
|
|
|
Log::info('Running CalDAV sync');
|
|
|
|
|
$this->info('Starting CalDAV sync');
|
2025-12-02 17:32:52 +01:00
|
|
|
|
|
|
|
|
$todos = $service->getTodos();
|
|
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
|
foreach ($todos as $todo) {
|
|
|
|
|
Todo::upsert($todo->attributesToArray(), 'id');
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 12:55:33 +01:00
|
|
|
Log::info("Synced " . count($todos) . " todos.");
|
2025-12-02 17:32:52 +01:00
|
|
|
$this->info("Synced " . count($todos) . " todos.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|