-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4079 from nextcloud/backport/3960/stable27
[stable27] fix: Emit notification on mentions
- Loading branch information
Showing
8 changed files
with
212 additions
and
18 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
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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Richdocuments\Controller; | ||
|
||
use OCA\Richdocuments\AppInfo\Application; | ||
use OCA\Richdocuments\Notification\Notifier; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\Attribute\UserRateLimit; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\Files\IRootFolder; | ||
use OCP\IRequest; | ||
use OCP\Notification\IManager; | ||
|
||
class MentionController extends Controller { | ||
public function __construct( | ||
$appName, | ||
IRequest $request, | ||
private IRootFolder $rootFolder, | ||
private IManager $manager, | ||
private ITimeFactory $timeFactory, | ||
private ?string $userId, | ||
) { | ||
parent::__construct($appName, $request); | ||
} | ||
|
||
#[NoAdminRequired] | ||
#[UserRateLimit(limit: 5, period: 120)] | ||
public function mention(int $fileId, string $mention): DataResponse { | ||
$userFolder = $this->rootFolder->getUserFolder($this->userId); | ||
$file = $userFolder->getById($fileId)[0]; | ||
if ($file === null) { | ||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$userFolder = $this->rootFolder->getUserFolder($mention); | ||
$file = $userFolder->getById($fileId)[0]; | ||
if ($file === null) { | ||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
|
||
$notification = $this->manager->createNotification(); | ||
$notification->setUser($mention) | ||
->setApp(Application::APPNAME) | ||
->setSubject(Notifier::TYPE_MENTIONED, [ | ||
Notifier::SUBJECT_MENTIONED_SOURCE_USER => $this->userId, | ||
Notifier::SUBJECT_MENTIONED_TARGET_USER => $mention, | ||
]) | ||
->setObject('file', (string)$fileId); | ||
|
||
if ($this->manager->getCount($notification) === 0) { | ||
$notification->setDateTime(\DateTime::createFromImmutable($this->timeFactory->now())); | ||
$this->manager->notify($notification); | ||
return new DataResponse([], Http::STATUS_OK); | ||
|
||
} | ||
|
||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OCA\Richdocuments\Notification; | ||
|
||
use InvalidArgumentException; | ||
use OC\User\NoUserException; | ||
use OCA\Richdocuments\AppInfo\Application; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\NotPermittedException; | ||
use OCP\IURLGenerator; | ||
use OCP\IUserManager; | ||
use OCP\L10N\IFactory; | ||
use OCP\Notification\INotification; | ||
use OCP\Notification\INotifier; | ||
|
||
class Notifier implements INotifier { | ||
public const TYPE_MENTIONED = 'mentioned'; | ||
public const SUBJECT_MENTIONED_SOURCE_USER = 'sourceUser'; | ||
public const SUBJECT_MENTIONED_TARGET_USER = 'targetUser'; | ||
|
||
public function __construct( | ||
private IFactory $factory, | ||
private IUserManager $userManager, | ||
private IURLGenerator $urlGenerator, | ||
private IRootFolder $rootFolder, | ||
) { | ||
} | ||
|
||
public function getID(): string { | ||
return 'richdocuments'; | ||
} | ||
|
||
public function getName(): string { | ||
return 'Office'; | ||
} | ||
|
||
public function prepare(INotification $notification, string $languageCode): INotification { | ||
if ($notification->getApp() !== Application::APPNAME) { | ||
throw new InvalidArgumentException('Application should be text instead of ' . $notification->getApp()); | ||
} | ||
|
||
$l = $this->factory->get(Application::APPNAME, $languageCode); | ||
|
||
switch ($notification->getSubject()) { | ||
case self::TYPE_MENTIONED: | ||
$parameters = $notification->getSubjectParameters(); | ||
$sourceUser = $parameters[self::SUBJECT_MENTIONED_SOURCE_USER]; | ||
$sourceUserDisplayName = $this->userManager->getDisplayName($sourceUser); | ||
$targetUser = $notification->getUser(); | ||
$fileId = (int)$notification->getObjectId(); | ||
|
||
if ($sourceUserDisplayName === null) { | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
try { | ||
$userFolder = $this->rootFolder->getUserFolder($targetUser); | ||
} catch (NotPermittedException|NoUserException $e) { | ||
throw new InvalidArgumentException(); | ||
} | ||
$node = $userFolder->getById($fileId)[0]; | ||
|
||
if ($node === null) { | ||
throw new InvalidArgumentException(); | ||
} | ||
|
||
$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $node->getId()]); | ||
|
||
$notification->setRichSubject($l->t('{user} has mentioned you in {node}'), [ | ||
'user' => [ | ||
'type' => 'user', | ||
'id' => $sourceUser, | ||
'name' => $sourceUserDisplayName, | ||
], | ||
'node' => [ | ||
'type' => 'file', | ||
'id' => (string)$node->getId(), | ||
'name' => $node->getName(), | ||
'path' => $userFolder->getRelativePath($node->getPath()) ?? '', | ||
'link' => $fileLink, | ||
], | ||
]); | ||
break; | ||
default: | ||
throw new InvalidArgumentException(); | ||
} | ||
$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('text', 'app-dark.svg'))); | ||
$notification->setLink($fileLink); | ||
$this->setParsedSubjectFromRichSubject($notification); | ||
return $notification; | ||
} | ||
|
||
protected function setParsedSubjectFromRichSubject(INotification $notification): void { | ||
$placeholders = $replacements = []; | ||
foreach ($notification->getRichSubjectParameters() as $placeholder => $parameter) { | ||
$placeholders[] = '{' . $placeholder . '}'; | ||
if ($parameter['type'] === 'file') { | ||
$replacements[] = $parameter['path']; | ||
} else { | ||
$replacements[] = $parameter['name']; | ||
} | ||
} | ||
|
||
$notification->setParsedSubject(str_replace($placeholders, $replacements, $notification->getRichSubject())); | ||
} | ||
} |
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