1111use OCA \Deck \Activity \ChangeSet ;
1212use OCA \Deck \BadRequestException ;
1313use OCA \Deck \Db \Acl ;
14- use OCA \Deck \Db \Assignment ;
1514use OCA \Deck \Db \AssignmentMapper ;
1615use OCA \Deck \Db \BoardMapper ;
1716use OCA \Deck \Db \Card ;
1817use OCA \Deck \Db \CardMapper ;
1918use OCA \Deck \Db \ChangeHelper ;
20- use OCA \Deck \Db \Label ;
2119use OCA \Deck \Db \LabelMapper ;
2220use OCA \Deck \Db \StackMapper ;
2321use OCA \Deck \Event \CardCreatedEvent ;
@@ -71,12 +69,19 @@ public function __construct(
7169 public function enrichCards (array $ cards ): array {
7270 $ user = $ this ->userManager ->get ($ this ->userId );
7371
74- $ cardIds = array_map (function (Card $ card ) use ($ user ): int {
72+ $ allCardIds = array_map (fn (Card $ card ) => $ card ->getId (), $ cards );
73+ $ attachmentCounts = $ this ->attachmentService ->countForCards ($ allCardIds );
74+
75+ // Pre-fetch all stacks for this batch in one query and index by ID
76+ $ stackIds = array_unique (array_map (fn (Card $ card ) => $ card ->getStackId (), $ cards ));
77+ $ stacksById = $ this ->stackMapper ->findByIds ($ stackIds );
78+
79+ $ cardIds = array_map (function (Card $ card ) use ($ user , $ attachmentCounts , $ stacksById ): int {
7580 // Everything done in here might be heavy as it is executed for every card
7681 $ cardId = $ card ->getId ();
7782 $ this ->cardMapper ->mapOwner ($ card );
7883
79- $ card ->setAttachmentCount ($ this -> attachmentService -> count ( $ cardId) );
84+ $ card ->setAttachmentCount ($ attachmentCounts [ $ cardId] ?? 0 );
8085
8186 // TODO We should find a better way just to get the comment count so we can save 1-3 queries per card here
8287 $ countComments = $ this ->commentsManager ->getNumberOfCommentsForObject ('deckCard ' , (string )$ card ->getId ());
@@ -85,7 +90,7 @@ public function enrichCards(array $cards): array {
8590 $ card ->setCommentsUnread ($ countUnreadComments );
8691 $ card ->setCommentsCount ($ countComments );
8792
88- $ stack = $ this ->stackMapper ->find ($ card ->getStackId ());
93+ $ stack = $ stacksById [ $ card -> getStackId ()] ?? $ this ->stackMapper ->find ($ card ->getStackId ());
8994 $ board = $ this ->boardService ->find ($ stack ->getBoardId (), false );
9095 $ card ->setRelatedStack ($ stack );
9196 $ card ->setRelatedBoard ($ board );
@@ -96,15 +101,19 @@ public function enrichCards(array $cards): array {
96101 $ assignedLabels = $ this ->labelMapper ->findAssignedLabelsForCards ($ cardIds );
97102 $ assignedUsers = $ this ->assignedUsersMapper ->findIn ($ cardIds );
98103
104+ // Pre-group labels and users by card ID
105+ $ labelsByCard = [];
106+ foreach ($ assignedLabels as $ label ) {
107+ $ labelsByCard [$ label ->getCardId ()][] = $ label ;
108+ }
109+ $ usersByCard = [];
110+ foreach ($ assignedUsers as $ assignment ) {
111+ $ usersByCard [$ assignment ->getCardId ()][] = $ assignment ;
112+ }
113+
99114 foreach ($ cards as $ card ) {
100- $ cardLabels = array_values (array_filter ($ assignedLabels , function (Label $ label ) use ($ card ) {
101- return $ label ->getCardId () === $ card ->getId ();
102- }));
103- $ cardAssignedUsers = array_values (array_filter ($ assignedUsers , function (Assignment $ assignment ) use ($ card ) {
104- return $ assignment ->getCardId () === $ card ->getId ();
105- }));
106- $ card ->setLabels ($ cardLabels );
107- $ card ->setAssignedUsers ($ cardAssignedUsers );
115+ $ card ->setLabels ($ labelsByCard [$ card ->getId ()] ?? []);
116+ $ card ->setAssignedUsers ($ usersByCard [$ card ->getId ()] ?? []);
108117 }
109118
110119 return array_map (
0 commit comments