Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ A signed request sees what the web shows you, such as your own pending commits a
- read your account, logs, replies, notifications (including grouped activity), and starred commits;
- commit, reply, and quote, with images, and delete your commits, with the same undo window as the web;
- preview commit drafts without publishing them;
- request a feature or report a bug as `/feature` and `/bug` do, with the same limit of 10 opened issues a day;
- follow or unfollow people and organizations, mute or unmute, and block or unblock, and list who you muted and blocked;
- read who to follow, also as it is suggested beside a profile or a commit, and mark a suggestion not interested;
- read the hot topics, and each one's commits from the past week with its recap;
Expand Down Expand Up @@ -641,9 +642,9 @@ Start a fresh commit draft with **`/bug `** or **`/feature `** to switch it into

Empty issue drafts show a short Markdown template: steps and expected versus actual behavior for bugs, or the problem and proposal for features. The first top-level `# Heading` becomes the issue title and is removed from the body; the remaining Markdown is preserved. Without a heading, the first line is used as the title and the full draft remains in the body.

Successful issue creation is limited to **ten issues per day per user**. Rejected drafts and failed requests do not use that allowance. If GitHub or the connection fails, the draft stays available. Check upstream before retrying an uncertain submission to avoid duplicates. Use a commit's Report control instead when reporting abusive content.
Successful issue creation is limited to **ten issues per day per user**, shared between the web and the apps. Rejected drafts and failed requests do not use that allowance. If GitHub or the connection fails, the draft stays available. Check upstream before retrying an uncertain submission to avoid duplicates. Use a commit's Report control instead when reporting abusive content.

Sources: [issue command UI](resources/js/components/issue-command.tsx), [issue validation](app/Http/Requests/CreateIssueRequest.php), [issue controller](app/Http/Controllers/IssueController.php).
Sources: [issue command UI](resources/js/components/issue-command.tsx), [issue validation](app/Http/Requests/CreateIssueRequest.php), [issue controller](app/Http/Controllers/IssueController.php), [daily limit](app/Http/Middleware/ThrottleIssues.php), [API issue controller](app/Http/Controllers/Api/IssueController.php).

### Exporting your data

Expand Down
3 changes: 3 additions & 0 deletions app/Enums/ApiErrorCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ enum ApiErrorCode: string
case TimedOut = 'timed_out';
case Banned = 'banned';
case CommitRulesRequired = 'commit_rules_required';
case GitHubIssueRefused = 'github_issue_refused';
case GitHubIssueUnanswered = 'github_issue_unanswered';

public function status(): int
{
Expand All @@ -33,6 +35,7 @@ public function status(): int
self::CommitUnavailable => 404,
self::PasteisExhausted => 422,
self::RateLimited => 429,
self::GitHubIssueRefused, self::GitHubIssueUnanswered => 502,
self::GitHubSyncFailed => 503,
};
}
Expand Down
16 changes: 16 additions & 0 deletions app/Exceptions/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ public static function gitHubSyncFailed(): self
return new self(ApiErrorCode::GitHubSyncFailed, __('Could not sync from GitHub. Try again in a minute.'));
}

/**
* GitHub refused the issue, so trying again is safe, as the web says it.
*/
public static function gitHubIssueRefused(): self
{
return new self(ApiErrorCode::GitHubIssueRefused, __('GitHub did not create the issue. Your draft is safe.'));
}

/**
* GitHub did not answer, so the issue may exist anyway and trying again may duplicate it, as the web says it.
*/
public static function gitHubIssueUnanswered(): self
{
return new self(ApiErrorCode::GitHubIssueUnanswered, __('GitHub did not respond. Check the upstream issues before trying again. Your draft is safe.'));
}

/**
* The user is banned, so the only thing left for them is deleting their account.
*/
Expand Down
38 changes: 38 additions & 0 deletions app/Http/Controllers/Api/IssueController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Actions\CreateIssue;
use App\Exceptions\ApiException;
use App\Http\Requests\CreateApiIssueRequest;
use App\Models\User;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\JsonResponse;

