Skip to content

Commit 90094ce

Browse files
committed
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.
1 parent 6d3a957 commit 90094ce

4 files changed

Lines changed: 112 additions & 3 deletions

File tree

‎KNOWLEDGE_BASE.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ A signed request sees what the web shows you, such as your own pending commits a
595595
- read who to follow, also as it is suggested beside a profile or a commit, and mark a suggestion not interested;
596596
- read the hot topics, and each one's commits from the past week with its recap;
597597
- 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;
598+
- read which joined guild brought each commit into your Following log, as the web's “because you joined” says;
598599
- star or unstar, and list who starred your own commit, newest first, 20 a page;
599600
- create polls on commits that are neither replies nor quotes, and vote on polls;
600601
- 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;

‎app/Http/Controllers/Api/LogController.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Http\Resources\CommitResource;
1010
use App\Models\Commit;
1111
use App\Models\User;
12+
use App\Queries\GuildCommitsQuery;
1213
use App\Queries\LatestLogCommitsQuery;
1314
use App\Queries\LogCommitsQuery;
1415
use App\Queries\ReplyPreviewsQuery;
@@ -22,15 +23,24 @@
2223
/**
2324
* Following, Latest or Trending, as the web shows it, 20 commits a page. With ?since=, it counts the commits newer
2425
* than that one, the way the web does: none on Trending, which is ranked rather than newest first. An empty
25-
* Following log suggests a few commits, as on the web.
26+
* Following log suggests a few commits, as on the web. On Following, each commit also says which joined guild
27+
* brought it there, or null, which is what allows display of the "because you joined" message.
2628
*/
27-
public function show(ShowLogRequest $request, #[CurrentUser] User $user, Log $log, LatestLogCommitsQuery $latest, SuggestedCommitsQuery $suggested, ReplyPreviewsQuery $replyPreviews): AnonymousResourceCollection
29+
public function show(ShowLogRequest $request, #[CurrentUser] User $user, Log $log, LatestLogCommitsQuery $latest, SuggestedCommitsQuery $suggested, ReplyPreviewsQuery $replyPreviews, GuildCommitsQuery $guilds): AnonymousResourceCollection
2830
{
2931
$since = $request->since();
3032
$commits = resolve(LogCommitsQuery::class)->paginate($log, $user, cursor: $request->cursor($log));
3133

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

36+
if ($log === Log::Following) {
37+
$joined = $guilds->joinedFor($commits->items(), $user);
38+
39+
foreach ($commits->items() as $commit) {
40+
$commit->setAttribute('joined_guild', $joined[$commit->id] ?? null);
41+
}
42+
}
43+
3444
return CommitResource::collection($commits)->additional([
3545
'log' => $log,
3646
'is_ranked' => $log->isRanked(),

‎app/Http/Resources/CommitResource.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
* 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,
2626
* it also has preview_segments, the excerpt a list shows of a long commit, the poll, with what the user may see of
2727
* its votes, and the commit it quotes, or null once the user may no longer see that one, while quoted_commit_id still
28-
* says it quoted something. In a list, reply_preview is the newest reply or quote the web shows under it. As on the
28+
* says it quoted something. In a list, reply_preview is the newest reply or quote the web shows under it, and on the
29+
* Following log, joined_guild is the handle of the joined guild that alone brought the commit there, or null. As on the
2930
* web, the counts, link_preview, images, starred, parent, poll, quoted_commit and reply_preview are only
3031
* there when they were loaded, so ancestors and the commits of notifications come without some of them. The images of
3132
* a commit that isn't published yet have temporary URLs, since the app has no web session to load them with.
@@ -87,6 +88,7 @@ public function toArray(Request $request): array
8788
'status' => $this->status,
8889
'is_pinned' => $this->author?->user?->pinned_commit_id === $this->id,
8990
'starred' => $this->whenHas('starred'),
91+
'joined_guild' => $this->whenHas('joined_guild'),
9092
'pasteis_count' => $this->pasteis_count,
9193
'deleted_at' => null,
9294
'parent' => $this->whenLoaded('parent', fn (Commit $parent): self => self::make($parent)),

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

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
declare(strict_types=1);
44

55
use App\Actions\UpdateCommitHotScores;
6+
use App\Enums\Feature;
67
use App\Enums\LivePlatform;
78
use App\Enums\Log;
89
use App\Jobs\DetectCommitLanguage;
910
use App\Models\Commit;
1011
use App\Models\CommitImage;
12+
use App\Models\Guild;
1113
use App\Models\LinkPreview;
1214
use App\Models\User;
1315
use App\Services\CommitBodies;
16+
use App\Services\Features;
1417
use Illuminate\Database\Eloquent\Factories\Sequence;
1518
use Illuminate\Support\Facades\DB;
1619
use Illuminate\Support\Facades\Queue;
@@ -409,3 +412,96 @@
409412
it('rejects requests that are not signed', function (): void {
410413
$this->getJson(route('api-log.show', 'latest'))->assertUnauthorized();
411414
});
415+
416+
it('says which joined guild brought a commit into the following log, as the web does', function (): void {
417+
resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false);
418+
$rust = Guild::factory()->create(['handle' => 'rust']);
419+
$rust->members()->attach($this->user, ['joined_at' => now()]);
420+
$this->user->following()->attach($followed = User::factory()->create());
421+
422+
$board = Commit::factory()->for($rust)->create(['created_at' => now()->subMinutes(3)]);
423+
$hashtag = Commit::factory()->create(['body' => 'Learning #RuSt', 'created_at' => now()->subMinutes(2)]);
424+
$theirs = Commit::factory()->for($followed)->create(['body' => 'Shipping #rust', 'created_at' => now()->subMinute()]);
425+
$mine = Commit::factory()->for($this->user)->create(['body' => 'My #rust']);
426+
427+
$data = collect(signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk()->json('data'))->keyBy('id');
428+
429+
expect($data->keys()->all())->toBe([$mine->id, $theirs->id, $hashtag->id, $board->id])
430+
->and($data[$board->id]['joined_guild'])->toBe('rust')
431+
->and($data[$hashtag->id]['joined_guild'])->toBe('rust')
432+
->and($data[$theirs->id])->toHaveKey('joined_guild', null)
433+
->and($data[$mine->id])->toHaveKey('joined_guild', null);
434+
});
435+
436+
it('leaves a muted guild out of the following log, naming no guild on what is left', function (): void {
437+
resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false);
438+
$rust = Guild::factory()->create(['handle' => 'rust']);
439+
$rust->members()->attach($this->user, ['joined_at' => now(), 'muted_at' => now()]);
440+
Commit::factory()->for($rust)->create();
441+
Commit::factory()->create(['body' => 'Learning #rust']);
442+
$mine = Commit::factory()->for($this->user)->create(['created_at' => now()->subMinute()]);
443+
444+
$response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))
445+
->assertOk()
446+
->assertJsonPath('data.*.id', [$mine->id]);
447+
448+
expect($response->json('data.0'))->toHaveKey('joined_guild', null);
449+
});
450+
451+
it('names no joined guild on the trending and latest logs', function (string $log): void {
452+
resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false);
453+
Guild::factory()->create(['handle' => 'rust'])->members()->attach($this->user, ['joined_at' => now()]);
454+
Commit::factory()->create(['body' => 'Learning #rust']);
455+
456+
$response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', $log))->assertOk()->assertJsonCount(1, 'data');
457+
458+
expect($response->json('data.0'))->not->toHaveKey('joined_guild');
459+
})->with(['trending', 'latest']);
460+
461+
it('names no joined guild once guilds are off', function (): void {
462+
resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false);
463+
$rust = Guild::factory()->create(['handle' => 'rust']);
464+
$rust->members()->attach($this->user, ['joined_at' => now()]);
465+
$this->user->following()->attach($followed = User::factory()->create());
466+
$theirs = Commit::factory()->for($followed)->create(['body' => 'Shipping #rust', 'created_at' => now()->subMinute()]);
467+
$hashtag = Commit::factory()->create(['body' => 'Learning #rust']);
468+
469+
signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))
470+
->assertOk()
471+
->assertJsonPath('data.*.id', [$hashtag->id, $theirs->id])
472+
->assertJsonPath('data.0.joined_guild', 'rust');
473+
474+
resolve(Features::class)->update(Feature::Guilds, false);
475+
476+
$response = signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))
477+
->assertOk()
478+
->assertJsonPath('data.*.id', [$theirs->id]);
479+
480+
expect($response->json('data.0'))->toHaveKey('joined_guild', null);
481+
});
482+
483+
it('does not query each commit or guild on the following log', function (): void {
484+
Queue::fake([DetectCommitLanguage::class]);
485+
resolve(Features::class)->update(Feature::Guilds, true, adminOnly: false);
486+
$rust = Guild::factory()->create(['handle' => 'rust']);
487+
$rust->members()->attach($this->user, ['joined_at' => now()]);
488+
signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk();
489+
490+
$queries = function (): int {
491+
DB::flushQueryLog();
492+
DB::enableQueryLog();
493+
494+
signedApiRequest($this->keyPair, 'GET', route('api-log.show', 'following'))->assertOk();
495+
496+
return count(DB::getQueryLog());
497+
};
498+
499+
Commit::factory()->for($rust)->create();
500+
Commit::factory()->create(['body' => 'Learning #rust']);
501+
$few = $queries();
502+
503+
Commit::factory()->for($rust)->count(4)->create();
504+
Commit::factory()->count(4)->create(['body' => 'Learning #rust']);
505+
506+
expect($queries())->toBe($few);
507+
});

0 commit comments

Comments
 (0)