diff options
author | Morris Jobke <hey@morrisjobke.de> | 2019-02-20 10:16:47 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-20 10:16:47 +0100 |
commit | 2b76e27aad331704ca647746010e68629577ec0c (patch) | |
tree | ba3fcae625789789a53686a60e5b733c698d0c60 /apps/dav/lib | |
parent | 10ae7af87f2d152e7b76e1105b051a1e07fbaa27 (diff) | |
parent | 3acde071f306a2b0092eb5c81116c8ded971eaeb (diff) | |
download | nextcloud-server-2b76e27aad331704ca647746010e68629577ec0c.tar.gz nextcloud-server-2b76e27aad331704ca647746010e68629577ec0c.zip |
Merge pull request #11832 from nextcloud/bugfix/9849/birthday_without_year
set birthday year to 1970 if no year, take X-APPLE-OMIT-YEAR into account
Diffstat (limited to 'apps/dav/lib')
-rw-r--r-- | apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php | 5 | ||||
-rw-r--r-- | apps/dav/lib/CalDAV/BirthdayService.php | 76 | ||||
-rw-r--r-- | apps/dav/lib/Migration/RegenerateBirthdayCalendars.php | 85 |
3 files changed, 152 insertions, 14 deletions
diff --git a/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php b/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php index c4279c5108c..dd6fca73c8f 100644 --- a/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php +++ b/apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php @@ -51,6 +51,7 @@ class GenerateBirthdayCalendarBackgroundJob extends QueuedJob { */ public function run($arguments) { $userId = $arguments['userId']; + $purgeBeforeGenerating = $arguments['purgeBeforeGenerating'] ?? false; // make sure admin didn't change his mind $isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes'); @@ -64,6 +65,10 @@ class GenerateBirthdayCalendarBackgroundJob extends QueuedJob { return; } + if ($purgeBeforeGenerating) { + $this->birthdayService->resetForUser($userId); + } + $this->birthdayService->syncUser($userId); } } diff --git a/apps/dav/lib/CalDAV/BirthdayService.php b/apps/dav/lib/CalDAV/BirthdayService.php index 0f6e819bcc5..0e8926e775a 100644 --- a/apps/dav/lib/CalDAV/BirthdayService.php +++ b/apps/dav/lib/CalDAV/BirthdayService.php @@ -32,6 +32,7 @@ use Exception; use OCA\DAV\CardDAV\CardDavBackend; use OCA\DAV\DAV\GroupPrincipalBackend; use OCP\IConfig; +use OCP\IDBConnection; use Sabre\VObject\Component\VCalendar; use Sabre\VObject\Component\VCard; use Sabre\VObject\DateTimeParser; @@ -56,6 +57,9 @@ class BirthdayService { /** @var IConfig */ private $config; + /** @var IDBConnection */ + private $dbConnection; + /** * BirthdayService constructor. * @@ -64,11 +68,12 @@ class BirthdayService { * @param GroupPrincipalBackend $principalBackend * @param IConfig $config; */ - public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config) { + public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config, IDBConnection $dbConnection) { $this->calDavBackEnd = $calDavBackEnd; $this->cardDavBackEnd = $cardDavBackEnd; $this->principalBackend = $principalBackend; $this->config = $config; + $this->dbConnection = $dbConnection; } /** @@ -85,9 +90,9 @@ class BirthdayService { $book = $this->cardDavBackEnd->getAddressBookById($addressBookId); $targetPrincipals[] = $book['principaluri']; $datesToSync = [ - ['postfix' => '', 'field' => 'BDAY', 'symbol' => '*'], - ['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†"], - ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭"], + ['postfix' => '', 'field' => 'BDAY', 'symbol' => '*', 'utfSymbol' => '🎂'], + ['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "†", 'utfSymbol' => '⚰️'], + ['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "⚭", 'utfSymbol' => '💍'], ]; foreach ($targetPrincipals as $principalUri) { if (!$this->isUserEnabled($principalUri)) { @@ -132,9 +137,9 @@ class BirthdayService { * @throws \Sabre\DAV\Exception\BadRequest */ public function ensureCalendarExists($principal) { - $book = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); - if (!is_null($book)) { - return $book; + $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); + if (!is_null($calendar)) { + return $calendar; } $this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [ '{DAV:}displayname' => 'Contact birthdays', @@ -150,9 +155,10 @@ class BirthdayService { * @param string $dateField * @param string $postfix * @param string $summarySymbol + * @param string $utfSummarySymbol * @return null|VCalendar */ - public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol) { + public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol, $utfSummarySymbol) { if (empty($cardData)) { return null; } @@ -191,10 +197,26 @@ class BirthdayService { } $unknownYear = false; + $originalYear = null; if (!$dateParts['year']) { - $birthday = '1900-' . $dateParts['month'] . '-' . $dateParts['date']; + $birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date']; $unknownYear = true; + } else { + $parameters = $birthday->parameters(); + if (isset($parameters['X-APPLE-OMIT-YEAR'])) { + $omitYear = $parameters['X-APPLE-OMIT-YEAR']; + if ($dateParts['year'] === $omitYear) { + $birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date']; + $unknownYear = true; + } + } else { + $originalYear = (int)$dateParts['year']; + + if ($originalYear < 1970) { + $birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date']; + } + } } try { @@ -202,12 +224,20 @@ class BirthdayService { } catch (Exception $e) { return null; } - if ($unknownYear) { - $summary = $doc->FN->getValue() . ' ' . $summarySymbol; + if ($this->dbConnection->supports4ByteText()) { + if ($unknownYear) { + $summary = $utfSummarySymbol . ' ' . $doc->FN->getValue(); + } else { + $summary = $utfSummarySymbol . ' ' . $doc->FN->getValue() . " ($originalYear)"; + } } else { - $year = (int)$date->format('Y'); - $summary = $doc->FN->getValue() . " ($summarySymbol$year)"; + if ($unknownYear) { + $summary = $doc->FN->getValue() . ' ' . $summarySymbol; + } else { + $summary = $doc->FN->getValue() . " ($summarySymbol$originalYear)"; + } } + $vCal = new VCalendar(); $vCal->VERSION = '2.0'; $vEvent = $vCal->createComponent('VEVENT'); @@ -226,6 +256,11 @@ class BirthdayService { $vEvent->{'RRULE'} = 'FREQ=YEARLY'; $vEvent->{'SUMMARY'} = $summary; $vEvent->{'TRANSP'} = 'TRANSPARENT'; + $vEvent->{'X-NEXTCLOUD-BC-FIELD-TYPE'} = $dateField; + $vEvent->{'X-NEXTCLOUD-BC-UNKNOWN-YEAR'} = $unknownYear ? '1' : '0'; + 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')); @@ -238,6 +273,19 @@ class BirthdayService { /** * @param string $user */ + public function resetForUser($user) { + $principal = 'principals/users/'.$user; + $calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI); + $calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR); + + foreach($calendarObjects as $calendarObject) { + $this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR); + } + } + + /** + * @param string $user + */ public function syncUser($user) { $principal = 'principals/users/'.$user; $this->ensureCalendarExists($principal); @@ -298,7 +346,7 @@ class BirthdayService { */ private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) { $objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics'; - $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol']); + $calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol'], $type['utfSymbol']); $existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri); if (is_null($calendarData)) { if (!is_null($existing)) { diff --git a/apps/dav/lib/Migration/RegenerateBirthdayCalendars.php b/apps/dav/lib/Migration/RegenerateBirthdayCalendars.php new file mode 100644 index 00000000000..2c2b4eb0e9a --- /dev/null +++ b/apps/dav/lib/Migration/RegenerateBirthdayCalendars.php @@ -0,0 +1,85 @@ +<?php +/** + * @copyright 2019 Georg Ehrke <oc.list@georgehrke.com> + * + * @author Georg Ehrke <oc.list@georgehrke.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ +namespace OCA\DAV\Migration; + +use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob; +use OCP\BackgroundJob\IJobList; +use OCP\IConfig; +use OCP\IUser; +use OCP\IUserManager; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + +class RegenerateBirthdayCalendars implements IRepairStep { + + /** @var IUserManager */ + private $userManager; + + /** @var IJobList */ + private $jobList; + + /** @var IConfig */ + private $config; + + /** + * @param IUserManager $userManager, + * @param IJobList $jobList + * @param IConfig $config + */ + public function __construct(IUserManager $userManager, + IJobList $jobList, + IConfig $config) { + $this->userManager = $userManager; + $this->jobList = $jobList; + $this->config = $config; + } + + /** + * @return string + */ + public function getName() { + return 'Regenerating birthday calendars to use new icons and fix old birthday events without year'; + } + + /** + * @param IOutput $output + */ + public function run(IOutput $output) { + // only run once + if ($this->config->getAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix') === 'yes') { + $output->info('Repair step already executed'); + return; + } + + $output->info('Adding background jobs to regenerate birthday calendar'); + $this->userManager->callForAllUsers(function(IUser $user) { + $this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [ + 'userId' => $user->getUID(), + 'purgeBeforeGenerating' => true + ]); + }); + + // if all were done, no need to redo the repair during next upgrade + $this->config->setAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix', 'yes'); + } +}
\ No newline at end of file |