final readonly class IssueController
{
/**
* Opens a /feature or /bug issue as the web does. When GitHub fails, the app keeps the draft.
*/
public function store(CreateApiIssueRequest $request, #[CurrentUser] User $user, CreateIssue $action): JsonResponse
{
try {
$issue = $action->handle(
$user,
$request->string('command')->value(),
$request->string('body')->value(),
$request->environment(),
);
} catch (ConnectionException) {
throw ApiException::gitHubIssueUnanswered();
} catch (RequestException) {
throw ApiException::gitHubIssueRefused();
}

return response()->json($issue, 201);
}
}
14 changes: 0 additions & 14 deletions app/Http/Controllers/IssueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,11 @@
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\RateLimiter;

final readonly class IssueController
{
private const int MaxIssuesPerDay = 10;

/**
* Only issues that were opened count towards the daily limit, so a rejected draft or a GitHub outage costs nothing.
*/
public function store(CreateIssueRequest $request, #[CurrentUser] User $user, CreateIssue $action): JsonResponse
{
$key = 'issues:'.$user->id;

if (RateLimiter::tooManyAttempts($key, self::MaxIssuesPerDay)) {
return response()->json(['message' => 'Issue limit reached: 10 a day. Try again tomorrow.'], 429);
}

try {
$issue = $action->handle(
$user,
Expand All @@ -45,8 +33,6 @@ public function store(CreateIssueRequest $request, #[CurrentUser] User $user, Cr
], 502);
}

RateLimiter::hit($key, 86400);

return response()->json($issue, 201);
}
}
58 changes: 58 additions & 0 deletions app/Http/Middleware/ThrottleIssues.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Enums\ApiErrorCode;
use App\Exceptions\ApiException;
use App\Models\User;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Symfony\Component\HttpFoundation\Response;

final readonly class ThrottleIssues
{
private const int PER_DAY = 10;

private const int DECAY_SECONDS = 86400;

/**
* One budget of issues a day, however they are opened. Only issues GitHub opened count, so a rejected draft or a
* GitHub outage costs nothing.
*
* @param Closure(Request): (Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = $request->user();
assert($user instanceof User);

$key = "issues:{$user->id}";

if (RateLimiter::tooManyAttempts($key, self::PER_DAY)) {
return $this->tooMany($request, RateLimiter::availableIn($key));
}

$response = $next($request);

if ($response->getStatusCode() === Response::HTTP_CREATED) {
RateLimiter::hit($key, self::DECAY_SECONDS);
}

return $response;
}

private function tooMany(Request $request, int $retryAfter): Response
{
$message = 'Issue limit reached: '.self::PER_DAY.' a day. Try again tomorrow.';

// The apps read JSON with a code...
if ($request->routeIs('api-issue.store')) {
return new ApiException(ApiErrorCode::RateLimited, $message, ['Retry-After' => $retryAfter])->render();
}

return response()->json(['message' => $message], 429);
}
}
34 changes: 34 additions & 0 deletions app/Http/Requests/CreateApiIssueRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\In;

final class CreateApiIssueRequest extends FormRequest
{
public function prepareForValidation(): void
{
CreateCommitRequest::normalizeBody($this);
}

/**
* The web's rules: text only, and the same environment fields, which a terminal fills as far as they apply.
*
* @return array<string, array<int, string|In>>
*/
public function rules(): array
{
return CreateIssueRequest::issueRules();
}

/**
* @return array<string, string>
*/
public function environment(): array
{
return CreateIssueRequest::filledEnvironment($this);
}
}
38 changes: 29 additions & 9 deletions app/Http/Requests/CreateIssueRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@ final class CreateIssueRequest extends FormRequest
*/
public const int MIN_BODY_LENGTH = 3;

public function prepareForValidation(): void
{
CreateCommitRequest::normalizeBody($this);
}

/**
* Issues carry text only: images are not accepted.
* The rules of every request that opens an issue, so the apps validate drafts as the web does.
*
* @return array<string, array<int, string|In>>
*/
public function rules(): array
public static function issueRules(): array
{
return [
'command' => ['required', Rule::in(['/feature', '/bug'])],
Expand All @@ -39,13 +34,38 @@ public function rules(): array
}

/**
* The environment fields the reporter filled, leaving out empty ones.
*
* @return array<string, string>
*/
public function environment(): array
public static function filledEnvironment(FormRequest $request): array
{
/** @var array<string, string|null> $environment */
$environment = $this->array('environment');
$environment = $request->array('environment');

return array_filter($environment, fn (?string $value): bool => $value !== null && $value !== '');
}

public function prepareForValidation(): void
{
CreateCommitRequest::normalizeBody($this);
}

/**
* Issues carry text only: images are not accepted.
*
* @return array<string, array<int, string|In>>
*/
public function rules(): array
{
return self::issueRules();
}

/**
* @return array<string, string>
*/
public function environment(): array
{
return self::filledEnvironment($this);
}
}
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use App\Http\Controllers\Api\DeviceController;
use App\Http\Controllers\Api\DevicePushTokenController;
use App\Http\Controllers\Api\HeadlineController;
use App\Http\Controllers\Api\IssueController;
use App\Http\Controllers\Api\IssueSuggestionController;
use App\Http\Controllers\Api\LiveFollowingController;
use App\Http\Controllers\Api\LogController;
Expand Down Expand Up @@ -97,6 +98,7 @@
use App\Http\Middleware\PreventPostingInTimeout;
use App\Http\Middleware\RequireCommitRules;
use App\Http\Middleware\ThrottleCommits;
use App\Http\Middleware\ThrottleIssues;
use App\Http\Middleware\UpdateLastSeenAt;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
Expand Down Expand Up @@ -241,6 +243,11 @@
->middleware('throttle:issue-suggestions')
->name('api-issue-suggestion.index');

// Feature requests and bug reports, opened on GitHub as the web's /feature and /bug commands open them...
Route::post('issues', [IssueController::class, 'store'])
->middleware([PreventPostingInTimeout::class, 'throttle:10,1', ThrottleIssues::class])
->name('api-issue.store');

// User Remotes: the switch from the web's settings, and the settings menu on its Remotes page...
Route::put('me/remotes', [UserRemotesController::class, 'update'])->name('api-user-remotes.update');
Route::put('me/remote-preferences', [UserRemotePreferencesController::class, 'update'])
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
use App\Http\Middleware\SerializePushSessions;
use App\Http\Middleware\SerializePushSubscriptions;
use App\Http\Middleware\ThrottleCommits;
use App\Http\Middleware\ThrottleIssues;
use App\Models\Profile;
use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -369,7 +370,7 @@

Route::middleware('auth')->group(function (): void {
Route::post('issues', [IssueController::class, 'store'])
->middleware([PreventPostingInTimeout::class, 'throttle:10,1'])
->middleware([PreventPostingInTimeout::class, 'throttle:10,1', ThrottleIssues::class])
->name('issue.store');

// Session...
Expand Down
Loading