-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33251 from cneukom/feature/birthday-calendar-remi…
…nder-settings Birthday Calendar Reminder Setting
- Loading branch information
Showing
2 changed files
with
99 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
* @author Sven Strickroth <[email protected]> | ||
* @author Thomas Müller <[email protected]> | ||
* @author Valdnet <[email protected]> | ||
* @author Cédric Neukom <[email protected]> | ||
* | ||
* @license AGPL-3.0 | ||
* | ||
|
@@ -86,6 +87,9 @@ public function onCardChanged(int $addressBookId, | |
|
||
$targetPrincipals = $this->getAllAffectedPrincipals($addressBookId); | ||
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId); | ||
if ($book === null) { | ||
return; | ||
} | ||
$targetPrincipals[] = $book['principaluri']; | ||
$datesToSync = [ | ||
['postfix' => '', 'field' => 'BDAY'], | ||
|
@@ -98,9 +102,14 @@ public function onCardChanged(int $addressBookId, | |
continue; | ||
} | ||
|
||
$reminderOffset = $this->getReminderOffsetForUser($principalUri); | ||
|
||
$calendar = $this->ensureCalendarExists($principalUri); | ||
if ($calendar === null) { | ||
return; | ||
} | ||
foreach ($datesToSync as $type) { | ||
$this->updateCalendar($cardUri, $cardData, $book, (int) $calendar['id'], $type); | ||
$this->updateCalendar($cardUri, $cardData, $book, (int) $calendar['id'], $type, $reminderOffset); | ||
} | ||
} | ||
} | ||
|
@@ -148,12 +157,14 @@ public function ensureCalendarExists(string $principal): ?array { | |
* @param $cardData | ||
* @param $dateField | ||
* @param $postfix | ||
* @param $reminderOffset | ||
* @return VCalendar|null | ||
* @throws InvalidDataException | ||
*/ | ||
public function buildDateFromContact(string $cardData, | ||
string $dateField, | ||
string $postfix):?VCalendar { | ||
public function buildDateFromContact(string $cardData, | ||
string $dateField, | ||
string $postfix, | ||
?string $reminderOffset):?VCalendar { | ||
if (empty($cardData)) { | ||
return null; | ||
} | ||
|
@@ -258,11 +269,13 @@ public function buildDateFromContact(string $cardData, | |
if ($originalYear !== null) { | ||
$vEvent->{'X-NEXTCLOUD-BC-YEAR'} = (string) $originalYear; | ||
} | ||
$alarm = $vCal->createComponent('VALARM'); | ||
$alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION'])); | ||
$alarm->add($vCal->createProperty('ACTION', 'DISPLAY')); | ||
$alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'})); | ||
$vEvent->add($alarm); | ||
if ($reminderOffset) { | ||
$alarm = $vCal->createComponent('VALARM'); | ||
$alarm->add($vCal->createProperty('TRIGGER', $reminderOffset, ['VALUE' => 'DURATION'])); | ||
$alarm->add($vCal->createProperty('ACTION', 'DISPLAY')); | ||
$alarm->add($vCal->createProperty('DESCRIPTION', $vEvent->{'SUMMARY'})); | ||
$vEvent->add($alarm); | ||
} | ||
$vCal->add($vEvent); | ||
return $vCal; | ||
} | ||
|
@@ -344,16 +357,18 @@ protected function getAllAffectedPrincipals(int $addressBookId) { | |
* @param array $book | ||
* @param int $calendarId | ||
* @param array $type | ||
* @param string $reminderOffset | ||
* @throws InvalidDataException | ||
* @throws \Sabre\DAV\Exception\BadRequest | ||
*/ | ||
private function updateCalendar(string $cardUri, | ||
string $cardData, | ||
array $book, | ||
int $calendarId, | ||
array $type):void { | ||
array $type, | ||
?string $reminderOffset):void { | ||
$objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; | ||
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix']); | ||
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $reminderOffset); | ||
$existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); | ||
if ($calendarData === null) { | ||
if ($existing !== null) { | ||
|
@@ -393,15 +408,28 @@ private function isGloballyEnabled():bool { | |
return $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes') === 'yes'; | ||
} | ||
|
||
/** | ||
* Extracts the userId part of a principal | ||
* | ||
* @param string $userPrincipal | ||
* @return string|null | ||
*/ | ||
private function principalToUserId(string $userPrincipal):?string { | ||
if (substr($userPrincipal, 0, 17) === 'principals/users/') { | ||
return substr($userPrincipal, 17); | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Checks if the user opted-out of birthday calendars | ||
* | ||
* @param string $userPrincipal The user principal to check for | ||
* @return bool | ||
*/ | ||
private function isUserEnabled(string $userPrincipal):bool { | ||
if (strpos($userPrincipal, 'principals/users/') === 0) { | ||
$userId = substr($userPrincipal, 17); | ||
$userId = $this->principalToUserId($userPrincipal); | ||
if ($userId !== null) { | ||
$isEnabled = $this->config->getUserValue($userId, 'dav', 'generateBirthdayCalendar', 'yes'); | ||
return $isEnabled === 'yes'; | ||
} | ||
|
@@ -410,6 +438,23 @@ private function isUserEnabled(string $userPrincipal):bool { | |
return true; | ||
} | ||
|
||
/** | ||
* Get the reminder offset value for a user. This is a duration string (e.g. | ||
* PT9H) or null if no reminder is wanted. | ||
* | ||
* @param string $userPrincipal | ||
* @return string|null | ||
*/ | ||
private function getReminderOffsetForUser(string $userPrincipal):?string { | ||
$userId = $this->principalToUserId($userPrincipal); | ||
if ($userId !== null) { | ||
return $this->config->getUserValue($userId, 'dav', 'birthdayCalendarReminderOffset', 'PT9H') ?: null; | ||
} | ||
|
||
// not sure how we got here, just be on the safe side and return the default value | ||
return 'PT9H'; | ||
} | ||
|
||
/** | ||
* Formats title of Birthday event | ||
* | ||
|
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