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 ;
3839use OCP \AppFramework \Db \TTransactional ;
3940use OCP \DB \Exception ;
4041use OCP \IDBConnection ;
42+ use OCP \IGroup ;
4143use OCP \Security \ISecureRandom ;
4244use Psr \Log \LoggerInterface ;
4345use Throwable ;
@@ -178,7 +180,7 @@ protected function generateShareToken(): ShareToken {
178180
179181 /**
180182 * @param string|null $userId
181- * @return array
183+ * @return array<int, View> Indexed by view id
182184 * @throws InternalError
183185 */
184186 public function findViewsSharedWithMe (?string $ userId = null ): array {
@@ -187,7 +189,7 @@ public function findViewsSharedWithMe(?string $userId = null): array {
187189
188190 /**
189191 * @param string|null $userId
190- * @return array
192+ * @return array<int, Table> Indexed by table id
191193 * @throws InternalError
192194 */
193195 public function findTablesSharedWithMe (?string $ userId = null ): array {
@@ -200,52 +202,38 @@ public function findTablesSharedWithMe(?string $userId = null): array {
200202 private function findElementsSharedWithMe (string $ elementType = 'table ' , ?string $ userId = null ): array {
201203 $ userId = $ this ->permissionsService ->preCheckUserId ($ userId );
202204
203- $ returnArray = [];
204-
205+ $ shares = [];
205206 try {
206- // get all views or tables that are shared with me as user
207- $ elementsSharedWithMe = $ this ->mapper ->findAllSharesFor ($ elementType , $ userId , $ userId );
207+ $ shares ['user ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , [$ userId ], $ userId );
208208
209- // get all views or tables that are shared with me by group
210209 $ 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- }
210+ $ userGroupIds = array_map (static fn (IGroup $ group ) => $ group ->getGid (), $ userGroups );
211+ $ shares ['groups ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroupIds , $ userId , ShareReceiverType::GROUP );
215212
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- }
213+ $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
214+ $ userCircleIds = array_map (static fn (Circle $ circle ) => $ circle ->getSingleId (), $ userCircles );
215+ $ shares ['circles ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircleIds , $ userId , ShareReceiverType::CIRCLE );
225216 } catch (Throwable $ e ) {
226- throw new InternalError ($ e ->getMessage ());
217+ throw new InternalError ($ e ->getMessage (), previous: $ e );
227218 }
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- }
219+
220+ $ elementIds = [];
221+ foreach (array_merge ($ shares ['user ' ], $ shares ['groups ' ], $ shares ['circles ' ]) as $ share ) {
222+ /** @var Share $share */
223+ $ elementIds [$ share ->getNodeId ()] = true ;
245224 }
246- return $ returnArray ;
247- }
225+ $ elementIds = array_keys ($ elementIds );
248226
227+ if ($ elementType === 'table ' ) {
228+ return $ this ->tableMapper ->findMany ($ elementIds );
229+ }
230+
231+ if ($ elementType === 'view ' ) {
232+ return $ this ->viewMapper ->findMany ($ elementIds );
233+ }
234+
235+ throw new InternalError ('Cannot find element of type ' . $ elementType );
236+ }
249237
250238 /**
251239 * @param int $elementId
0 commit comments