1616use OCA \Deck \Db \User ;
1717use OCA \Deck \Federation \DeckFederationProxy ;
1818use OCA \Deck \Model \OptionalNullableValue ;
19+ use OCA \Deck \Sharing \DeckShareProvider ;
1920use OCP \AppFramework \Http \DataResponse ;
2021use OCP \Federation \ICloudIdManager ;
2122use OCP \IURLGenerator ;
@@ -27,10 +28,10 @@ public function __construct(
2728 private ICloudIdManager $ cloudIdManager ,
2829 private DeckFederationProxy $ proxy ,
2930 private ConfigService $ configService ,
30- private BoardService $ boardService ,
3131 private PermissionService $ permissionService ,
3232 private BoardMapper $ boardMapper ,
3333 private IURLGenerator $ urlGenerator ,
34+ private DeckShareProvider $ deckShareProvider ,
3435 private ?string $ userId ,
3536 ) {
3637 }
@@ -586,4 +587,155 @@ public function leaveBoardOnRemote(Board $localBoard): void {
586587 $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/boards/ ' . $ localBoard ->getExternalId () . '/leave ' ;
587588 $ this ->proxy ->post ($ participantCloudId ->getId (), $ shareToken , $ url );
588589 }
590+
591+ public function localizeRemoteAttachments (Board $ localBoard , array $ attachments ): array {
592+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
593+ foreach ($ attachments as $ i => $ attachment ) {
594+ $ createdByUser = $ this ->localizeRemoteUser ($ localBoard , ['uid ' => $ attachment ['createdBy ' ]]);
595+ if ($ createdByUser instanceof FederatedUser) {
596+ $ attachments [$ i ]['createdBy ' ] = $ createdByUser ->getCloudId ()->getId ();
597+ $ attachments [$ i ]['createdByRemote ' ] = $ createdByUser ->getCloudId ()->getRemote ();
598+ $ attachments [$ i ]['extendedData ' ]['attachmentCreator ' ]['id ' ] = $ createdByUser ->getCloudId ()->getId ();
599+ $ attachments [$ i ]['extendedData ' ]['attachmentCreator ' ]['remote ' ] = $ createdByUser ->getCloudId ()->getRemote ();
600+ $ attachments [$ i ]['extendedData ' ]['attachmentCreator ' ]['displayName ' ] = $ createdByUser ->getCloudId ()->getId ();
601+ }
602+ if (!empty ($ attachment ['extendedData ' ]['shareToken ' ])) {
603+ $ file = $ this ->deckShareProvider ->ensureAcceptRemoteShare ($ attachment ['extendedData ' ]['shareToken ' ], $ ownerCloudId ->getRemote ());
604+
605+ if (empty ($ file )) {
606+ unset($ attachments [$ i ]);
607+ continue ;
608+ }
609+
610+ $ attachments [$ i ]['extendedData ' ] = array_merge ($ attachments [$ i ]['extendedData ' ], $ file );
611+ unset($ attachments [$ i ]['extendedData ' ]['shareToken ' ]);
612+ }
613+ }
614+ return $ attachments ;
615+ }
616+
617+ public function getAttachmentsFromRemote (Board $ localBoard , int $ cardId ): array {
618+ $ this ->configService ->ensureFederationEnabled ();
619+ $ shareToken = $ localBoard ->getShareToken ();
620+ $ participantCloudId = $ this ->cloudIdManager ->getCloudId ($ this ->userId , null );
621+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
622+ $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/attachments ' ;
623+ $ resp = $ this ->proxy ->get ($ participantCloudId ->getId (), $ shareToken , $ url , [
624+ 'boardId ' => $ localBoard ->getExternalId (),
625+ ]);
626+ $ attachments = $ this ->proxy ->getOcsData ($ resp );
627+ return $ this ->localizeRemoteAttachments ($ localBoard , $ attachments );
628+ }
629+
630+ public function createAttachmentOnRemote (Board $ localBoard , int $ cardId , string $ type , string $ data = '' , ?array $ uploadedFile = null ): array {
631+ $ this ->configService ->ensureFederationEnabled ();
632+ $ this ->permissionService ->checkPermission ($ this ->boardMapper , $ localBoard ->getId (), Acl::PERMISSION_EDIT , $ this ->userId , false , false );
633+ $ shareToken = $ localBoard ->getShareToken ();
634+ $ participantCloudId = $ this ->cloudIdManager ->getCloudId ($ this ->userId , null );
635+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
636+ $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/attachment ' ;
637+ if ($ uploadedFile !== null ) {
638+ $ content = fopen ($ uploadedFile ['tmp_name ' ], 'rb ' );
639+ if ($ content === false ) {
640+ throw new Exception ('Could not read uploaded file ' );
641+ }
642+ $ params = [
643+ [
644+ 'name ' => 'boardId ' ,
645+ 'contents ' => (string )$ localBoard ->getExternalId (),
646+ ],
647+ [
648+ 'name ' => 'file ' ,
649+ 'contents ' => $ content ,
650+ 'filename ' => $ uploadedFile ['name ' ],
651+ ],
652+ [
653+ 'name ' => 'type ' ,
654+ 'contents ' => $ type ,
655+ ],
656+ [
657+ 'name ' => 'data ' ,
658+ 'contents ' => $ data ,
659+ ],
660+ ];
661+ $ resp = $ this ->proxy ->post ($ participantCloudId ->getId (), $ shareToken , $ url , ['multipart ' => $ params ]);
662+ return $ this ->proxy ->getOcsData ($ resp );
663+ }
664+ $ resp = $ this ->proxy ->post ($ participantCloudId ->getId (), $ shareToken , $ url , [
665+ 'boardId ' => $ localBoard ->getExternalId (),
666+ 'type ' => $ type ,
667+ 'data ' => $ data ,
668+ ]);
669+ return $ this ->proxy ->getOcsData ($ resp );
670+ }
671+
672+ public function updateAttachmentOnRemote (Board $ localBoard , int $ cardId , int $ attachmentId , string $ data , string $ type = 'file ' , ?array $ uploadedFile = null ): array {
673+ $ this ->configService ->ensureFederationEnabled ();
674+ $ this ->permissionService ->checkPermission ($ this ->boardMapper , $ localBoard ->getId (), Acl::PERMISSION_EDIT , $ this ->userId , false , false );
675+ $ shareToken = $ localBoard ->getShareToken ();
676+ $ participantCloudId = $ this ->cloudIdManager ->getCloudId ($ this ->userId , null );
677+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
678+ $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/attachments/ ' . $ attachmentId ;
679+ if ($ uploadedFile !== null ) {
680+ $ content = fopen ($ uploadedFile ['tmp_name ' ], 'rb ' );
681+ if ($ content === false ) {
682+ throw new Exception ('Could not read uploaded file ' );
683+ }
684+ $ params = [
685+ [
686+ 'name ' => 'boardId ' ,
687+ 'contents ' => (string )$ localBoard ->getExternalId (),
688+ ],
689+ [
690+ 'name ' => 'file ' ,
691+ 'contents ' => $ content ,
692+ 'filename ' => $ uploadedFile ['name ' ],
693+ ],
694+ [
695+ 'name ' => 'type ' ,
696+ 'contents ' => $ type ,
697+ ],
698+ [
699+ 'name ' => 'data ' ,
700+ 'contents ' => $ data ,
701+ ],
702+ ];
703+ $ resp = $ this ->proxy ->put ($ participantCloudId ->getId (), $ shareToken , $ url , ['multipart ' => $ params ]);
704+ return $ this ->proxy ->getOcsData ($ resp );
705+ }
706+ $ params = [
707+ 'boardId ' => $ localBoard ->getExternalId (),
708+ 'data ' => $ data ,
709+ 'type ' => $ type ,
710+ ];
711+ $ resp = $ this ->proxy ->put ($ participantCloudId ->getId (), $ shareToken , $ url , $ params );
712+ return $ this ->proxy ->getOcsData ($ resp );
713+ }
714+
715+ public function deleteAttachmentOnRemote (Board $ localBoard , int $ cardId , int $ attachmentId , string $ type = 'file ' ): array {
716+ $ this ->configService ->ensureFederationEnabled ();
717+ $ this ->permissionService ->checkPermission ($ this ->boardMapper , $ localBoard ->getId (), Acl::PERMISSION_EDIT , $ this ->userId , false , false );
718+ $ shareToken = $ localBoard ->getShareToken ();
719+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
720+ $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/attachments/ ' . $ attachmentId ;
721+ $ resp = $ this ->proxy ->delete ($ ownerCloudId ->getId (), $ shareToken , $ url , [
722+ 'boardId ' => $ localBoard ->getExternalId (),
723+ 'type ' => $ type ,
724+ ]);
725+ return $ this ->proxy ->getOcsData ($ resp );
726+ }
727+
728+ public function restoreAttachmentOnRemote (Board $ localBoard , int $ cardId , int $ attachmentId , string $ type = 'file ' ): array {
729+ $ this ->configService ->ensureFederationEnabled ();
730+ $ this ->permissionService ->checkPermission ($ this ->boardMapper , $ localBoard ->getId (), Acl::PERMISSION_EDIT , $ this ->userId , false , false );
731+ $ shareToken = $ localBoard ->getShareToken ();
732+ $ participantCloudId = $ this ->cloudIdManager ->getCloudId ($ this ->userId , null );
733+ $ ownerCloudId = $ this ->cloudIdManager ->resolveCloudId ($ localBoard ->getOwner ());
734+ $ url = $ ownerCloudId ->getRemote () . '/ocs/v2.php/apps/deck/api/v1.0/cards/ ' . $ cardId . '/attachments/ ' . $ attachmentId . '/restore ' ;
735+ $ resp = $ this ->proxy ->put ($ participantCloudId ->getId (), $ shareToken , $ url , [
736+ 'boardId ' => $ localBoard ->getExternalId (),
737+ 'type ' => $ type ,
738+ ]);
739+ return $ this ->proxy ->getOcsData ($ resp );
740+ }
589741}
0 commit comments