diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index 14ab784d0cf1..b0d10d0b0682 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -60,12 +60,13 @@ public function __construct(array $urlParams = []) { $server->getRootFolder() ); }); - $container->registerService('ExternalSharesController', function (SimpleContainer $c) { + $container->registerService('ExternalSharesController', function (SimpleContainer $c) use ($server) { return new ExternalSharesController( $c->query('AppName'), $c->query('Request'), $c->query('ExternalManager'), - $c->query('HttpClientService') + $c->query('HttpClientService'), + $server->getEventDispatcher() ); }); diff --git a/apps/files_sharing/lib/Controllers/ExternalSharesController.php b/apps/files_sharing/lib/Controllers/ExternalSharesController.php index fcd4d5f16cd3..284a23af1f02 100644 --- a/apps/files_sharing/lib/Controllers/ExternalSharesController.php +++ b/apps/files_sharing/lib/Controllers/ExternalSharesController.php @@ -30,6 +30,8 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\Http\Client\IClientService; use OCP\AppFramework\Http\DataResponse; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\GenericEvent; /** * Class ExternalSharesController @@ -42,20 +44,29 @@ class ExternalSharesController extends Controller { private $externalManager; /** @var IClientService */ private $clientService; + /** + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + private $dispatcher; /** + * ExternalSharesController constructor. + * * @param string $appName * @param IRequest $request * @param \OCA\Files_Sharing\External\Manager $externalManager * @param IClientService $clientService + * @param EventDispatcherInterface $eventDispatcher */ public function __construct($appName, IRequest $request, \OCA\Files_Sharing\External\Manager $externalManager, - IClientService $clientService) { + IClientService $clientService, + EventDispatcherInterface $eventDispatcher) { parent::__construct($appName, $request); $this->externalManager = $externalManager; $this->clientService = $clientService; + $this->dispatcher = $eventDispatcher; } /** @@ -76,6 +87,16 @@ public function index() { * @return JSONResponse */ public function create($id) { + $shareInfo = $this->externalManager->getShare($id); + $event = new GenericEvent(null, + [ + 'shareAcceptedFrom' => $shareInfo['owner'], + 'sharedAcceptedBy' => $shareInfo['user'], + 'sharedItem' => $shareInfo['name'], + 'remoteUrl' => $shareInfo['remote'] + ] + ); + $this->dispatcher->dispatch('remoteshare.accepted', $event); $this->externalManager->acceptShare($id); return new JSONResponse(); } @@ -88,6 +109,16 @@ public function create($id) { * @return JSONResponse */ public function destroy($id) { + $shareInfo = $this->externalManager->getShare($id); + $event = new GenericEvent(null, + [ + 'shareAcceptedFrom' => $shareInfo['owner'], + 'sharedAcceptedBy' => $shareInfo['user'], + 'sharedItem' => $shareInfo['name'], + 'remoteUrl' => $shareInfo['remote'] + ] + ); + $this->dispatcher->dispatch('remoteshare.declined', $event); $this->externalManager->declineShare($id); return new JSONResponse(); } diff --git a/apps/files_sharing/tests/Controllers/ExternalShareControllerTest.php b/apps/files_sharing/tests/Controllers/ExternalShareControllerTest.php index 4ca92a631f97..d8d619928ed5 100644 --- a/apps/files_sharing/tests/Controllers/ExternalShareControllerTest.php +++ b/apps/files_sharing/tests/Controllers/ExternalShareControllerTest.php @@ -28,9 +28,11 @@ use OCP\AppFramework\Http\JSONResponse; use OCP\Http\Client\IClientService; use OCP\IRequest; +use Symfony\Component\EventDispatcher\GenericEvent; /** * Class ExternalShareControllerTest + * @group DB * * @package OCA\Files_Sharing\Controllers */ @@ -60,7 +62,8 @@ public function getExternalShareController() { 'files_sharing', $this->request, $this->externalManager, - $this->clientService + $this->clientService, + \OC::$server->getEventDispatcher() ); } @@ -79,7 +82,15 @@ public function testCreate() { ->method('acceptShare') ->with(4); + $called = array(); + \OC::$server->getEventDispatcher()->addListener('remoteshare.accepted', function ($event) use (&$called) { + $called[] = 'remoteshare.accepted'; + array_push($called, $event); + }); $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->create(4)); + + $this->assertSame('remoteshare.accepted', $called[0]); + $this->assertTrue($called[1] instanceof GenericEvent); } public function testDestroy() { @@ -88,7 +99,16 @@ public function testDestroy() { ->method('declineShare') ->with(4); + $called = array(); + \OC::$server->getEventDispatcher()->addListener('remoteshare.declined', function ($event) use (&$called) { + $called[] = 'remoteshare.declined'; + array_push($called, $event); + }); + $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->destroy(4)); + + $this->assertSame('remoteshare.declined', $called[0]); + $this->assertTrue($called[1] instanceof GenericEvent); } public function testRemoteWithValidHttps() {