-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Birthday Calendar Reminder Setting #33251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
miaulalala
merged 2 commits into
nextcloud:master
from
cneukom:feature/birthday-calendar-reminder-settings
Jul 26, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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); | ||
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show fixed
Hide fixed
|
||
| } | ||
| } | ||
| } | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -341,16 +354,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 { | ||
tcitworld marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $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) { | ||
|
|
@@ -390,15 +405,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'; | ||
| } | ||
|
|
@@ -407,6 +435,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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.