Repository navigation
feat: show paginated guild members #1598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Http\Requests; | ||
|
|
||
| use App\Enums\Feature; | ||
| use App\Services\Features; | ||
| use Illuminate\Auth\Access\Response; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| final class ListGuildMembersRequest extends FormRequest | ||
| { | ||
| public function authorize(Features $features): Response | ||
| { | ||
| return $features->available(Feature::Guilds, $this->user()) ? Response::allow() : Response::denyAsNotFound(); | ||
| } | ||
|
|
||
| /** @return array<string, list<string>> */ | ||
| public function rules(): array | ||
| { | ||
| return ['page' => ['sometimes', 'integer', 'min:1']]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| import { Head, InfiniteScroll, Link } from '@inertiajs/react'; | ||
| import { ArrowLeft } from 'lucide-react'; | ||
| import type { CSSProperties } from 'react'; | ||
| import { show } from '@/actions/App/Http/Controllers/GuildController'; | ||
| import { EmptyFeed } from '@/components/commit-feed'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { Spinner } from '@/components/ui/spinner'; | ||
| import { UserAvatar } from '@/components/user-avatar'; | ||
| import AppLayout from '@/layouts/app-layout'; | ||
| import { laneColor } from '@/lib/utils'; | ||
| import { show as profile } from '@/routes/user'; | ||
| import type { User } from '@/types'; | ||
| import type { Guild } from '@/types/guild'; | ||
|
|
||
| export default function Members({ | ||
| guild, | ||
| users, | ||
| }: { | ||
| guild: Pick<Guild, 'handle' | 'name'>; | ||
| users: { | ||
| data: (Pick< | ||
| User, | ||
| 'id' | 'handle' | 'name' | 'avatar_url' | 'bio' | 'lane' | ||
| > & { is_moderator: boolean })[]; | ||
| }; | ||
| }) { | ||
| return ( | ||
| <> | ||
| <Head title={`Members · ${guild.name}`} /> | ||
| <header className="sticky top-0 z-10 flex h-14 items-center gap-3 border-b bg-card px-4 sm:px-6"> | ||
| <Link | ||
| href={show(guild.handle)} | ||
| prefetch | ||
| aria-label={`Back to ${guild.name}`} | ||
| className="text-muted-foreground hover:text-foreground" | ||
| > | ||
| <ArrowLeft className="size-4" /> | ||
| </Link> | ||
| <h1 | ||
| className="min-w-0 truncate font-semibold" | ||
| title={guild.name} | ||
| > | ||
| {guild.name} | ||
| </h1> | ||
| <p className="shrink-0 text-sm text-muted-foreground"> | ||
| Members | ||
| </p> | ||
| </header> | ||
| {users.data.length === 0 ? ( | ||
| <div className="px-6 py-16 text-center sm:px-12"> | ||
| <EmptyFeed command="fatal: no members yet"> | ||
| Nobody has joined this guild yet. | ||
| </EmptyFeed> | ||
| </div> | ||
| ) : ( | ||
| <InfiniteScroll | ||
| data="users" | ||
| manual | ||
| onlyNext | ||
| preserveUrl | ||
| next={({ hasMore, loading, fetch }) => | ||
| hasMore && ( | ||
| <div className="flex justify-center py-4"> | ||
| <Button | ||
| variant="outline" | ||
| onClick={fetch} | ||
| disabled={loading} | ||
| > | ||
| <Spinner | ||
| className={loading ? '' : 'invisible'} | ||
| /> | ||
| Load more | ||
| </Button> | ||
| </div> | ||
| ) | ||
| } | ||
| > | ||
| <ul className="divide-y"> | ||
| {users.data.map((person) => ( | ||
| <li key={person.id}> | ||
| <Link | ||
| href={profile(person)} | ||
| prefetch | ||
| className="flex min-w-0 items-start gap-3 px-4 py-3 hover:bg-muted/50 sm:px-6" | ||
| > | ||
| <UserAvatar | ||
| user={person} | ||
| className="size-10 shrink-0 ring-2 ring-(--lane) ring-offset-2 ring-offset-card" | ||
| style={ | ||
| { | ||
| '--lane': laneColor( | ||
| person.lane, | ||
| ), | ||
| } as CSSProperties | ||
| } | ||
| /> | ||
| <div className="min-w-0"> | ||
| <div className="flex min-w-0 items-baseline gap-1"> | ||
| <p | ||
| className="truncate font-medium" | ||
| title={person.name} | ||
| > | ||
| {person.name} | ||
| </p> | ||
| {person.is_moderator && ( | ||
| <span className="shrink-0 text-sm text-muted-foreground"> | ||
| (moderator) | ||
| </span> | ||
| )} | ||
| </div> | ||
| <p | ||
| className="truncate font-mono text-sm text-muted-foreground" | ||
| title={`@${person.handle}`} | ||
| > | ||
| @{person.handle} | ||
| </p> | ||
| {person.bio && ( | ||
| <p className="mt-1 line-clamp-2 text-sm wrap-anywhere"> | ||
| {person.bio} | ||
| </p> | ||
| )} | ||
| </div> | ||
| </Link> | ||
| </li> | ||
| ))} | ||
| </ul> | ||
| </InfiniteScroll> | ||
| )} | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| Members.layout = AppLayout; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
tests/Feature/Controllers/GuildMembershipControllerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use App\Models\Guild; | ||
| use App\Models\User; | ||
|
|
||
| beforeEach(function (): void { | ||
| config(['app.admins' => ['admin']]); | ||
| }); | ||
|
|
||
| it('lists the moderator first then joined members newest first in pages of fifty without private preferences', function (): void { | ||
| $viewer = User::factory()->create(['handle' => 'admin']); | ||
| $moderator = User::factory()->create(); | ||
| $guild = Guild::factory()->create(['user_id' => $moderator->id]); | ||
| $older = User::factory()->create(); | ||
| $members = User::factory()->count(50)->create(); | ||
| $guild->members()->attach($moderator, ['joined_at' => now()->subDays(2)]); | ||
| $guild->members()->attach($older, ['joined_at' => now()->subDay()]); | ||
| $guild->members()->attach($members->modelKeys(), ['joined_at' => now()]); | ||
| $guild->muters()->attach(User::factory()->create(), ['muted_at' => now()]); | ||
| Guild::factory()->create()->members()->attach(User::factory()->create(), ['joined_at' => now()]); | ||
|
|
||
| $this->actingAs($viewer)->get(route('guild-membership.index', $guild->handle)) | ||
| ->assertOk()->assertInertia(fn ($page) => $page | ||
| ->component('guild/members') | ||
| ->where('guild.handle', $guild->handle) | ||
| ->has('users.data', 50) | ||
| ->where('users.data.0.id', $moderator->id) | ||
| ->where('users.data.0.is_moderator', true) | ||
| ->where('users.data.1.is_moderator', false) | ||
| ->where('users.data', fn ($users): bool => collect($users)->pluck('id')->all() === [$moderator->id, ...$members->sortByDesc('id')->take(49)->values()->modelKeys()]) | ||
| ->where('users.current_page', 1) | ||
| ->where('users.per_page', 50) | ||
| ->where('users.next_page_url', route('guild-membership.index', ['guild' => $guild->handle, 'page' => 2])) | ||
| ->missing('users.data.0.email') | ||
| ->missing('users.data.0.pivot')); | ||
|
|
||
| $this->get(route('guild-membership.index', ['guild' => $guild->handle, 'page' => 2])) | ||
| ->assertOk()->assertInertia(fn ($page) => $page | ||
| ->has('users.data', 2) | ||
| ->where('users.data.0.id', $members->sortBy('id')->first()->id) | ||
| ->where('users.data.1.id', $older->id) | ||
| ->where('users.next_page_url', null)); | ||
| }); | ||
|
|
||
| it('keeps paused guild members accessible and supports an empty list', function (): void { | ||
| $viewer = User::factory()->create(['handle' => 'admin']); | ||
| $guild = Guild::factory()->create(['user_id' => null]); | ||
|
|
||
| $this->actingAs($viewer)->get(route('guild-membership.index', $guild->handle)) | ||
| ->assertOk()->assertInertia(fn ($page) => $page->has('users.data', 0)->where('users.next_page_url', null)); | ||
|
|
||
| $guild->members()->attach($viewer, ['joined_at' => now()]); | ||
|
|
||
| $this->get(route('guild-membership.index', $guild->handle)) | ||
| ->assertOk()->assertInertia(fn ($page) => $page->has('users.data', 1)->where('users.data.0.id', $viewer->id)->where('users.data.0.is_moderator', false)); | ||
| }); | ||
|
|
||
| it('requires sign in and the guild preview feature to list members', function (): void { | ||
| $guild = Guild::factory()->create(); | ||
|
|
||
| $this->get(route('guild-membership.index', $guild->handle))->assertRedirectToRoute('home'); | ||
| $this->actingAs(User::factory()->create())->get(route('guild-membership.index', $guild->handle))->assertNotFound(); | ||
| }); | ||
|
|
||
| it('rejects invalid member pages', function (mixed $page): void { | ||
| $guild = Guild::factory()->create(); | ||
|
|
||
| $this->actingAs(User::factory()->create(['handle' => 'admin'])) | ||
| ->getJson(route('guild-membership.index', ['guild' => $guild->handle, 'page' => $page])) | ||
| ->assertUnprocessable()->assertJsonValidationErrors('page'); | ||
| })->with([0, 'not-a-page']); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
meh. ship it