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 \IUserManager ;
4244use OCP \Security \IHasher ;
4345use OCP \Security \ISecureRandom ;
@@ -194,7 +196,7 @@ protected function generateShareToken(): ShareToken {
194196
195197 /**
196198 * @param string|null $userId
197- * @return array
199+ * @return array<int, View> Indexed by view id
198200 * @throws InternalError
199201 */
200202 public function findViewsSharedWithMe (?string $ userId = null ): array {
@@ -203,7 +205,7 @@ public function findViewsSharedWithMe(?string $userId = null): array {
203205
204206 /**
205207 * @param string|null $userId
206- * @return array
208+ * @return array<int, Table> Indexed by table id
207209 * @throws InternalError
208210 */
209211 public function findTablesSharedWithMe (?string $ userId = null ): array {
@@ -216,52 +218,38 @@ public function findTablesSharedWithMe(?string $userId = null): array {
216218 private function findElementsSharedWithMe (string $ elementType = 'table ' , ?string $ userId = null ): array {
217219 $ userId = $ this ->permissionsService ->preCheckUserId ($ userId );
218220
219- $ returnArray = [];
220-
221+ $ shares = [];
221222 try {
222- // get all views or tables that are shared with me as user
223- $ elementsSharedWithMe = $ this ->mapper ->findAllSharesFor ($ elementType , $ userId , $ userId );
223+ $ shares ['user ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , [$ userId ], $ userId );
224224
225- // get all views or tables that are shared with me by group
226225 $ userGroups = $ this ->userHelper ->getGroupsForUser ($ userId );
227- foreach ($ userGroups as $ userGroup ) {
228- $ shares = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroup ->getGid (), $ userId , ShareReceiverType::GROUP );
229- $ elementsSharedWithMe = array_merge ($ elementsSharedWithMe , $ shares );
230- }
226+ $ userGroupIds = array_map (static fn (IGroup $ group ) => $ group ->getGid (), $ userGroups );
227+ $ shares ['groups ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userGroupIds , $ userId , ShareReceiverType::GROUP );
231228
232- // get all views or tables that are shared with me by circle
233- if ($ this ->circleHelper ->isCirclesEnabled ()) {
234- $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
235-
236- foreach ($ userCircles as $ userCircle ) {
237- $ shares = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircle ->getSingleId (), $ userId , ShareReceiverType::CIRCLE );
238- $ elementsSharedWithMe = array_merge ($ elementsSharedWithMe , $ shares );
239- }
240- }
229+ $ userCircles = $ this ->circleHelper ->getUserCircles ($ userId );
230+ $ userCircleIds = array_map (static fn (Circle $ circle ) => $ circle ->getSingleId (), $ userCircles );
231+ $ shares ['circles ' ] = $ this ->mapper ->findAllSharesFor ($ elementType , $ userCircleIds , $ userId , ShareReceiverType::CIRCLE );
241232 } catch (Throwable $ e ) {
242- throw new InternalError ($ e ->getMessage ());
233+ throw new InternalError ($ e ->getMessage (), previous: $ e );
243234 }
244- foreach ($ elementsSharedWithMe as $ share ) {
245- try {
246- if ($ elementType === 'table ' ) {
247- $ element = $ this ->tableMapper ->find ($ share ->getNodeId ());
248- } elseif ($ elementType === 'view ' ) {
249- $ element = $ this ->viewMapper ->find ($ share ->getNodeId ());
250- } else {
251- throw new InternalError ('Cannot find element of type ' . $ elementType );
252- }
253- // Check if en element with this id is already in the result array
254- $ index = array_search ($ element ->getId (), array_column ($ returnArray , 'id ' ), true );
255- if (!$ index ) {
256- $ returnArray [] = $ element ;
257- }
258- } catch (DoesNotExistException |Exception |MultipleObjectsReturnedException $ e ) {
259- throw new InternalError ($ e ->getMessage ());
260- }
235+
236+ $ elementIds = [];
237+ foreach (array_merge ($ shares ['user ' ], $ shares ['groups ' ], $ shares ['circles ' ]) as $ share ) {
238+ /** @var Share $share */
239+ $ elementIds [$ share ->getNodeId ()] = true ;
261240 }
262- return $ returnArray ;
263- }
241+ $ elementIds = array_keys ($ elementIds );
264242
243+ if ($ elementType === 'table ' ) {
244+ return $ this ->tableMapper ->findMany ($ elementIds );
245+ }
246+
247+ if ($ elementType === 'view ' ) {
248+ return $ this ->viewMapper ->findMany ($ elementIds );
249+ }
250+
251+ throw new InternalError ('Cannot find element of type ' . $ elementType );
252+ }
265253
266254 /**
267255 * @param int $elementId
0 commit comments