Skip to content

Commit

Permalink
changes to allow nextcloud notifications via unified push. requires r…
Browse files Browse the repository at this point in the history
…elated changes in nextcloud app
  • Loading branch information
gavine99 committed Sep 4, 2024
1 parent 94d259e commit 665200d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
39 changes: 37 additions & 2 deletions lib/private/Notification/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use OCP\Support\Subscription\IRegistry;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\LoggerInterface;
use OCP\Server;
use OCP\App\IAppManager;

class Manager implements IManager {
/** @var ICache */
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,28 @@ 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.');
}
}

}


/**
* Identifier of the notifier, only use [a-z0-9_]
*
Expand Down Expand Up @@ -382,7 +417,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
*/
public function notifyDelete(string $user, ?int $id, ?INotification $notification): void;

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

0 comments on commit 665200d

Please sign in to comment.