- ///Process the attribution
- protected function execute(InputInterface $input, OutputInterface $output): int {
- //Compute period
- $period = new \DatePeriod(
- //Start from last week
- new \DateTime('-1 week'),
- //Iterate on each day
- new \DateInterval('P1D'),
- //End with next 2 week
- new \DateTime('+2 week')
- );
+ //Retrieve calendar events
+ try {
+ //Get events
+ $events = $this->cache->get(
+ //Cache key
+ //XXX: set to command.calendar.$mail
+ $cacheKey,
+ //Fetch mail calendar event list
+ function (ItemInterface $item) use ($calendar, &$synchronized): array {
+ //Expire after 1h
+ $item->expiresAfter(3600);
+
+ //Set synchronized
+ $synchronized = new \DateTime('now');
+
+ //Init events
+ $events = [];
+
+ //Set filters
+ //TODO: add a filter to only retrieve
+ $filters = [
+ //XXX: every event even deleted one to be able to update them
+ 'showDeleted' => true,
+ //XXX: every instances
+ 'singleEvents' => false,
+ //XXX: select only domain events
+ 'privateExtendedProperty' => 'domain='.$this->domain
+ #TODO: restrict events even more by time or updated datetime ? (-1 week to +2 week)
+ //TODO: fetch events one day before and one day after to avoid triggering double insert duplicate key 409 errors :=) on google
+ #'timeMin' => $period->getStartDate()->format(\DateTime::ISO8601),
+ #'timeMax' => $period->getEndDate()->format(\DateTime::ISO8601)
+ /*, 'iCalUID' => 'airlibre/?????'*//*'orderBy' => 'startTime', */
+ //updatedMin => new \DateTime('-1 week') ?
+ ];
+
+ //Set page token
+ $pageToken = null;
+
+ //Iterate until next page token is null
+ do {
+ //Get calendar events list
+ //XXX: see vendor/google/apiclient-services/src/Calendar/Resource/Events.php +289
+ $eventList = $this->service->events->listEvents($calendar['mail'], ['pageToken' => $pageToken]+$filters);
+
+ //Iterate on items
+ foreach($eventList->getItems() as $event) {
+ //With extended properties
+ if (($properties = $event->getExtendedProperties()) && ($private = $properties->getPrivate()) && isset($private['id']) && ($id = $private['id']) && isset($private['domain']) && $private['domain'] == $this->domain) {
+ //Add event
+ $events[$id] = $event;
+ //XXX: 3rd party events without matching prefix and id are skipped
+ #} else {
+ # #echo 'Skipping '.$event->getId().':'.$event->getSummary()."\n";
+ # echo 'Skipping '.$id.':'.$event->getSummary()."\n";
+ }
+ }
+ } while ($pageToken = $eventList->getNextPageToken());
+
+ //Return events
+ return $events;
+ }
+ );
+ //Catch exception
+ } catch(\Google\Service\Exception $e) {
+ //With 401 or code
+ //XXX: see https://cloud.google.com/apis/design/errors
+ if ($e->getCode() == 401 || $e->getCode() == 403) {
+ //Show error
+ fprintf(STDERR, 'Unable to list calendar %d events: %s', $calendar['id'], $e->getMessage()?:'');