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
2 changes: 1 addition & 1 deletion PRODUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Identity is GitHub. Every account is a real GitHub user, and the profile (avatar
- Sign-in is GitHub OAuth only. Profile changes happen on GitHub ("Edit on GitHub"), then sync at next login or with "Sync from GitHub" (once a minute).
- Everything except the landing page, the public read API, and profile feeds requires login.
- Public read API: read-only JSON under `/api/v1`, no authentication, for public profiles (with counts), a user's commits or replies, and single commits. It returns exactly what a guest's link preview may show: approved, non-deleted commits only (with their images), approved reply counts, and a parent only while it is approved. Never emails or other hidden profile fields. 60 requests a minute per IP.
- Signed API: the mobile app uses the same `/api/v1`, signing every request with an SSH key it keeps on the device, which the user approved as a device (no passwords or API tokens). The app signs in like an OAuth app with PKCE: it opens `/mobile/authorize` in a browser, the user signs in with GitHub if they need to and approves it, and the app redeems the one-time code for the device with a request signed by that key. The approval is the proof, so the key doesn't need to be on GitHub, and a key that is on GitHub, like one made for the GitHub Action, can't use the API without it. Devices are listed, with when each was last used, and revoked under Settings → Devices. A signed request to a public endpoint gets what the web shows that user, such as their own pending commits. Signed-only endpoints read the log, replies, and notifications, and commit, reply, delete, and follow, with the web's validation, moderation, and rate limits. A bad or expired signature is a 401, never the public view. 120 requests a minute per user.
- Signed API: the mobile app uses the same `/api/v1`, signing every request with an SSH key it keeps on the device, which the user approved as a device (no passwords or API tokens). The app signs in like an OAuth app with PKCE: it opens `/mobile/authorize` in a browser, the user signs in with GitHub if they need to and approves it, and the app redeems the one-time code for the device with a request signed by that key. The approval is the proof, so the key doesn't need to be on GitHub, and a key that is on GitHub, like one made for the GitHub Action, can't use the API without it. Devices are listed, with when each was last used, and revoked under Settings → Devices. A signed request to a public endpoint gets what the web shows that user, such as their own pending commits. Signed-only endpoints read the log, replies, notifications, and saved commits, and commit, reply, delete, follow, star, and save, with the web's validation, moderation, and rate limits. A bad or expired signature is a 401, never the public view. 120 requests a minute per user.
- Profile feeds: every profile has an Atom feed of its latest 50 approved top-level commits at `/@{username}/feed.atom`, their images as enclosures, advertised with `<link rel="alternate">` on the profile's guest preview.
- First sign-in leads to a short setup at `/onboarding`: confirm the mirrored profile, pick a color, follow a few suggested developers, learn the composer, `?` shortcuts and terminal commits, then land on the log (or the page the user was headed to). Skippable at any step (Skip setup or Esc); finishing or skipping ends it for good. Accounts that existed before it never see it.
- Profile links (`/@{username}`) and links to approved commits (`/@{username}/commits/{id}`) unfurl with a link preview (the author and their bio or counts, or the commit body and reply count) for guests and crawlers; a guest opening one sees the landing page, and the profile or commit itself stays behind login. Pending and rejected commits are a 404 for guests.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Every profile also has an Atom feed of its latest 50 top-level commits at `/@{us

## Signed requests

The mobile app signs its requests to `/api/v1` with an SSH key it keeps on the device, which the user approved on the web (see [Signing in](#signing-in)). A signed request to the endpoints above gets what the web shows that user. The log, replies, notifications, committing, deleting and following only take signed requests. Signed requests are limited to 120 a minute per user.
The mobile app signs its requests to `/api/v1` with an SSH key it keeps on the device, which the user approved on the web (see [Signing in](#signing-in)). A signed request to the endpoints above gets what the web shows that user. The log, replies, notifications, saved commits, committing, deleting, following, starring and saving only take signed requests. Signed requests are limited to 120 a minute per user.

Commits in a signed response carry what the web's cards show: `preview_segments`, the excerpt a list shows of a long commit (or `null`), the `poll` with the user's view of its votes and each option's `percentage`, the `quoted_commit` (a tombstone once deleted, or `null` when the user may not see it, while `quoted_commit_id` still says a commit was quoted), and in lists the `reply_preview` the web shows under each commit. Its `next_cursor` continues with `GET /api/v1/commits/{id}/replies?newest=1&cursor=…`, as on the web.

Expand Down Expand Up @@ -124,7 +124,7 @@ An error is JSON with a `message` to show the user. The errors the app has to te
| `401` | `device_not_approved` | Nobody approved the key as a device, or it was revoked. |
| `403` | `user_blocked` | The user blocked the profile they asked for. Neither of them sees the other's commits. |
| `403` | `user_unavailable` | The profile isn't available to the user, because it blocked them. |
| `429` | `rate_limited` | Over 120 signed requests a minute for the user, 10 commits a minute, 30 follows and unfollows a minute, or 60 failed signatures a minute from one IP. `Retry-After` says when to try again. |
| `429` | `rate_limited` | Over 120 signed requests a minute for the user, 10 commits a minute, 30 follows and unfollows a minute, 60 stars and unstars a minute, 60 saves and unsaves a minute, or 60 failed signatures a minute from one IP. `Retry-After` says when to try again. |

Other errors have no `code`, like a `422` for a commit that fails validation or a `404` for one that doesn't exist. Unsigned requests to the public endpoints get the same errors as before, without codes.

Expand Down
50 changes: 50 additions & 0 deletions app/Http/Controllers/Api/BookmarkController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Actions\CreateBookmark;
use App\Actions\DeleteBookmark;
use App\Http\Requests\CreateApiBookmarkRequest;
use App\Http\Requests\DeleteApiBookmarkRequest;
use App\Http\Resources\CommitResource;
use App\Models\Commit;
use App\Models\User;
use App\Queries\CommitPageQuery;
use App\Queries\ReplyPreviewsQuery;
use App\Queries\SavedCommitsQuery;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;

final readonly class BookmarkController
{
/**
* The commits the user saved, last saved first, with the newest reply under each, as on the web.
*/
public function index(#[CurrentUser] User $user, SavedCommitsQuery $saved, ReplyPreviewsQuery $replyPreviews): AnonymousResourceCollection
{
$commits = $saved->builder($user)->cursorPaginate(20);

$replyPreviews->load($commits->items(), $user);

return CommitResource::collection($commits);
}

/**
* Answers with the commit as its page shows it, so the app gets the new is_saved.
*/
public function store(CreateApiBookmarkRequest $request, #[CurrentUser] User $user, Commit $commit, CreateBookmark $action, CommitPageQuery $page): CommitResource
{
$action->handle($user, $commit);

return CommitResource::make($page->get($commit, $user));
}

public function destroy(DeleteApiBookmarkRequest $request, #[CurrentUser] User $user, Commit $commit, DeleteBookmark $action, CommitPageQuery $page): CommitResource
{
$action->handle($user, $commit);

return CommitResource::make($page->get($commit, $user));
}
}
35 changes: 35 additions & 0 deletions app/Http/Controllers/Api/CommitStarController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Api;

use App\Actions\CreateCommitStar;
use App\Actions\DeleteCommitStar;
use App\Http\Requests\CreateApiCommitStarRequest;
use App\Http\Requests\DeleteApiCommitStarRequest;
use App\Http\Resources\CommitResource;
use App\Models\Commit;
use App\Models\User;
use App\Queries\CommitPageQuery;
use Illuminate\Container\Attributes\CurrentUser;

final readonly class CommitStarController
{
/**
* Answers with the commit as its page shows it, so the app gets the new stars_count and starred.
*/
public function store(CreateApiCommitStarRequest $request, #[CurrentUser] User $user, Commit $commit, CreateCommitStar $action, CommitPageQuery $page): CommitResource
{
$action->handle($user, $commit);

return CommitResource::make($page->get($commit, $user));
}

public function destroy(DeleteApiCommitStarRequest $request, #[CurrentUser] User $user, Commit $commit, DeleteCommitStar $action, CommitPageQuery $page): CommitResource
{
$action->handle($user, $commit);

return CommitResource::make($page->get($commit, $user));
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/CreateApiBookmarkRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class CreateApiBookmarkRequest extends FormRequest
{
public function authorize(): Response
{
$user = $this->user();
assert($user instanceof User);

$commit = $this->route('commit');
assert($commit instanceof Commit);

return $commit->isVisibleTo($user) ? Response::allow() : Response::denyAsNotFound();
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
40 changes: 40 additions & 0 deletions app/Http/Requests/CreateApiCommitStarRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class CreateApiCommitStarRequest extends FormRequest
{
/**
* Like on the web, the user may star any commit they may see but their own. One they may not see looks like it does
* not exist.
*/
public function authorize(): Response
{
$user = $this->user();
assert($user instanceof User);

$commit = $this->route('commit');
assert($commit instanceof Commit);

return match (true) {
$commit->user_id === $user->id => Response::deny(__('You cannot star your own commit.')),
$commit->isVisibleTo($user) => Response::allow(),
default => Response::denyAsNotFound(),
};
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
32 changes: 32 additions & 0 deletions app/Http/Requests/DeleteApiBookmarkRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class DeleteApiBookmarkRequest extends FormRequest
{
public function authorize(): Response
{
$user = $this->user();
assert($user instanceof User);

$commit = $this->route('commit');
assert($commit instanceof Commit);

return $commit->isVisibleTo($user) ? Response::allow() : Response::denyAsNotFound();
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
35 changes: 35 additions & 0 deletions app/Http/Requests/DeleteApiCommitStarRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace App\Http\Requests;

use App\Models\Commit;
use App\Models\User;
use Illuminate\Auth\Access\Response;
use Illuminate\Foundation\Http\FormRequest;

final class DeleteApiCommitStarRequest extends FormRequest
{
/**
* The web lets the user unstar any commit, but the API answers with the commit, so it has to be one they may see.
*/
public function authorize(): Response
{
$user = $this->user();
assert($user instanceof User);

$commit = $this->route('commit');
assert($commit instanceof Commit);

return $commit->isVisibleTo($user) ? Response::allow() : Response::denyAsNotFound();
}

/**
* @return array{}
*/
public function rules(): array
{
return [];
}
}
19 changes: 19 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

declare(strict_types=1);

use App\Http\Controllers\Api\BookmarkController;
use App\Http\Controllers\Api\CommitController as ApiCommitController;
use App\Http\Controllers\Api\CommitReplyController;
use App\Http\Controllers\Api\CommitStarController;
use App\Http\Controllers\Api\CurrentUserController;
use App\Http\Controllers\Api\DeviceController;
use App\Http\Controllers\Api\LogController;
Expand Down Expand Up @@ -63,6 +65,23 @@
->withTrashed()
->name('api-commit-reply.index');

// Private saved commits...
Route::get('saved', [BookmarkController::class, 'index'])->name('api-bookmark.index');
Route::put('commits/{commit}/bookmark', [BookmarkController::class, 'store'])
->middleware('throttle:bookmarks')
->name('api-bookmark.store');
Route::delete('commits/{commit}/bookmark', [BookmarkController::class, 'destroy'])
->middleware('throttle:bookmarks')
->name('api-bookmark.destroy');

// Commit Star...
Route::post('commits/{commit}/star', [CommitStarController::class, 'store'])
->middleware('throttle:stars')
->name('api-commit-star.store');
Route::delete('commits/{commit}/star', [CommitStarController::class, 'destroy'])
->middleware('throttle:stars')
->name('api-commit-star.destroy');

// User Follow...
Route::post('users/{user:username}/follow', [UserFollowController::class, 'store'])
->middleware('throttle:follows')
Expand Down
Loading
Loading