From 7d648222a31a5ad4e6a457696438ba766cfa5958 Mon Sep 17 00:00:00 2001 From: Shane Smith Date: Tue, 24 Nov 2026 01:12:38 +1030 Subject: [PATCH 1/2] [Guilds] Join and leave a guild, and see its members The guild page's header now has Join or Leave beside the icon, as a profile has Follow, with Commit (posting to the board) where Remote sits. Share stays alone in the top bar. Joining or leaving changes the button and the member count at once, puts them back if devnull refuses, and unmutes a guild you join, as devnull does. Tapping the member count opens the guild's members, as the web lists them: avatar, name, handle and bio, with "(moderator)" on the moderator's row while they're a member, and no Follow buttons. --- app/Devnull/Client.php | 24 ++ app/Devnull/Data/Guild.php | 15 ++ app/Devnull/Data/GuildMembers.php | 28 +++ app/Devnull/ResponseCache.php | 35 +++ app/NativeComponents/Guild.php | 62 +++-- app/NativeComponents/GuildMembers.php | 232 ++++++++++++++++++ .../views/native/guild-members.blade.php | 28 +++ .../native/partials/connection.blade.php | 18 +- .../native/partials/guild-header.blade.php | 28 ++- routes/mobile.php | 2 + tests/Feature/Screens/GuildMembersTest.php | 124 ++++++++++ tests/Feature/Screens/GuildPostTest.php | 20 +- tests/Feature/Screens/GuildTest.php | 88 +++++++ tests/Pest.php | 22 ++ tests/Unit/Devnull/GuildMembersTest.php | 22 ++ 15 files changed, 708 insertions(+), 40 deletions(-) create mode 100644 app/Devnull/Data/GuildMembers.php create mode 100644 app/NativeComponents/GuildMembers.php create mode 100644 resources/views/native/guild-members.blade.php create mode 100644 tests/Feature/Screens/GuildMembersTest.php create mode 100644 tests/Unit/Devnull/GuildMembersTest.php diff --git a/app/Devnull/Client.php b/app/Devnull/Client.php index a5dab4bb..741434bc 100644 --- a/app/Devnull/Client.php +++ b/app/Devnull/Client.php @@ -9,6 +9,7 @@ use App\Devnull\Data\DraftImage; use App\Devnull\Data\Guild; use App\Devnull\Data\GuildBoard; +use App\Devnull\Data\GuildMembers; use App\Devnull\Data\IncomingCall; use App\Devnull\Data\MessageVoice; use App\Devnull\Data\Notification; @@ -483,6 +484,29 @@ public function guild(string $handle, ?string $page = null): ?GuildBoard return GuildBoard::fromArray($this->get('guilds/'.rawurlencode($handle), ['page' => $page])); } + /** + * Who's in a guild, 50 a page, and who moderates it. + */ + public function guildMembers(string $handle, ?string $page = null): GuildMembers + { + return GuildMembers::fromArray($this->get('guilds/'.rawurlencode($handle).'/members', ['page' => $page])); + } + + /** + * Joins a guild, as the web's Join does. Joining one you muted unmutes it. + */ + public function joinGuild(string $handle): void + { + $this->send('POST', 'guilds/'.rawurlencode($handle).'/membership'); + $this->cache->guildMembershipChanged(rawurlencode($handle), true); + } + + public function leaveGuild(string $handle): void + { + $this->send('DELETE', 'guilds/'.rawurlencode($handle).'/membership'); + $this->cache->guildMembershipChanged(rawurlencode($handle), false); + } + /** * A page of your Remotes inbox, newest first, for the web's Open, Fetched * or Stashed tab, continuing before a conversation. diff --git a/app/Devnull/Data/Guild.php b/app/Devnull/Data/Guild.php index 48afbad8..1ce489a3 100644 --- a/app/Devnull/Data/Guild.php +++ b/app/Devnull/Data/Guild.php @@ -72,6 +72,21 @@ public function hashtag(): string return '#'.$this->handle; } + /** + * The guild once you've joined or left it: its members counted to match, and unmuted by joining, as devnull does. + */ + public function withJoined(bool $isJoined): self + { + $change = $isJoined === $this->isJoined ? 0 : ($isJoined ? 1 : -1); + + return new self(...[ + ...get_object_vars($this), + 'memberCount' => max(0, $this->memberCount + $change), + 'isJoined' => $isJoined, + 'isMuted' => $isJoined ? false : $this->isMuted, + ]); + } + /** * What an image element gets for the icon, or null when it can't be drawn: * there is no icon, or its signed link has expired and no copy was diff --git a/app/Devnull/Data/GuildMembers.php b/app/Devnull/Data/GuildMembers.php new file mode 100644 index 00000000..4ff12488 --- /dev/null +++ b/app/Devnull/Data/GuildMembers.php @@ -0,0 +1,28 @@ + $people + */ + public function __construct(public Page $people, public ?string $moderatorId) {} + + /** + * @param array $json + */ + public static function fromArray(array $json): self + { + $moderatorId = $json['moderator_id'] ?? null; + + return new self( + people: Page::fromArray($json, Connection::fromArray(...)), + moderatorId: is_string($moderatorId) ? $moderatorId : null, + ); + } +} diff --git a/app/Devnull/ResponseCache.php b/app/Devnull/ResponseCache.php index 3bc8e7be..18355ec4 100644 --- a/app/Devnull/ResponseCache.php +++ b/app/Devnull/ResponseCache.php @@ -55,6 +55,7 @@ final class ResponseCache '#^scheduled-commits$#', '#^guilds$#', '#^guilds/[^/]+$#', + '#^guilds/[^/]+/members$#', ]; /** @@ -514,6 +515,40 @@ public function organizationFollowChanged(string $handle, bool $following): void } } + /** + * You joined or left a guild: wherever it's kept, on its page or in the directory, it says so, with its members + * counted to match, and joining unmutes it, as devnull does. Its kept members go, as you came or went there. + */ + public function guildMembershipChanged(string $handle, bool $joined): void + { + $change = function (array $guild) use ($handle, $joined): array { + if (($guild['handle'] ?? null) !== rawurldecode($handle) || ($guild['is_joined'] ?? false) === $joined) { + return $guild; + } + + return [ + ...$guild, + 'is_joined' => $joined, + 'member_count' => max(0, (int) ($guild['member_count'] ?? 0) + ($joined ? 1 : -1)), + ...($joined ? ['is_muted' => false] : []), + ]; + }; + + $this->patch("guilds/{$handle}", [], fn (array $json): array => is_array($json['guild'] ?? null) ? [...$json, 'guild' => $change($json['guild'])] : $json); + + if (($userId = $this->userId()) !== null) { + foreach ($this->index($userId) as $meta) { + if ($meta['path'] === 'guilds') { + $this->patch('guilds', $meta['query'], fn (array $json): array => is_array($json['data'] ?? null) + ? [...$json, 'data' => array_map(fn (mixed $guild): mixed => is_array($guild) ? $change($guild) : $guild, $json['data'])] + : $json); + } + } + } + + $this->forget("guilds/{$handle}/members"); + } + /** * Marks someone followed, or not, wherever a kept list of people says whether you follow them. */ diff --git a/app/NativeComponents/Guild.php b/app/NativeComponents/Guild.php index d1497565..11f17339 100644 --- a/app/NativeComponents/Guild.php +++ b/app/NativeComponents/Guild.php @@ -32,10 +32,10 @@ /** * A guild's page, as the web shows it at "/guilds/{handle}": its icon or - * emoji, its name, "#handle · Public guild · N members", what it's about and - * how to post there, then its bulletin board, the commits posted in it or - * tagged with its hashtag, newest first, with the one its moderator pinned on - * top. + * emoji beside Commit and Join or Leave, its name, "#handle · Public guild · + * N members" (the count opens its members), what it's about and how to post + * there, then its bulletin board, the commits posted in it or tagged with its + * hashtag, newest first, with the one its moderator pinned on top. * * It opens from the directory, or a commit's "posted in" pill, beneath the tab * it was tapped in (see OpensGuilds), on the board as it was kept last time, @@ -44,7 +44,7 @@ * so a page loaded later can repeat a commit already showing: those go once, * as does the pinned commit, which devnull also sends in its place. * - * The bar's New commit posts to the board, as the web's guild page does, + * The header's Commit posts to the board, as the web's guild page does, * from the compose sheet (see OpensCommitSheet::composeInGuildSheet()), while * the guild isn't paused. The commit shows on top of the board, staged, until * devnull has it, as a new commit does on the Log. If devnull turns it down, @@ -119,36 +119,60 @@ public function navTitle(): string } /** - * Share, then New commit while the guild takes posts, as the Log has it. + * Share. Commit and Join are in the header, as Remote and Follow are on a profile. */ public function navigationOptions(): ?NavBarOptions { - $options = NavBarOptions::make()->action( + return NavBarOptions::make()->action( NavAction::make('share') ->icon(ios: Lucide::Share->ios(), android: Lucide::Share2->android()) ->a11yLabel('Share guild') ->press('share'), ); + } + /** + * The header's Commit: the sheet, for a commit on this guild's board. + */ + public function composeForGuild(): void + { if ($this->takesPosts()) { - $options->action( - NavAction::make('compose') - ->icon(ios: Lucide::SquarePen->ios(), android: Lucide::SquarePen->android()) - ->a11yLabel('New commit') - ->press('composeForGuild'), - ); + $this->composeInGuildSheet($this->handle()); + } + } + + /** + * The header's Join or Leave. It changes at once, with the member count, and goes back if devnull refuses. + */ + public function toggleJoined(): void + { + if ($this->guild === null) { + return; } - return $options; + $before = $this->guild; + $this->guild = $before->withJoined(! $before->isJoined); + + $done = $this->attempt(function (Client $devnull) use ($before): bool { + $before->isJoined + ? $devnull->leaveGuild($before->handle) + : $devnull->joinGuild($before->handle); + + return true; + }); + + if ($done !== true) { + $this->guild = $before; + } } /** - * The bar's New commit: the sheet, for a commit on this guild's board. + * The member count: who's in the guild, titled with its name. */ - public function composeForGuild(): void + public function openMembers(): void { - if ($this->takesPosts()) { - $this->composeInGuildSheet($this->handle()); + if ($this->guild !== null) { + $this->openInTab('/guilds/'.rawurlencode($this->guild->handle).'/members', ['guildName' => $this->guild->name]); } } @@ -261,7 +285,7 @@ protected function showCommitTurnedDown(PendingWrite $write): void } /** - * The bar's New commit is this page's, so a post to this board devnull turned down opens here again. + * The header's Commit is this page's, so a post to this board devnull turned down opens here again. */ protected function opensComposer(): bool { diff --git a/app/NativeComponents/GuildMembers.php b/app/NativeComponents/GuildMembers.php new file mode 100644 index 00000000..ef8ae131 --- /dev/null +++ b/app/NativeComponents/GuildMembers.php @@ -0,0 +1,232 @@ + */ + public array $people = []; + + public ?string $nextCursor = null; + + /** + * The id of the person moderating the guild. A paused guild has none. + */ + public ?string $moderatorId = null; + + /** + * Whether the list has had people to show, kept or fresh. Until then it shows skeleton rows. + */ + public bool $loaded = false; + + /** + * devnull has no guild by this handle, though it has guilds. + */ + public bool $missing = false; + + /** + * Guilds are switched off for you: devnull answered the directory with a 404 too. + */ + public bool $unavailable = false; + + public function mount(): void + { + $passed = $this->data('guildName'); + $this->guildName = is_string($passed) && $passed !== '' ? $passed : $this->keptGuildName(); + + $this->showKept(); + $this->revalidate(); + } + + public function onResume(): void + { + $this->forgetRefreshes(); + $this->showKept(); + $this->revalidate(); + } + + public function navTitle(): string + { + return $this->guildName ?? '#'.$this->handle(); + } + + public function navigationOptions(): ?NavBarOptions + { + return NavBarOptions::make()->subtitle('Members'); + } + + /** + * Pull to refresh. + */ + public function load(): void + { + $this->refresh($this->listPath(), [], $this->takeFresh(...), again: true); + } + + /** + * The next page, less the people already showing. + */ + public function loadMore(): void + { + if ($this->nextCursor === null) { + return; + } + + $members = $this->attempt(fn (Client $devnull): MembersPage => $devnull->guildMembers($this->handle(), $this->nextCursor)); + + if ($members === null) { + return; + } + + $shown = array_flip(array_map(fn (Connection $person): string => $person->user->id, $this->people)); + $this->people = [...$this->people, ...array_values(array_filter($members->people->items, fn (Connection $person): bool => ! isset($shown[$person->user->id])))]; + $this->nextCursor = $members->people->nextCursor; + $this->moderatorId = $members->moderatorId; + } + + public function open(string $username): void + { + foreach ($this->people as $person) { + if ($person->user->username === $username) { + $this->openInTab('/users/'.rawurlencode($username), ['user' => $person->user]); + + return; + } + } + } + + protected function revalidate(): void + { + $this->refresh($this->listPath(), [], $this->takeFresh(...)); + } + + /** + * A 404 for the members is a guild devnull no longer has, unless it has no guilds for you at all, as on the + * guild's page (see Guild::refused): only the directory can tell, so it's asked. + */ + protected function refused(ApiException $exception, string $path): bool + { + if ($path === 'guilds' && $exception->status === 404) { + $this->unavailable = true; + app(Account::class)->rememberGuildsAvailable(false); + + return true; + } + + if ($path !== $this->listPath() || $exception->status !== 404) { + return false; + } + + $this->people = []; + $this->nextCursor = null; + $this->moderatorId = null; + + $this->refresh('guilds', [], function (): void { + $this->missing = true; + app(Account::class)->rememberGuildsAvailable(true); + }); + + return true; + } + + protected function showsContent(): bool + { + return $this->loaded; + } + + private function showKept(): void + { + if (($json = $this->cached($this->listPath())) !== null) { + $this->takeFresh($json); + } + } + + /** + * The fresh first page over what's showing: new people go on top, people + * no longer in it go, and pages loaded further down stay. + * + * @param array $json + */ + private function takeFresh(array $json): void + { + $members = MembersPage::fromArray($json); + + $fresh = $members->people->hasMore() + ? FreshPage::over($this->people, $this->nextCursor, $members->people)->withNewer() + : new FreshPage($members->people->items, null); + + $this->people = $fresh->items; + $this->nextCursor = $fresh->nextCursor; + $this->moderatorId = $members->moderatorId; + $this->loaded = true; + $this->missing = false; + $this->unavailable = false; + } + + /** + * The name on the guild's own page, as last kept, for a title when none was passed. + */ + private function keptGuildName(): ?string + { + $json = $this->cached('guilds/'.rawurlencode($this->handle())); + $name = $json === null ? null : GuildBoard::fromArray($json)?->guild->name; + + return $name === '' ? null : $name; + } + + private function listPath(): string + { + return 'guilds/'.rawurlencode($this->handle()).'/members'; + } + + private function handle(): string + { + return (string) $this->param('handle'); + } + + public function render(): View + { + return view('native.guild-members', [ + 'handle' => $this->handle(), + 'viewer' => $this->me(), + 'moderatorId' => $this->moderatorId, + ]); + } +} diff --git a/resources/views/native/guild-members.blade.php b/resources/views/native/guild-members.blade.php new file mode 100644 index 00000000..75df6c2a --- /dev/null +++ b/resources/views/native/guild-members.blade.php @@ -0,0 +1,28 @@ + + @include('native.partials.error') + @include('native.partials.refresh-failed') + + + @if ($unavailable) + @include('native.partials.empty', ['command' => 'fatal: guilds are not available', 'message' => "Guilds aren't open to you yet."]) + @elseif ($missing) + @include('native.partials.empty', ['command' => 'fatal: no such guild #'.$handle, 'message' => 'This guild is no longer available.']) + @elseif (! $loaded) + @if ($error === null) + @foreach (range(1, 6) as $skeleton) + @include('native.partials.connection-loading', ['ref' => 'loading-'.$skeleton, 'label' => 'Loading members']) + @endforeach + @endif + @elseif ($people === []) + @include('native.partials.empty', ['command' => 'fatal: no members yet', 'message' => 'Nobody has joined this guild yet.']) + @else + @foreach ($people as $person) + @include('native.partials.connection', ['person' => $person, 'isMe' => false, 'showsFollow' => false, 'showsFollowsYou' => false, 'isModerator' => $moderatorId !== null && $person->user->id === $moderatorId, 'myColour' => null]) + @endforeach + + @if ($nextCursor !== null) + @include('native.partials.connection-loading', ['ref' => 'loading-more', 'label' => 'Loading more people']) + @endif + @endif + + diff --git a/resources/views/native/partials/connection.blade.php b/resources/views/native/partials/connection.blade.php index a6307f22..ae8e4557 100644 --- a/resources/views/native/partials/connection.blade.php +++ b/resources/views/native/partials/connection.blade.php @@ -4,10 +4,15 @@ up to two lines of bio. Follow sits on the right, a fixed width so Following takes the same room, except on your own row. A tap anywhere else opens them. + Every list passes $showsFollowsYou, false where Follows you says nothing new (your own followers) or the web leaves + it off (a guild's members). A list with no Follow (the guild's members, as the web draws them) passes + $showsFollow = false, and the moderator's row passes $isModerator, which puts "(moderator)" after the name and + ", moderator" on what screen readers say. + No press-* feedback: on iOS it holds any drag that starts on the row, and these rows cover the whole list. --}} @php($user = $person->user) - + @@ -16,7 +21,14 @@ - {{ $user->name }} + @if ($isModerator ?? false) + + {{ $user->name }} + (moderator) + + @else + {{ $user->name }} + @endif {{-- EDGE's rows don't wrap on iOS, so the handle gives way to the badge. --}} {{ $user->handle() }} @@ -31,7 +43,7 @@ @endif - @if (! $isMe) + @if (! $isMe && ($showsFollow ?? true)) {{-- A button sizes to its label, so it fills a slot of its own. --}} {{ $person->isFollowed ? 'Following' : 'Follow' }} diff --git a/resources/views/native/partials/guild-header.blade.php b/resources/views/native/partials/guild-header.blade.php index 6ea637d7..e0102f81 100644 --- a/resources/views/native/partials/guild-header.blade.php +++ b/resources/views/native/partials/guild-header.blade.php @@ -1,14 +1,28 @@ {{-- A guild's header, at the top of its board (see guild.blade.php), in the web's words: its icon or emoji (see - guild-mark), its name, "#handle · Public guild · N members", what it's about, then how to post there, or that it's - paused while it has no moderator. + guild-mark) with Commit and Join or Leave beside it, as a profile has Remote and Follow, its name, + "#handle · Public guild · N members", the count opening its members, what it's about, then how to post there, or + that it's paused while it has no moderator. Commit, which posts to the board, shows while the guild takes posts. --}} @php($members = \App\Devnull\Display::count($guild->memberCount, 'member', 'members')) - - @include('native.partials.guild-mark', ['guild' => $guild, 'size' => 64, 'ref' => 'guild-header-mark']) - + + + @include('native.partials.guild-mark', ['guild' => $guild, 'size' => 64, 'ref' => 'guild-header-mark']) + + + + @unless ($guild->isPaused) + Commit + @endunless + @if ($guild->isJoined) + Leave + @else + Join + @endif + + {{ $guild->name }} @@ -17,7 +31,9 @@ · {{ $guild->isPaused ? 'Public guild · Paused' : 'Public guild' }} · - {{ $members }} + + {{ $members }} + diff --git a/routes/mobile.php b/routes/mobile.php index a0c78122..274e9dc2 100644 --- a/routes/mobile.php +++ b/routes/mobile.php @@ -9,6 +9,7 @@ use App\NativeComponents\Connections; use App\NativeComponents\Conversation; use App\NativeComponents\Guild; +use App\NativeComponents\GuildMembers; use App\NativeComponents\Guilds; use App\NativeComponents\Launch; use App\NativeComponents\Layouts\TabsLayout; @@ -74,5 +75,6 @@ Route::native("{$tab}/search", Search::class); Route::native("{$tab}/guilds", Guilds::class); Route::native("{$tab}/guilds/{handle}", Guild::class); + Route::native("{$tab}/guilds/{handle}/members", GuildMembers::class); } }); diff --git a/tests/Feature/Screens/GuildMembersTest.php b/tests/Feature/Screens/GuildMembersTest.php new file mode 100644 index 00000000..f437ed4e --- /dev/null +++ b/tests/Feature/Screens/GuildMembersTest.php @@ -0,0 +1,124 @@ + 'http://devnull.test']); + signIn(); +}); + +it('lists the members with their name, handle and bio, and marks the moderator', function () { + fakeApi([ + 'GET /api/v1/guilds/rust/members' => [guildMembersJson([ + 'grace' => ['bio' => 'Found the first bug.', 'follows_you' => true], + 'linus' => [], + ], moderatorId: 'id-grace')], + ]); + + $screen = Native::visit('/log/guilds/rust/members', ['guildName' => 'Rustaceans']) + ->assertSee('Grace') + ->assertSee('@grace') + ->assertSee('Found the first bug.') + ->assertSee('Linus'); + + expect(treeShows(nodeByRef($screen, 'person-grace'), '(moderator)'))->toBeTrue() + ->and(treeShows(nodeByRef($screen, 'person-linus'), '(moderator)'))->toBeFalse() + // Follow and Follows you are both off here, as on the web, even for someone who follows you. + ->and(rendersRef($screen, 'follow-grace'))->toBeFalse() + ->and(rendersRef($screen, 'follow-linus'))->toBeFalse() + ->and(rendersRef($screen, 'follows-you-grace'))->toBeFalse() + ->and(treeShows(nodeByRef($screen, 'person-grace'), 'Follows you'))->toBeFalse(); +}); + +it('tells screen readers which member is the moderator', function () { + fakeApi([ + 'GET /api/v1/guilds/rust/members' => [guildMembersJson(['grace' => [], 'linus' => []], moderatorId: 'id-grace')], + ]); + + $screen = Native::visit('/log/guilds/rust/members'); + + expect(nodeByRef($screen, 'person-grace')['props']['a11y_label'])->toEndWith(', moderator') + ->and(nodeByRef($screen, 'person-linus')['props']['a11y_label'])->not->toEndWith(', moderator'); +}); + +it('marks nobody when the guild has no moderator', function () { + fakeApi(['GET /api/v1/guilds/rust/members' => [guildMembersJson(['grace' => []])]]); + + $screen = Native::visit('/log/guilds/rust/members')->assertSee('Grace'); + + expect(treeShows(nodeByRef($screen, 'person-grace'), '(moderator)'))->toBeFalse(); +}); + +it('marks nobody when the moderator has left the guild', function () { + fakeApi(['GET /api/v1/guilds/rust/members' => [guildMembersJson(['grace' => [], 'linus' => []], moderatorId: 'id-ada')]]); + + $screen = Native::visit('/log/guilds/rust/members')->assertSee('Grace'); + + expect(treeShows(nodeByRef($screen, 'person-grace'), '(moderator)'))->toBeFalse() + ->and(treeShows(nodeByRef($screen, 'person-linus'), '(moderator)'))->toBeFalse(); +}); + +it('says when the guild is gone', function () { + fakeApi([ + 'GET /api/v1/guilds/rust/members' => [['message' => 'Not Found'], 404], + 'GET /api/v1/guilds' => [['data' => [], 'links' => ['next' => null]]], + ]); + + finishRefreshes(Native::visit('/log/guilds/rust/members')) + ->assertSee('fatal: no such guild #rust') + ->assertSee('This guild is no longer available.'); +}); + +it('says guilds are switched off when the directory is gone too', function () { + fakeApi(['GET /api/v1/guilds/rust/members' => [['message' => 'Not Found'], 404]]); + + finishRefreshes(Native::visit('/log/guilds/rust/members')) + ->assertSee('fatal: guilds are not available') + ->assertSee("Guilds aren't open to you yet."); +}); + +it('opens their profile from a row', function () { + fakeApi(['GET /api/v1/guilds/rust/members' => [guildMembersJson(['grace' => []])]]); + + Native::visit('/log/guilds/rust/members')->tap('person-grace')->assertNavigatedTo('/log/users/grace'); +}); + +it('titles the screen with the passed guild name, else the kept guild, else the handle, under Members', function () { + fakeApi(['GET /api/v1/guilds/rust/members' => [guildMembersJson(['grace' => []])]]); + + $passed = Native::visit('/log/guilds/rust/members', ['guildName' => 'Rustaceans'])->assertNavTitle('Rustaceans'); + + expect(findNode($passed->tree(), fn (array $node): bool => ($node['props']['nav_subtitle'] ?? null) === 'Members'))->not->toBeNull(); + + Native::visit('/log/guilds/rust/members')->assertNavTitle('#rust'); + + keep('guilds/rust', boardJson(['guild' => guildJson(['name' => 'Kept Rust'])])); + Native::visit('/log/guilds/rust/members')->assertNavTitle('Kept Rust'); +}); + +it('speaks git when nobody has joined', function () { + fakeApi(['GET /api/v1/guilds/rust/members' => [guildMembersJson([])]]); + + Native::visit('/log/guilds/rust/members') + ->assertSee('fatal: no members yet') + ->assertSee('Nobody has joined this guild yet.'); +}); + +it('loads the next page without repeating anyone', function () { + Http::fake(fn (Request $request) => str_contains($request->url(), 'page=2') + ? Http::response(guildMembersJson(['grace' => [], 'linus' => []])) + : Http::response(guildMembersJson(['ada' => [], 'grace' => []], nextPage: 2))); + + $screen = Native::visit('/log/guilds/rust/members') + ->assertDontSee('Linus') + ->call('loadMore') + ->assertSee('Ada') + ->assertSee('Linus'); + + expect(findNodes($screen->tree(), fn (array $node): bool => ($node['ref'] ?? null) === 'person-grace'))->toHaveCount(1) + ->and($screen->get('nextCursor'))->toBeNull(); + + Http::assertSent(fn (Request $request): bool => $request->url() === 'http://devnull.test/api/v1/guilds/rust/members?page=2'); +}); diff --git a/tests/Feature/Screens/GuildPostTest.php b/tests/Feature/Screens/GuildPostTest.php index 30091fdb..2ef3ae69 100644 --- a/tests/Feature/Screens/GuildPostTest.php +++ b/tests/Feature/Screens/GuildPostTest.php @@ -61,27 +61,23 @@ function pressEditOnToast(TestableComponent $screen, int $writeId): TestableComp return $screen->emitNative(ToastActionPressed::class, ['toastId' => 'toast-1', 'actionId' => 'edit', 'data' => ['write' => $writeId]]); } -it('offers New commit beside Share once the guild is loaded', function () { - holdRefreshes(); +it('offers Commit in the header, leaving Share alone in the bar', function () { fakeApi(['GET /api/v1/guilds/rust' => [boardOf(['c1'])]]); - $loading = Native::visit('/log/guilds/rust'); - - expect(hasTopBarAction($loading, 'compose'))->toBeFalse(); - $loaded = finishRefreshes($loading); + $screen = finishRefreshes(Native::visit('/log/guilds/rust'))->tap('guild-commit'); - expect(hasTopBarAction($loaded, 'share'))->toBeTrue() - ->and(hasTopBarAction($loaded, 'compose'))->toBeTrue() - ->and(findNode($loaded->tree(), fn (array $node): bool => ($node['props']['id'] ?? null) === 'compose')['props']['a11y_label'] ?? null)->toBe('New commit'); + expect(treeShows(nodeByRef($screen, 'guild-commit'), 'Commit'))->toBeTrue() + ->and(hasTopBarAction($screen, 'share'))->toBeTrue() + ->and(hasTopBarAction($screen, 'compose'))->toBeFalse() + ->and($screen->instance()->sheetOpen)->toBeTrue(); }); -it('offers no New commit on a paused guild, as the web has no composer there', function () { +it('offers no Commit on a paused guild, as the web has no composer there', function () { fakeApi(['GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['is_paused' => true])])]]); $paused = finishRefreshes(Native::visit('/log/guilds/rust'))->call('composeForGuild'); - expect(hasTopBarAction($paused, 'share'))->toBeTrue() - ->and(hasTopBarAction($paused, 'compose'))->toBeFalse() + expect(rendersRef($paused, 'guild-commit'))->toBeFalse() ->and($paused->instance()->sheetOpen)->toBeFalse(); }); diff --git a/tests/Feature/Screens/GuildTest.php b/tests/Feature/Screens/GuildTest.php index 62898810..234af8fb 100644 --- a/tests/Feature/Screens/GuildTest.php +++ b/tests/Feature/Screens/GuildTest.php @@ -93,6 +93,94 @@ function guildMeta(TestableComponent $screen): string ->assertNativeCalled('Share.Url', fn (array $params): bool => $params['url'] === 'http://devnull.test/guilds/rust'); }); +it('opens the members from the member count', function () { + serveBoard(['' => boardOf(['c1'])]); + + $screen = finishRefreshes(Native::visit('/log/guilds/rust')) + ->tap('guild-members') + ->assertNavigatedTo('/log/guilds/rust/members'); + + expect($screen->navigationIntent()->data)->toBe(['guildName' => 'Rust']); +}); + +it('joins and leaves, counting the members to match', function () { + fakeApi([ + 'GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['member_count' => 3])])], + 'POST /api/v1/guilds/rust/membership' => [null, 204], + 'DELETE /api/v1/guilds/rust/membership' => [null, 204], + ]); + + $screen = finishRefreshes(Native::visit('/log/guilds/rust')); + + expect(treeShows(nodeByRef($screen, 'guild-join'), 'Join'))->toBeTrue(); + + $screen->tap('guild-join'); + + expect(treeShows(nodeByRef($screen, 'guild-join'), 'Leave'))->toBeTrue() + ->and(guildMeta($screen))->toBe('#rust · Public guild · 4 members') + ->and(kept('guilds/rust')['guild']['is_joined'])->toBeTrue() + ->and(kept('guilds/rust')['guild']['member_count'])->toBe(4); + + $screen->tap('guild-join'); + + expect(treeShows(nodeByRef($screen, 'guild-join'), 'Join'))->toBeTrue() + ->and(guildMeta($screen))->toBe('#rust · Public guild · 3 members') + ->and(kept('guilds/rust')['guild']['is_joined'])->toBeFalse() + ->and(kept('guilds/rust')['guild']['member_count'])->toBe(3); + + Http::assertSent(fn (Request $request): bool => $request->method() === 'POST' && $request->url() === 'http://devnull.test/api/v1/guilds/rust/membership'); + Http::assertSent(fn (Request $request): bool => $request->method() === 'DELETE' && $request->url() === 'http://devnull.test/api/v1/guilds/rust/membership'); +}); + +it('counts a join in the kept directory and forgets the kept members', function () { + fakeApi([ + 'GET /api/v1/guilds/rust' => [boardOf(['c1'])], + 'POST /api/v1/guilds/rust/membership' => [null, 204], + ]); + keep('guilds', ['data' => [guildJson(['handle' => 'go']), guildJson(['member_count' => 3])], 'links' => ['next' => null]]); + keep('guilds/rust/members', guildMembersJson(['grace' => []])); + + finishRefreshes(Native::visit('/log/guilds/rust'))->tap('guild-join'); + + expect(kept('guilds')['data'][1]['is_joined'])->toBeTrue() + ->and(kept('guilds')['data'][1]['member_count'])->toBe(4) + ->and(kept('guilds')['data'][0]['is_joined'])->toBeFalse() + ->and(kept('guilds/rust/members'))->toBeNull(); +}); + +it('unmutes a guild by joining it, as devnull does', function () { + fakeApi([ + 'GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['is_muted' => true])])], + 'POST /api/v1/guilds/rust/membership' => [null, 204], + ]); + + $screen = finishRefreshes(Native::visit('/log/guilds/rust'))->tap('guild-join'); + + expect($screen->instance()->guild->isMuted)->toBeFalse() + ->and(kept('guilds/rust')['guild']['is_muted'])->toBeFalse(); +}); + +it('puts Join back when devnull refuses', function () { + fakeApi([ + 'GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['member_count' => 3])])], + 'POST /api/v1/guilds/rust/membership' => [['message' => 'Too Many Attempts.'], 429], + ]); + + $screen = finishRefreshes(Native::visit('/log/guilds/rust'))->tap('guild-join')->assertSee('Too Many Attempts.'); + + expect(treeShows(nodeByRef($screen, 'guild-join'), 'Join'))->toBeTrue() + ->and(guildMeta($screen))->toBe('#rust · Public guild · 3 members') + ->and(kept('guilds/rust')['guild']['is_joined'])->toBeFalse(); +}); + +it('offers Join on a paused guild', function () { + fakeApi(['GET /api/v1/guilds/rust' => [boardOf(['c1'], overrides: ['guild' => guildJson(['is_paused' => true])])]]); + + $screen = finishRefreshes(Native::visit('/log/guilds/rust')); + + expect(treeShows(nodeByRef($screen, 'guild-join'), 'Join'))->toBeTrue(); +}); + it('shows the pinned commit first, once', function () { serveBoard(['' => boardOf(['c1', 'c2', 'c3'], overrides: ['pinned_commit' => commitJson(['id' => 'c2'])])]); diff --git a/tests/Pest.php b/tests/Pest.php index 6eb917e7..b18d5e70 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -512,6 +512,28 @@ function boardJson(array $overrides = []): array ]; } +/** + * A guild members answer, as `GET /guilds/{handle}/members` sends it. People are [username => overrides], each with + * the id "id-{username}". + * + * @param array> $people + * @return array + */ +function guildMembersJson(array $people, ?string $moderatorId = null, ?int $nextPage = null, string $handle = 'rust'): array +{ + $rows = []; + + foreach ($people as $username => $overrides) { + $rows[] = userJson(['id' => "id-{$username}", 'handle' => $username, 'name' => ucfirst($username), 'bio' => null, 'lane' => 2, 'is_followed' => false, 'follows_you' => false, ...$overrides]); + } + + return [ + 'data' => $rows, + 'moderator_id' => $moderatorId, + 'links' => ['next' => $nextPage === null ? null : "http://devnull.test/api/v1/guilds/{$handle}/members?page={$nextPage}"], + ]; +} + /** * A board page of commits by id, newest first, with no pin unless one is given. * diff --git a/tests/Unit/Devnull/GuildMembersTest.php b/tests/Unit/Devnull/GuildMembersTest.php new file mode 100644 index 00000000..68f277f4 --- /dev/null +++ b/tests/Unit/Devnull/GuildMembersTest.php @@ -0,0 +1,22 @@ + [userJson(['id' => 'u1', 'handle' => 'ada']) + ['is_followed' => true]], + 'moderator_id' => 'u1', + 'links' => ['next' => 'https://dev-null.sh/api/v1/guilds/rust/members?page=2'], + ]); + + expect($members->people->items)->toHaveCount(1) + ->and($members->people->items[0]->user->username)->toBe('ada') + ->and($members->people->items[0]->isFollowed)->toBeTrue() + ->and($members->people->nextCursor)->toBe('2') + ->and($members->moderatorId)->toBe('u1'); +}); + +it('has no moderator when the answer has none', function () { + expect(GuildMembers::fromArray(['data' => []])->moderatorId)->toBeNull() + ->and(GuildMembers::fromArray(['data' => [], 'moderator_id' => null])->moderatorId)->toBeNull(); +}); From bc406cfb2d24fc4800a57f99e73695a90cd66cf0 Mon Sep 17 00:00:00 2001 From: Shane Smith Date: Tue, 24 Nov 2026 02:45:36 +1030 Subject: [PATCH 2/2] [Guilds] Open a guild from its hashtag in a commit or message A hashtag that names a guild now opens the guild instead of a search. Any non-guild hashtag still opens search instead. In a commit it shows as a chip: the guild's name in medium mono on a wash, in a colour picked from its handle. The guild's icon can't sit in text yet, so it's left out for now. In a Remotes message the name keeps the bubble's link colour, which a guild's colour could clash with. --- app/Devnull/Data/BodyRun.php | 5 +- app/Devnull/Data/Commit.php | 9 ++-- app/Devnull/Data/CommitBody.php | 28 +++++++++-- app/Devnull/Data/Guild.php | 36 ++++++++++++++ app/Devnull/Data/Message.php | 1 + app/Devnull/Data/ScheduledCommit.php | 5 +- app/NativeComponents/Concerns/OpensGuilds.php | 3 +- app/NativeComponents/Conversation.php | 2 + app/NativeComponents/ScheduledCommits.php | 2 + .../native/partials/commit-body.blade.php | 13 ++++- .../native/partials/message-body.blade.php | 6 ++- tests/Feature/Screens/CommitDetailTest.php | 47 +++++++++++++++++++ tests/Feature/Screens/ConversationTest.php | 16 +++++++ tests/Unit/Devnull/GuildTest.php | 7 +++ 14 files changed, 166 insertions(+), 14 deletions(-) diff --git a/app/Devnull/Data/BodyRun.php b/app/Devnull/Data/BodyRun.php index 02295ad5..b6cb096d 100644 --- a/app/Devnull/Data/BodyRun.php +++ b/app/Devnull/Data/BodyRun.php @@ -8,11 +8,12 @@ * draws with a swatch of it, or a hashtag, which searches commits for it. A * link's text is the URL as written, and a color's or a hashtag's is as * written, `#` included. A mention of a GitHub organization has its handle in - * $username too, and $isOrganization set. + * $username too, and $isOrganization set. A hashtag that names a guild has + * that guild ($guild), and opens it instead. */ final readonly class BodyRun { - public function __construct(public string $text, public ?string $username = null, public bool $isCode = false, public bool $isWordmark = false, public ?BodyLink $link = null, public ?string $color = null, public bool $isOrganization = false, public bool $isHashtag = false) {} + public function __construct(public string $text, public ?string $username = null, public bool $isCode = false, public bool $isWordmark = false, public ?BodyLink $link = null, public ?string $color = null, public bool $isOrganization = false, public bool $isHashtag = false, public ?Guild $guild = null) {} public function isMention(): bool { diff --git a/app/Devnull/Data/Commit.php b/app/Devnull/Data/Commit.php index e7b87ada..5cc4b5b4 100644 --- a/app/Devnull/Data/Commit.php +++ b/app/Devnull/Data/Commit.php @@ -125,6 +125,9 @@ public static function fromArray(array $data): self $previewSegments = null; } + $guild = self::guild($data); + $guilds = Guild::mentions($data['guild_mentions'] ?? null, $guild); + return new self( id: $id, body: $body, @@ -140,7 +143,7 @@ public static function fromArray(array $data): self mentions: $mentions, linkPreview: is_array($data['link_preview'] ?? null) ? LinkPreview::fromArray($data['link_preview']) : null, images: self::listOf($data['images'] ?? null, CommitImage::fromArray(...)), - content: CommitBody::blocks($scoreCard === null ? $body : ScoreCard::withoutRun($body), $segments, $mentions), + content: CommitBody::blocks($scoreCard === null ? $body : ScoreCard::withoutRun($body), $segments, $mentions, $guilds), parent: is_array($data['parent'] ?? null) ? self::fromArray($data['parent']) : null, shortHash: is_string($data['short_hash'] ?? null) && $data['short_hash'] !== '' ? $data['short_hash'] : Display::shortHash($id), seriesId: is_string($data['series_id'] ?? null) ? $data['series_id'] : null, @@ -149,11 +152,11 @@ public static function fromArray(array $data): self poll: is_array($data['poll'] ?? null) ? Poll::fromArray($data['poll']) : null, quotedCommitId: is_scalar($data['quoted_commit_id'] ?? null) && (string) $data['quoted_commit_id'] !== '' ? (string) $data['quoted_commit_id'] : null, quotedCommit: is_array($data['quoted_commit'] ?? null) ? self::fromArray($data['quoted_commit']) : null, - previewContent: $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments, $mentions), + previewContent: $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments, $mentions, $guilds), replyPreview: is_array($data['reply_preview'] ?? null) ? ReplyPreview::fromArray($data['reply_preview']) : null, pasteisCount: max(0, (int) ($data['pasteis_count'] ?? 0)), scoreCard: $scoreCard, - guild: self::guild($data), + guild: $guild, ); } diff --git a/app/Devnull/Data/CommitBody.php b/app/Devnull/Data/CommitBody.php index 0adf5784..1cb98b1e 100644 --- a/app/Devnull/Data/CommitBody.php +++ b/app/Devnull/Data/CommitBody.php @@ -114,9 +114,10 @@ private static function parts(string $body): array /** * @param list>|null $segments The `body_segments`, or null when the API sent none. * @param list $mentions + * @param array $guilds The guilds mentioned by hashtag, keyed by lowercase handle (see Guild::mentions()). * @return list */ - public static function blocks(string $body, ?array $segments, array $mentions = []): array + public static function blocks(string $body, ?array $segments, array $mentions = [], array $guilds = []): array { $segments ??= $body === '' ? [] : [['type' => 'text', 'text' => $body]]; @@ -166,7 +167,7 @@ public static function blocks(string $body, ?array $segments, array $mentions = break; case 'block': - array_push($blocks, ...self::paragraphs($runs)); + array_push($blocks, ...self::paragraphs(self::withGuilds($runs, $guilds))); $runs = []; [$code, $cursor] = self::fencedCode($body, $cursor); @@ -178,11 +179,32 @@ public static function blocks(string $body, ?array $segments, array $mentions = } } - array_push($blocks, ...self::paragraphs($runs)); + array_push($blocks, ...self::paragraphs(self::withGuilds($runs, $guilds))); return $blocks; } + /** + * Gives each hashtag that names a guild that guild, matched without case as devnull matches it, so it opens the + * guild rather than a search, as the web's guild mentions do. + * + * @param list $runs + * @param array $guilds + * @return list + */ + private static function withGuilds(array $runs, array $guilds): array + { + if ($guilds === []) { + return $runs; + } + + return array_map(function (BodyRun $run) use ($guilds): BodyRun { + $guild = $run->isHashtag ? ($guilds[mb_strtolower(ltrim($run->text, '#'))] ?? null) : null; + + return $guild === null ? $run : new BodyRun($run->text, isHashtag: true, guild: $guild); + }, $runs); + } + /** * Splits a paragraph's runs into paragraphs and quotes, as the web's * bodyBlocks() does: prose that starts a line with `>` quotes that line, diff --git a/app/Devnull/Data/Guild.php b/app/Devnull/Data/Guild.php index 1ce489a3..5b00ba6d 100644 --- a/app/Devnull/Data/Guild.php +++ b/app/Devnull/Data/Guild.php @@ -3,6 +3,7 @@ namespace App\Devnull\Data; use App\Devnull\ImageCache; +use App\Devnull\Lane; /** * A guild is a public group around a hashtag. @@ -64,6 +65,41 @@ public static function fromArray(array $data): ?self ); } + /** + * The guilds mentioned by hashtag in a commit or a message, keyed by lowercase handle, read from the + * `guild_mentions` devnull sends with it. The guild a commit was posted in ($postedIn) is added when the map lacks + * it, so its own hashtag opens it even when devnull leaves it out, as on the web. devnull sends none when guilds are off for you. + * + * @return array + */ + public static function mentions(mixed $guildMentions, ?self $postedIn = null): array + { + $guilds = []; + + foreach (is_array($guildMentions) ? $guildMentions : [] as $data) { + $guild = is_array($data) ? self::fromArray($data) : null; + + if ($guild !== null) { + $guilds[mb_strtolower($guild->handle)] = $guild; + } + } + + if ($postedIn !== null) { + $guilds[mb_strtolower($postedIn->handle)] ??= $postedIn; + } + + return $guilds; + } + + /** + * The lane colour its mark in a commit's text is drawn in, picked from its handle, as text can't hold its icon or + * emoji. Any case of the handle picks the same lane, so a guild always wears the same colour. + */ + public function lane(): int + { + return Lane::for(mb_strtolower($this->handle)); + } + /** * Its hashtag, as commits write it and search takes it. */ diff --git a/app/Devnull/Data/Message.php b/app/Devnull/Data/Message.php index bed3442c..4a971d4e 100644 --- a/app/Devnull/Data/Message.php +++ b/app/Devnull/Data/Message.php @@ -89,6 +89,7 @@ public static function fromArray(array $data): self fn (mixed $mention): ?Mention => is_array($mention) ? Mention::fromArray($mention) : null, is_array($data['mentions'] ?? null) ? $data['mentions'] : [], ))), + Guild::mentions($data['guild_mentions'] ?? null), ), replyToId: is_int($data['reply_to_id'] ?? null) ? $data['reply_to_id'] : null, replyTo: $replyTo === null ? null : [ diff --git a/app/Devnull/Data/ScheduledCommit.php b/app/Devnull/Data/ScheduledCommit.php index 31280b98..941a973f 100644 --- a/app/Devnull/Data/ScheduledCommit.php +++ b/app/Devnull/Data/ScheduledCommit.php @@ -51,12 +51,13 @@ public static function fromArray(array $data): self $previewSegments = is_array($data['preview_segments'] ?? null) ? array_values(array_filter($data['preview_segments'], is_array(...))) : null; $options = is_array($data['poll_options'] ?? null) ? array_values(array_filter($data['poll_options'], is_string(...))) : null; $status = $data['status'] ?? null; + $guilds = Guild::mentions($data['guild_mentions'] ?? null); return new self( id: (string) ($data['id'] ?? ''), body: $body, - content: $body === null ? [] : CommitBody::blocks($body, $segments), - previewContent: $body === null || $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments), + content: $body === null ? [] : CommitBody::blocks($body, $segments, guilds: $guilds), + previewContent: $body === null || $previewSegments === null ? null : CommitBody::blocks($body, $previewSegments, guilds: $guilds), pollOptions: $options, imageCount: max(0, (int) ($data['image_count'] ?? 0)), scheduledAt: Commit::date($data['scheduled_at'] ?? null) ?? CarbonImmutable::now(), diff --git a/app/NativeComponents/Concerns/OpensGuilds.php b/app/NativeComponents/Concerns/OpensGuilds.php index 553562c0..bebc4252 100644 --- a/app/NativeComponents/Concerns/OpensGuilds.php +++ b/app/NativeComponents/Concerns/OpensGuilds.php @@ -5,7 +5,8 @@ /** * A tapped guild opens its own screen (see App\NativeComponents\Guild), in * the tab it was tapped in (see OpensInTab, which the screen uses too): from - * a row of the directory, or the "posted in" pill on a commit. + * a row of the directory, the "posted in" pill on a commit, or a hashtag that + * names a guild in a commit or a message. */ trait OpensGuilds { diff --git a/app/NativeComponents/Conversation.php b/app/NativeComponents/Conversation.php index ef23ac67..4c437629 100644 --- a/app/NativeComponents/Conversation.php +++ b/app/NativeComponents/Conversation.php @@ -24,6 +24,7 @@ use App\Icons\Lucide; use App\Models\PendingWrite; use App\NativeComponents\Concerns\AttachesPhotos; +use App\NativeComponents\Concerns\OpensGuilds; use App\NativeComponents\Concerns\OpensHashtags; use App\NativeComponents\Concerns\OpensInTab; use App\NativeComponents\Concerns\PlaysVoiceNotes; @@ -104,6 +105,7 @@ class Conversation extends NativeComponent { use AttachesPhotos; + use OpensGuilds; use OpensHashtags; use OpensInTab; use PlaysVoiceNotes; diff --git a/app/NativeComponents/ScheduledCommits.php b/app/NativeComponents/ScheduledCommits.php index 2b0be955..e439b18e 100644 --- a/app/NativeComponents/ScheduledCommits.php +++ b/app/NativeComponents/ScheduledCommits.php @@ -9,6 +9,7 @@ use App\Devnull\SiteLink; use App\Devnull\Timezone; use App\Devnull\Toasts; +use App\NativeComponents\Concerns\OpensGuilds; use App\NativeComponents\Concerns\OpensHashtags; use App\NativeComponents\Concerns\OpensInTab; use App\NativeComponents\Concerns\RefreshesInBackground; @@ -37,6 +38,7 @@ */ class ScheduledCommits extends NativeComponent { + use OpensGuilds; use OpensHashtags; use OpensInTab; use RefreshesInBackground; diff --git a/resources/views/native/partials/commit-body.blade.php b/resources/views/native/partials/commit-body.blade.php index 2e447d8c..8358b644 100644 --- a/resources/views/native/partials/commit-body.blade.php +++ b/resources/views/native/partials/commit-body.blade.php @@ -13,8 +13,13 @@ A hex color has a square of that color before it, as the web's swatch, held to it by a narrow no-break space so they wrap together. - A hashtag is medium weight in Commit Blue and searches commits for it on a tap, as the web's. In the composer's - preview ($static), as in the web's static preview, it stays plain text. + A hashtag is medium weight in Commit Blue and searches commits for it on a tap, as the web's. One that names a + guild is a chip instead, as the web's guild mention is, opening the guild: its name in medium mono on a wash, in a lane + colour picked from its handle (see Guild::lane), as text runs can't hold its icon. It's 14pt so its letters sit + near the middle of the wash, which fills the whole line's height. Text runs can't take padding, so a thin space + in the body font sits on the wash at each end (a mono one would be a whole character wide), held to the name by + word joiners. They can't round it either. In the composer's preview ($static), as in the web's static preview, + it stays plain text. A quote, lines that start with ">", sits behind an amber rule on the left like the web's blockquote, its runs drawn as a paragraph's. @@ -32,6 +37,8 @@ @php($ink = ($muted ?? false) ? 'text-theme-ink-muted' : 'text-theme-ink') @php($folded = ! ($focused ?? false) && $commit->isFolded() && ! in_array($commit->id, $unfolded ?? [], true)) @php($linkIndex = 0) +@php($chipStart = "\u{2009}\u{2060}") +@php($chipEnd = "\u{2060}\u{2009}") @foreach ($folded ? $commit->previewContent : $commit->content as $block) @if ($block->isCode()) @@ -55,6 +62,8 @@ @elseif ($run->isColor())   +@elseif ($run->guild && ! ($static ?? false)) + @elseif ($run->isHashtag && ! ($static ?? false)) @elseif ($run->isWordmark) diff --git a/resources/views/native/partials/message-body.blade.php b/resources/views/native/partials/message-body.blade.php index a69fc811..31e23f27 100644 --- a/resources/views/native/partials/message-body.blade.php +++ b/resources/views/native/partials/message-body.blade.php @@ -6,7 +6,9 @@ your own bubbles. A link is in $linkInk, Commit Blue off your lane and the lane's label colour on it, with a named link's owner muted in $linkPrefixInk, and opens by its place among the body's links (see Conversation::openMessageLink). - A hashtag is medium weight in $mentionInk, as a mention is, and searches commits for it on a tap. + A hashtag is medium weight in $mentionInk, as a mention is, and searches commits for it on a tap. One that names a + guild shows its name in medium mono, with no wash, as inline code is here, and opens the guild. It's in $mentionInk, not + the guild's lane colour (see Guild::lane), which could match the colour of your own bubble. $maxLines caps the body to the first block at that many lines, for the pinned sheet's preview, so its links work before Show more. --}} @@ -34,6 +36,8 @@ @elseif ($run->isColor())   +@elseif ($run->guild) + @elseif ($run->isHashtag) @else diff --git a/tests/Feature/Screens/CommitDetailTest.php b/tests/Feature/Screens/CommitDetailTest.php index fc0da284..e6a25dfc 100644 --- a/tests/Feature/Screens/CommitDetailTest.php +++ b/tests/Feature/Screens/CommitDetailTest.php @@ -608,6 +608,53 @@ function fakeThread(array $commit = [], array $replies = [], array $extra = []): expect($screen->instance()->getNavigationIntent()->data)->toBe(['query' => '#laravel']); }); +it('draws a guild hashtag as a chip with its name in its lane colour that opens the guild, and any other hashtag as a search', function () { + fakeThread([ + 'body' => 'Ship #ANDROID and #rust, not #laravel', + 'guild_mentions' => [ + 'android' => ['handle' => 'android', 'name' => 'Android Devs', 'emoji' => '🤖', 'icon_url' => null], + 'rust' => ['handle' => 'rust', 'name' => 'Rustaceans', 'emoji' => '💻', 'icon_url' => 'https://dev-null.sh/guild-icons/rust.png'], + ], + ]); + + $screen = Native::visit('/log/commits/c2'); + + expect(paragraphRuns($screen, 'Ship'))->toBe([ + ['Ship '], + ["\u{2009}Android Devs\u{2009}", 'link'], + [' and '], + ["\u{2009}Rustaceans\u{2009}", 'link'], + [', not '], + ['#laravel', 'hashtag'], + ]); + + $chip = findNode($screen->tree(), fn (array $node): bool => ($node['props']['text'] ?? null) === 'Android Devs'); + + // #android hashes to lane 3, teal. + expect($chip['props']['font_name'] ?? null)->toBe('mono-medium') + ->and($chip['style']['bg_color'] ?? null)->not->toBeNull() + ->and(strtolower($chip['props']['color']))->toBe(strtolower((string) config('native-ui.theme.light.lane-3'))); + + $screen->press("openGuild('android')")->assertNavigatedTo('/log/guilds/android'); +}); + +it('links its own guild\'s hashtag in a commit posted there, even without guild mentions', function () { + fakeThread(['body' => 'Hello #rust', 'guild' => ['handle' => 'rust', 'name' => 'Rustaceans', 'emoji' => '🦀']]); + + expect(paragraphRuns(Native::visit('/log/commits/c2'), 'Hello'))->toBe([['Hello '], ["\u{2009}Rustaceans\u{2009}", 'link']]); +}); + +it('names its own guild as devnull mentions it when the commit only knows that guild\'s handle', function () { + fakeThread([ + 'body' => 'Hello #rust', + 'guild' => null, + 'guild_handle' => 'rust', + 'guild_mentions' => ['rust' => ['handle' => 'rust', 'name' => 'Rustaceans', 'emoji' => '🦀', 'icon_url' => null]], + ]); + + expect(paragraphRuns(Native::visit('/log/commits/c2'), 'Hello'))->toBe([['Hello '], ["\u{2009}Rustaceans\u{2009}", 'link']]); +}); + it('draws a hex color after a square of that color, in light and dark mode alike', function () { $body = 'Brand orange is #ff8a4c, not #123'; diff --git a/tests/Feature/Screens/ConversationTest.php b/tests/Feature/Screens/ConversationTest.php index 5e0634ef..4fccdc03 100644 --- a/tests/Feature/Screens/ConversationTest.php +++ b/tests/Feature/Screens/ConversationTest.php @@ -749,6 +749,22 @@ function pulledAvatars(TestableComponent $screen, int $id): array $screen->press("openHashtag('#nativephp')")->assertNavigatedTo('/remotes/search'); }); +it('links a guild hashtag in a message to its guild by name', function () { + serveRemoteThread(threadJson([ + remoteMessageJson(['id' => 1, 'body' => 'loving #android lately', 'guild_mentions' => ['android' => ['handle' => 'android', 'name' => 'Android', 'emoji' => '🤖', 'icon_url' => null]]]), + ], ['read_through_id' => 1])); + + $screen = finishRefreshes(Native::visit('/remotes/conversations/ada')); + + expect(paragraphRuns($screen, 'loving'))->toBe([ + ['loving '], + ['Android', 'link'], + [' lately'], + ]); + + $screen->press("openGuild('android')")->assertNavigatedTo('/remotes/guilds/android'); +}); + it('opens no link preview that is not a web page', function () { serveRemoteThread(threadJson([ remoteMessageJson(['id' => 1, 'body' => 'Tap me', 'link_preview' => ['url' => 'javascript:alert(1)', 'title' => 'Tap me']]), diff --git a/tests/Unit/Devnull/GuildTest.php b/tests/Unit/Devnull/GuildTest.php index b3331cd7..1083be89 100644 --- a/tests/Unit/Devnull/GuildTest.php +++ b/tests/Unit/Devnull/GuildTest.php @@ -42,3 +42,10 @@ ->and(Guild::fromArray(['handle' => 'go', 'icon_url' => ''])->iconUrl)->toBeNull() ->and(Guild::fromArray(['handle' => 'php'])->iconUrl)->toBeNull(); }); + +it('takes a lane colour from its handle, the same whatever its case', function () { + expect(Guild::fromArray(['handle' => 'android'])->lane())->toBe(3) + ->and(Guild::fromArray(['handle' => 'Android'])->lane())->toBe(3) + ->and(Guild::fromArray(['handle' => 'apple'])->lane())->toBe(2) + ->and(Guild::fromArray(['handle' => 'nativephp'])->lane())->toBe(5); +});