]> source.dussan.org Git - nextcloud-server.git/commitdiff
Unify code to get shares of a file or also including subfiles
authorJohn Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
Mon, 19 Aug 2019 08:53:29 +0000 (10:53 +0200)
committerDaniel Calviño Sánchez <danxuliu@gmail.com>
Fri, 25 Oct 2019 19:41:12 +0000 (21:41 +0200)
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
apps/files_sharing/lib/Controller/ShareAPIController.php

index 134ff6378e79ccb0e64c2fd466d7f0b137d8f396..e44ca84a09f1bac566c238ff0cb2eb77967c4983 100644 (file)
@@ -612,10 +612,10 @@ class ShareAPIController extends OCSController {
 
        /**
         * @param \OCP\Files\Folder $folder
-        * @return DataResponse
+        * @return array
         * @throws OCSBadRequestException
         */
-       private function getSharesInDir(Node $folder): DataResponse {
+       private function getSharesInDir(Node $folder): array {
                if (!($folder instanceof \OCP\Files\Folder)) {
                        throw new OCSBadRequestException($this->l->t('Not a directory'));
                }
@@ -630,41 +630,13 @@ class ShareAPIController extends OCSController {
 
                // filter out duplicate shares
                $known = [];
-               $shares = array_filter($shares, function($share) use (&$known) {
+               return array_filter($shares, function($share) use (&$known) {
                        if (in_array($share->getId(), $known)) {
                                return false;
                        }
                        $known[] = $share->getId();
                        return true;
                });
-
-               $formatted = $miniFormatted = [];
-               $resharingRight = false;
-               foreach ($shares as $share) {
-                       if ($share->getSharedWith() === $this->currentUser) {
-                               continue;
-                       }
-
-                       try {
-                               $format = $this->formatShare($share);
-
-                               $formatted[] = $format;
-                               if ($share->getSharedBy() === $this->currentUser) {
-                                       $miniFormatted[] = $format;
-                               }
-                               if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $folder)) {
-                                       $resharingRight = true;
-                               }
-                       } catch (\Exception $e) {
-                               //Ignore this share
-                       }
-               }
-
-               if (!$resharingRight) {
-                       $formatted = $miniFormatted;
-               }
-
-               return new DataResponse($formatted);
        }
 
        /**
@@ -713,35 +685,45 @@ class ShareAPIController extends OCSController {
                        return $result;
                }
 
-               if ($subfiles === 'true') {
-                       $result = $this->getSharesInDir($path);
-                       return $result;
-               }
-
                if ($reshares === 'true') {
                        $reshares = true;
                } else {
                        $reshares = false;
                }
 
-               // Get all shares
-               $shares = $this->getAllShares($path, $reshares);
+               if ($subfiles === 'true') {
+                       $shares = $this->getSharesInDir($path);
+                       $recipientNode = null;
+               } else {
+                       // get all shares
+                       $shares = $this->getAllShares($path, $reshares);
+                       $recipientNode = $path;
+               }
 
+               // process all shares
                $formatted = $miniFormatted = [];
                $resharingRight = false;
                foreach ($shares as $share) {
                        /** @var IShare $share */
+
+                       // do not list the shares of the current user
                        if ($share->getSharedWith() === $this->currentUser) {
                                continue;
                        }
 
                        try {
-                               $format = $this->formatShare($share, $path);
+                               $format = $this->formatShare($share, $recipientNode);
                                $formatted[] = $format;
+
+                               // let's also build a list of shares created
+                               // by the current user only, in case
+                               // there is no resharing rights
                                if ($share->getSharedBy() === $this->currentUser) {
                                        $miniFormatted[] = $format;
                                }
 
+                               // check if one of those share is shared with me
+                               // and if I have resharing rights on it
                                if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $path)) {
                                        $resharingRight = true;
                                }