Add initial Todo items and CalDAV sync
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Services\CaldavService;
|
||||
use App\Models\Todo;
|
||||
use App\Models\TodoType;
|
||||
use Sabre\VObject\Component\VCalendar;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class CaldavSyncCommand extends Command
|
||||
{
|
||||
protected $signature = 'caldav:sync {--calendar= : optional calendar path}';
|
||||
protected $description = 'Sync CalDAV VTODOs into local todos table';
|
||||
|
||||
public function handle(CaldavService $service)
|
||||
{
|
||||
$this->info('Starting CalDAV sync...');
|
||||
|
||||
$todos = $service->getTodos();
|
||||
|
||||
$count = 0;
|
||||
foreach ($todos as $todo) {
|
||||
Todo::upsert($todo->attributesToArray(), 'id');
|
||||
$count++;
|
||||
}
|
||||
|
||||
$this->info("Synced " . count($todos) . " todos.");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
|
||||
class Kernel extends ConsoleKernel
|
||||
{
|
||||
/**
|
||||
* Register the Artisan commands for the application.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $commands = [
|
||||
\App\Console\Commands\CaldavSyncCommand::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* Define the application's command schedule.
|
||||
*/
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
// Beispiel: CalDAV Sync jede Stunde
|
||||
$schedule->command('caldav:sync')->hourly();
|
||||
|
||||
// Alternativen:
|
||||
// $schedule->command('caldav:sync')->daily();
|
||||
// $schedule->command('caldav:sync --calendar=/calendars/me/default/')->dailyAt('02:00');
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the commands for the application.
|
||||
*/
|
||||
protected function commands(): void
|
||||
{
|
||||
$this->load(__DIR__ . '/Commands');
|
||||
|
||||
require base_path('routes/console.php');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Todo;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use App\Support\ApiDataTransformer;
|
||||
|
||||
class TodoController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$todos = Todo::with('type')->get();
|
||||
return ApiDataTransformer::snakeToCamel($todos->toArray());
|
||||
}
|
||||
|
||||
public function show(string $id)
|
||||
{
|
||||
return Todo::with('type')->findOrFail($id);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'id' => 'nullable|string',
|
||||
'etag' => 'nullable|string',
|
||||
'title' => 'required|string',
|
||||
'description' => 'nullable|string',
|
||||
'type_id' => 'nullable|exists:todo_types,id',
|
||||
'url' => 'nullable|url',
|
||||
'due_date' => 'nullable|date',
|
||||
'recurring' => 'nullable|string',
|
||||
'priority' => 'nullable|integer',
|
||||
'status' => 'nullable|string',
|
||||
'parent' => 'nullable|string',
|
||||
'object' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$data['id'] = $data['id'] ?? (string) Str::uuid();
|
||||
$now = now();
|
||||
$data['created_at'] = $data['created_at'] ?? $now;
|
||||
$data['last_modified'] = $now;
|
||||
|
||||
$todo = Todo::create($data);
|
||||
|
||||
return response()->json($todo, 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$todo = Todo::findOrFail($id);
|
||||
|
||||
$data = $request->validate([
|
||||
'etag' => 'nullable|string',
|
||||
'title' => 'sometimes|required|string',
|
||||
'description' => 'nullable|string',
|
||||
'type_id' => 'nullable|exists:todo_types,id',
|
||||
'url' => 'nullable|url',
|
||||
'due_date' => 'nullable|date',
|
||||
'recurring' => 'nullable|string',
|
||||
'priority' => 'nullable|integer',
|
||||
'status' => 'nullable|string',
|
||||
'parent' => 'nullable|string',
|
||||
'object' => 'nullable|string',
|
||||
]);
|
||||
|
||||
$data['last_modified'] = now();
|
||||
|
||||
$todo->update($data);
|
||||
|
||||
return response()->json($todo);
|
||||
}
|
||||
|
||||
public function destroy(string $id)
|
||||
{
|
||||
$todo = Todo::findOrFail($id);
|
||||
$todo->delete();
|
||||
|
||||
return response()->noContent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Todo extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $primaryKey = 'id';
|
||||
public $incrementing = false;
|
||||
protected $keyType = 'string';
|
||||
|
||||
// Wir verwalten created_at / last_modified manuell
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'id',
|
||||
'etag',
|
||||
'title',
|
||||
'description',
|
||||
'type_id',
|
||||
'url',
|
||||
'due_date',
|
||||
'recurring',
|
||||
'priority',
|
||||
'status',
|
||||
'created_at',
|
||||
'last_modified',
|
||||
'parent',
|
||||
'object',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'due_date' => 'datetime',
|
||||
'created_at' => 'datetime',
|
||||
'last_modified' => 'datetime',
|
||||
];
|
||||
|
||||
public function type()
|
||||
{
|
||||
return $this->belongsTo(TodoType::class, 'type_id');
|
||||
}
|
||||
|
||||
public function parentTodo()
|
||||
{
|
||||
return $this->belongsTo(self::class, 'parent');
|
||||
}
|
||||
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class TodoType extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name'];
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
public function todos()
|
||||
{
|
||||
return $this->hasMany(Todo::class, 'type_id');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Todo;
|
||||
use Sabre\DAV\Client;
|
||||
use Sabre\DAV\Xml\Service;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Sabre\VObject;
|
||||
|
||||
|
||||
class CalendarInfo
|
||||
{
|
||||
public $id;
|
||||
public $displayName;
|
||||
public $url;
|
||||
public $ctag;
|
||||
public $syncToken;
|
||||
public $color;
|
||||
}
|
||||
|
||||
|
||||
class CaldavService
|
||||
{
|
||||
protected Client $client;
|
||||
protected string $baseUrl;
|
||||
protected string $userName;
|
||||
protected CalendarInfo $calendar;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$cfg = config('caldav', []);
|
||||
|
||||
$this->calendar = new CalendarInfo();
|
||||
$this->baseUrl = rtrim($cfg['base_url'] ?? '', '/') . '/';
|
||||
$this->userName = $cfg['username'] ?? '';
|
||||
$this->calendar->displayName = $cfg['calendar_name'] ?? '';
|
||||
|
||||
if (
|
||||
empty($this->baseUrl) ||
|
||||
empty($this->userName) ||
|
||||
empty($cfg['password'] ?? '') ||
|
||||
empty($this->calendar->displayName)
|
||||
) {
|
||||
Log::warning('CalDAV config incomplete', ['base' => $this->baseUrl, 'calendarName' => $this->calendar->displayName, 'userName' => $this->userName]);
|
||||
}
|
||||
|
||||
$this->client = new Client([
|
||||
'baseUri' => $this->baseUrl,
|
||||
'userName' => $cfg['username'] ?? null,
|
||||
'password' => $cfg['password'] ?? null,
|
||||
'authType' => \Sabre\DAV\Client::AUTH_BASIC,
|
||||
'userAgent' => $cfg['user_agent'] ?? 'caldav-client',
|
||||
]);
|
||||
|
||||
$this->getCalendarInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests all calendars of the user and gets the ID that belongs to the calendar name
|
||||
*/
|
||||
public function getCalendarId()
|
||||
{
|
||||
$response = $this->client->propFind(
|
||||
'calendars/' . $this->userName . '/',
|
||||
[
|
||||
'{DAV:}resourcetype',
|
||||
'{DAV:}displayname',
|
||||
],
|
||||
1
|
||||
);
|
||||
|
||||
foreach ($response as $href => $node) {
|
||||
if (array_key_exists('{DAV:}displayname', $node) && strcasecmp($node['{DAV:}displayname'], $this->calendar->displayName) == 0) {
|
||||
$this->calendar->url = $href;
|
||||
|
||||
$parts = explode('/', trim($href, '/'));
|
||||
return array_pop($parts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Request information about the calendar (ctag, sync-token, color)
|
||||
*/
|
||||
public function getCalendarInfo()
|
||||
{
|
||||
$this->calendar->id = $this->getCalendarId();
|
||||
|
||||
$response = $this->client->propFind(
|
||||
'calendars/' . $this->userName . '/' . $this->calendar->id,
|
||||
[
|
||||
'{DAV:}displayname',
|
||||
'{http://calendarserver.org/ns/}getctag',
|
||||
'{DAV:}sync-token',
|
||||
'{http://apple.com/ns/ical/}calendar-color',
|
||||
]
|
||||
);
|
||||
|
||||
// TODO: store ctag to compare whether the calendar has changed
|
||||
$this->calendar->ctag = $response['{http://calendarserver.org/ns/}getctag'];
|
||||
$this->calendar->syncToken = $response['{DAV:}sync-token'];
|
||||
$this->calendar->color = $response['{http://apple.com/ns/ical/}calendar-color'];
|
||||
|
||||
return $this->calendar;
|
||||
}
|
||||
|
||||
public function getTodos()
|
||||
{
|
||||
$body = '<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">'
|
||||
. ' <d:prop>'
|
||||
. ' <d:getetag />'
|
||||
. ' <c:calendar-data />'
|
||||
. ' </d:prop>'
|
||||
. ' <c:filter>'
|
||||
. ' <c:comp-filter name="VCALENDAR">'
|
||||
. ' <c:comp-filter name="VTODO" />'
|
||||
. ' </c:comp-filter>'
|
||||
. ' </c:filter>'
|
||||
. '</c:calendar-query>';
|
||||
|
||||
$headers = [
|
||||
'Depth' => '1',
|
||||
'Content-Type' => 'application/xml; charset=utf-8',
|
||||
'Prefer' => 'return-minimal'
|
||||
];
|
||||
|
||||
$response = $this->client->request(
|
||||
'REPORT',
|
||||
'calendars/' . $this->userName . '/' . $this->calendar->id,
|
||||
$body,
|
||||
$headers
|
||||
);
|
||||
|
||||
|
||||
// Sabre\DAV\Xml\Service
|
||||
$service = new Service();
|
||||
// Sabre\DAV\Xml\Response\MultiStatus
|
||||
$multiStatus = $service->parse($response['body']);
|
||||
// Sabre\DAV\Xml\Element\Response
|
||||
$responses = $multiStatus->getResponses();
|
||||
|
||||
$todos = [];
|
||||
foreach ($responses as $key => $response) {
|
||||
$href = $response->getHref();
|
||||
$properties = $response->getResponseProperties();
|
||||
$calendarData = $properties[200]['{urn:ietf:params:xml:ns:caldav}calendar-data'];
|
||||
$etag = $properties[200]['{DAV:}getetag'];
|
||||
$vcalendar = VObject\Reader::read($calendarData);
|
||||
|
||||
// Create new todo
|
||||
$todo = new Todo();
|
||||
|
||||
// Map VTODO -> App\Model\Todo
|
||||
$todo->id = isset($vcalendar->VTODO->UID) ? $vcalendar->VTODO->UID->getValue() : uniqid();
|
||||
$todo->etag = $etag;
|
||||
$todo->title = isset($vcalendar->VTODO->SUMMARY) ? $vcalendar->VTODO->SUMMARY->getValue() : 'Neues Todo';
|
||||
if (isset($vcalendar->VTODO->DESCRIPTION)) $todo->description = $vcalendar->VTODO->DESCRIPTION->getValue();
|
||||
$todo->type_id = 2;
|
||||
$todo->url = $href;
|
||||
if (isset($vcalendar->VTODO->DUE)) $todo->due_date = $vcalendar->VTODO->DUE->getDateTime();
|
||||
if(isset($vcalendar->VTODO->RRULE)) $todo->recurring = $vcalendar->VTODO->RRULE->getValue();
|
||||
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();
|
||||
|
||||
$todos[] = $todo;
|
||||
}
|
||||
|
||||
return $todos;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user