Skip to content

Commit

Permalink
fixup! feat: OCC and OCS Calendar Import/Export
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Jan 26, 2025
1 parent 4006d99 commit c69a4fb
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 16 deletions.
70 changes: 59 additions & 11 deletions apps/dav/lib/CalDAV/CalendarImpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\Component\VTimeZone;
use Sabre\VObject\ITip\Message;
use Sabre\VObject\Node;
use Sabre\VObject\Property;
use Sabre\VObject\Reader;
use Sabre\VObject\UUIDUtil;
Expand Down Expand Up @@ -293,18 +294,41 @@ public function export(?CalendarExportRange $range = null): Generator {
* @since 31.0.0
*
*/
public function import(CalendarImportSettings $settings, VCalendar ...$vObjects): array {
public function import(CalendarImportSettings $settings, callable $generator): array {

$calendarId = $this->getKey();
$outcome = [];
foreach ($vObjects as $vObject) {

foreach ($generator($settings) as $vObject) {
$components = $vObject->getBaseComponents();
// determine if the object has only one base component type
if (count($components) > 1) {
throw new InvalidArgumentException('Import failure: objects can not contain more than one base instance object');
if ($settings->errors === 1) {
throw new InvalidArgumentException('Error importing calendar object, discovered object with multiple base component types');
}
$outcome['mbct'] = ['One or more objects discovered with multiple base component types'];
continue;
}
// determine if the object has a uid
if (!isset($components[0]->UID)) {
if ($settings->errors === 1) {
throw new InvalidArgumentException('Error importing calendar object, discovered object without a UID');
}
$outcome['noid'] = ['One or more objects discovered without a UID'];
continue;
}
$uid = $components[0]->UID->getValue();

// validate object
if ($settings->validate !== 0) {
$issues = $this->validateComponent($vObject, true, 3);
if ($settings->validate === 1 && $issues !== []) {
$outcome[$uid] = $issues;
continue;
} elseif ($settings->validate === 2 && $issues !== []) {
throw new InvalidArgumentException('Error importing calendar object <' . $uid . '>, ' . $issues[0]);
}
}

$objectId = $this->backend->getCalendarObjectByUID($this->calendarInfo['principaluri'], $uid);
$objectData = $vObject->serialize();

Expand All @@ -316,17 +340,41 @@ public function import(CalendarImportSettings $settings, VCalendar ...$vObjects)
$objectId,
$objectData
);
} elseif ($objectId !== null && $settings->supersede) {
$this->backend->updateCalendarObject(
$calendarId,
$objectId,
$objectData
);
$outcome[$uid] = ['Created'];
} elseif ($objectId !== null) {
if ($settings->supersede) {
$this->backend->updateCalendarObject(
$calendarId,
$objectId,
$objectData
);
$outcome[$uid] = ['Updated'];
} else {
$outcome[$uid] = ['Exists'];
}
}
}

return $outcome;

}

private function validateComponent(VCalendar $vObject, bool $repair, int $level): array {
// validate component(S)
$issues = $vObject->validate(Node::PROFILE_CALDAV);
// attempt to repair
if ($repair && count($issues) > 0) {
$issues = $vObject->validate(Node::REPAIR);
}
// filter out messages based on level
$result = [];
foreach ($issues as $key => $issue) {
if (isset($issue['level']) && $issue['level'] >= $level) {
$result[] = $issue['message'];
}
}

return $result;
}

}
5 changes: 2 additions & 3 deletions lib/public/Calendar/CalendarImportSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class CalendarImportSettings {

public string $format = 'ical';
public bool $supersede = false;
public bool $emitEvent = false;
public bool $emitITip = false;
public int $bulk = 32;
public int $validate = 1; // 0 - no validation, 1 - validate and skip on issue, 2 - validate and fail on issue
public int $errors = 1; // 0 - continue, 1 - fail
public int $validate = 1; // 0 - no validation, 1 - validate and skip on issue, 2 - validate and fail on issue

}
4 changes: 2 additions & 2 deletions lib/public/Calendar/ICalendarImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ interface ICalendarImport {
*
* @since 31.0.0
*
* @param VCalendar $vObjects
*
*/
public function import(CalendarImportSettings $settings, VCalendar ...$vObjects): array;
public function import(CalendarImportSettings $settings, callable $generator): array;

}

0 comments on commit c69a4fb

Please sign in to comment.