Skip to content

Commit 536e157

Browse files
committed
Dismiss reminder notifications from passed events
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent 97743df commit 536e157

File tree

2 files changed

+83
-7
lines changed

2 files changed

+83
-7
lines changed

apps/dav/lib/CalDAV/Reminder/Notifier.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use OCP\IL10N;
3737
use OCP\IURLGenerator;
3838
use OCP\L10N\IFactory;
39+
use OCP\Notification\AlreadyProcessedException;
3940
use OCP\Notification\INotification;
4041
use OCP\Notification\INotifier;
4142

@@ -223,6 +224,12 @@ private function getTitleFromParameters(array $parameters):string {
223224
private function generateDateString(array $parameters):string {
224225
$startDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['start_atom']);
225226
$endDateTime = DateTime::createFromFormat(\DateTime::ATOM, $parameters['end_atom']);
227+
228+
// If the event has already ended, dismiss the notification
229+
if ($endDateTime < $this->timeFactory->getDateTime()) {
230+
throw new AlreadyProcessedException();
231+
}
232+
226233
$isAllDay = $parameters['all_day'];
227234
$diff = $startDateTime->diff($endDateTime);
228235

apps/dav/tests/unit/CalDAV/Reminder/NotifierTest.php

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,25 @@
3636
use OCP\IL10N;
3737
use OCP\IURLGenerator;
3838
use OCP\L10N\IFactory;
39+
use OCP\Notification\AlreadyProcessedException;
3940
use OCP\Notification\INotification;
41+
use PHPUnit\Framework\MockObject\MockObject;
4042
use Test\TestCase;
4143

4244
class NotifierTest extends TestCase {
4345
/** @var Notifier */
4446
protected $notifier;
4547

46-
/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
48+
/** @var IFactory|MockObject */
4749
protected $factory;
4850

49-
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
51+
/** @var IURLGenerator|MockObject */
5052
protected $urlGenerator;
5153

52-
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
54+
/** @var IL10N|MockObject */
5355
protected $l10n;
5456

55-
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
57+
/** @var ITimeFactory|MockObject */
5658
protected $timeFactory;
5759

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

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

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

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

134136
$notification->expects($this->once())
@@ -165,6 +167,28 @@ public function dataPrepare(): array {
165167
],
166168
"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"
167169
],
170+
[
171+
'calendar_reminder',
172+
[
173+
'title' => 'Title of this event',
174+
'start_atom' => '2005-08-15T13:00:00+02:00',
175+
],
176+
'Title of this event (1 hour ago)',
177+
[
178+
'title' => 'Title of this event',
179+
'description' => null,
180+
'location' => 'NC Headquarters',
181+
'all_day' => false,
182+
'start_atom' => '2005-08-15T13:00:00+02:00',
183+
'start_is_floating' => false,
184+
'start_timezone' => 'Europe/Berlin',
185+
'end_atom' => '2005-08-15T15:00:00+02:00',
186+
'end_is_floating' => false,
187+
'end_timezone' => 'Europe/Berlin',
188+
'calendar_displayname' => 'Personal',
189+
],
190+
"Calendar: Personal\r\nDate: 2005-08-15T13:00:00+02:00, 2005-08-15T13:00:00+02:00 - 2005-08-15T15:00:00+02:00 (Europe/Berlin)\r\nWhere: NC Headquarters"
191+
],
168192
];
169193
}
170194

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

185209
$notification->expects($this->once())
@@ -222,4 +246,49 @@ public function testPrepare(string $subjectType, array $subjectParams, string $s
222246

223247
$this->assertEquals($notification, $return);
224248
}
249+
250+
public function testPassedEvent(): void {
251+
/** @var INotification|MockObject $notification */
252+
$notification = $this->createMock(INotification::class);
253+
254+
$notification->expects($this->once())
255+
->method('getApp')
256+
->willReturn(Application::APP_ID);
257+
$notification->expects($this->once())
258+
->method('getSubject')
259+
->willReturn('calendar_reminder');
260+
$notification->expects($this->once())
261+
->method('getSubjectParameters')
262+
->willReturn([
263+
'title' => 'Title of this event',
264+
'start_atom' => '2005-08-15T08:00:00+02:00'
265+
]);
266+
267+
$notification->expects($this->once())
268+
->method('getMessageParameters')
269+
->willReturn([
270+
'title' => 'Title of this event',
271+
'description' => null,
272+
'location' => 'NC Headquarters',
273+
'all_day' => false,
274+
'start_atom' => '2005-08-15T08:00:00+02:00',
275+
'start_is_floating' => false,
276+
'start_timezone' => 'Europe/Berlin',
277+
'end_atom' => '2005-08-15T13:00:00+02:00',
278+
'end_is_floating' => false,
279+
'end_timezone' => 'Europe/Berlin',
280+
'calendar_displayname' => 'Personal',
281+
]);
282+
283+
$notification->expects($this->once())
284+
->method('setParsedSubject')
285+
->with('Title of this event (6 hours ago)')
286+
->willReturnSelf();
287+
288+
$this->expectException(AlreadyProcessedException::class);
289+
290+
$return = $this->notifier->prepare($notification, 'en');
291+
292+
$this->assertEquals($notification, $return);
293+
}
225294
}

0 commit comments

Comments
 (0)