Skip to content
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

set birthday year to 1970 if no year, take X-APPLE-OMIT-YEAR into account #11832

Merged
merged 3 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>WebDAV</name>
<summary>WebDAV endpoint</summary>
<description>WebDAV endpoint</description>
<version>1.9.1</version>
<version>1.9.2</version>
<licence>agpl</licence>
<author>owncloud.org</author>
<namespace>DAV</namespace>
Expand All @@ -28,6 +28,7 @@
<repair-steps>
<post-migration>
<step>OCA\DAV\Migration\FixBirthdayCalendarComponent</step>
<step>OCA\DAV\Migration\RegenerateBirthdayCalendars</step>
<step>OCA\DAV\Migration\CalDAVRemoveEmptyValue</step>
<step>OCA\DAV\Migration\BuildCalendarSearchIndex</step>
<step>OCA\DAV\Migration\RefreshWebcalJobRegistrar</step>
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
'OCA\\DAV\\Migration\\ChunkCleanup' => $baseDir . '/../lib/Migration/ChunkCleanup.php',
'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => $baseDir . '/../lib/Migration/FixBirthdayCalendarComponent.php',
'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => $baseDir . '/../lib/Migration/RefreshWebcalJobRegistrar.php',
'OCA\\DAV\\Migration\\RegenerateBirthdayCalendars' => $baseDir . '/../lib/Migration/RegenerateBirthdayCalendars.php',
'OCA\\DAV\\Migration\\RemoveClassifiedEventActivity' => $baseDir . '/../lib/Migration/RemoveClassifiedEventActivity.php',
'OCA\\DAV\\Migration\\Version1004Date20170825134824' => $baseDir . '/../lib/Migration/Version1004Date20170825134824.php',
'OCA\\DAV\\Migration\\Version1004Date20170919104507' => $baseDir . '/../lib/Migration/Version1004Date20170919104507.php',
Expand Down
1 change: 1 addition & 0 deletions apps/dav/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class ComposerStaticInitDAV
'OCA\\DAV\\Migration\\ChunkCleanup' => __DIR__ . '/..' . '/../lib/Migration/ChunkCleanup.php',
'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => __DIR__ . '/..' . '/../lib/Migration/FixBirthdayCalendarComponent.php',
'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => __DIR__ . '/..' . '/../lib/Migration/RefreshWebcalJobRegistrar.php',
'OCA\\DAV\\Migration\\RegenerateBirthdayCalendars' => __DIR__ . '/..' . '/../lib/Migration/RegenerateBirthdayCalendars.php',
'OCA\\DAV\\Migration\\RemoveClassifiedEventActivity' => __DIR__ . '/..' . '/../lib/Migration/RemoveClassifiedEventActivity.php',
'OCA\\DAV\\Migration\\Version1004Date20170825134824' => __DIR__ . '/..' . '/../lib/Migration/Version1004Date20170825134824.php',
'OCA\\DAV\\Migration\\Version1004Date20170919104507' => __DIR__ . '/..' . '/../lib/Migration/Version1004Date20170919104507.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __construct(BirthdayService $birthdayService,
*/
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');
Expand All @@ -64,6 +65,10 @@ public function run($arguments) {
return;
}

if ($purgeBeforeGenerating) {
$this->birthdayService->resetForUser($userId);
}

$this->birthdayService->syncUser($userId);
}
}
76 changes: 62 additions & 14 deletions apps/dav/lib/CalDAV/BirthdayService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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;
Expand All @@ -56,6 +57,9 @@ class BirthdayService {
/** @var IConfig */
private $config;

/** @var IDBConnection */
private $dbConnection;

/**
* BirthdayService constructor.
*
Expand All @@ -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;
}

/**
Expand All @@ -85,9 +90,9 @@ public function onCardChanged($addressBookId, $cardUri, $cardData) {
$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)) {
Expand Down Expand Up @@ -132,9 +137,9 @@ public function onCardDeleted($addressBookId, $cardUri) {
* @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',
Expand All @@ -150,9 +155,10 @@ public function ensureCalendarExists($principal) {
* @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;
}
Expand Down Expand Up @@ -191,23 +197,47 @@ public function buildDateFromContact($cardData, $dateField, $postfix, $summarySy
}

$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 {
$date = new \DateTime($birthday);
} 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');
Expand All @@ -226,6 +256,11 @@ public function buildDateFromContact($cardData, $dateField, $postfix, $summarySy
$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'));
Expand All @@ -235,6 +270,19 @@ public function buildDateFromContact($cardData, $dateField, $postfix, $summarySy
return $vCal;
}

/**
* @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
*/
Expand Down Expand Up @@ -298,7 +346,7 @@ protected function getAllAffectedPrincipals($addressBookId) {
*/
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)) {
Expand Down
85 changes: 85 additions & 0 deletions apps/dav/lib/Migration/RegenerateBirthdayCalendars.php
Original file line number Diff line number Diff line change
@@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,39 @@ public function testRun() {
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));

$this->birthdayService->expects($this->never())
->method('resetForUser')
->with('user123');

$this->birthdayService->expects($this->once())
->method('syncUser')
->with('user123');

$this->backgroundJob->run(['userId' => 'user123']);
}

public function testRunAndReset() {
$this->config->expects($this->once())
->method('getAppValue')
->with('dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));

$this->config->expects($this->once())
->method('getUserValue')
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
->will($this->returnValue('yes'));

$this->birthdayService->expects($this->once())
->method('resetForUser')
->with('user123');

$this->birthdayService->expects($this->once())
->method('syncUser')
->with('user123');

$this->backgroundJob->run(['userId' => 'user123', 'purgeBeforeGenerating' => true]);
}

public function testRunGloballyDisabled() {
$this->config->expects($this->once())
->method('getAppValue')
Expand Down
Loading