-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't send invitation emails for past events
Signed-off-by: Georg Ehrke <[email protected]>
- Loading branch information
1 parent
6be5dc9
commit 86f2866
Showing
4 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @copyright Copyright (c) 2017, Georg Ehrke | ||
* | ||
* @author Thomas Müller <[email protected]> | ||
* @author Georg Ehrke <[email protected]> | ||
* | ||
* @license AGPL-3.0 | ||
* | ||
|
@@ -21,10 +23,15 @@ | |
*/ | ||
namespace OCA\DAV\CalDAV\Schedule; | ||
|
||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\ILogger; | ||
use OCP\Mail\IMailer; | ||
use Sabre\VObject\Component\VCalendar; | ||
use Sabre\VObject\DateTimeParser; | ||
use Sabre\VObject\ITip; | ||
use Sabre\CalDAV\Schedule\IMipPlugin as SabreIMipPlugin; | ||
use Sabre\VObject\Recur\EventIterator; | ||
|
||
/** | ||
* iMIP handler. | ||
* | ||
|
@@ -47,15 +54,23 @@ class IMipPlugin extends SabreIMipPlugin { | |
/** @var ILogger */ | ||
private $logger; | ||
|
||
/** @var ITimeFactory */ | ||
private $timeFactory; | ||
|
||
const MAX_DATE = '2038-01-01'; | ||
|
||
/** | ||
* Creates the email handler. | ||
* | ||
* @param IMailer $mailer | ||
* @param ILogger $logger | ||
* @param ITimeFactory $timeFactory | ||
*/ | ||
function __construct(IMailer $mailer, ILogger $logger) { | ||
function __construct(IMailer $mailer, ILogger $logger, ITimeFactory $timeFactory) { | ||
parent::__construct(''); | ||
$this->mailer = $mailer; | ||
$this->logger = $logger; | ||
$this->timeFactory = $timeFactory; | ||
} | ||
|
||
/** | ||
|
@@ -85,6 +100,11 @@ function schedule(ITip\Message $iTipMessage) { | |
return; | ||
} | ||
|
||
// don't send out mails for events that already took place | ||
if ($this->isEventInThePast($iTipMessage->message)) { | ||
return; | ||
} | ||
|
||
$sender = substr($iTipMessage->sender, 7); | ||
$recipient = substr($iTipMessage->recipient, 7); | ||
|
||
|
@@ -125,4 +145,47 @@ function schedule(ITip\Message $iTipMessage) { | |
} | ||
} | ||
|
||
/** | ||
* check if event took place in the past already | ||
* @param VCalendar $vObject | ||
* @return bool | ||
*/ | ||
private function isEventInThePast(VCalendar $vObject) { | ||
$component = $vObject->VEVENT; | ||
|
||
$firstOccurrence = $component->DTSTART->getDateTime()->getTimeStamp(); | ||
// Finding the last occurrence is a bit harder | ||
if (!isset($component->RRULE)) { | ||
if (isset($component->DTEND)) { | ||
$lastOccurrence = $component->DTEND->getDateTime()->getTimeStamp(); | ||
} elseif (isset($component->DURATION)) { | ||
$endDate = clone $component->DTSTART->getDateTime(); | ||
$endDate->add(DateTimeParser::parse($component->DURATION->getValue())); | ||
$lastOccurrence = $endDate->getTimeStamp(); | ||
} elseif (!$component->DTSTART->hasTime()) { | ||
$endDate = clone $component->DTSTART->getDateTime(); | ||
$endDate->modify('+1 day'); | ||
$lastOccurrence = $endDate->getTimeStamp(); | ||
} else { | ||
$lastOccurrence = $firstOccurrence; | ||
} | ||
} else { | ||
$it = new EventIterator($vObject, (string)$component->UID); | ||
$maxDate = new \DateTime(self::MAX_DATE); | ||
if ($it->isInfinite()) { | ||
$lastOccurrence = $maxDate->getTimestamp(); | ||
} else { | ||
$end = $it->getDtEnd(); | ||
while($it->valid() && $end < $maxDate) { | ||
$end = $it->getDtEnd(); | ||
$it->next(); | ||
|
||
} | ||
$lastOccurrence = $end->getTimestamp(); | ||
} | ||
} | ||
|
||
$currentTime = $this->timeFactory->getTime(); | ||
return $lastOccurrence < $currentTime; | ||
} | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2016, ownCloud, Inc. | ||
* @copyright Copyright (c) 2017, Georg Ehrke | ||
* | ||
* @author Joas Schilling <[email protected]> | ||
* @author Thomas Müller <[email protected]> | ||
* @author Georg Ehrke <[email protected]> | ||
* | ||
* @license AGPL-3.0 | ||
* | ||
|
@@ -25,6 +27,7 @@ | |
|
||
use OC\Mail\Mailer; | ||
use OCA\DAV\CalDAV\Schedule\IMipPlugin; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\ILogger; | ||
use Sabre\VObject\Component\VCalendar; | ||
use Sabre\VObject\ITip\Message; | ||
|
@@ -40,15 +43,18 @@ public function testDelivery() { | |
$mailer->expects($this->once())->method('send'); | ||
/** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */ | ||
$logger = $this->getMockBuilder('OC\Log')->disableOriginalConstructor()->getMock(); | ||
$timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock(); | ||
$timeFactory->method('getTime')->willReturn(1); | ||
|
||
$plugin = new IMipPlugin($mailer, $logger); | ||
$plugin = new IMipPlugin($mailer, $logger, $timeFactory); | ||
$message = new Message(); | ||
$message->method = 'REQUEST'; | ||
$message->message = new VCalendar(); | ||
$message->message->add('VEVENT', [ | ||
'UID' => $message->uid, | ||
'SEQUENCE' => $message->sequence, | ||
'SUMMARY' => 'Fellowship meeting', | ||
'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800 | ||
]); | ||
$message->sender = 'mailto:[email protected]'; | ||
$message->recipient = 'mailto:[email protected]'; | ||
|
@@ -69,15 +75,18 @@ public function testFailedDelivery() { | |
$mailer->method('send')->willThrowException(new \Exception()); | ||
/** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */ | ||
$logger = $this->getMockBuilder('OC\Log')->disableOriginalConstructor()->getMock(); | ||
$timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock(); | ||
$timeFactory->method('getTime')->willReturn(1); | ||
|
||
$plugin = new IMipPlugin($mailer, $logger); | ||
$plugin = new IMipPlugin($mailer, $logger, $timeFactory); | ||
$message = new Message(); | ||
$message->method = 'REQUEST'; | ||
$message->message = new VCalendar(); | ||
$message->message->add('VEVENT', [ | ||
'UID' => $message->uid, | ||
'SEQUENCE' => $message->sequence, | ||
'SUMMARY' => 'Fellowship meeting', | ||
'DTSTART' => new \DateTime('2017-01-01 00:00:00') // 1483228800 | ||
]); | ||
$message->sender = 'mailto:[email protected]'; | ||
$message->recipient = 'mailto:[email protected]'; | ||
|
@@ -90,4 +99,53 @@ public function testFailedDelivery() { | |
$this->assertEquals('text/calendar; charset=UTF-8; method=REQUEST', $mailMessage->getSwiftMessage()->getContentType()); | ||
} | ||
|
||
/** | ||
* @dataProvider dataNoMessageSendForPastEvents | ||
*/ | ||
public function testNoMessageSendForPastEvents($veventParams, $expectsMail) { | ||
$mailMessage = new \OC\Mail\Message(new \Swift_Message()); | ||
/** @var Mailer | \PHPUnit_Framework_MockObject_MockObject $mailer */ | ||
$mailer = $this->getMockBuilder('OC\Mail\Mailer')->disableOriginalConstructor()->getMock(); | ||
$mailer->method('createMessage')->willReturn($mailMessage); | ||
$mailer->expects($this->once())->method('send'); | ||
/** @var ILogger | \PHPUnit_Framework_MockObject_MockObject $logger */ | ||
$logger = $this->getMockBuilder('OC\Log')->disableOriginalConstructor()->getMock(); | ||
$timeFactory = $this->getMockBuilder(ITimeFactory::class)->disableOriginalConstructor()->getMock(); | ||
$timeFactory->method('getTime')->willReturn(1496912528); | ||
|
||
$plugin = new IMipPlugin($mailer, $logger, $timeFactory); | ||
$message = new Message(); | ||
$message->method = 'REQUEST'; | ||
$message->message = new VCalendar(); | ||
$message->message->add('VEVENT', array_merge([ | ||
'UID' => $message->uid, | ||
'SEQUENCE' => $message->sequence, | ||
'SUMMARY' => 'Fellowship meeting', | ||
], $veventParams)); | ||
$message->sender = 'mailto:[email protected]'; | ||
$message->recipient = 'mailto:[email protected]'; | ||
|
||
$plugin->schedule($message); | ||
|
||
if ($expectsMail) { | ||
$this->assertEquals('1.1', $message->getScheduleStatus()); | ||
} else { | ||
$this->assertEquals(false, $message->getScheduleStatus()); | ||
} | ||
} | ||
|
||
public function dataNoMessageSendForPastEvents() { | ||
return [ | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00')], false], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00')], false], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-12-31 00:00:00')], true], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => new \DateInterval('P1D')], false], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DURATION' => new \DateInterval('P1Y')], true], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY'], true], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=3'], false], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20170301T000000Z'], false], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;COUNT=33'], true], | ||
[['DTSTART' => new \DateTime('2017-01-01 00:00:00'), 'DTEND' => new \DateTime('2017-01-01 00:00:00'), 'RRULE' => 'FREQ=WEEKLY;UNTIL=20171001T000000Z'], true], | ||
]; | ||
} | ||
} |