Skip to content

Commit 2b76e27

Browse files
authored
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
2 parents 10ae7af + 3acde07 commit 2b76e27

File tree

9 files changed

+390
-34
lines changed

9 files changed

+390
-34
lines changed

apps/dav/appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<repair-steps>
2929
<post-migration>
3030
<step>OCA\DAV\Migration\FixBirthdayCalendarComponent</step>
31+
<step>OCA\DAV\Migration\RegenerateBirthdayCalendars</step>
3132
<step>OCA\DAV\Migration\CalDAVRemoveEmptyValue</step>
3233
<step>OCA\DAV\Migration\BuildCalendarSearchIndex</step>
3334
<step>OCA\DAV\Migration\RefreshWebcalJobRegistrar</step>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@
159159
'OCA\\DAV\\Migration\\ChunkCleanup' => $baseDir . '/../lib/Migration/ChunkCleanup.php',
160160
'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => $baseDir . '/../lib/Migration/FixBirthdayCalendarComponent.php',
161161
'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => $baseDir . '/../lib/Migration/RefreshWebcalJobRegistrar.php',
162+
'OCA\\DAV\\Migration\\RegenerateBirthdayCalendars' => $baseDir . '/../lib/Migration/RegenerateBirthdayCalendars.php',
162163
'OCA\\DAV\\Migration\\RemoveClassifiedEventActivity' => $baseDir . '/../lib/Migration/RemoveClassifiedEventActivity.php',
163164
'OCA\\DAV\\Migration\\RemoveOrphanEventsAndContacts' => $baseDir . '/../lib/Migration/RemoveOrphanEventsAndContacts.php',
164165
'OCA\\DAV\\Migration\\Version1004Date20170825134824' => $baseDir . '/../lib/Migration/Version1004Date20170825134824.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class ComposerStaticInitDAV
174174
'OCA\\DAV\\Migration\\ChunkCleanup' => __DIR__ . '/..' . '/../lib/Migration/ChunkCleanup.php',
175175
'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => __DIR__ . '/..' . '/../lib/Migration/FixBirthdayCalendarComponent.php',
176176
'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => __DIR__ . '/..' . '/../lib/Migration/RefreshWebcalJobRegistrar.php',
177+
'OCA\\DAV\\Migration\\RegenerateBirthdayCalendars' => __DIR__ . '/..' . '/../lib/Migration/RegenerateBirthdayCalendars.php',
177178
'OCA\\DAV\\Migration\\RemoveClassifiedEventActivity' => __DIR__ . '/..' . '/../lib/Migration/RemoveClassifiedEventActivity.php',
178179
'OCA\\DAV\\Migration\\RemoveOrphanEventsAndContacts' => __DIR__ . '/..' . '/../lib/Migration/RemoveOrphanEventsAndContacts.php',
179180
'OCA\\DAV\\Migration\\Version1004Date20170825134824' => __DIR__ . '/..' . '/../lib/Migration/Version1004Date20170825134824.php',

apps/dav/lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function __construct(BirthdayService $birthdayService,
5151
*/
5252
public function run($arguments) {
5353
$userId = $arguments['userId'];
54+
$purgeBeforeGenerating = $arguments['purgeBeforeGenerating'] ?? false;
5455

5556
// make sure admin didn't change his mind
5657
$isGloballyEnabled = $this->config->getAppValue('dav', 'generateBirthdayCalendar', 'yes');
@@ -64,6 +65,10 @@ public function run($arguments) {
6465
return;
6566
}
6667

68+
if ($purgeBeforeGenerating) {
69+
$this->birthdayService->resetForUser($userId);
70+
}
71+
6772
$this->birthdayService->syncUser($userId);
6873
}
6974
}

