Skip to content
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

Unified push nextcloud notifications #47763

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions lib/private/Notification/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OC\Notification;

use OC\AppFramework\Bootstrap\Coordinator;
use OCP\App\IAppManager;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IUserManager;
Expand All @@ -23,6 +24,7 @@
use OCP\Notification\INotifier;
use OCP\Notification\UnknownNotificationException;
use OCP\RichObjectStrings\IValidator;
use OCP\Server;
use OCP\Support\Subscription\IRegistry;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -72,7 +74,18 @@ public function __construct(
* @since 17.0.0
*/
public function registerApp(string $appClass): void {
$this->appClasses[] = $appClass;
// ensure Nextcloud Notification app is always added at the front of the array so it gets processed first
// for new notifications and last for marking processed or sending deletes.
// this is because it sets the unique id and adds the notification to the data store that other apps might need
// access to in order to be compatible
$appManager = Server::get(IAppManager::class);
if (($appManager !== null) &&
($appManager->isInstalled('notifications') === true) &&
($appClass === 'OCA\Notifications\App')) {
array_unshift($this->appClasses, $appClass);
} else {
$this->appClasses[] = $appClass;
}
}

/**
Expand Down Expand Up @@ -306,6 +319,26 @@ public function notify(INotification $notification): void {
}
}

public function notifyDelete(string $user, ?int $id, ?INotification $notification): void {
if ($notification && !$notification->isValid()) {
throw new IncompleteNotificationException('The given notification is invalid');
}

$apps = array_reverse($this->getApps());

foreach ($apps as $app) {
try {
$app->notifyDelete($user, $id, $notification);
} catch (IncompleteNotificationException) {
} catch (\InvalidArgumentException $e) {
// todo 33.0.0 Log as warning
// todo 39.0.0 Log as error
$this->logger->debug(get_class($app) . '::notify() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\IncompleteNotificationException when the notification is incomplete for your app and otherwise handle all \InvalidArgumentException yourself.');
Comment on lines +333 to +336
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (\InvalidArgumentException $e) {
// todo 33.0.0 Log as warning
// todo 39.0.0 Log as error
$this->logger->debug(get_class($app) . '::notify() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\IncompleteNotificationException when the notification is incomplete for your app and otherwise handle all \InvalidArgumentException yourself.');

But I'm also not sure about the other catch.

}
}
}


/**
* Identifier of the notifier, only use [a-z0-9_]
*
Expand Down Expand Up @@ -382,7 +415,7 @@ public function prepare(INotification $notification, string $languageCode): INot
* @param INotification $notification
*/
public function markProcessed(INotification $notification): void {
$apps = $this->getApps();
$apps = array_reverse($this->getApps());

foreach ($apps as $app) {
$app->markProcessed($notification);
Expand Down
10 changes: 10 additions & 0 deletions lib/public/Notification/IApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ interface IApp {
*/
public function notify(INotification $notification): void;

/**
* @param string $user user
* @param ?int $id id as used by notification app
* @param INotification $notification
* @throws IncompleteNotificationException When the notification does not have all required fields set
* @since 9.0.0
* @since 30.0.0 throws {@see IncompleteNotificationException} instead of \InvalidArgumentException
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @since 9.0.0
* @since 30.0.0 throws {@see IncompleteNotificationException} instead of \InvalidArgumentException
* @since 31.0.0

*/
public function notifyDelete(string $user, ?int $id, ?INotification $notification): void;

/**
* @param INotification $notification
* @since 9.0.0
Expand Down