Connect CalDAV todos to DB-Items, finish todo component and add it to pipeline items
This commit is contained in:
@@ -7,13 +7,14 @@
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use App\Services\CaldavService;
|
||||
use App\Models\Todo;
|
||||
use App\Models\PipelineItem;
|
||||
|
||||
class CaldavSyncCommand extends Command
|
||||
{
|
||||
protected $signature = 'caldav:sync';
|
||||
protected $description = 'Sync CalDAV VTODOs into local todos table';
|
||||
|
||||
public function handle(CaldavService $service)
|
||||
public function handle(CaldavService $service)
|
||||
{
|
||||
// only run every 5 minutes although the task is called every minute
|
||||
$cacheKey = 'caldav_sync_last_run';
|
||||
@@ -30,9 +31,53 @@ public function handle(CaldavService $service)
|
||||
|
||||
$count = 0;
|
||||
foreach ($todos as $todo) {
|
||||
Todo::upsert($todo->attributesToArray(), 'id');
|
||||
// Only update the fields that are present in CalDAV
|
||||
$data = $todo->attributesToArray();
|
||||
$data = array_intersect_key($data, array_flip([
|
||||
'id',
|
||||
'etag',
|
||||
'title',
|
||||
'description',
|
||||
'type_id',
|
||||
'url',
|
||||
'due_date',
|
||||
'recurring',
|
||||
'priority',
|
||||
'status',
|
||||
'created_at',
|
||||
'last_modified',
|
||||
'parent',
|
||||
'object',
|
||||
]));
|
||||
|
||||
// Parse the title to extract the todoable title and todo title
|
||||
if (isset($data['title'])) {
|
||||
$titleParts = explode('] ', $data['title'], 2);
|
||||
if (count($titleParts) === 2) {
|
||||
$todoableTitle = trim($titleParts[0], '[]');
|
||||
$todoTitle = $titleParts[1];
|
||||
|
||||
// Find the todoable model by title
|
||||
$models = [
|
||||
'PipelineItem' => PipelineItem::class,
|
||||
// Add other models here as needed
|
||||
];
|
||||
|
||||
foreach ($models as $modelName => $modelClass) {
|
||||
$todoable = $modelClass::where('title', $todoableTitle)->first();
|
||||
if ($todoable) {
|
||||
$data['todoable_type'] = 'App\\Models\\' . $modelName;
|
||||
$data['todoable_id'] = $todoable->id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Todo::upsert($data, '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) {
|
||||
|
||||
Reference in New Issue
Block a user