From 58acc499b97c41f0acf44e3f12c1253e66363897 Mon Sep 17 00:00:00 2001 From: Shane Smith Date: Thu, 19 Nov 2026 21:50:26 +1030 Subject: [PATCH] feat: tell API clients which guild made a commit appear in Following The signed Following log now gives each commit a joined_guild: the handle of the joined guild that brought that commit there, or null. This matches how the web's "because you joined" message works. Trending, Latest, suggestions and nested commits are unchanged. --- KNOWLEDGE_BASE.md | 1 + app/Http/Controllers/Api/LogController.php | 14 ++- app/Http/Resources/CommitResource.php | 4 +- .../Controllers/Api/LogControllerTest.php | 96 +++++++++++++++++++ 4 files changed, 112 insertions(+), 3 deletions(-) diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index 1073cf7f31..c13b828d10 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -596,6 +596,7 @@ A signed request sees what the web shows you, such as your own pending commits a - read the hot topics, and each one's commits from the past week with its recap; - list guilds as the Guilds page does, 24 a page, searched by name, handle or description, with whether you joined or muted each one and whether it is paused, while guilds are available to you; - read a guild's board as its page does, 30 threads a page with reply previews, with the guild, its pinned commit and whether you moderate it, while guilds are available to you. The pinned commit also keeps its place in the list, so an app that shows it apart skips it there by its id; +- read which joined guild brought each commit into your Following log, as the web's “because you joined” says; - star or unstar, and list who starred your own commit, newest first, 20 a page; - create polls on commits that are neither replies nor quotes, and vote on polls; - change Remotes settings, read and send messages with images, link cards, and voice notes in conversations and orgs, mark them pulled, react to messages and list your reactions, pin and unpin org messages as a maintainer, find people, and start orgs; diff --git a/app/Http/Controllers/Api/LogController.php b/app/Http/Controllers/Api/LogController.php index 98eb1deec4..aa1d2fd2fe 100644 --- a/app/Http/Controllers/Api/LogController.php +++ b/app/Http/Controllers/Api/LogController.php @@ -9,6 +9,7 @@ use App\Http\Resources\CommitResource; use App\Models\Commit; use App\Models\User; +use App\Queries\GuildCommitsQuery; use App\Queries\LatestLogCommitsQuery; use App\Queries\LogCommitsQuery; use App\Queries\ReplyPreviewsQuery; @@ -22,15 +23,24 @@ /** * Following, Latest or Trending, as the web shows it, 20 commits a page. With ?since=, it counts the commits newer * than that one, the way the web does: none on Trending, which is ranked rather than newest first. An empty - * Following log suggests a few commits, as on the web. + * Following log suggests a few commits, as on the web. On Following, each commit also says which joined guild + * brought it there, or null, which is what allows display of the "because you joined" message. */ - public function show(ShowLogRequest $request, #[CurrentUser] User $user, Log $log, LatestLogCommitsQuery $latest, SuggestedCommitsQuery $suggested, ReplyPreviewsQuery $replyPreviews): AnonymousResourceCollection + public function show(ShowLogRequest $request, #[CurrentUser] User $user, Log $log, LatestLogCommitsQuery $latest, SuggestedCommitsQuery $suggested, ReplyPreviewsQuery $replyPreviews, GuildCommitsQuery $guilds): AnonymousResourceCollection { $since = $request->since(); $commits = resolve(LogCommitsQuery::class)->paginate($log, $user, cursor: $request->cursor($log)); $replyPreviews->load($commits->items(), $user); + if ($log === Log::Following) { + $joined = $guilds->joinedFor($commits->items(), $user); + + foreach ($commits->items() as $commit) { + $commit->setAttribute('joined_guild', $joined[$commit->id] ?? null); + } + } + return CommitResource::collection($commits)->additional([ 'log' => $log, 'is_ranked' => $log->isRanked(), diff --git a/app/Http/Resources/CommitResource.php b/app/Http/Resources/CommitResource.php index 10929956e7..9065ddd477 100644 --- a/app/Http/Resources/CommitResource.php +++ b/app/Http/Resources/CommitResource.php @@ -25,7 +25,8 @@ * and how many pastéis it was given, which the app only shows in pastéis mode, as the web does. Like the web's cards, * it also has preview_segments, the excerpt a list shows of a long commit, the poll, with what the user may see of * its votes, and the commit it quotes, or null once the user may no longer see that one, while quoted_commit_id still - * says it quoted something. In a list, reply_preview is the newest reply or quote the web shows under it. As on the + * says it quoted something. In a list, reply_preview is the newest reply or quote the web shows under it, and on the + * Following log, joined_guild is the handle of the joined guild that alone brought the commit there, or null. As on the * web, the counts, link_preview, images, starred, parent, poll, quoted_commit and reply_preview are only * there when they were loaded, so ancestors and the commits of notifications come without some of them. The images of * a commit that isn't published yet have temporary URLs, since the app has no web session to load them with. For the @@ -88,6 +89,7 @@ public function toArray(Request $request): array 'status' => $this->status, 'is_pinned' => $this->author?->user?->pinned_commit_id === $this->id, 'starred' => $this->whenHas('starred'), + 'joined_guild' => $this->whenHas('joined_guild'), 'pasteis_count' => $this->pasteis_count, 'deleted_at' => null, 'parent' => $this->whenLoaded('parent', fn (Commit $parent): self => self::make($parent)), diff --git a/tests/Feature/Controllers/Api/LogControllerTest.php b/tests/Feature/Controllers/Api/LogControllerTest.php index f4e294b791..11367da9e2 100644 --- a/tests/Feature/Controllers/Api/LogControllerTest.php +++ b/tests/Feature/Controllers/Api/LogControllerTest.php @@ -3,14 +3,17 @@ declare(strict_types=1); use App\Actions\UpdateCommitHotScores; +use App\Enums\Feature; use App\Enums\LivePlatform; use App\Enums\Log; use App\Jobs\DetectCommitLanguage; use App\Models\Commit; use App\Models\CommitImage; +use App\Models\Guild; use App\Models\LinkPreview; use App\Models\User; use App\Services\CommitBodies; +use App\Services\Features; use Illuminate\Database\Eloquent\Factories\Sequence; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Queue; @@ -409,3 +412,96 @@ it('rejects requests that are not signed', function (): void { $this->getJson(route('api-log.show', 'latest'))->assertUnauthorized(); }); + +it('says which joined guild brought a commit into the following log, as the web does', function (): void { + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); + $rust = Guild::factory()->create(['handle' => 'rust']); + $rust->members()->attach($this->user, ['joined_at' => now()]); + $this->user->following()->attach($followed = User::factory()->create()); + + $board = Commit::factory()->for($rust)->create(['created_at' => now()->subMinutes(3)]); + $hashtag = Commit::factory()->create(['body' => 'Learning #RuSt', 'created_at' => now()->subMinutes(2)]); + $theirs = Commit::factory()->for($followed)->create(['body' => 'Shipping #rust', 'created_at' => now()->subMinute()]); + $mine = Commit::factory()->for($this->user)->create(['body' => 'My #rust']); + + $data = collect(signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk()->json('data'))->keyBy('id'); + + expect($data->keys()->all())->toBe([$mine->id, $theirs->id, $hashtag->id, $board->id]) + ->and($data[$board->id]['joined_guild'])->toBe('rust') + ->and($data[$hashtag->id]['joined_guild'])->toBe('rust') + ->and($data[$theirs->id])->toHaveKey('joined_guild', null) + ->and($data[$mine->id])->toHaveKey('joined_guild', null); +}); + +it('leaves a muted guild out of the following log, naming no guild on what is left', function (): void { + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); + $rust = Guild::factory()->create(['handle' => 'rust']); + $rust->members()->attach($this->user, ['joined_at' => now(), 'muted_at' => now()]); + Commit::factory()->for($rust)->create(); + Commit::factory()->create(['body' => 'Learning #rust']); + $mine = Commit::factory()->for($this->user)->create(['created_at' => now()->subMinute()]); + + $response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following')) + ->assertOk() + ->assertJsonPath('data.*.id', [$mine->id]); + + expect($response->json('data.0'))->toHaveKey('joined_guild', null); +}); + +it('names no joined guild on the trending and latest logs', function (string $log): void { + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); + Guild::factory()->create(['handle' => 'rust'])->members()->attach($this->user, ['joined_at' => now()]); + Commit::factory()->create(['body' => 'Learning #rust']); + + $response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', $log))->assertOk()->assertJsonCount(1, 'data'); + + expect($response->json('data.0'))->not->toHaveKey('joined_guild'); +})->with(['trending', 'latest']); + +it('names no joined guild once guilds are off', function (): void { + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); + $rust = Guild::factory()->create(['handle' => 'rust']); + $rust->members()->attach($this->user, ['joined_at' => now()]); + $this->user->following()->attach($followed = User::factory()->create()); + $theirs = Commit::factory()->for($followed)->create(['body' => 'Shipping #rust', 'created_at' => now()->subMinute()]); + $hashtag = Commit::factory()->create(['body' => 'Learning #rust']); + + signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following')) + ->assertOk() + ->assertJsonPath('data.*.id', [$hashtag->id, $theirs->id]) + ->assertJsonPath('data.0.joined_guild', 'rust'); + + resolve(Features::class)->update(Feature::Guilds, false); + + $response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following')) + ->assertOk() + ->assertJsonPath('data.*.id', [$theirs->id]); + + expect($response->json('data.0'))->toHaveKey('joined_guild', null); +}); + +it('does not query each commit or guild on the following log', function (): void { + Queue::fake([DetectCommitLanguage::class]); + resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false); + $rust = Guild::factory()->create(['handle' => 'rust']); + $rust->members()->attach($this->user, ['joined_at' => now()]); + signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk(); + + $queries = function (): int { + DB::flushQueryLog(); + DB::enableQueryLog(); + + signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk(); + + return count(DB::getQueryLog()); + }; + + Commit::factory()->for($rust)->create(); + Commit::factory()->create(['body' => 'Learning #rust']); + $few = $queries(); + + Commit::factory()->for($rust)->count(4)->create(); + Commit::factory()->count(4)->create(['body' => 'Learning #rust']); + + expect($queries())->toBe($few); +});