-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setup codebase for Notifications within the plugin. #71
Open
retrevor
wants to merge
1
commit into
main
Choose a base branch
from
feature/notifications
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+242
−35
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2024, MetaProvide Holding EKF | ||
* | ||
* @author Ron Trevor <ecoron@proton.me> | ||
* | ||
* @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\Files_External_Ethswarm\AppInfo; | ||
|
||
class AppConstants { | ||
// Application-wide constants go here | ||
public const APP_NAME = 'files_external_ethswarm'; | ||
} |
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 |
---|---|---|
|
@@ -23,15 +23,14 @@ | |
|
||
namespace OCA\Files_External_Ethswarm\Backend; | ||
|
||
|
||
use OCA\Files_External_Ethswarm\AppInfo\AppConstants; | ||
use OCA\Files_External\Lib\Backend\Backend; | ||
use OCA\Files_External\Lib\DefinitionParameter; | ||
use OCA\Files_External\Lib\StorageConfig; | ||
use OCA\Files_External\Service\GlobalStoragesService; | ||
use OCA\Files_External_Ethswarm\Auth\License; | ||
use OCP\IConfig; | ||
use OCP\IL10N; | ||
use OCP\IUser; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class BeeSwarm extends Backend | ||
|
@@ -69,10 +68,10 @@ public function __construct(string $appName, IL10N $l, IConfig $config, LoggerIn | |
$this->logger = $logger; | ||
$this->globalStoragesService = $globalStoragesService; | ||
$this | ||
->setIdentifier('files_external_ethswarm') | ||
->setIdentifier(AppConstants::APP_NAME) | ||
->addIdentifierAlias('\OC\Files\External_Storage\BeeSwarm') // legacy compat | ||
->setStorageClass('\OCA\Files_External_Ethswarm\Storage\BeeSwarm') | ||
->setText($l->t('Swarm By Hejbit')) | ||
->setText($l->t('Swarm')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think @JoaoSRaposo wanted to have the HejBit in name in the settings page. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes :) |
||
->addParameters([ | ||
(new DefinitionParameter(self::OPTION_HOST_URL, $l->t('Server URL'))) | ||
->setTooltip($l->t("License Server URL")), | ||
|
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,117 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2024, MetaProvide Holding EKF | ||
* | ||
* @author Ron Trevor <ecoron@proton.me> | ||
* | ||
* @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\Files_External_Ethswarm\Notification; | ||
|
||
use OCA\Files_External_Ethswarm\AppInfo\AppConstants; | ||
|
||
class Notifier implements \OCP\Notification\INotifier { | ||
protected $factory; | ||
protected $url; | ||
|
||
public function __construct(\OCP\L10N\IFactory $factory, | ||
\OCP\IURLGenerator $urlGenerator) { | ||
$this->factory = $factory; | ||
$this->url = $urlGenerator; | ||
} | ||
|
||
/** | ||
* Identifier of the notifier, only use [a-z0-9_] | ||
* @return string | ||
*/ | ||
public function getID(): string { | ||
return AppConstants::APP_NAME; | ||
} | ||
|
||
/** | ||
* Human-readable name describing the notifier | ||
* @return string | ||
*/ | ||
public function getName(): string { | ||
return $this->factory->get(AppConstants::APP_NAME)->t('Hejbit External Storage'); | ||
} | ||
|
||
/** | ||
* @param \OCP\Notification\INotification $notification | ||
* @param string $languageCode The code of the language that should be used to prepare the notification | ||
*/ | ||
public function prepare(\OCP\Notification\INotification $notification, string $languageCode): \OCP\Notification\INotification { | ||
if ($notification->getApp() !== AppConstants::APP_NAME) { | ||
// Not my app => throw | ||
throw new \InvalidArgumentException(); | ||
} | ||
|
||
// Read the language from the notification | ||
$l = $this->factory->get(AppConstants::APP_NAME, $languageCode); | ||
|
||
// Deal with known subjects | ||
switch ($notification->getSubject()) { | ||
// Set the parsed subject, message and action labels | ||
case 'swarm-fileupload': | ||
// Set rich subject, see https://github.com/nextcloud/server/issues/1706 for more information | ||
// and https://github.com/nextcloud/server/blob/master/lib/public/RichObjectStrings/Definitions.php | ||
// for a list of defined objects and their parameters. | ||
$parameters = $notification->getSubjectParameters(); | ||
$notification->setRichSubject($l->t('Your file \'{filename}\' was decentralized.'), | ||
[ | ||
'filename' => [ | ||
'type' => 'file', | ||
'id' => '', | ||
'name' => basename($parameters['path']), | ||
'path' => $parameters['path'], | ||
], | ||
]); | ||
|
||
// Set the plain text subject automatically | ||
$this->setParsedSubjectFromRichSubject($notification); | ||
return $notification; | ||
|
||
default: | ||
// Unknown subject => Unknown notification => throw | ||
throw new \InvalidArgumentException(); | ||
} | ||
} | ||
|
||
/** | ||
* This is a little helper function which automatically sets the simple parsed subject | ||
* based on the rich subject you set. This is also the default behaviour of the API | ||
* since Nextcloud 26, but in case you would like to return simpler or other strings, | ||
* this function allows you to take over. | ||
* | ||
* @param \OCP\Notification\INotification $notification | ||
*/ | ||
protected function setParsedSubjectFromRichSubject(\OCP\Notification\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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
/** | ||
* @copyright Copyright (c) 2024, MetaProvide Holding EKF | ||
* | ||
* @author Ron Trevor <ecoron@proton.me> | ||
* | ||
* @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\Files_External_Ethswarm\Service; | ||
|
||
use OCA\Files_External_Ethswarm\AppInfo\AppConstants; | ||
use OCP\Notification\IManager; | ||
use OCP\IUserManager; | ||
use OCP\IUserSession; | ||
|
||
class NotificationService { | ||
private $notificationManager; | ||
private $userManager; | ||
private $userSession; | ||
|
||
public function __construct(IManager $notificationManager, IUserManager $userManager, IUserSession $userSession) { | ||
$this->notificationManager = $notificationManager; | ||
$this->userManager = $userManager; | ||
$this->userSession = $userSession; | ||
} | ||
|
||
public function sendTemporaryNotification($subject, $path) { | ||
// Create a notification | ||
$notification = $this->notificationManager->createNotification(); | ||
$notification->setApp(AppConstants::APP_NAME); | ||
$userId = $this->userSession->getUser()->getUID(); | ||
$notification->setUser($userId); | ||
$notification->setSubject($subject, ['path' => $path]); | ||
$notification->setObject('temporary', $userId); // Marks the notification as temporary | ||
$notification->setDateTime(new \DateTime()); | ||
|
||
// Send the notification | ||
$this->notificationManager->notify($notification); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason to update the version like this for this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The version should be higher than the lastest that we publish. I think @frozenbanana controls the app versionning on the release process.