Skip to content

Commit

Permalink
Dismiss reminder notifications from passed events
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Jan 13, 2021
1 parent 97743df commit c76aa78
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
7 changes: 6 additions & 1 deletion apps/dav/lib/CalDAV/Reminder/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;

Expand Down Expand Up @@ -177,8 +178,12 @@ private function prepareNotificationSubject(INotification $notification): void {

if ($diff->invert) {
$title = $this->l10n->t('%s (in %s)', [$title, $diffLabel]);
} else {
} elseif ($now->getTimestamp() - $startTime->getTimestamp() < 10 * 60) {
// Allow to show the notifications for 10 minutes after the start of the event
$title = $this->l10n->t('%s (%s ago)', [$title, $diffLabel]);
} else {
// Otherwise we dismiss it
throw new AlreadyProcessedException();
}

$notification->setParsedSubject($title);
Expand Down
62 changes: 55 additions & 7 deletions apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,25 @@
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class NotifierTest extends TestCase {
/** @var Notifier */
protected $notifier;

/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
/** @var IFactory|MockObject */
protected $factory;

/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
/** @var IURLGenerator|MockObject */
protected $urlGenerator;

/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
/** @var IL10N|MockObject */
protected $l10n;

/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
/** @var ITimeFactory|MockObject */
protected $timeFactory;

protected function setUp(): void {
Expand Down Expand Up @@ -111,7 +113,7 @@ public function testPrepareWrongApp(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Notification not from this app');

/** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand All @@ -128,7 +130,7 @@ public function testPrepareWrongSubject() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown subject');

/** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand Down Expand Up @@ -165,6 +167,28 @@ public function dataPrepare(): array {
],
"Calendar: Personal\r\nDate: 2005-08-15T15:52:01+02:00, 2005-08-15T15:52:01+02:00 - 2005-08-15T17:52:01+02:00 (Europe/Berlin)\r\nWhere: NC Headquarters"
],
[
'calendar_reminder',
[
'title' => 'Title of this event',
'start_atom' => '2005-08-15T13:51:00+02:00',
],
'Title of this event (9 minutes ago)',
[
'title' => 'Title of this event',
'description' => null,
'location' => 'NC Headquarters',
'all_day' => false,
'start_atom' => '2005-08-15T13:51:00+02:00',
'start_is_floating' => false,
'start_timezone' => 'Europe/Berlin',
'end_atom' => '2005-08-15T17:52:01+02:00',
'end_is_floating' => false,
'end_timezone' => 'Europe/Berlin',
'calendar_displayname' => 'Personal',
],
"Calendar: Personal\r\nDate: 2005-08-15T13:51:00+02:00, 2005-08-15T13:51:00+02:00 - 2005-08-15T17:52:01+02:00 (Europe/Berlin)\r\nWhere: NC Headquarters"
],
];
}

Expand All @@ -179,7 +203,7 @@ public function dataPrepare(): array {
* @throws \Exception
*/
public function testPrepare(string $subjectType, array $subjectParams, string $subject, array $messageParams, string $message): void {
/** @var INotification|\PHPUnit\Framework\MockObject\MockObject $notification */
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
Expand Down Expand Up @@ -222,4 +246,28 @@ public function testPrepare(string $subjectType, array $subjectParams, string $s

$this->assertEquals($notification, $return);
}

public function testPassedEvent(): void {
/** @var INotification|MockObject $notification */
$notification = $this->createMock(INotification::class);

$notification->expects($this->once())
->method('getApp')
->willReturn(Application::APP_ID);
$notification->expects($this->once())
->method('getSubject')
->willReturn('calendar_reminder');
$notification->expects($this->once())
->method('getSubjectParameters')
->willReturn([
'title' => 'Title of this event',
'start_atom' => '2005-08-15T13:30:00+02:00',
]);

$this->expectException(AlreadyProcessedException::class);

$return = $this->notifier->prepare($notification, 'en');

$this->assertEquals($notification, $return);
}
}

0 comments on commit c76aa78

Please sign in to comment.