-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Implement Notifications * Notification adjustments * Add documentation * Remove code smells * Update psalm-baseline to ignore IRootFolder dependency
- Loading branch information
Showing
15 changed files
with
995 additions
and
35 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,127 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Robin Windey <[email protected]> | ||
* | ||
* @author Robin Windey <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\WorkflowOcr\Notification; | ||
|
||
use OCA\WorkflowOcr\AppInfo\Application; | ||
use OCP\Files\File; | ||
use OCP\Files\IRootFolder; | ||
use OCP\IURLGenerator; | ||
use OCP\L10N\IFactory; | ||
use OCP\Notification\AlreadyProcessedException; | ||
use OCP\Notification\INotification; | ||
use OCP\Notification\INotifier; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class Notifier implements INotifier { | ||
/** @var IFactory*/ | ||
private $l10nFactory; | ||
/** @var IURLGenerator */ | ||
private $urlGenerator; | ||
/** @var IRootFolder */ | ||
private $rootFolder; | ||
/** @var LoggerInterface */ | ||
private $logger; | ||
|
||
public function __construct(IFactory $factory, | ||
IURLGenerator $urlGenerator, | ||
IRootFolder $rootFolder, | ||
LoggerInterface $logger) { | ||
$this->l10nFactory = $factory; | ||
$this->urlGenerator = $urlGenerator; | ||
$this->rootFolder = $rootFolder; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Identifier of the notifier, only use [a-z0-9_] | ||
* @return string | ||
*/ | ||
public function getID(): string { | ||
return Application::APP_NAME; | ||
} | ||
|
||
/** | ||
* Human readable name describing the notifier | ||
* @return string | ||
*/ | ||
public function getName(): string { | ||
return $this->l10nFactory->get(Application::APP_NAME)->t('Workflow OCR'); | ||
} | ||
|
||
/** | ||
* @param INotification $notification | ||
* @param string $languageCode The code of the language that should be used to prepare the notification | ||
*/ | ||
public function prepare(INotification $notification, string $languageCode): INotification { | ||
if ($notification->getApp() !== Application::APP_NAME) { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
$notification->setIcon($this->urlGenerator->imagePath(Application::APP_NAME, 'app-dark.svg')); | ||
$l = $this->l10nFactory->get(Application::APP_NAME, $languageCode); | ||
|
||
// Currently we only support sending notifications for ocr_error | ||
if ($notification->getSubject() !== 'ocr_error') { | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
$message = $notification->getSubjectParameters()['message']; | ||
$notification | ||
->setParsedSubject($l->t('Workflow OCR error')) | ||
->setParsedMessage($message); | ||
// Only add file info if we have one ... | ||
if ($notification->getObjectType() === 'file' && $notification->getObjectId()) { | ||
$richParams = $this->getRichParamForFile($notification); | ||
$notification->setRichSubject($l->t('Workflow OCR error for file {file}'), $richParams); | ||
} | ||
return $notification; | ||
} | ||
|
||
private function getRichParamForFile(INotification $notification) : array { | ||
try { | ||
$userFolder = $this->rootFolder->getUserFolder($notification->getUser()); | ||
/** @var File[] */ | ||
$files = $userFolder->getById($notification->getObjectId()); | ||
/** @var File $file */ | ||
$file = array_shift($files); | ||
$relativePath = $userFolder->getRelativePath($file->getPath()); | ||
} catch (\Throwable $th) { | ||
$this->logger->error($th->getMessage(), ['exception' => $th]); | ||
throw new AlreadyProcessedException(); | ||
} | ||
|
||
return [ | ||
'file' => [ | ||
'type' => 'file', | ||
'id' => $file->getId(), | ||
'name' => $file->getName(), | ||
'path' => $relativePath, | ||
'link' => $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $notification->getObjectId()]) | ||
] | ||
]; | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Robin Windey <[email protected]> | ||
* | ||
* @author Robin Windey <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\WorkflowOcr\Service; | ||
|
||
interface INotificationService { | ||
|
||
/** | ||
* Create a new notification for the given user if the OCR process of the given file failed. | ||
* @param string $userId The user ID of the user that should receive the notification. | ||
* @param string $message The error message that should be displayed in the notification. | ||
* @param int $fileId Optional file ID of the file that failed to OCR. If given, user can jump to the file via link. | ||
*/ | ||
public function createErrorNotification(?string $userId, string $message, int $fileId = null); | ||
} |
Oops, something went wrong.