@@ -612,60 +612,31 @@ private function getSharedWithMe($node = null, bool $includeTags): DataResponse
612612
613613 /**
614614 * @param \OCP\Files\Folder $folder
615- * @return DataResponse
615+ * @return array
616616 * @throws OCSBadRequestException
617617 */
618- private function getSharesInDir (Node $ folder ): DataResponse {
618+ private function getSharesInDir (Node $ folder ): array {
619619 if (!($ folder instanceof \OCP \Files \Folder)) {
620620 throw new OCSBadRequestException ($ this ->l ->t ('Not a directory ' ));
621621 }
622622
623623 $ nodes = $ folder ->getDirectoryListing ();
624- /** @var \OCP\Share\IShare[] $shares */
625- $ shares = [];
626- foreach ($ nodes as $ node ) {
627624
628- $ shares = array_merge ($ shares , $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_USER , $ node , true , -1 , 0 ));
629- $ shares = array_merge ($ shares , $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_GROUP , $ node , true , -1 , 0 ));
630- $ shares = array_merge ($ shares , $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_LINK , $ node , true , -1 , 0 ));
631- if ($ this ->shareManager ->shareProviderExists (Share::SHARE_TYPE_EMAIL )) {
632- $ shares = array_merge ($ shares , $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_EMAIL , $ node , true , -1 , 0 ));
633- }
634- if ($ this ->shareManager ->outgoingServer2ServerSharesAllowed ()) {
635- $ shares = array_merge ($ shares , $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_REMOTE , $ node , true , -1 , 0 ));
636- }
637- $ shares = array_merge ($ shares , $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_ROOM , $ node , true , -1 , 0 ));
638- }
625+ /** @var \OCP\Share\IShare[] $shares */
626+ $ shares = array_reduce ($ nodes , function ($ carry , $ node ) {
627+ $ carry = array_merge ($ carry , $ this ->getAllShares ($ node , true ));
628+ return $ carry ;
629+ }, []);
639630
640- $ formatted = $ miniFormatted = [];
641- $ resharingRight = false ;
631+ // filter out duplicate shares
642632 $ known = [];
643- foreach ($ shares as $ share ) {
644- if (in_array ($ share ->getId (), $ known ) || $ share ->getSharedWith () === $ this ->currentUser ) {
645- continue ;
646- }
647-
648- try {
649- $ format = $ this ->formatShare ($ share );
650-
651- $ known [] = $ share ->getId ();
652- $ formatted [] = $ format ;
653- if ($ share ->getSharedBy () === $ this ->currentUser ) {
654- $ miniFormatted [] = $ format ;
655- }
656- if (!$ resharingRight && $ this ->shareProviderResharingRights ($ this ->currentUser , $ share , $ folder )) {
657- $ resharingRight = true ;
658- }
659- } catch (\Exception $ e ) {
660- //Ignore this share
633+ return array_filter ($ shares , function ($ share ) use ($ shares , $ known ) {
634+ if (in_array ($ share ->getId (), $ shares )) {
635+ return false ;
661636 }
662- }
663-
664- if (!$ resharingRight ) {
665- $ formatted = $ miniFormatted ;
666- }
667-
668- return new DataResponse ($ formatted );
637+ $ known [] = $ share ->getId ();
638+ return true ;
639+ });
669640 }
670641
671642 /**
@@ -714,59 +685,46 @@ public function getShares(
714685 return $ result ;
715686 }
716687
717- if ($ subfiles === 'true ' ) {
718- $ result = $ this ->getSharesInDir ($ path );
719- return $ result ;
720- }
721-
722688 if ($ reshares === 'true ' ) {
723689 $ reshares = true ;
724690 } else {
725691 $ reshares = false ;
726692 }
727693
728- // Get all shares
729- $ userShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_USER , $ path , $ reshares , -1 , 0 );
730- $ groupShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_GROUP , $ path , $ reshares , -1 , 0 );
731- $ linkShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_LINK , $ path , $ reshares , -1 , 0 );
732- if ($ this ->shareManager ->shareProviderExists (Share::SHARE_TYPE_EMAIL )) {
733- $ mailShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_EMAIL , $ path , $ reshares , -1 , 0 );
734- } else {
735- $ mailShares = [];
736- }
737- if ($ this ->shareManager ->shareProviderExists (Share::SHARE_TYPE_CIRCLE )) {
738- $ circleShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_CIRCLE , $ path , $ reshares , -1 , 0 );
739- } else {
740- $ circleShares = [];
741- }
742- $ roomShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_ROOM , $ path , $ reshares , -1 , 0 );
743-
744- $ shares = array_merge ($ userShares , $ groupShares , $ linkShares , $ mailShares , $ circleShares , $ roomShares );
745694
746- if ($ this ->shareManager ->outgoingServer2ServerSharesAllowed ()) {
747- $ federatedShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_REMOTE , $ path , $ reshares , -1 , 0 );
748- $ shares = array_merge ($ shares , $ federatedShares );
749- }
750-
751- if ($ this ->shareManager ->outgoingServer2ServerGroupSharesAllowed ()) {
752- $ federatedShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_REMOTE_GROUP , $ path , $ reshares , -1 , 0 );
753- $ shares = array_merge ($ shares , $ federatedShares );
695+ if ($ subfiles === 'true ' ) {
696+ $ shares = $ this ->getSharesInDir ($ path );
697+ } else {
698+ // get all shares
699+ $ shares = $ this ->getAllShares ($ path , $ reshares );
754700 }
755701
702+ // process all shares
756703 $ formatted = $ miniFormatted = [];
757704 $ resharingRight = false ;
758705 foreach ($ shares as $ share ) {
759- /** @var IShare $share */
706+
707+ // check if one of those share is shared with me
708+ // and if I have resharing rights on it
709+ if (!$ resharingRight && $ this ->shareProviderResharingRights ($ this ->currentUser , $ share , $ path )) {
710+ $ resharingRight = true ;
711+ }
712+
713+ // do not list the shares of the current user
714+ if ($ share ->getSharedWith () === $ this ->currentUser ) {
715+ continue ;
716+ }
717+
760718 try {
761719 $ format = $ this ->formatShare ($ share , $ path );
762720 $ formatted [] = $ format ;
721+
722+ // let's also build a list of shares created
723+ // by the current user only, in case
724+ // there is no resharing rights
763725 if ($ share ->getSharedBy () === $ this ->currentUser ) {
764726 $ miniFormatted [] = $ format ;
765727 }
766-
767- if (!$ resharingRight && $ this ->shareProviderResharingRights ($ this ->currentUser , $ share , $ path )) {
768- $ resharingRight = true ;
769- }
770728 } catch (\Exception $ e ) {
771729 //Ignore share
772730 }
@@ -1324,4 +1282,53 @@ private function shareProviderResharingRights(string $userId, IShare $share, $no
13241282 return false ;
13251283 }
13261284
1285+ /**
1286+ * Get all the shares for the current user
1287+ *
1288+ * @param string $path
1289+ * @param boolean $reshares
1290+ * @return void
1291+ */
1292+ private function getAllShares ($ path = null , $ reshares = false ) {
1293+ // Get all shares
1294+ $ userShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_USER , $ path , $ reshares , -1 , 0 );
1295+ $ groupShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_GROUP , $ path , $ reshares , -1 , 0 );
1296+ $ linkShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_LINK , $ path , $ reshares , -1 , 0 );
1297+
1298+ // EMAIL SHARES
1299+ if ($ this ->shareManager ->shareProviderExists (Share::SHARE_TYPE_EMAIL )) {
1300+ $ mailShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_EMAIL , $ path , $ reshares , -1 , 0 );
1301+ } else {
1302+ $ mailShares = [];
1303+ }
1304+
1305+ // CIRCLE SHARES
1306+ if ($ this ->shareManager ->shareProviderExists (Share::SHARE_TYPE_CIRCLE )) {
1307+ $ circleShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_CIRCLE , $ path , $ reshares , -1 , 0 );
1308+ } else {
1309+ $ circleShares = [];
1310+ }
1311+
1312+ // TALK SHARES
1313+ if ($ this ->shareManager ->shareProviderExists (Share::SHARE_TYPE_ROOM )) {
1314+ $ roomShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_ROOM , $ path , $ reshares , -1 , 0 );
1315+ } else {
1316+ $ circleShares = [];
1317+ }
1318+
1319+ // FEDERATION
1320+ if ($ this ->shareManager ->outgoingServer2ServerSharesAllowed ()) {
1321+ $ federatedShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_REMOTE , $ path , $ reshares , -1 , 0 );
1322+ } else {
1323+ $ federatedShares = [];
1324+ }
1325+ if ($ this ->shareManager ->outgoingServer2ServerGroupSharesAllowed ()) {
1326+ $ federatedGroupShares = $ this ->shareManager ->getSharesBy ($ this ->currentUser , Share::SHARE_TYPE_REMOTE_GROUP , $ path , $ reshares , -1 , 0 );
1327+ } else {
1328+ $ federatedGroupShares = [];
1329+ }
1330+
1331+ return array_merge ($ userShares , $ groupShares , $ linkShares , $ mailShares , $ circleShares , $ roomShares , $ federatedShares , $ federatedGroupShares );
1332+ }
1333+
13271334}
0 commit comments