Skip to content

Commit 04283a2

Browse files
committed
feat: Implement event so collectives can inject custom notification messages
Signed-off-by: Julius Knorr <jus@bitgrid.net>
1 parent 3f4c598 commit 04283a2

4 files changed

Lines changed: 51 additions & 15 deletions

File tree

composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
'OCA\\Text\\DirectEditing\\TextDirectEditor' => $baseDir . '/../lib/DirectEditing/TextDirectEditor.php',
3030
'OCA\\Text\\DirectEditing\\TextDocumentCreator' => $baseDir . '/../lib/DirectEditing/TextDocumentCreator.php',
3131
'OCA\\Text\\Event\\LoadEditor' => $baseDir . '/../lib/Event/LoadEditor.php',
32+
'OCA\\Text\\Event\\MentionEvent' => $baseDir . '/../lib/Event/MentionEvent.php',
3233
'OCA\\Text\\Exception\\DocumentHasUnsavedChangesException' => $baseDir . '/../lib/Exception/DocumentHasUnsavedChangesException.php',
3334
'OCA\\Text\\Exception\\DocumentSaveConflictException' => $baseDir . '/../lib/Exception/DocumentSaveConflictException.php',
3435
'OCA\\Text\\Exception\\InvalidDocumentBaseVersionEtagException' => $baseDir . '/../lib/Exception/InvalidDocumentBaseVersionEtagException.php',

composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class ComposerStaticInitText
4444
'OCA\\Text\\DirectEditing\\TextDirectEditor' => __DIR__ . '/..' . '/../lib/DirectEditing/TextDirectEditor.php',
4545
'OCA\\Text\\DirectEditing\\TextDocumentCreator' => __DIR__ . '/..' . '/../lib/DirectEditing/TextDocumentCreator.php',
4646
'OCA\\Text\\Event\\LoadEditor' => __DIR__ . '/..' . '/../lib/Event/LoadEditor.php',
47+
'OCA\\Text\\Event\\MentionEvent' => __DIR__ . '/..' . '/../lib/Event/MentionEvent.php',
4748
'OCA\\Text\\Exception\\DocumentHasUnsavedChangesException' => __DIR__ . '/..' . '/../lib/Exception/DocumentHasUnsavedChangesException.php',
4849
'OCA\\Text\\Exception\\DocumentSaveConflictException' => __DIR__ . '/..' . '/../lib/Exception/DocumentSaveConflictException.php',
4950
'OCA\\Text\\Exception\\InvalidDocumentBaseVersionEtagException' => __DIR__ . '/..' . '/../lib/Exception/InvalidDocumentBaseVersionEtagException.php',

lib/Event/MentionEvent.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
11+
namespace OCA\Text\Event;
12+
13+
use OCP\EventDispatcher\Event;
14+
use OCP\Files\File;
15+
use OCP\Notification\INotification;
16+
17+
class MentionEvent extends Event {
18+
public function __construct(
19+
private INotification $notification,
20+
private File $file,
21+
) {
22+
parent::__construct();
23+
}
24+
25+
public function getNotification(): INotification {
26+
return $this->notification;
27+
}
28+
29+
public function getFile(): File {
30+
return $this->file;
31+
}
32+
}

lib/Notification/Notifier.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
namespace OCA\Text\Notification;
1010

1111
use OC\User\NoUserException;
12+
use OCA\Text\Event\MentionEvent;
13+
use OCP\EventDispatcher\IEventDispatcher;
14+
use OCP\Files\File;
1215
use OCP\Files\IRootFolder;
1316
use OCP\Files\NotPermittedException;
1417
use OCP\IURLGenerator;
@@ -24,16 +27,13 @@ class Notifier implements INotifier {
2427
public const SUBJECT_MENTIONED_SOURCE_USER = 'sourceUser';
2528
public const SUBJECT_MENTIONED_TARGET_USER = 'targetUser';
2629

27-
private IFactory $factory;
28-
private IURLGenerator $url;
29-
private IUserManager $userManager;
30-
private IRootFolder $rootFolder;
31-
32-
public function __construct(IFactory $factory, IUserManager $userManager, IURLGenerator $urlGenerator, IRootFolder $rootFolder) {
33-
$this->factory = $factory;
34-
$this->userManager = $userManager;
35-
$this->url = $urlGenerator;
36-
$this->rootFolder = $rootFolder;
30+
public function __construct(
31+
private IFactory $factory,
32+
private IUserManager $userManager,
33+
private IURLGenerator $urlGenerator,
34+
private IRootFolder $rootFolder,
35+
private IEventDispatcher $eventDispatcher,
36+
) {
3737
}
3838

3939
public function getID(): string {
@@ -50,7 +50,7 @@ public function prepare(INotification $notification, string $languageCode): INot
5050
}
5151

5252
$l = $this->factory->get('text', $languageCode);
53-
53+
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('text', 'app-dark.svg')));
5454
switch ($notification->getSubject()) {
5555
case self::TYPE_MENTIONED:
5656
$parameters = $notification->getSubjectParameters();
@@ -70,12 +70,13 @@ public function prepare(INotification $notification, string $languageCode): INot
7070
}
7171
$node = $userFolder->getFirstNodeById($fileId);
7272

73-
if ($node === null) {
73+
if (!$node instanceof File) {
7474
throw new AlreadyProcessedException();
7575
}
7676

77-
$fileLink = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $node->getId()]);
77+
$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $node->getId()]);
7878

79+
$notification->setLink($fileLink);
7980
$notification->setRichSubject($l->t('{user} has mentioned you in the text document {node}'), [
8081
'user' => [
8182
'type' => 'user',
@@ -90,12 +91,13 @@ public function prepare(INotification $notification, string $languageCode): INot
9091
'link' => $fileLink,
9192
],
9293
]);
94+
95+
$this->eventDispatcher->dispatchTyped(new MentionEvent($notification, $node));
9396
break;
9497
default:
9598
throw new UnknownNotificationException();
9699
}
97-
$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('text', 'app-dark.svg')));
98-
$notification->setLink($fileLink);
100+
99101
$this->setParsedSubjectFromRichSubject($notification);
100102
return $notification;
101103
}

0 commit comments

Comments
 (0)