1414use OCA \FederatedFileSharing \FederatedShareProvider ;
1515use OCA \Federation \TrustedServers ;
1616use OCA \Files_Sharing \Activity \Providers \RemoteShares ;
17+ use OCA \Files_Sharing \External \ExternalShare ;
1718use OCA \Files_Sharing \External \Manager ;
1819use OCA \GlobalSiteSelector \Service \SlaveService ;
20+ use OCA \Polls \Db \Share ;
1921use OCP \Activity \IManager as IActivityManager ;
2022use OCP \App \IAppManager ;
2123use OCP \AppFramework \QueryException ;
4446use OCP \Share \IManager ;
4547use OCP \Share \IProviderFactory ;
4648use OCP \Share \IShare ;
49+ use OCP \Snowflake \IGenerator ;
4750use OCP \Util ;
4851use Psr \Log \LoggerInterface ;
4952use SensitiveParameter ;
@@ -72,13 +75,11 @@ public function __construct(
7275 private IFilenameValidator $ filenameValidator ,
7376 private readonly IProviderFactory $ shareProviderFactory ,
7477 private readonly SetupManager $ setupManager ,
78+ private readonly IGenerator $ snowflakeGenerator ,
7579 ) {
7680 }
7781
78- /**
79- * @return string
80- */
81- public function getShareType () {
82+ public function getShareType (): string {
8283 return 'file ' ;
8384 }
8485
@@ -93,7 +94,7 @@ public function getShareType() {
9394 * @throws HintException
9495 * @since 14.0.0
9596 */
96- public function shareReceived (ICloudFederationShare $ share ) {
97+ public function shareReceived (ICloudFederationShare $ share ): string {
9798 if (!$ this ->isS2SEnabled (true )) {
9899 throw new ProviderCouldNotAddShareException ('Server does not support federated cloud sharing ' , '' , Http::STATUS_SERVICE_UNAVAILABLE );
99100 }
@@ -103,7 +104,8 @@ public function shareReceived(ICloudFederationShare $share) {
103104 throw new ProviderCouldNotAddShareException ('Unsupported protocol for data exchange. ' , '' , Http::STATUS_NOT_IMPLEMENTED );
104105 }
105106
106- [$ ownerUid , $ remote ] = $ this ->addressHandler ->splitUserRemote ($ share ->getOwner ());
107+ [, $ remote ] = $ this ->addressHandler ->splitUserRemote ($ share ->getOwner ());
108+
107109 // for backward compatibility make sure that the remote url stored in the
108110 // database ends with a trailing slash
109111 if (!str_ends_with ($ remote , '/ ' )) {
@@ -113,17 +115,15 @@ public function shareReceived(ICloudFederationShare $share) {
113115 $ token = $ share ->getShareSecret ();
114116 $ name = $ share ->getResourceName ();
115117 $ owner = $ share ->getOwnerDisplayName () ?: $ share ->getOwner ();
116- $ sharedBy = $ share ->getSharedByDisplayName ();
117118 $ shareWith = $ share ->getShareWith ();
118119 $ remoteId = $ share ->getProviderId ();
119120 $ sharedByFederatedId = $ share ->getSharedBy ();
120121 $ ownerFederatedId = $ share ->getOwner ();
121122 $ shareType = $ this ->mapShareTypeToNextcloud ($ share ->getShareType ());
122123
123- // if no explicit information about the person who created the share was send
124+ // if no explicit information about the person who created the share was sent
124125 // we assume that the share comes from the owner
125126 if ($ sharedByFederatedId === null ) {
126- $ sharedBy = $ owner ;
127127 $ sharedByFederatedId = $ ownerFederatedId ;
128128 }
129129
@@ -159,9 +159,19 @@ public function shareReceived(ICloudFederationShare $share) {
159159 }
160160 }
161161
162+ $ externalShare = new ExternalShare ();
163+ $ externalShare ->setId ($ this ->snowflakeGenerator ->nextId ());
164+ $ externalShare ->setRemote ($ remote );
165+ $ externalShare ->setRemoteId ($ remoteId );
166+ $ externalShare ->setShareToken ($ token );
167+ $ externalShare ->setPassword ('' );
168+ $ externalShare ->setName ($ name );
169+ $ externalShare ->setOwner ($ owner );
170+ $ externalShare ->setShareType ($ shareType );
171+ $ externalShare ->setAccepted (IShare::STATUS_PENDING );
172+
162173 try {
163- $ this ->externalShareManager ->addShare ($ remote , $ token , '' , $ name , $ owner , $ shareType , false , $ userOrGroup , $ remoteId );
164- $ shareId = Server::get (IDBConnection::class)->lastInsertId ('*PREFIX*share_external ' );
174+ $ this ->externalShareManager ->addShare ($ externalShare , $ userOrGroup );
165175
166176 // get DisplayName about the owner of the share
167177 $ ownerDisplayName = $ this ->getUserDisplayName ($ ownerFederatedId );
@@ -184,14 +194,14 @@ public function shareReceived(ICloudFederationShare $share) {
184194 ->setType ('remote_share ' )
185195 ->setSubject (RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED , [$ ownerFederatedId , trim ($ name , '/ ' ), $ ownerDisplayName ])
186196 ->setAffectedUser ($ shareWith )
187- ->setObject ('remote_share ' , $ shareId , $ name );
197+ ->setObject ('remote_share ' , $ externalShare -> getId () , $ name );
188198 Server::get (IActivityManager::class)->publish ($ event );
189- $ this ->notifyAboutNewShare ($ shareWith , $ shareId , $ ownerFederatedId , $ sharedByFederatedId , $ name , $ ownerDisplayName );
199+ $ this ->notifyAboutNewShare ($ shareWith , $ externalShare -> getId () , $ ownerFederatedId , $ sharedByFederatedId , $ name , $ ownerDisplayName );
190200
191201 // If auto-accept is enabled, accept the share
192202 if ($ this ->federatedShareProvider ->isFederatedTrustedShareAutoAccept () && $ trustedServers ?->isTrustedServer($ remote ) === true ) {
193203 /** @var IUser $userOrGroup */
194- $ this ->externalShareManager ->acceptShare ($ shareId , $ userOrGroup );
204+ $ this ->externalShareManager ->acceptShare ($ externalShare , $ userOrGroup );
195205 }
196206 } else {
197207 /** @var IGroup $userOrGroup */
@@ -202,18 +212,18 @@ public function shareReceived(ICloudFederationShare $share) {
202212 ->setType ('remote_share ' )
203213 ->setSubject (RemoteShares::SUBJECT_REMOTE_SHARE_RECEIVED , [$ ownerFederatedId , trim ($ name , '/ ' ), $ ownerDisplayName ])
204214 ->setAffectedUser ($ user ->getUID ())
205- ->setObject ('remote_share ' , $ shareId , $ name );
215+ ->setObject ('remote_share ' , $ externalShare -> getId () , $ name );
206216 Server::get (IActivityManager::class)->publish ($ event );
207- $ this ->notifyAboutNewShare ($ user ->getUID (), $ shareId , $ ownerFederatedId , $ sharedByFederatedId , $ name , $ ownerDisplayName );
217+ $ this ->notifyAboutNewShare ($ user ->getUID (), $ externalShare -> getId () , $ ownerFederatedId , $ sharedByFederatedId , $ name , $ ownerDisplayName );
208218
209219 // If auto-accept is enabled, accept the share
210220 if ($ this ->federatedShareProvider ->isFederatedTrustedShareAutoAccept () && $ trustedServers ?->isTrustedServer($ remote ) === true ) {
211- $ this ->externalShareManager ->acceptShare ($ shareId , $ user );
221+ $ this ->externalShareManager ->acceptShare ($ externalShare , $ user );
212222 }
213223 }
214224 }
215225
216- return $ shareId ;
226+ return $ externalShare -> getId () ;
217227 } catch (\Exception $ e ) {
218228 $ this ->logger ->error ('Server can not add remote share. ' , [
219229 'app ' => 'files_sharing ' ,
@@ -275,7 +285,7 @@ private function mapShareTypeToNextcloud($shareType) {
275285 return $ result ;
276286 }
277287
278- private function notifyAboutNewShare ($ shareWith , $ shareId , $ ownerFederatedId , $ sharedByFederatedId , $ name , $ displayName ): void {
288+ private function notifyAboutNewShare ($ shareWith , string $ shareId , $ ownerFederatedId , $ sharedByFederatedId , string $ name , string $ displayName ): void {
279289 $ notification = $ this ->notificationManager ->createNotification ();
280290 $ notification ->setApp ('files_sharing ' )
281291 ->setUser ($ shareWith )
@@ -813,7 +823,7 @@ public function getFederationIdFromSharedSecret(
813823 return '' ;
814824 }
815825
816- return $ share[ ' user ' ] . '@ ' . $ share[ ' remote ' ] ;
826+ return $ share-> getUser () . '@ ' . $ share-> getRemote () ;
817827 }
818828
819829 // if uid_owner is a local account, the request comes from the recipient
0 commit comments