Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\OCM\Events\OCMNotificationReceivedEvent;
use OCP\OCM\IOCMDiscoveryService;
use OCP\Security\Signature\Exceptions\IncomingRequestException;
use OCP\Security\Signature\IIncomingSignedRequest;
Expand Down Expand Up @@ -404,6 +405,15 @@ public function receiveNotification($notificationType, $resourceType, $providerI
);
}

try {
$notificationObject = $this->factory->getCloudFederationNotification();
$notificationObject->setMessage($notificationType, $resourceType, $providerId, $notification);
$notificationEvent = new OCMNotificationReceivedEvent($notificationObject);
$this->dispatcher->dispatchTyped($notificationEvent);
} catch (\Exception $e) {
$this->logger->warning('error while dispatching OCM notification received event', ['exception' => $e]);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What would be the expected error? the code doesn't look very dangerous

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My thinking is that this runs at the very end of the function, where everything has been successful so far. If one of the listeners fail, that problem could bubble up and cause an issue for something that has already been successful for all intents and purposes. Am I wrong in that assumption? Should I remove the try/catch?

}

return new JSONResponse($result, Http::STATUS_CREATED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\CloudFederationApi\Tests;

use OC\Federation\CloudFederationNotification;
use OCA\CloudFederationAPI\Config;
use OCA\CloudFederationAPI\Controller\RequestHandlerController;
use OCA\CloudFederationAPI\Db\FederatedInvite;
Expand All @@ -18,6 +19,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Federation\ICloudFederationFactory;
use OCP\Federation\ICloudFederationProvider;
use OCP\Federation\ICloudFederationProviderManager;
use OCP\Federation\ICloudIdManager;
use OCP\IAppConfig;
Expand All @@ -26,6 +28,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\OCM\Events\OCMNotificationReceivedEvent;
use OCP\OCM\IOCMDiscoveryService;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -126,4 +129,43 @@ public function testInviteAccepted(): void {

$this->assertEquals($json, $this->requestHandlerController->inviteAccepted($recipientProvider, $token, $recipientId, $recipientEmail, $recipientName));
}

public function testNotificationReceived(): void {
$notificationType = 'SHARE_ACCEPTED';
$resourceType = 'file';
$providerId = '1337';
$notification = ['sharedSecret' => 'secret'];
$notificationObject = new CloudFederationNotification();

$this->appConfig->method('getValueBool')->willReturn(true);
$provider = $this->createMock(ICloudFederationProvider::class);
$provider->method('notificationReceived')->willReturn([]);
$this->cloudFederationFactory->method('getCloudFederationNotification')
->willReturn($notificationObject);
$this->cloudFederationProviderManager->method('getCloudFederationProvider')
->willReturn($provider);

$this->eventDispatcher->expects(self::once())
->method('dispatchTyped')
->with(self::callback(
fn (
OCMNotificationReceivedEvent $event)
=> $event->getNotification() === $notificationObject
)
);
$response = $this->requestHandlerController->receiveNotification(
$notificationType,
$resourceType,
$providerId,
$notification
);
self::assertEquals(Http::STATUS_CREATED, $response->getStatus());
self::assertEquals([
'notificationType' => $notificationType,
'resourceType' => $resourceType,
'providerId' => $providerId,
'notification' => $notification,
], $notificationObject->getMessage());

}
}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@
'OCP\\OCM\\Enum\\ParamType' => $baseDir . '/lib/public/OCM/Enum/ParamType.php',
'OCP\\OCM\\Events\\LocalOCMDiscoveryEvent' => $baseDir . '/lib/public/OCM/Events/LocalOCMDiscoveryEvent.php',
'OCP\\OCM\\Events\\OCMEndpointRequestEvent' => $baseDir . '/lib/public/OCM/Events/OCMEndpointRequestEvent.php',
'OCP\\OCM\\Events\\OCMNotificationReceivedEvent' => $baseDir . '/lib/public/OCM/Events/OCMNotificationReceivedEvent.php',
'OCP\\OCM\\Events\\ResourceTypeRegisterEvent' => $baseDir . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.php',
'OCP\\OCM\\Exceptions\\OCMArgumentException' => $baseDir . '/lib/public/OCM/Exceptions/OCMArgumentException.php',
'OCP\\OCM\\Exceptions\\OCMCapabilityException' => $baseDir . '/lib/public/OCM/Exceptions/OCMCapabilityException.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\OCM\\Enum\\ParamType' => __DIR__ . '/../../..' . '/lib/public/OCM/Enum/ParamType.php',
'OCP\\OCM\\Events\\LocalOCMDiscoveryEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/LocalOCMDiscoveryEvent.php',
'OCP\\OCM\\Events\\OCMEndpointRequestEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/OCMEndpointRequestEvent.php',
'OCP\\OCM\\Events\\OCMNotificationReceivedEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/OCMNotificationReceivedEvent.php',
'OCP\\OCM\\Events\\ResourceTypeRegisterEvent' => __DIR__ . '/../../..' . '/lib/public/OCM/Events/ResourceTypeRegisterEvent.php',
'OCP\\OCM\\Exceptions\\OCMArgumentException' => __DIR__ . '/../../..' . '/lib/public/OCM/Exceptions/OCMArgumentException.php',
'OCP\\OCM\\Exceptions\\OCMCapabilityException' => __DIR__ . '/../../..' . '/lib/public/OCM/Exceptions/OCMCapabilityException.php',
Expand Down
35 changes: 35 additions & 0 deletions lib/public/OCM/Events/OCMNotificationReceivedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCP\OCM\Events;

use OCP\AppFramework\Attribute\Listenable;
use OCP\EventDispatcher\Event;
use OCP\Federation\ICloudFederationNotification;

/**
* @since 35.0.0
*/
#[Listenable(since: '35.0.0')]
class OCMNotificationReceivedEvent extends Event {
/**
* @since 35.0.0
*/
public function __construct(
private readonly ICloudFederationNotification $notification,
) {
parent::__construct();
}

/**
* @since 35.0.0
*/
public function getNotification(): ICloudFederationNotification {
return $this->notification;
}
}
Loading