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(); +});