|
19 | 19 | use OCA\Deck\Db\ChangeHelper; |
20 | 20 | use OCA\Deck\Db\Label; |
21 | 21 | use OCA\Deck\Db\LabelMapper; |
| 22 | +use OCA\Deck\Db\Stack; |
22 | 23 | use OCA\Deck\Db\StackMapper; |
23 | 24 | use OCA\Deck\Event\CardCreatedEvent; |
24 | 25 | use OCA\Deck\Event\CardDeletedEvent; |
@@ -64,31 +65,47 @@ public function __construct( |
64 | 65 | ) { |
65 | 66 | } |
66 | 67 |
|
67 | | - public function enrichCards($cards) { |
| 68 | + /** |
| 69 | + * @param Card[] $cards |
| 70 | + * @return CardDetails[] |
| 71 | + */ |
| 72 | + public function enrichCards(array $cards, Stack $stack = null): array { |
| 73 | + if (!$cards) { |
| 74 | + return []; |
| 75 | + } |
| 76 | + |
68 | 77 | $user = $this->userManager->get($this->userId); |
69 | 78 |
|
70 | | - $cardIds = array_map(function (Card $card) use ($user) { |
| 79 | + $cardIds = array_map(function (Card $card) use ($user, $stack) { |
71 | 80 | // Everything done in here might be heavy as it is executed for every card |
72 | 81 | $cardId = $card->getId(); |
73 | 82 | $this->cardMapper->mapOwner($card); |
74 | 83 |
|
75 | 84 | $card->setAttachmentCount($this->attachmentService->count($cardId)); |
76 | 85 |
|
77 | | - // TODO We should find a better way just to get the comment count so we can save 1-3 queries per card here |
78 | | - $countComments = $this->commentsManager->getNumberOfCommentsForObject('deckCard', (string)$card->getId()); |
79 | | - $lastRead = $countComments > 0 ? $this->commentsManager->getReadMark('deckCard', (string)$card->getId(), $user) : null; |
80 | | - $countUnreadComments = $lastRead ? $this->commentsManager->getNumberOfCommentsForObject('deckCard', (string)$card->getId(), $lastRead) : 0; |
81 | | - $card->setCommentsUnread($countUnreadComments); |
82 | | - $card->setCommentsCount($countComments); |
83 | | - |
84 | | - $stack = $this->stackMapper->find($card->getStackId()); |
| 86 | + if ($stack === null) { |
| 87 | + $stack = $this->stackMapper->find($card->getStackId()); |
| 88 | + } |
85 | 89 | $board = $this->boardService->find($stack->getBoardId(), false); |
86 | 90 | $card->setRelatedStack($stack); |
87 | 91 | $card->setRelatedBoard($board); |
88 | 92 |
|
89 | | - return $card->getId(); |
| 93 | + return $cardId; |
90 | 94 | }, $cards); |
91 | 95 |
|
| 96 | + $commentsCountPerCardId = $this->commentsManager->getNumberOfCommentsForObjects('deckCard', $cardIds); |
| 97 | + $unreadCommentsCountPerCardId = $this->commentsManager->getNumberOfUnreadCommentsForObjects('deckCard', $cardIds, $user); |
| 98 | + |
| 99 | + foreach ($commentsCountPerCardId as $cardId => $commentCounts) { |
| 100 | + foreach ($cards as $card) { |
| 101 | + if ($card->getId() === $cardId) { |
| 102 | + $card->setCommentsUnread($unreadCommentsCountPerCardId[$cardId]); |
| 103 | + $card->setCommentsCount($commentCounts); |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
92 | 109 | $assignedLabels = $this->labelMapper->findAssignedLabelsForCards($cardIds); |
93 | 110 | $assignedUsers = $this->assignedUsersMapper->findIn($cardIds); |
94 | 111 |
|
|
0 commit comments