From 9489c1a689ab2e04faa642d09f1816f421fd59a5 Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Wed, 11 May 2022 10:09:11 +0000
Subject: [PATCH] perform redirect after commenting on a news, fixes #907

Closes #907

Merge request studip/studip!515
---
 app/controllers/course/overview.php           |  2 +-
 app/controllers/institute/overview.php        |  2 +-
 app/controllers/news.php                      | 15 ++++++++++---
 app/controllers/profile.php                   |  2 +-
 app/controllers/studip_controller.php         | 22 +++++++++++++++++++
 app/views/news/_commentbox.php                | 12 +++++-----
 .../core/NewsWidget/NewsWidget.php            |  2 +-
 7 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/app/controllers/course/overview.php b/app/controllers/course/overview.php
index 9246fe3aebc..3d209e6c652 100644
--- a/app/controllers/course/overview.php
+++ b/app/controllers/course/overview.php
@@ -55,7 +55,7 @@ class Course_OverviewController extends AuthenticatedController
         }
 
         // Fetch news
-        $response   = $this->relay('news/display/' . $this->course_id);
+        $response   = $this->relayWithRedirect('news/display/' . $this->course_id);
         $this->news = $response->body;
 
         // Fetch  votes
diff --git a/app/controllers/institute/overview.php b/app/controllers/institute/overview.php
index 29b5fcac6b8..38cf38111bf 100644
--- a/app/controllers/institute/overview.php
+++ b/app/controllers/institute/overview.php
@@ -132,7 +132,7 @@ class Institute_OverviewController extends AuthenticatedController
         }
 
         // Fetch news
-        $response = $this->relay('news/display/' . $this->institute_id);
+        $response = $this->relayWithRedirect('news/display/' . $this->institute_id);
         $this->news = $response->body;
 
         // Fetch  votes
diff --git a/app/controllers/news.php b/app/controllers/news.php
index a8d5237f1ae..e98704e3506 100644
--- a/app/controllers/news.php
+++ b/app/controllers/news.php
@@ -59,7 +59,6 @@ class NewsController extends StudipController
      * Widget controller to produce the formally known show_votes()
      *
      * @param String $range_id range id of the news to get displayed
-     * @return array() Array of votes
      */
     public function display_action($range_id)
     {
@@ -81,11 +80,21 @@ class NewsController extends StudipController
         // Check if user wrote a comment
         if (Request::submitted('accept') && trim(Request::get('comment_content')) && Request::isPost()) {
             CSRFProtection::verifySecurityToken();
-            StudipComment::create([
-                'object_id' => Request::get('comsubmit'),
+
+            $news_id = Request::get('comsubmit');
+            $comment = StudipComment::create([
+                'object_id' => $news_id,
                 'user_id' => $GLOBALS['user']->id,
                 'content' => trim(Request::get('comment_content'))
             ]);
+
+            $url = URLHelper::getURL(Request::url() . "#newscomment-{$comment->id}", [
+                'contentbox_open' => $news_id,
+                'comments'        => true,
+            ]);
+
+            $this->redirect($url);
+            return;
         }
 
         // Check if user wants to remove a announcement
diff --git a/app/controllers/profile.php b/app/controllers/profile.php
index cf9e1f51f61..c5a136f78bc 100644
--- a/app/controllers/profile.php
+++ b/app/controllers/profile.php
@@ -116,7 +116,7 @@ class ProfileController extends AuthenticatedController
         $show_admin = ($this->perm->have_perm('autor') && $this->user->user_id == $this->current_user->user_id)
                    || (Deputy::isEditActivated() && Deputy::isDeputy($this->user->user_id, $this->current_user->user_id, true));
         if (Visibility::verify('news', $this->current_user->user_id) || $show_admin) {
-            $response   = $this->relay('news/display/' . $this->current_user->user_id);
+            $response   = $this->relayWithRedirect('news/display/' . $this->current_user->user_id);
             $this->news = $response->body;
         }
 
diff --git a/app/controllers/studip_controller.php b/app/controllers/studip_controller.php
index 45e0bc25476..ffe04e3ad29 100644
--- a/app/controllers/studip_controller.php
+++ b/app/controllers/studip_controller.php
@@ -534,6 +534,28 @@ abstract class StudipController extends Trails_Controller
         return call_user_func_array([$controller, 'perform_relayed'], $args);
     }
 
+    /**
+     * Relays current request and performs redirect if neccessary.
+     *
+     * @param string $to_uri a trails route
+     * @return Trails_Response
+     *
+     * @see StudipController::relay()
+     */
+    public function relayWithRedirect(...$args): Trails_Response
+    {
+        $response = $this->relay(...$args);
+
+        // If the relayed action should perform a redirect, do so
+        if (isset($response->headers['Location'])) {
+            header("Location: {$response->headers['Location']}");
+            page_close();
+            die;
+        }
+
+        return $response;
+    }
+
     /**
      * perform a given action/parameter string from an relayed request
      * before_filter and after_filter methods are not called
diff --git a/app/views/news/_commentbox.php b/app/views/news/_commentbox.php
index 87d4fadbd65..3e3033a69ae 100644
--- a/app/views/news/_commentbox.php
+++ b/app/views/news/_commentbox.php
@@ -1,9 +1,9 @@
-<article class="comment open">
-    <time><?= reltime($comment[3]) ?></time>
+<article class="comment open" id="newscomment-<?= htmlReady($comment['comment_id']) ?>">
+    <time><?= reltime($comment['mkdate']) ?></time>
     <h1>#<?= $index + 1 ?>
-        <a href="<?= URLHelper::getLink('dispatch.php/profile?username=' . $comment[2]) ?>">
-             <?= htmlReady($comment[1]) ?>
+        <a href="<?= URLHelper::getLink('dispatch.php/profile', ['username' => $comment['username']]) ?>">
+             <?= htmlReady($comment['fullname']) ?>
         </a>
     </h1>
-    <?= formatReady($comment[0]) ?>
-</article>
\ No newline at end of file
+    <?= formatReady($comment['content']) ?>
+</article>
diff --git a/public/plugins_packages/core/NewsWidget/NewsWidget.php b/public/plugins_packages/core/NewsWidget/NewsWidget.php
index 8dd150bec88..a2d8c3334af 100644
--- a/public/plugins_packages/core/NewsWidget/NewsWidget.php
+++ b/public/plugins_packages/core/NewsWidget/NewsWidget.php
@@ -22,7 +22,7 @@ class NewsWidget extends StudIPPlugin implements PortalPlugin
     {
         $dispatcher = new StudipDispatcher();
         $controller = new NewsController($dispatcher);
-        $response = $controller->relay('news/display/studip');
+        $response = $controller->relayWithRedirect('news/display/studip');
         $template = $GLOBALS['template_factory']->open('shared/string');
         $template->content = $response->body;
 
-- 
GitLab