apps/dav/lib/CalDAV/BirthdayService.php

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCA\DAV\CardDAV\CardDavBackend;
3333
use OCA\DAV\DAV\GroupPrincipalBackend;
3434
use OCP\IConfig;
35+
use OCP\IDBConnection;
3536
use Sabre\VObject\Component\VCalendar;
3637
use Sabre\VObject\Component\VCard;
3738
use Sabre\VObject\DateTimeParser;
@@ -56,6 +57,9 @@ class BirthdayService {
5657
/** @var IConfig */
5758
private $config;
5859

60+
/** @var IDBConnection */
61+
private $dbConnection;
62+
5963
/**
6064
* BirthdayService constructor.
6165
*
@@ -64,11 +68,12 @@ class BirthdayService {
6468
* @param GroupPrincipalBackend $principalBackend
6569
* @param IConfig $config;
6670
*/
67-
public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config) {
71+
public function __construct(CalDavBackend $calDavBackEnd, CardDavBackend $cardDavBackEnd, GroupPrincipalBackend $principalBackend, IConfig $config, IDBConnection $dbConnection) {
6872
$this->calDavBackEnd = $calDavBackEnd;
6973
$this->cardDavBackEnd = $cardDavBackEnd;
7074
$this->principalBackend = $principalBackend;
7175
$this->config = $config;
76+
$this->dbConnection = $dbConnection;
7277
}
7378

7479
/**
@@ -85,9 +90,9 @@ public function onCardChanged($addressBookId, $cardUri, $cardData) {
8590
$book = $this->cardDavBackEnd->getAddressBookById($addressBookId);
8691
$targetPrincipals[] = $book['principaluri'];
8792
$datesToSync = [
88-
['postfix' => '', 'field' => 'BDAY', 'symbol' => '*'],
89-
['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => ""],
90-
['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => ""],
93+
['postfix' => '', 'field' => 'BDAY', 'symbol' => '*', 'utfSymbol' => '🎂'],
94+
['postfix' => '-death', 'field' => 'DEATHDATE', 'symbol' => "", 'utfSymbol' => '⚰️'],
95+
['postfix' => '-anniversary', 'field' => 'ANNIVERSARY', 'symbol' => "", 'utfSymbol' => '💍'],
9196
];
9297
foreach ($targetPrincipals as $principalUri) {
9398
if (!$this->isUserEnabled($principalUri)) {
@@ -132,9 +137,9 @@ public function onCardDeleted($addressBookId, $cardUri) {
132137
* @throws \Sabre\DAV\Exception\BadRequest
133138
*/
134139
public function ensureCalendarExists($principal) {
135-
$book = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
136-
if (!is_null($book)) {
137-
return $book;
140+
$calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
141+
if (!is_null($calendar)) {
142+
return $calendar;
138143
}
139144
$this->calDavBackEnd->createCalendar($principal, self::BIRTHDAY_CALENDAR_URI, [
140145
'{DAV:}displayname' => 'Contact birthdays',
@@ -150,9 +155,10 @@ public function ensureCalendarExists($principal) {
150155
* @param string $dateField
151156
* @param string $postfix
152157
* @param string $summarySymbol
158+
* @param string $utfSummarySymbol
153159
* @return null|VCalendar
154160
*/
155-
public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol) {
161+
public function buildDateFromContact($cardData, $dateField, $postfix, $summarySymbol, $utfSummarySymbol) {
156162
if (empty($cardData)) {
157163
return null;
158164
}
@@ -191,23 +197,47 @@ public function buildDateFromContact($cardData, $dateField, $postfix, $summarySy
191197
}
192198

193199
$unknownYear = false;
200+
$originalYear = null;
194201
if (!$dateParts['year']) {
195-
$birthday = '1900-' . $dateParts['month'] . '-' . $dateParts['date'];
202+
$birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date'];
196203

197204
$unknownYear = true;
205+
} else {
206+
$parameters = $birthday->parameters();
207+
if (isset($parameters['X-APPLE-OMIT-YEAR'])) {
208+
$omitYear = $parameters['X-APPLE-OMIT-YEAR'];
209+
if ($dateParts['year'] === $omitYear) {
210+
$birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date'];
211+
$unknownYear = true;
212+
}
213+
} else {
214+
$originalYear = (int)$dateParts['year'];
215+
216+
if ($originalYear < 1970) {
217+
$birthday = '1970-' . $dateParts['month'] . '-' . $dateParts['date'];
218+
}
219+
}
198220
}
199221

200222
try {
201223
$date = new \DateTime($birthday);
202224
} catch (Exception $e) {
203225
return null;
204226
}
205-
if ($unknownYear) {
206-
$summary = $doc->FN->getValue() . ' ' . $summarySymbol;
227+
if ($this->dbConnection->supports4ByteText()) {
228+
if ($unknownYear) {
229+
$summary = $utfSummarySymbol . ' ' . $doc->FN->getValue();
230+
} else {
231+
$summary = $utfSummarySymbol . ' ' . $doc->FN->getValue() . " ($originalYear)";
232+
}
207233
} else {
208-
$year = (int)$date->format('Y');
209-
$summary = $doc->FN->getValue() . " ($summarySymbol$year)";
234+
if ($unknownYear) {
235+
$summary = $doc->FN->getValue() . ' ' . $summarySymbol;
236+
} else {
237+
$summary = $doc->FN->getValue() . " ($summarySymbol$originalYear)";
238+
}
210239
}
240+
211241
$vCal = new VCalendar();
212242
$vCal->VERSION = '2.0';
213243
$vEvent = $vCal->createComponent('VEVENT');
@@ -226,6 +256,11 @@ public function buildDateFromContact($cardData, $dateField, $postfix, $summarySy
226256
$vEvent->{'RRULE'} = 'FREQ=YEARLY';
227257
$vEvent->{'SUMMARY'} = $summary;
228258
$vEvent->{'TRANSP'} = 'TRANSPARENT';
259+
$vEvent->{'X-NEXTCLOUD-BC-FIELD-TYPE'} = $dateField;
260+
$vEvent->{'X-NEXTCLOUD-BC-UNKNOWN-YEAR'} = $unknownYear ? '1' : '0';
261+
if ($originalYear !== null) {
262+
$vEvent->{'X-NEXTCLOUD-BC-YEAR'} = (string) $originalYear;
263+
}
229264
$alarm = $vCal->createComponent('VALARM');
230265
$alarm->add($vCal->createProperty('TRIGGER', '-PT0M', ['VALUE' => 'DURATION']));
231266
$alarm->add($vCal->createProperty('ACTION', 'DISPLAY'));
@@ -235,6 +270,19 @@ public function buildDateFromContact($cardData, $dateField, $postfix, $summarySy
235270
return $vCal;
236271
}
237272

273+
/**
274+
* @param string $user
275+
*/
276+
public function resetForUser($user) {
277+
$principal = 'principals/users/'.$user;
278+
$calendar = $this->calDavBackEnd->getCalendarByUri($principal, self::BIRTHDAY_CALENDAR_URI);
279+
$calendarObjects = $this->calDavBackEnd->getCalendarObjects($calendar['id'], CalDavBackend::CALENDAR_TYPE_CALENDAR);
280+
281+
foreach($calendarObjects as $calendarObject) {
282+
$this->calDavBackEnd->deleteCalendarObject($calendar['id'], $calendarObject['uri'], CalDavBackend::CALENDAR_TYPE_CALENDAR);
283+
}
284+
}
285+
238286
/**
239287
* @param string $user
240288
*/
@@ -298,7 +346,7 @@ protected function getAllAffectedPrincipals($addressBookId) {
298346
*/
299347
private function updateCalendar($cardUri, $cardData, $book, $calendarId, $type) {
300348
$objectUri = $book['uri'] . '-' . $cardUri . $type['postfix'] . '.ics';
301-
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol']);
349+
$calendarData = $this->buildDateFromContact($cardData, $type['field'], $type['postfix'], $type['symbol'], $type['utfSymbol']);
302350
$existing = $this->calDavBackEnd->getCalendarObject($calendarId, $objectUri);
303351
if (is_null($calendarData)) {
304352
if (!is_null($existing)) {
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* @copyright 2019 Georg Ehrke <[email protected]>
4+
*
5+
* @author Georg Ehrke <[email protected]>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
namespace OCA\DAV\Migration;
24+
25+
use OCA\DAV\BackgroundJob\GenerateBirthdayCalendarBackgroundJob;
26+
use OCP\BackgroundJob\IJobList;
27+
use OCP\IConfig;
28+
use OCP\IUser;
29+
use OCP\IUserManager;
30+
use OCP\Migration\IOutput;
31+
use OCP\Migration\IRepairStep;
32+
33+
class RegenerateBirthdayCalendars implements IRepairStep {
34+
35+
/** @var IUserManager */
36+
private $userManager;
37+
38+
/** @var IJobList */
39+
private $jobList;
40+
41+
/** @var IConfig */
42+
private $config;
43+
44+
/**
45+
* @param IUserManager $userManager,
46+
* @param IJobList $jobList
47+
* @param IConfig $config
48+
*/
49+
public function __construct(IUserManager $userManager,
50+
IJobList $jobList,
51+
IConfig $config) {
52+
$this->userManager = $userManager;
53+
$this->jobList = $jobList;
54+
$this->config = $config;
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
public function getName() {
61+
return 'Regenerating birthday calendars to use new icons and fix old birthday events without year';
62+
}
63+
64+
/**
65+
* @param IOutput $output
66+
*/
67+
public function run(IOutput $output) {
68+
// only run once
69+
if ($this->config->getAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix') === 'yes') {
70+
$output->info('Repair step already executed');
71+
return;
72+
}
73+
74+
$output->info('Adding background jobs to regenerate birthday calendar');
75+
$this->userManager->callForAllUsers(function(IUser $user) {
76+
$this->jobList->add(GenerateBirthdayCalendarBackgroundJob::class, [
77+
'userId' => $user->getUID(),
78+
'purgeBeforeGenerating' => true
79+
]);
80+
});
81+
82+
// if all were done, no need to redo the repair during next upgrade
83+
$this->config->setAppValue('dav', 'regeneratedBirthdayCalendarsForYearFix', 'yes');
84+
}
85+
}

apps/dav/tests/unit/BackgroundJob/GenerateBirthdayCalendarBackgroundJobTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,39 @@ public function testRun() {
6262
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
6363
->will($this->returnValue('yes'));
6464

65+
$this->birthdayService->expects($this->never())
66+
->method('resetForUser')
67+
->with('user123');
68+
6569
$this->birthdayService->expects($this->once())
6670
->method('syncUser')
6771
->with('user123');
6872

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

76+
public function testRunAndReset() {
77+
$this->config->expects($this->once())
78+
->method('getAppValue')
79+
->with('dav', 'generateBirthdayCalendar', 'yes')
80+
->will($this->returnValue('yes'));
81+
82+
$this->config->expects($this->once())
83+
->method('getUserValue')
84+
->with('user123', 'dav', 'generateBirthdayCalendar', 'yes')
85+
->will($this->returnValue('yes'));
86+
87+
$this->birthdayService->expects($this->once())
88+
->method('resetForUser')
89+
->with('user123');
90+
91+
$this->birthdayService->expects($this->once())
92+
->method('syncUser')
93+
->with('user123');
94+
95+
$this->backgroundJob->run(['userId' => 'user123', 'purgeBeforeGenerating' => true]);
96+
}
97+
7298
public function testRunGloballyDisabled() {
7399
$this->config->expects($this->once())
74100
->method('getAppValue')

0 commit comments

Comments
 (0)