From 86e61c7ace453dad2d1050056967fd59ffe7a870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Ho=CC=88=C3=9Fl?= Date: Sat, 12 Oct 2024 13:38:11 +0200 Subject: [PATCH 1/3] Begin fix for private comment caching --- controllers/ConsultationController.php | 17 +++--- models/db/AmendmentComment.php | 39 ------------ models/db/MotionComment.php | 38 ------------ models/db/User.php | 39 ++++++++++++ views/consultation/LayoutHelper.php | 7 +-- .../_index_private_comment_list.php | 61 +++++++++++++++++++ views/consultation/index_layout_tags.php | 7 +-- views/consultation/tag_motion_list.php | 9 ++- 8 files changed, 120 insertions(+), 97 deletions(-) create mode 100644 views/consultation/_index_private_comment_list.php diff --git a/controllers/ConsultationController.php b/controllers/ConsultationController.php index 4ad5130e9f..b402f28020 100644 --- a/controllers/ConsultationController.php +++ b/controllers/ConsultationController.php @@ -312,20 +312,15 @@ public function actionIndex(): ResponseInterface $this->consultationSidebar($this->consultation); $myself = User::getCurrentUser(); - if ($myself) { - $myMotions = $myself->getMySupportedMotionsByConsultation($this->consultation); - $myAmendments = $myself->getMySupportedAmendmentsByConsultation($this->consultation); - } else { - $myMotions = null; - $myAmendments = null; - } return new HtmlResponse($this->render('index', [ 'cache' => $cache, 'consultation' => $this->consultation, 'myself' => $myself, - 'myMotions' => $myMotions, - 'myAmendments' => $myAmendments, + 'myMotions' => $myself?->getMySupportedMotionsByConsultation($this->consultation), + 'myAmendments' => $myself?->getMySupportedAmendmentsByConsultation($this->consultation), + 'myMotionComments' => $myself?->getMyPrivatelyCommentedMotionsByConsultation($this->consultation), + 'myAmendmentComments' => $myself?->getMyPrivatelyCommentedAmendmentsByConsultation($this->consultation), ])); } @@ -593,10 +588,14 @@ private function tagMotionResolutionList(int $tagId, bool $isResolutionList): Re return new HtmlErrorResponse(404, 'Tag not found'); } + $myself = User::getCurrentUser(); + return new HtmlResponse($this->render('tag_motion_list', [ 'tag' => $tag, 'cache' => LayoutHelper::getTagMotionListCache($this->consultation, $tag, $isResolutionList), 'isResolutionList' => $isResolutionList, + 'myMotionComments' => $myself?->getMyPrivatelyCommentedMotionsByConsultation($this->consultation), + 'myAmendmentComments' => $myself?->getMyPrivatelyCommentedAmendmentsByConsultation($this->consultation), ])); } diff --git a/models/db/AmendmentComment.php b/models/db/AmendmentComment.php index c25e6ff065..2b55a5715e 100644 --- a/models/db/AmendmentComment.php +++ b/models/db/AmendmentComment.php @@ -121,45 +121,6 @@ public static function getNewestByConsultation(Consultation $consultation, int $ })); } - private static array $cachedComments = []; - - /** - * @return AmendmentComment[][] - */ - public static function getAllForUserAndConsultationByMotion(Consultation $consultation, ?User $user, int $status): array - { - if (!$user) { - return []; - } - - $key = $user->id . '.' . $status; - if (isset(self::$cachedComments[$key])) { - return self::$cachedComments[$key]; - } - - $invisibleStatuses = array_map('intval', $consultation->getStatuses()->getInvisibleMotionStatuses()); - /** @var AmendmentComment[] $allComments */ - $allComments = static::find()->joinWith('amendment', true)->joinWith('amendment.motionJoin', true) - ->where('amendmentComment.status = ' . intval($status)) - ->andWhere('amendmentComment.userId = ' . intval($user->id)) - ->andWhere('amendment.status NOT IN (' . implode(', ', $invisibleStatuses) . ')') - ->andWhere('motion.status NOT IN (' . implode(', ', $invisibleStatuses) . ')') - ->andWhere('motion.consultationId = ' . intval($consultation->id)) - ->orderBy('amendmentComment.paragraph') - ->all(); - - $byAmendment = []; - foreach ($allComments as $comment) { - if (!isset($byAmendment[$comment->amendmentId])) { - $byAmendment[$comment->amendmentId] = []; - } - $byAmendment[$comment->amendmentId][] = $comment; - } - - self::$cachedComments[$key] = $byAmendment; - return $byAmendment; - } - public function getMotionTitle(): string { $amendment = $this->getIMotion(); diff --git a/models/db/MotionComment.php b/models/db/MotionComment.php index 6e2ff9ad77..ccb07d038f 100644 --- a/models/db/MotionComment.php +++ b/models/db/MotionComment.php @@ -126,44 +126,6 @@ public static function getNewestByConsultation(Consultation $consultation, int $ })); } - private static array $cachedComments = []; - - /** - * @return MotionComment[][] - */ - public static function getAllForUserAndConsultationByMotion(Consultation $consultation, ?User $user, int $status): array - { - if (!$user) { - return []; - } - - $key = $user->id . '.' . $status; - if (isset(self::$cachedComments[$key])) { - return self::$cachedComments[$key]; - } - - $invisibleStatuses = array_map('intval', $consultation->getStatuses()->getInvisibleMotionStatuses()); - /** @var MotionComment[] $allComments */ - $allComments = static::find()->joinWith('motion', true) - ->where('motionComment.status = ' . intval($status)) - ->andWhere('motionComment.userId = ' . intval($user->id)) - ->andWhere('motion.status NOT IN (' . implode(', ', $invisibleStatuses) . ')') - ->andWhere('motion.consultationId = ' . intval($consultation->id)) - ->orderBy('motionComment.paragraph') - ->all(); - - $byMotion = []; - foreach ($allComments as $comment) { - if (!isset($byMotion[$comment->motionId])) { - $byMotion[$comment->motionId] = []; - } - $byMotion[$comment->motionId][] = $comment; - } - - self::$cachedComments[$key] = $byMotion; - return $byMotion; - } - public function getConsultation(): ?Consultation { $motion = $this->getIMotion(); diff --git a/models/db/User.php b/models/db/User.php index d4542931fd..bf957a64ef 100644 --- a/models/db/User.php +++ b/models/db/User.php @@ -583,6 +583,45 @@ public function getMySupportedAmendmentsByConsultation(Consultation $consultatio return $supporters; } + /** + * @return MotionComment[] + */ + public function getMyPrivatelyCommentedMotionsByConsultation(Consultation $consultation): array + { + $query = MotionComment::find(); + $query->innerJoin( + 'motion', + 'motionComment.motionId = motion.id' + ); + $query->where('motion.status != ' . intval(Motion::STATUS_DELETED)); + $query->andWhere('motion.consultationId = ' . intval($consultation->id)); + $query->andWhere('motionComment.userId = ' . intval($this->id)); + $query->andWhere('motionComment.status = ' . MotionComment::STATUS_PRIVATE); + $query->orderBy('motion.titlePrefix ASC, motion.dateCreation DESC, motion.id DESC'); + + return $query->all(); + } + + /** + * @return AmendmentComment[] + */ + public function getMyPrivatelyCommentedAmendmentsByConsultation(Consultation $consultation): array + { + $query = AmendmentComment::find(); + $query->innerJoin( + 'amendment', + 'amendmentComment.amendmentId = amendment.id' + ); + $query->innerJoin('motion', 'motion.id = amendment.motionId'); + $query->where('motion.status != ' . IntVal(Motion::STATUS_DELETED)); + $query->andWhere('amendment.status != ' . IntVal(Motion::STATUS_DELETED)); + $query->andWhere('motion.consultationId = ' . IntVal($consultation->id)); + $query->andWhere('amendmentComment.userId = ' . IntVal($this->id)); + $query->andWhere('amendmentComment.status = ' . AmendmentComment::STATUS_PRIVATE); + $query->orderBy('motion.titlePrefix ASC, amendment.titlePrefix ASC, amendment.dateCreation DESC'); + + return $query->all(); + } public function getNotificationUnsubscribeCode(): string { diff --git a/views/consultation/LayoutHelper.php b/views/consultation/LayoutHelper.php index da5b26520b..ac8f90c98d 100644 --- a/views/consultation/LayoutHelper.php +++ b/views/consultation/LayoutHelper.php @@ -96,9 +96,7 @@ public static function flushViewCaches(Consultation $consultation): void private static function getMotionLineContent(Motion $motion, Consultation $consultation): string { $return = '

' . "\n"; - - $privateMotionComments = MotionComment::getAllForUserAndConsultationByMotion($consultation, User::getCurrentUser(), MotionComment::STATUS_PRIVATE); - $return .= LayoutHelper::getPrivateCommentIndicator($motion, $privateMotionComments, []); + $return .= ''; $motionUrl = UrlHelper::createMotionUrl($motion); $return .= ''; @@ -157,8 +155,7 @@ private static function getAmendmentLineContent(Amendment $amendment): string $return = ''; $consultation = $amendment->getMyConsultation(); - $privateAmendmentComments = AmendmentComment::getAllForUserAndConsultationByMotion($consultation, User::getCurrentUser(), AmendmentComment::STATUS_PRIVATE); - $return .= LayoutHelper::getPrivateCommentIndicator($amendment, [], $privateAmendmentComments); + $return .= ''; $title = ($amendment->showTitlePrefix() ? $amendment->getFormattedTitlePrefix() : \Yii::t('amend', 'amendment')); $return .= 'motionId])) { + $motionComments[$comment->motionId] = []; + } + if ($comment->paragraph === -1) { + $motionComments[$comment->motionId][] = $comment->text; + } else { + $motionComments[$comment->motionId][] = str_replace('%NO%', (string) $comment->paragraph, Yii::t('motion', 'private_notes_para')) . + ': ' . $comment->text; + } +} + +$amendmentComments = []; +foreach ($myAmendmentComments as $comment) { + if (!isset($amendmentComments[$comment->amendmentId])) { + $amendmentComments[$comment->amendmentId] = []; + } + if ($comment->paragraph === -1) { + $amendmentComments[$comment->amendmentId][] = $comment->text; + } else { + $amendmentComments[$comment->amendmentId][] = str_replace('%NO%', (string) $comment->paragraph, Yii::t('motion', 'private_notes_para')) . + ': ' . $comment->text; + } +} + +echo '

'; diff --git a/views/consultation/index_layout_tags.php b/views/consultation/index_layout_tags.php index 7c104e5b87..3b2e2e007a 100644 --- a/views/consultation/index_layout_tags.php +++ b/views/consultation/index_layout_tags.php @@ -73,8 +73,6 @@ /** @var int[] $tagIds */ $tagIds = []; $hasNoTagMotions = false; -$privateMotionComments = MotionComment::getAllForUserAndConsultationByMotion($consultation, User::getCurrentUser(), MotionComment::STATUS_PRIVATE); -$privateAmendmentComments = AmendmentComment::getAllForUserAndConsultationByMotion($consultation, User::getCurrentUser(), AmendmentComment::STATUS_PRIVATE); $layout->addOnLoadJS('$(\'[data-toggle="tooltip"]\').tooltip();'); @@ -177,14 +175,13 @@ if ($imotion->isInScreeningProcess()) { $classes[] = 'unscreened'; } - $privateComment = LayoutHelper::getPrivateCommentIndicator($imotion, $privateMotionComments, $privateAmendmentComments); echo ''; if (!$consultation->getSettings()->hideTitlePrefix) { - echo '' . $privateComment . Html::encode($imotion->getFormattedTitlePrefix(LayoutHooks::CONTEXT_MOTION_LIST)) . ''; + echo '' . Html::encode($imotion->getFormattedTitlePrefix(LayoutHooks::CONTEXT_MOTION_LIST)) . ''; } echo ''; if ($consultation->getSettings()->hideTitlePrefix) { - echo $privateComment; + echo ''; } echo '