-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ocp): calendar event builder api
Signed-off-by: Richard Steinmetz <[email protected]>
- Loading branch information
Showing
6 changed files
with
248 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\Calendar; | ||
|
||
use DateTimeInterface; | ||
use OCP\Calendar\ICalendarEventBuilder; | ||
use OCP\Calendar\ICreateFromString; | ||
use Sabre\VObject\Component\VCalendar; | ||
use Sabre\VObject\Component\VEvent; | ||
|
||
class CalendarEventBuilder implements ICalendarEventBuilder { | ||
private ?DateTimeInterface $startDate = null; | ||
private ?DateTimeInterface $endDate = null; | ||
private ?string $summary = null; | ||
private ?string $description = null; | ||
private ?string $location = null; | ||
private ?array $organizer = null; | ||
private array $attendees = []; | ||
|
||
public function __construct( | ||
private readonly string $uid, | ||
) { | ||
} | ||
|
||
public function setStartDate(DateTimeInterface $start): ICalendarEventBuilder { | ||
$this->startDate = $start; | ||
return $this; | ||
} | ||
|
||
public function setEndDate(DateTimeInterface $end): ICalendarEventBuilder { | ||
$this->endDate = $end; | ||
return $this; | ||
} | ||
|
||
public function setSummary(string $summary): ICalendarEventBuilder { | ||
$this->summary = $summary; | ||
return $this; | ||
} | ||
|
||
public function setDescription(string $description): ICalendarEventBuilder { | ||
$this->description = $description; | ||
return $this; | ||
} | ||
|
||
public function setLocation(string $location): ICalendarEventBuilder { | ||
$this->location = $location; | ||
return $this; | ||
} | ||
|
||
public function setOrganizer(string $email, ?string $commonName = null): ICalendarEventBuilder { | ||
$this->organizer = [$email, $commonName]; | ||
return $this; | ||
} | ||
|
||
public function addAttendee(string $email, ?string $commonName = null): ICalendarEventBuilder { | ||
$this->attendees[] = [$email, $commonName]; | ||
return $this; | ||
} | ||
|
||
public function toIcs(): ?string { | ||
if ($this->startDate === null || $this->endDate === null || $this->summary === null) { | ||
return null; | ||
} | ||
|
||
if ($this->organizer === null && $this->attendees !== []) { | ||
return null; | ||
} | ||
|
||
$vcalendar = new VCalendar(); | ||
$props = [ | ||
'UID' => $this->uid, | ||
'SUMMARY' => $this->summary, | ||
'DTSTART' => $this->startDate, | ||
'DTEND' => $this->endDate, | ||
]; | ||
if ($this->description !== null) { | ||
$props['DESCRIPTION'] = $this->description; | ||
} | ||
if ($this->location !== null) { | ||
$props['LOCATION'] = $this->location; | ||
} | ||
/** @var VEvent $vevent */ | ||
$vevent = $vcalendar->add('VEVENT', $props); | ||
if ($this->organizer !== null) { | ||
self::addAttendeeToVEvent($vevent, 'ORGANIZER', $this->organizer); | ||
} | ||
foreach ($this->attendees as $attendee) { | ||
self::addAttendeeToVEvent($vevent, 'ATTENDEE', $attendee); | ||
} | ||
return $vcalendar->serialize(); | ||
} | ||
|
||
public function createInCalendar(ICreateFromString $calendar): string { | ||
$fileName = $this->uid . '.ics'; | ||
$calendar->createFromString($fileName, $this->toIcs()); | ||
return $fileName; | ||
} | ||
|
||
/** | ||
* @param array{0: string, 1: ?string} $tuple A tuple of [$email, $commonName] where $commonName may be null. | ||
*/ | ||
private static function addAttendeeToVEvent(VEvent $vevent, string $name, array $tuple): void { | ||
[$email, $cn] = $tuple; | ||
if (!str_starts_with($email, 'mailto:')) { | ||
$email = "mailto:$email"; | ||
} | ||
$params = []; | ||
if ($cn !== null) { | ||
$params['CN'] = $cn; | ||
} | ||
$vevent->add($name, $email, $params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCP\Calendar; | ||
|
||
use DateTimeInterface; | ||
use OCP\Calendar\Exceptions\CalendarException; | ||
|
||
/** | ||
* The calendar event builder can be used to conveniently build a calendar event and then serialize | ||
* it to a ICS string. The ICS string can be submitted to calendar instances implementing the | ||
* \OCP\Calendar\ICreateFromString interface. | ||
* | ||
* All setters return self to allow chaining method calls. | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
interface ICalendarEventBuilder { | ||
/** | ||
* Set the start date, time and time zone. | ||
* This property is required! | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function setStartDate(DateTimeInterface $start): self; | ||
|
||
/** | ||
* Set the end date, time and time zone. | ||
* This property is required! | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function setEndDate(DateTimeInterface $end): self; | ||
|
||
/** | ||
* Set the event summary or title. | ||
* This property is required! | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function setSummary(string $summary): self; | ||
|
||
/** | ||
* Set the event description. | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function setDescription(string $description): self; | ||
|
||
/** | ||
* Set the event location. It can either be a physical address or a URL. | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function setLocation(string $location): self; | ||
|
||
/** | ||
* Set the event organizer. | ||
* This property is required if attendees are added! | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function setOrganizer(string $email, ?string $commonName = null): self; | ||
|
||
/** | ||
* Add a new attendee to the event. | ||
* Adding at least one attendee requires also setting the organizer! | ||
* | ||
* @since 31.0.0 | ||
*/ | ||
public function addAttendee(string $email, ?string $commonName = null): self; | ||
|
||
/** | ||
* Serialize the built event to an ICS string if all required properties set. | ||
* | ||
* @since 31.0.0 | ||
* | ||
* @return string|null The serialized ICS string if all required properties have been set or null otherwise | ||
*/ | ||
public function toIcs(): ?string; | ||
|
||
/** | ||
* Create the event in the given calendar. | ||
* | ||
* @throws CalendarException If writing the event to the calendar fails | ||
* @return string The filename of the created event | ||
*/ | ||
public function createInCalendar(ICreateFromString $calendar): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters