Skip to content

Commit

Permalink
Merge pull request #34909 from nextcloud/fix/21370/duplicate-notifica…
Browse files Browse the repository at this point in the history
…tions-2

Fix duplicate event email notifications
  • Loading branch information
PVince81 authored Nov 4, 2022
2 parents b9bbb25 + 304b716 commit 7ec9226
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 18 deletions.
3 changes: 3 additions & 0 deletions apps/dav/lib/CalDAV/Reminder/INotificationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -42,10 +43,12 @@ interface INotificationProvider {
*
* @param VEvent $vevent
* @param string $calendarDisplayName
* @param string[] $principalEmailAddresses All email addresses associated to the principal owning the calendar object
* @param IUser[] $users
* @return void
*/
public function send(VEvent $vevent,
string $calendarDisplayName,
array $principalEmailAddresses,
array $users = []): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -82,11 +83,13 @@ public function __construct(LoggerInterface $logger,
*
* @param VEvent $vevent
* @param string $calendarDisplayName
* @param string[] $principalEmailAddresses
* @param IUser[] $users
* @return void
*/
abstract public function send(VEvent $vevent,
string $calendarDisplayName,
array $principalEmailAddresses,
array $users = []): void;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,28 @@ public function __construct(IConfig $config,
*
* @param VEvent $vevent
* @param string $calendarDisplayName
* @param string[] $principalEmailAddresses
* @param array $users
* @throws \Exception
*/
public function send(VEvent $vevent,
string $calendarDisplayName,
array $principalEmailAddresses,
array $users = []):void {
$fallbackLanguage = $this->getFallbackLanguage();

$organizerEmailAddress = null;
if (isset($vevent->ORGANIZER)) {
$organizerEmailAddress = $this->getEMailAddressOfAttendee($vevent->ORGANIZER);
}

$emailAddressesOfSharees = $this->getEMailAddressesOfAllUsersWithWriteAccessToCalendar($users);
$emailAddressesOfAttendees = $this->getAllEMailAddressesFromEvent($vevent);
$emailAddressesOfAttendees = [];
if (count($principalEmailAddresses) === 0
|| ($organizerEmailAddress && in_array($organizerEmailAddress, $principalEmailAddresses, true))
) {
$emailAddressesOfAttendees = $this->getAllEMailAddressesFromEvent($vevent);
}

// Quote from php.net:
// If the input arrays have the same string keys, then the later value for that key will overwrite the previous one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Citharel <nextcloud@tcit.fr>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -73,11 +74,13 @@ public function __construct(IConfig $config,
*
* @param VEvent $vevent
* @param string $calendarDisplayName
* @param string[] $principalEmailAddresses
* @param IUser[] $users
* @throws \Exception
*/
public function send(VEvent $vevent,
string $calendarDisplayName = null,
string $calendarDisplayName,
array $principalEmailAddresses,
array $users = []):void {
if ($this->config->getAppValue('dav', 'sendEventRemindersPush', 'no') !== 'yes') {
return;
Expand Down
17 changes: 15 additions & 2 deletions apps/dav/lib/CalDAV/Reminder/ReminderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Citharel <nextcloud@tcit.fr>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand All @@ -32,6 +33,7 @@

use DateTimeImmutable;
use OCA\DAV\CalDAV\CalDavBackend;
use OCA\DAV\Connector\Sabre\Principal;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\IGroup;
Expand Down Expand Up @@ -76,6 +78,9 @@ class ReminderService {
/** @var LoggerInterface */
private $logger;

/** @var Principal */
private $principalConnector;

public const REMINDER_TYPE_EMAIL = 'EMAIL';
public const REMINDER_TYPE_DISPLAY = 'DISPLAY';
public const REMINDER_TYPE_AUDIO = 'AUDIO';
Expand All @@ -98,7 +103,8 @@ public function __construct(Backend $backend,
CalDavBackend $caldavBackend,
ITimeFactory $timeFactory,
IConfig $config,
LoggerInterface $logger) {
LoggerInterface $logger,
Principal $principalConnector) {
$this->backend = $backend;
$this->notificationProviderManager = $notificationProviderManager;
$this->userManager = $userManager;
Expand All @@ -107,6 +113,7 @@ public function __construct(Backend $backend,
$this->timeFactory = $timeFactory;
$this->config = $config;
$this->logger = $logger;
$this->principalConnector = $principalConnector;
}

/**
Expand Down Expand Up @@ -175,12 +182,18 @@ public function processReminders() :void {
$users[] = $user;
}

$userPrincipalEmailAddresses = [];
$userPrincipal = $this->principalConnector->getPrincipalByPath($reminder['principaluri']);
if ($userPrincipal) {
$userPrincipalEmailAddresses = $this->principalConnector->getEmailAddressesOfPrincipal($userPrincipal);
}

$this->logger->debug('Reminder {id} will be sent to {numUsers} users', [
'id' => $reminder['id'],
'numUsers' => count($users),
]);
$notificationProvider = $this->notificationProviderManager->getProvider($reminder['type']);
$notificationProvider->send($vevent, $reminder['displayname'], $users);
$notificationProvider->send($vevent, $reminder['displayname'], $userPrincipalEmailAddresses, $users);

$this->deleteOrProcessNext($reminder, $vevent);
}
Expand Down
20 changes: 11 additions & 9 deletions apps/dav/lib/CalDAV/Schedule/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @author Joas Schilling <coding@schilljs.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Citharel <nextcloud@tcit.fr>
* @author Richard Steinmetz <richard@steinmetz.cloud>
*
* @license GNU AGPL version 3 or any later version
*
Expand Down Expand Up @@ -46,6 +47,7 @@
use Sabre\VObject\Component\VCalendar;
use Sabre\VObject\Component\VEvent;
use Sabre\VObject\DateTimeParser;
use Sabre\VObject\Document;
use Sabre\VObject\FreeBusyGenerator;
use Sabre\VObject\ITip;
use Sabre\VObject\Parameter;
Expand Down Expand Up @@ -164,6 +166,14 @@ public function calendarObjectChange(RequestInterface $request, ResponseInterfac
* @inheritDoc
*/
public function scheduleLocalDelivery(ITip\Message $iTipMessage):void {
/** @var Component|null $vevent */
$vevent = $iTipMessage->message->VEVENT ?? null;

// Strip VALARMs from incoming VEVENT
if ($vevent && isset($vevent->VALARM)) {
$vevent->remove('VALARM');
}

parent::scheduleLocalDelivery($iTipMessage);

// We only care when the message was successfully delivered locally
Expand Down Expand Up @@ -200,18 +210,10 @@ public function scheduleLocalDelivery(ITip\Message $iTipMessage):void {
return;
}

if (!isset($iTipMessage->message)) {
if (!$vevent) {
return;
}

$vcalendar = $iTipMessage->message;
if (!isset($vcalendar->VEVENT)) {
return;
}

/** @var Component $vevent */
$vevent = $vcalendar->VEVENT;

// We don't support autoresponses for recurrencing events for now
if (isset($vevent->RRULE) || isset($vevent->RDATE)) {
return;
Expand Down
40 changes: 40 additions & 0 deletions apps/dav/lib/Connector/Sabre/Principal.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,44 @@ public function getCircleMembership($principal):array {

return [];
}

/**
* Get all email addresses associated to a principal.
*
* @param array $principal Data from getPrincipal*()
* @return string[] All email addresses without the mailto: prefix
*/
public function getEmailAddressesOfPrincipal(array $principal): array {
$emailAddresses = [];

if (($primaryAddress = $principal['{http://sabredav.org/ns}email-address'])) {
$emailAddresses[] = $primaryAddress;
}

if (isset($principal['{DAV:}alternate-URI-set'])) {
foreach ($principal['{DAV:}alternate-URI-set'] as $address) {
if (str_starts_with($address, 'mailto:')) {
$emailAddresses[] = substr($address, 7);
}
}
}

if (isset($principal['{urn:ietf:params:xml:ns:caldav}calendar-user-address-set'])) {
foreach ($principal['{urn:ietf:params:xml:ns:caldav}calendar-user-address-set'] as $address) {
if (str_starts_with($address, 'mailto:')) {
$emailAddresses[] = substr($address, 7);
}
}
}

if (isset($principal['{http://calendarserver.org/ns/}email-address-set'])) {
foreach ($principal['{http://calendarserver.org/ns/}email-address-set'] as $address) {
if (str_starts_with($address, 'mailto:')) {
$emailAddresses[] = substr($address, 7);
}
}
}

return array_values(array_unique($emailAddresses));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function setUp(): void {

public function testSendWithoutAttendees():void {
[$user1, $user2, $user3, , $user5] = $users = $this->getUsers();
$principalEmailAddresses = [$user1->getEmailAddress()];

$enL10N = $this->createMock(IL10N::class);
$enL10N->method('t')
Expand Down Expand Up @@ -170,11 +171,12 @@ public function testSendWithoutAttendees():void {
$this->setupURLGeneratorMock(2);

$vcalendar = $this->getNoAttendeeVCalendar();
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $principalEmailAddresses, $users);
}

public function testSendWithAttendees(): void {
public function testSendWithAttendeesWhenOwnerIsOrganizer(): void {
[$user1, $user2, $user3, , $user5] = $users = $this->getUsers();
$principalEmailAddresses = [$user1->getEmailAddress()];

$enL10N = $this->createMock(IL10N::class);
$enL10N->method('t')
Expand Down Expand Up @@ -266,7 +268,81 @@ public function testSendWithAttendees(): void {
$this->setupURLGeneratorMock(2);

$vcalendar = $this->getAttendeeVCalendar();
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $users);
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $principalEmailAddresses, $users);
}

public function testSendWithAttendeesWhenOwnerIsAttendee(): void {
[$user1, $user2, $user3] = $this->getUsers();
$users = [$user2, $user3];
$principalEmailAddresses = [$user2->getEmailAddress()];

$deL10N = $this->createMock(IL10N::class);
$deL10N->method('t')
->willReturnArgument(0);
$deL10N->method('l')
->willReturnArgument(0);

$this->l10nFactory
->method('getUserLanguage')
->willReturnMap([
[$user2, 'de'],
[$user3, 'de'],
]);

$this->l10nFactory
->method('findGenericLanguage')
->willReturn('en');

$this->l10nFactory
->method('languageExists')
->willReturnMap([
['dav', 'de', true],
]);

$this->l10nFactory
->method('get')
->willReturnMap([
['dav', 'de', null, $deL10N],
]);

$template1 = $this->getTemplateMock();
$message12 = $this->getMessageMock('uid2@example.com', $template1);
$message13 = $this->getMessageMock('uid3@example.com', $template1);

$this->mailer->expects(self::once())
->method('createEMailTemplate')
->with('dav.calendarReminder')
->willReturnOnConsecutiveCalls(
$template1,
);
$this->mailer->expects($this->atLeastOnce())
->method('validateMailAddress')
->willReturnMap([
['foo1@example.org', true],
['foo3@example.org', true],
['foo4@example.org', true],
['uid1@example.com', true],
['uid2@example.com', true],
['uid3@example.com', true],
['invalid', false],
]);
$this->mailer->expects($this->exactly(2))
->method('createMessage')
->with()
->willReturnOnConsecutiveCalls(
$message12,
$message13,
);
$this->mailer->expects($this->exactly(2))
->method('send')
->withConsecutive(
[$message12],
[$message13],
)->willReturn([]);
$this->setupURLGeneratorMock(1);

$vcalendar = $this->getAttendeeVCalendar();
$this->provider->send($vcalendar->VEVENT, $this->calendarDisplayName, $principalEmailAddresses, $users);
}

/**
Expand Down Expand Up @@ -376,6 +452,14 @@ private function getAttendeeVCalendar():VCalendar {
'DESCRIPTION' => 'DESCRIPTION 456',
]);

$vcalendar->VEVENT->add(
'ORGANIZER',
'mailto:uid1@example.com',
[
'LANG' => 'en'
]
);

$vcalendar->VEVENT->add(
'ATTENDEE',
'mailto:foo1@example.org',
Expand Down
Loading

0 comments on commit 7ec9226

Please sign in to comment.