1212use DateTime ;
1313
1414use InvalidArgumentException ;
15+ use OCA \Circles \Model \Circle ;
1516use OC \User \User ;
17+ use OCP \IUserManager ;
1618use OCA \Tables \AppInfo \Application ;
1719use OCA \Tables \Constants \ShareReceiverType ;
1820use OCA \Tables \Db \Context ;
3941use OCP \AppFramework \Db \TTransactional ;
4042use OCP \DB \Exception ;
4143use OCP \IDBConnection ;
42- use OCP \IUserManager ;
44+ use OCP \IGroup ;
4345use OCP \Security \ISecureRandom ;
4446use Psr \Log \LoggerInterface ;
4547use Throwable ;
@@ -181,7 +183,7 @@ protected function generateShareToken(): ShareToken {
181183
182184 /**
183185 * @param string|null $userId
184- * @return array
186+ * @return array<int, View> Indexed by view id
185187 * @throws InternalError
186188 */
187189 public function findViewsSharedWithMe (?string $ userId = null ): array {
@@ -190,7 +192,7 @@ public function findViewsSharedWithMe(?string $userId = null): array {
190192
191193 /**
192194 * @param string|null $userId
193- * @return array
195+ * @return array<int, Table> Indexed by table id
194196 * @throws InternalError
195197 */
196198 public function findTablesSharedWithMe (?string $ userId = null ): array {
@@ -203,52 +205,38 @@ public function findTablesSharedWithMe(?string $userId = null): array {
203205 private function findElementsSharedWithMe (string $ elementType = 'table ' , ?string $ userId = null ): array {
204206 $ userId = $ this ->permissionsService ->preCheckUserId ($ userId );
205207
206- $ returnArray = [];
207-
208+ $ shares = [];
208209 try {
209- // get all views or tables that are shared with me as user
210- $ elementsSharedWithMe = $ this ->mapper ->findAllSharesFor ($ elementType , $ userId , $ userId );
210+ $ shares ['user ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , [$ userId ], $ userId );
211211
212- // get all views or tables that are shared with me by group
213212 $ userGroups = $ this ->userHelper ->getGroupsForUser ($ userId );
214- foreach ($ userGroups as $ userGroup ) {
215- $ shares = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroup ->getGid (), $ userId , ShareReceiverType::GROUP );
216- $ elementsSharedWithMe = array_merge ($ elementsSharedWithMe , $ shares );
217- }
213+ $ userGroupIds = array_map (static fn (IGroup $ group ) => $ group ->getGid (), $ userGroups );
214+ $ shares ['groups ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroupIds , $ userId , ShareReceiverType::GROUP );
218215
219- // get all views or tables that are shared with me by circle
220- if ($ this ->circleHelper ->isCirclesEnabled ()) {
221- $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
222-
223- foreach ($ userCircles as $ userCircle ) {
224- $ shares = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircle ->getSingleId (), $ userId , ShareReceiverType::CIRCLE );
225- $ elementsSharedWithMe = array_merge ($ elementsSharedWithMe , $ shares );
226- }
227- }
216+ $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
217+ $ userCircleIds = array_map (static fn (Circle $ circle ) => $ circle ->getSingleId (), $ userCircles );
218+ $ shares ['circles ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircleIds , $ userId , ShareReceiverType::CIRCLE );
228219 } catch (Throwable $ e ) {
229- throw new InternalError ($ e ->getMessage ());
220+ throw new InternalError ($ e ->getMessage (), previous: $ e );
230221 }
231- foreach ($ elementsSharedWithMe as $ share ) {
232- try {
233- if ($ elementType === 'table ' ) {
234- $ element = $ this ->tableMapper ->find ($ share ->getNodeId ());
235- } elseif ($ elementType === 'view ' ) {
236- $ element = $ this ->viewMapper ->find ($ share ->getNodeId ());
237- } else {
238- throw new InternalError ('Cannot find element of type ' . $ elementType );
239- }
240- // Check if en element with this id is already in the result array
241- $ index = array_search ($ element ->getId (), array_column ($ returnArray , 'id ' ));
242- if (!$ index ) {
243- $ returnArray [] = $ element ;
244- }
245- } catch (DoesNotExistException |Exception |MultipleObjectsReturnedException $ e ) {
246- throw new InternalError ($ e ->getMessage ());
247- }
222+
223+ $ elementIds = [];
224+ foreach (array_merge ($ shares ['user ' ], $ shares ['groups ' ], $ shares ['circles ' ]) as $ share ) {
225+ /** @var Share $share */
226+ $ elementIds [$ share ->getNodeId ()] = true ;
227+ }
228+ $ elementIds = array_keys ($ elementIds );
229+
230+ if ($ elementType === 'table ' ) {
231+ return $ this ->tableMapper ->findMany ($ elementIds );
248232 }
249- return $ returnArray ;
250- }
251233
234+ if ($ elementType === 'view ' ) {
235+ return $ this ->viewMapper ->findMany ($ elementIds );
236+ }
237+
238+ throw new InternalError ('Cannot find element of type ' . $ elementType );
239+ }
252240
253241 /**
254242 * @param int $elementId
@@ -286,7 +274,6 @@ public function create(int $nodeId, string $nodeType, string $receiver, string $
286274 $ this ->logger ->error ($ e ->getMessage (), ['exception ' => $ e ]);
287275 throw new InternalError (get_class ($ this ) . ' - ' . __FUNCTION__ . ': ' . $ e ->getMessage ());
288276 }
289-
290277 $ time = new DateTime ();
291278 $ item = new Share ();
292279 $ item ->setSender ($ sender );
@@ -583,6 +570,7 @@ public function findSharedWithUserIds(int $elementId, string $elementType): arra
583570 }
584571 }
585572
573+
586574 /**
587575 * @param int $nodeId
588576 * @param array $share
@@ -636,5 +624,4 @@ public function importShare(int $nodeId, array $share, string $userId): void {
636624 $ this ->logger ->error ('Failed to import share: ' . $ e ->getMessage (), ['exception ' => $ e , 'share ' => $ share ]);
637625 }
638626 }
639-
640627}
0 commit comments