Skip to content

Commit 682399f

Browse files
feat: let API clients ask for more reply previews (#1528)
* feat: let API clients ask for more reply previews * docs: describe replies_per_page on signed API lists
1 parent f445951 commit 682399f

3 files changed

Lines changed: 31 additions & 5 deletions

File tree

‎KNOWLEDGE_BASE.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,8 @@ A signed request sees what the web shows you, such as your own pending commits a
602602
- list your devices and revoke them;
603603
- delete your account.
604604

605+
Signed lists show each commit with its newest reply or quote. Add `?replies_per_page=` with 3, 5, 10, 15, 20, or 25 to show that many replies and quotes, like the replies choice in Settings → Appearance.
606+
605607
Signed request bodies are JSON, or multipart when they carry images or a voice note, so app commits can have images with descriptions or a poll but no link-card choice. Before your first app submission, accept the [commit rules](#moderation) through the web composer. Signed requests are limited to **120 requests per minute per user**. Settings → Devices lists your approved devices, with the app's name when it gave one and when each was last used, and revokes them.
606608

607609
Sources: [signed requests](app/Services/SignedRequests.php), [sign-in page](app/Http/Controllers/MobileAuthorizationController.php), [devices settings](resources/js/pages/device/index.tsx), [API routes](routes/api.php).

‎app/Enums/RepliesPerPage.php‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ enum RepliesPerPage: int
1717
case TwentyFive = 25;
1818

1919
/**
20-
* Kept in a cookie, like the theme, so guests can pick it too. API clients page on from the newest reply alone.
20+
* Kept in a cookie, like the theme, so guests can pick it too. API clients ask with ?replies_per_page= and
21+
* otherwise page on from the newest reply alone.
2122
*/
2223
public static function of(Request $request): self
2324
{
24-
if ($request->is('api/*')) {
25-
return self::One;
26-
}
25+
[$picked, $default] = $request->is('api/*')
26+
? [$request->query('replies_per_page'), self::One]
27+
: [$request->cookie('replies_per_page'), self::Three];
2728

28-
return array_find(self::cases(), fn (self $count): bool => (string) $count->value === $request->cookie('replies_per_page')) ?? self::Three;
29+
return array_find(self::cases(), fn (self $count): bool => (string) $count->value === $picked) ?? $default;
2930
}
3031
}

‎tests/Feature/Controllers/Api/LogControllerTest.php‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,29 @@
233233
->assertJsonPath('meta.next_cursor', null);
234234
});
235235

236+
it('shows as many reply previews as the client asks for, newest first', function (): void {
237+
$commit = Commit::factory()->create(['created_at' => now()->subHour()]);
238+
$replies = Commit::factory()->for($commit, 'parent')->count(4)
239+
->state(new Sequence(fn (Sequence $sequence): array => ['created_at' => now()->subMinutes(10 - $sequence->index)]))
240+
->create();
241+
242+
$response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', ['latest', 'replies_per_page' => 3]));
243+
244+
$response->assertOk()
245+
->assertJsonPath('data.0.reply_preview.data.*.id', [$replies[3]->id, $replies[2]->id, $replies[1]->id]);
246+
247+
expect($response->json('data.0.reply_preview.next_cursor'))->toBeString();
248+
});
249+
250+
it('shows one reply preview when the client asks for a count the web does not offer', function (string $count): void {
251+
$commit = Commit::factory()->create(['created_at' => now()->subHour()]);
252+
Commit::factory()->for($commit, 'parent')->count(4)->create();
253+
254+
$response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', ['latest', 'replies_per_page' => $count]));
255+
256+
$response->assertOk()->assertJsonCount(1, 'data.0.reply_preview.data');
257+
})->with(['7', '3abc']);
258+
236259
it('does not query each commit or author', function (): void {
237260
Queue::fake([DetectCommitLanguage::class]);
238261
signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'latest'))->assertOk();

0 commit comments

Comments
 (0)