3434use OCP \Federation \Exceptions \BadRequestException ;
3535use OCP \Federation \Exceptions \ProviderCouldNotAddShareException ;
3636use OCP \Federation \Exceptions \ShareNotFoundException ;
37+ use OCP \Federation \ICloudFederationFactory ;
3738use OCP \Federation \ICloudFederationProvider ;
39+ use OCP \Federation \ICloudFederationProviderManager ;
3840use OCP \Federation \ICloudFederationShare ;
3941use OCP \Federation \ICloudIdManager ;
4042use OCP \Files \NotFoundException ;
@@ -75,6 +77,12 @@ class CloudFederationProviderFiles implements ICloudFederationProvider {
7577 /** @var IURLGenerator */
7678 private $ urlGenerator ;
7779
80+ /** @var ICloudFederationFactory */
81+ private $ cloudFederationFactory ;
82+
83+ /** @var ICloudFederationProviderManager */
84+ private $ cloudFederationProviderManager ;
85+
7886 /**
7987 * CloudFederationProvider constructor.
8088 *
@@ -87,6 +95,8 @@ class CloudFederationProviderFiles implements ICloudFederationProvider {
8795 * @param IActivityManager $activityManager
8896 * @param INotificationManager $notificationManager
8997 * @param IURLGenerator $urlGenerator
98+ * @param ICloudFederationFactory $cloudFederationFactory
99+ * @param ICloudFederationProviderManager $cloudFederationProviderManager
90100 */
91101 public function __construct (IAppManager $ appManager ,
92102 FederatedShareProvider $ federatedShareProvider ,
@@ -96,7 +106,9 @@ public function __construct(IAppManager $appManager,
96106 ICloudIdManager $ cloudIdManager ,
97107 IActivityManager $ activityManager ,
98108 INotificationManager $ notificationManager ,
99- IURLGenerator $ urlGenerator
109+ IURLGenerator $ urlGenerator ,
110+ ICloudFederationFactory $ cloudFederationFactory ,
111+ ICloudFederationProviderManager $ cloudFederationProviderManager
100112 ) {
101113 $ this ->appManager = $ appManager ;
102114 $ this ->federatedShareProvider = $ federatedShareProvider ;
@@ -107,6 +119,8 @@ public function __construct(IAppManager $appManager,
107119 $ this ->activityManager = $ activityManager ;
108120 $ this ->notificationManager = $ notificationManager ;
109121 $ this ->urlGenerator = $ urlGenerator ;
122+ $ this ->cloudFederationFactory = $ cloudFederationFactory ;
123+ $ this ->cloudFederationProviderManager = $ cloudFederationProviderManager ;
110124 }
111125
112126
@@ -258,15 +272,18 @@ public function notificationReceived($notificationType, $providerId, array $noti
258272 case 'SHARE_ACCEPTED ' :
259273 $ this ->shareAccepted ($ providerId , $ notification );
260274 return ;
275+ case 'SHARE_DECLINED ' :
276+ $ this ->shareDeclined ($ providerId , $ notification );
277+ return ;
261278 }
262279
263280
264- throw new ActionNotSupportedException ( $ notification );
281+ throw new BadRequestException ([ $ notificationType ] );
265282 }
266283
267284 /**
268- * @param $id
269- * @param $notification
285+ * @param string $id
286+ * @param array $notification
270287 * @return bool
271288 * @throws ActionNotSupportedException
272289 * @throws AuthenticationFailedException
@@ -276,8 +293,8 @@ public function notificationReceived($notificationType, $providerId, array $noti
276293 */
277294 private function shareAccepted ($ id , $ notification ) {
278295
279- if (!$ this ->isS2SEnabled (true )) {
280- throw new ActionNotSupportedException ('Server does not support federated cloud sharing ' , '' , Http:: STATUS_SERVICE_UNAVAILABLE );
296+ if (!$ this ->isS2SEnabled ()) {
297+ throw new ActionNotSupportedException ('Server does not support federated cloud sharing ' );
281298 }
282299
283300 if (!isset ($ notification ['sharedSecret ' ])) {
@@ -311,7 +328,6 @@ private function shareAccepted($id, $notification) {
311328 return true ;
312329 }
313330
314-
315331 /**
316332 * @param IShare $share
317333 * @throws ShareNotFoundException
@@ -334,6 +350,81 @@ protected function executeAcceptShare(IShare $share) {
334350 $ this ->activityManager ->publish ($ event );
335351 }
336352
353+ /**
354+ * @param string $id
355+ * @param array $notification
356+ * @throws ActionNotSupportedException
357+ * @throws AuthenticationFailedException
358+ * @throws BadRequestException
359+ * @throws ShareNotFound
360+ * @throws ShareNotFoundException
361+ * @throws \OC\HintException
362+ */
363+ protected function shareDeclined ($ id , $ notification ) {
364+
365+ if (!$ this ->isS2SEnabled ()) {
366+ throw new ActionNotSupportedException ('Server does not support federated cloud sharing ' );
367+ }
368+
369+ if (!isset ($ notification ['sharedSecret ' ])) {
370+ throw new BadRequestException (['sharedSecret ' ]);
371+ }
372+
373+ $ token = $ notification ['sharedSecret ' ];
374+
375+ $ share = $ this ->federatedShareProvider ->getShareById ($ id );
376+
377+ $ this ->verifyShare ($ share , $ token );
378+
379+ if ($ share ->getShareOwner () !== $ share ->getSharedBy ()) {
380+ list (, $ remote ) = $ this ->addressHandler ->splitUserRemote ($ share ->getSharedBy ());
381+ $ remoteId = $ this ->federatedShareProvider ->getRemoteId ($ share );
382+ $ notification = $ this ->cloudFederationFactory ->getCloudFederationNotification ();
383+ $ notification ->setMessage (
384+ 'SHARE_DECLINED ' ,
385+ 'file ' ,
386+ $ remoteId ,
387+ [
388+ 'sharedSecret ' => $ token ,
389+ 'message ' => 'Recipient declined the re-share '
390+ ]
391+
392+ );
393+ $ this ->cloudFederationProviderManager ->sendNotification ($ remote , $ notification );
394+ }
395+
396+ $ this ->executeDeclineShare ($ share );
397+
398+ }
399+
400+ /**
401+ * delete declined share and create a activity
402+ *
403+ * @param IShare $share
404+ * @throws ShareNotFoundException
405+ */
406+ protected function executeDeclineShare (IShare $ share ) {
407+ $ this ->federatedShareProvider ->removeShareFromTable ($ share );
408+
409+ try {
410+ $ fileId = (int )$ share ->getNode ()->getId ();
411+ list ($ file , $ link ) = $ this ->getFile ($ this ->getCorrectUid ($ share ), $ fileId );
412+ } catch (\Exception $ e ) {
413+ throw new ShareNotFoundException ();
414+ }
415+
416+ $ event = $ this ->activityManager ->generateEvent ();
417+ $ event ->setApp ('files_sharing ' )
418+ ->setType ('remote_share ' )
419+ ->setAffectedUser ($ this ->getCorrectUid ($ share ))
420+ ->setSubject (RemoteShares::SUBJECT_REMOTE_SHARE_DECLINED , [$ share ->getSharedWith (), [$ fileId => $ file ]])
421+ ->setObject ('files ' , $ fileId , $ file )
422+ ->setLink ($ link );
423+ $ this ->activityManager ->publish ($ event );
424+
425+ }
426+
427+
337428 /**
338429 * get file
339430 *
0 commit comments