1212use DateTime ;
1313
1414use InvalidArgumentException ;
15+ use OCA \Circles \Model \Circle ;
1516use OCA \Tables \AppInfo \Application ;
1617use OCA \Tables \Constants \ShareReceiverType ;
1718use OCA \Tables \Db \Context ;
@@ -178,7 +179,7 @@ protected function generateShareToken(): ShareToken {
178179
179180 /**
180181 * @param string|null $userId
181- * @return array
182+ * @return array<int, View> Indexed by view id
182183 * @throws InternalError
183184 */
184185 public function findViewsSharedWithMe (?string $ userId = null ): array {
@@ -187,7 +188,7 @@ public function findViewsSharedWithMe(?string $userId = null): array {
187188
188189 /**
189190 * @param string|null $userId
190- * @return array
191+ * @return array<int, Table> Indexed by table id
191192 * @throws InternalError
192193 */
193194 public function findTablesSharedWithMe (?string $ userId = null ): array {
@@ -200,52 +201,38 @@ public function findTablesSharedWithMe(?string $userId = null): array {
200201 private function findElementsSharedWithMe (string $ elementType = 'table ' , ?string $ userId = null ): array {
201202 $ userId = $ this ->permissionsService ->preCheckUserId ($ userId );
202203
203- $ returnArray = [];
204-
204+ $ shares = [];
205205 try {
206- // get all views or tables that are shared with me as user
207- $ elementsSharedWithMe = $ this ->mapper ->findAllSharesFor ($ elementType , $ userId , $ userId );
206+ $ shares ['user ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , [$ userId ], $ userId );
208207
209- // get all views or tables that are shared with me by group
210208 $ userGroups = $ this ->userHelper ->getGroupsForUser ($ userId );
211- foreach ($ userGroups as $ userGroup ) {
212- $ shares = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroup ->getGid (), $ userId , ShareReceiverType::GROUP );
213- $ elementsSharedWithMe = array_merge ($ elementsSharedWithMe , $ shares );
214- }
209+ $ userGroupIds = array_map (static fn ($ group ) => $ group ->getGid (), $ userGroups );
210+ $ shares ['groups ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroupIds , $ userId , ShareReceiverType::GROUP );
215211
216- // get all views or tables that are shared with me by circle
217- if ($ this ->circleHelper ->isCirclesEnabled ()) {
218- $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
219-
220- foreach ($ userCircles as $ userCircle ) {
221- $ shares = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircle ->getSingleId (), $ userId , ShareReceiverType::CIRCLE );
222- $ elementsSharedWithMe = array_merge ($ elementsSharedWithMe , $ shares );
223- }
224- }
212+ $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
213+ $ userCircleIds = array_map (static fn (Circle $ circle ) => $ circle ->getSingleId (), $ userCircles );
214+ $ shares ['circles ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircleIds , $ userId , ShareReceiverType::CIRCLE );
225215 } catch (Throwable $ e ) {
226- throw new InternalError ($ e ->getMessage ());
216+ throw new InternalError ($ e ->getMessage (), previous: $ e );
227217 }
228- foreach ($ elementsSharedWithMe as $ share ) {
229- try {
230- if ($ elementType === 'table ' ) {
231- $ element = $ this ->tableMapper ->find ($ share ->getNodeId ());
232- } elseif ($ elementType === 'view ' ) {
233- $ element = $ this ->viewMapper ->find ($ share ->getNodeId ());
234- } else {
235- throw new InternalError ('Cannot find element of type ' . $ elementType );
236- }
237- // Check if en element with this id is already in the result array
238- $ index = array_search ($ element ->getId (), array_column ($ returnArray , 'id ' ));
239- if (!$ index ) {
240- $ returnArray [] = $ element ;
241- }
242- } catch (DoesNotExistException |Exception |MultipleObjectsReturnedException $ e ) {
243- throw new InternalError ($ e ->getMessage ());
244- }
218+
219+ $ elementIds = [];
220+ foreach (array_merge ($ shares ['user ' ], $ shares ['groups ' ], $ shares ['circles ' ]) as $ share ) {
221+ /** @var Share $share */
222+ $ elementIds [$ share ->getNodeId ()] = true ;
245223 }
246- return $ returnArray ;
247- }
224+ $ elementIds = array_keys ($ elementIds );
248225
226+ if ($ elementType === 'table ' ) {
227+ return $ this ->tableMapper ->findMany ($ elementIds );
228+ }
229+
230+ if ($ elementType === 'view ' ) {
231+ return $ this ->viewMapper ->findMany ($ elementIds );
232+ }
233+
234+ throw new InternalError ('Cannot find element of type ' . $ elementType );
235+ }
249236
250237 /**
251238 * @param int $elementId
0 commit comments