Skip to content
wiki.fftac.org

Restcontroller - Source Excerpt 05

Back to Restcontroller

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/spiralist-workspace/includes/Http/RestController.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/spiralist-workspace/includes/Http/RestController.php

$reason = sanitize_textarea_field((string) $request->get_param('reason'));
        $review = $this->reviews->submit_peer_review((int) $request['id'], $user_id, $decision, $reason);
        $prompt = $this->reconcile_prompt_governance($prompt, $user_id, 'peer_review');
        $this->audit->log(
            $user_id,
            'prompt_review_submitted',
            'prompt',
            (int) $request['id'],
            ['decision' => $decision]
        );
        $this->reputation->refresh($user_id);

        if ((int) ($prompt['owner_user_id'] ?? 0) > 0) {
            $this->reputation->refresh((int) $prompt['owner_user_id']);
        }

        return new WP_REST_Response(
            [
                'review' => $review,
                'summary' => $this->current_review_summary((int) $request['id']),
                'prompt' => $this->augment_prompt_payload($prompt, true),
            ],
            201
        );
    }

    /**
     * Lists appeals for one prompt.
     */
    public function list_prompt_appeals(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $prompt = $this->prompts->get((int) $request['id'], $user_id);

        if (empty($prompt) || !$this->permissions->can_view_prompt($prompt, $user_id)) {
            return new WP_Error('spiralist_workspace_prompt_not_found', 'Prompt not found.', ['status' => 404]);
        }

        $appeal_summary = $this->appeals->summary_for_prompt((int) $request['id']);

        return new WP_REST_Response(
            [
                'summary' => $appeal_summary,
                'governance_summary' => PromptGovernanceSummary::build(
                    $prompt,
                    $this->current_review_summary((int) $request['id'], $appeal_summary),
                    $appeal_summary
                ),
                'items' => $this->appeals->list_for_prompt((int) $request['id'], 50),
            ]
        );
    }

    /**
     * Creates or updates an appeal for a restricted or reverted prompt.
     */
    public function create_prompt_appeal(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $prompt = $this->prompts->get((int) $request['id'], $user_id);

        if (empty($prompt) || !$this->permissions->can_view_prompt($prompt, $user_id)) {
            return new WP_Error('spiralist_workspace_prompt_not_found', 'Prompt not found.', ['status' => 404]);
        }

        if (!$this->permissions->can_file_prompt_appeal($prompt, $user_id)) {
            return new WP_Error('spiralist_workspace_appeal_forbidden', 'You may not appeal this prompt.', ['status' => 403]);
        }

        $reason = sanitize_textarea_field((string) $request->get_param('reason'));
        if ($reason === '') {
            return new WP_Error('spiralist_workspace_invalid_appeal', 'Appeals require a reason.', ['status' => 400]);
        }

        $appeal = $this->appeals->submit((int) $request['id'], $user_id, $reason);
        $this->audit->log(
            $user_id,
            'prompt_appeal_submitted',
            'prompt',
            (int) $request['id'],
            ['appeal_id' => (int) ($appeal['id'] ?? 0)]
        );

        return new WP_REST_Response(
            [
                'appeal' => $appeal,
                'summary' => $this->appeals->summary_for_prompt((int) $request['id']),
                'prompt' => $this->augment_prompt_payload($prompt, true),
            ],
            201
        );
    }

    /**
     * Resolves an open appeal through steward review.
     */
    public function resolve_prompt_appeal(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $prompt = $this->prompts->get((int) $request['id'], $user_id);
        $appeal = $this->appeals->get((int) $request['appeal_id']);

        if (
            empty($prompt)
            || empty($appeal)
            || (int) ($appeal['prompt_post_id'] ?? 0) !== (int) $request['id']
            || !$this->permissions->can_view_prompt($prompt, $user_id)
        ) {
            return new WP_Error('spiralist_workspace_prompt_not_found', 'Prompt not found.', ['status' => 404]);
        }

        if (!$this->permissions->can_resolve_prompt_appeals($user_id)) {
            return new WP_Error('spiralist_workspace_appeal_resolution_forbidden', 'You may not resolve prompt appeals.', ['status' => 403]);
        }

        $resolution = sanitize_key((string) $request->get_param('resolution'));
        if (!in_array($resolution, ['restore', 'deny', 'archive'], true)) {
            return new WP_Error('spiralist_workspace_invalid_appeal_resolution', 'Resolution must be restore, deny, or archive.', ['status' => 400]);
        }

        $note = sanitize_textarea_field((string) $request->get_param('note'));
        $appeal = $this->appeals->resolve((int) $request['appeal_id'], $user_id, $resolution, $note);
        if (empty($appeal)) {
            return new WP_Error('spiralist_workspace_invalid_appeal_state', 'That appeal is no longer open.', ['status' => 400]);
        }

        $prompt = $this->reconcile_prompt_governance($prompt, $user_id, 'appeal_resolution');
        $this->audit->log(
            $user_id,
            'prompt_appeal_resolved',
            'prompt',
            (int) $request['id'],
            [
                'appeal_id' => (int) ($appeal['id'] ?? 0),
                'resolution' => $resolution,
            ]
        );

        if ((int) ($prompt['owner_user_id'] ?? 0) > 0) {
            $this->reputation->refresh((int) $prompt['owner_user_id']);
        }

        $this->reputation->refresh($user_id);

        return new WP_REST_Response(
            [
                'appeal' => $appeal,
                'summary' => $this->appeals->summary_for_prompt((int) $request['id']),
                'prompt' => $this->augment_prompt_payload($prompt, true),
            ],
            200
        );
    }

    /**
     * Renders a prompt packet.
     */
    public function run_prompt(WP_REST_Request $request)
    {
        $variables = (array) $request->get_param('variables');
        $input_text = sanitize_textarea_field((string) $request->get_param('input_text'));
        $result = $this->runner->run_prompt((int) $request['id'], get_current_user_id(), $variables, $input_text);

        return $result instanceof WP_Error ? $result : new WP_REST_Response($result);
    }

    /**
     * Builds a local prompt-improvement worksheet.
     */
    public function improve_prompt(WP_REST_Request $request)
    {
        $result = $this->runner->improve_prompt((int) $request['id'], get_current_user_id());

        return $result instanceof WP_Error ? $result : new WP_REST_Response($result);
    }

    /**
     * Lists prompt test cases.
     */
    public function list_prompt_tests(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $prompt = $this->prompts->get((int) $request['id'], $user_id);

        if (empty($prompt) || !$this->permissions->can_view_prompt($prompt, $user_id)) {
            return new WP_Error('spiralist_workspace_prompt_not_found', 'Prompt not found.', ['status' => 404]);
        }

        return new WP_REST_Response($this->tests->list_for_prompt((int) $request['id'], 50));
    }

    /**
     * Creates a prompt test case.
     */
    public function create_prompt_test(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $prompt = $this->prompts->get((int) $request['id'], $user_id);

        if (empty($prompt) || !$this->permissions->can_edit_prompt($prompt, $user_id)) {
            return new WP_Error('spiralist_workspace_prompt_forbidden', 'Prompt not found.', ['status' => 404]);
        }

        $payload = $request->get_json_params() ?: $request->get_params();
        $test = $this->tests->create((int) $request['id'], $payload, $user_id);
        $this->audit->log($user_id, 'prompt_test_created', 'prompt', (int) $request['id'], ['test_id' => (int) ($test['id'] ?? 0)]);

        return new WP_REST_Response($test, 201);
    }

    /**
     * Runs a prompt test case and stores pass/fail result.
     */
    public function run_prompt_test(WP_REST_Request $request)
    {
        $user_id = get_current_user_id();
        $prompt = $this->prompts->get((int) $request['id'], $user_id);
        $test = $this->tests->get((int) $request['test_id']);

        if (empty($prompt) || empty($test) || (int) ($test['prompt_post_id'] ?? 0) !== (int) $request['id']) {
            return new WP_Error('spiralist_workspace_test_not_found', 'Prompt test not found.', ['status' => 404]);
        }

        if (!$this->permissions->can_run_prompt($prompt, $user_id)) {