public function render(
        stdClass $user,
        post_entity $firstpost,
        array $replies
    ) : string {
        global $CFG;

        $displaymode = $this->displaymode;
        $capabilitymanager = $this->capabilitymanager;

        // Make sure we can render.
        if (!$capabilitymanager->can_view_discussions($user)) {
            throw new moodle_exception('noviewdiscussionspermission', 'mod_forum');
        }

        $posts = array_merge([$firstpost], array_values($replies));

        if ($this->postprocessfortemplate !== null) {
            $exporteddiscussion = ($this->postprocessfortemplate) ($this->discussion, $user, $this->forum);
        } else {
            $exporteddiscussion = $this->get_exported_discussion($user);
        }

        $exporteddiscussion = array_merge($exporteddiscussion, [
            'notifications' => $this->get_notifications($user),
            'html' => [
                'posts' => $this->postsrenderer->render($user, [$this->forum], [$this->discussion], $posts),
                'modeselectorform' => $this->get_display_mode_selector_html($displaymode),
                'subscribe' => null,
                'movediscussion' => null,
                'pindiscussion' => null,
                'neighbourlinks' => $this->get_neighbour_links_html(),
                'exportdiscussion' => !empty($CFG->enableportfolios) ? $this->get_export_discussion_html() : null
            ]
        ]);

        $capabilities = (array) $exporteddiscussion['capabilities'];

        if ($capabilities['subscribe']) {
            $exporteddiscussion['html']['subscribe'] = $this->get_subscription_button_html();
        }

        if ($capabilities['move']) {
            $exporteddiscussion['html']['movediscussion'] = $this->get_move_discussion_html();
        }

        return $this->renderer->render_from_template('mod_forum/forum_discussion', $exporteddiscussion);
    }