Skip to content

Commit 0eaeef2

Browse files
Partition events and exceptions beforehand
Signed-off-by: Marcel Robitaille <mail@marcelrobitaille.me>
1 parent f89e699 commit 0eaeef2

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

lib/Service/GoogleCalendarAPIService.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,11 @@ private function mapTime(array $obj): string {
102102

103103
/**
104104
* @param Event $e The event from which to generate the data.
105-
* @param array<Event> $events The collection of all events.
105+
* @param array<Event> $exceptions The events that represent recurring exceptions.
106106
* @param int $ncCalId The id of the event's calendar.
107107
* @param array $eventColors The event colors mapping.
108108
*/
109-
private function generateEventData(array $e, array $events, int $ncCalId, array $eventColors): string {
109+
private function generateEventData(array $e, array $exceptions, int $ncCalId, array $eventColors): string {
110110
$eventData = 'BEGIN:VEVENT' . "\n";
111111

112112
$eventData .= 'UID:' . strval($ncCalId) . '-' . $e['iCalUID'] . "\n";
@@ -199,9 +199,9 @@ private function generateEventData(array $e, array $events, int $ncCalId, array
199199
$eventData .= 'CLASS:PUBLIC' . "\n"
200200
. 'END:VEVENT' . "\n";
201201

202-
foreach ($events as $candidateEvent) {
203-
if (($candidateEvent['recurringEventId'] == $e['id']) && ($candidateEvent['id'] != $e['id'])) {
204-
$eventData .= $this->generateEventData($candidateEvent, $events, $ncCalId, $eventColors);
202+
foreach ($exceptions as $candiateException) {
203+
if (($candiateException['recurringEventId'] == $e['id']) && ($candiateException['id'] != $e['id'])) {
204+
$eventData .= $this->generateEventData($candiateException, $exceptions, $ncCalId, $eventColors);
205205
}
206206
}
207207

@@ -332,9 +332,23 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
332332
date_default_timezone_set('UTC');
333333
$allEvents = $this->config->getUserValue($userId, Application::APP_ID, 'consider_all_events', '1') === '1';
334334
$eventsGenerator = $this->getCalendarEvents($userId, $calId, $allEvents);
335-
$events = iterator_to_array($eventsGenerator);
335+
336+
// Normal events
337+
$events = [];
338+
// Exceptions to recurring events (recurringEventId set).
339+
$exceptions = [];
340+
341+
foreach ($eventsGenerator as $e) {
342+
if (isset($e['recurringEventId'])) {
343+
array_push($exceptions, $e);
344+
} else {
345+
array_push($events, $e);
346+
}
347+
}
348+
336349
$nbAdded = 0;
337350
$nbUpdated = 0;
351+
338352
/** @var Event $e */
339353
foreach ($events as $e) {
340354
$objectUri = $e['id'];
@@ -364,12 +378,7 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
364378
}
365379
}
366380

367-
// For recurring events, the parent event recursively calls generateEventData
368-
if (isset($e['recurringEventId'])) {
369-
continue;
370-
}
371-
372-
$eventData = $this->generateEventData($e, $events, $ncCalId, $eventColors);
381+
$eventData = $this->generateEventData($e, $exceptions, $ncCalId, $eventColors);
373382

374383
if ($eventData == '') {
375384
continue;
@@ -419,7 +428,7 @@ public function importCalendar(string $userId, string $calId, string $calName, ?
419428
* @param string $userId
420429
* @param string $calId
421430
* @param bool $allEvents
422-
* @return Generator
431+
* @return Generator<Event>
423432
*/
424433
private function getCalendarEvents(string $userId, string $calId, bool $allEvents): Generator {
425434
$params = [

0 commit comments

Comments
 (0)