From 472db313db18f52eb52fb10e29a0735368bd3e8c Mon Sep 17 00:00:00 2001 From: sulimanbenhalim Date: Sun, 29 Nov 2026 05:53:43 +0200 Subject: [PATCH] Find organizations in Search under the developers, as the web does --- app/Devnull/Client.php | 7 ++- app/NativeComponents/Search.php | 28 +++++++--- resources/views/native/search.blade.php | 31 +++++++++-- tests/Feature/Screens/PartialFramesTest.php | 2 +- tests/Feature/Screens/SearchTest.php | 58 +++++++++++++++++---- 5 files changed, 103 insertions(+), 23 deletions(-) diff --git a/app/Devnull/Client.php b/app/Devnull/Client.php index 02e48f3e..c288f952 100644 --- a/app/Devnull/Client.php +++ b/app/Devnull/Client.php @@ -102,8 +102,11 @@ /** Whom the commit composers suggest for an @mention, as the web's composers ask. */ public const string MentionSuggestionsPath = 'search/mentions'; - /** The people matching a name or handle, as the web's search finds them, with whether you follow each one. */ - public const string UserSearchPath = 'search/users'; + /** + * The people and the organizations with a page matching a name or handle, as the web's search finds them, with + * whether you follow each person. + */ + public const string ProfileSearchPath = 'search/profiles'; /** The commits nearest in meaning to a term, closest first, as the web's search finds them. */ public const string CommitSearchPath = 'search/commits'; diff --git a/app/NativeComponents/Search.php b/app/NativeComponents/Search.php index ab3fdf1e..ac60bcae 100644 --- a/app/NativeComponents/Search.php +++ b/app/NativeComponents/Search.php @@ -5,6 +5,7 @@ use App\Devnull\Client; use App\Devnull\Data\Commit; use App\Devnull\Data\Connection; +use App\Devnull\Data\User; use App\Devnull\RowCache; use App\Devnull\Stash; use App\Devnull\Timeline; @@ -21,9 +22,9 @@ /** * The web's search, as its phone header opens it: one field for developers or * commits. As you type, the developers whose name or handle matches come - * first, with Follow beside them, then the commits nearest in meaning to what - * you typed, closest first. Before you type it offers the web's searches to - * try. + * first, with Follow beside them, then the organizations that have a page on + * devnull, then the commits nearest in meaning to what you typed, closest + * first. Before you type it offers the web's searches to try. * * Both are asked for on a background thread, and each answer only lands * under the term it was found for. What's showing stays while the next term's @@ -74,7 +75,14 @@ class Search extends NativeComponent public array $people = []; /** - * The term the people were found for, so a slow answer never lands under a newer one. + * The organizations found with the people, which open their page as the web's do. + * + * @var list + */ + public array $organizations = []; + + /** + * The term the people and organizations were found for, so a slow answer never lands under a newer one. */ public ?string $peopleFoundFor = null; @@ -181,7 +189,8 @@ protected function searchesCommits(string $term): bool } /** - * Asks devnull for the developers and the commits matching the term. An emptied field shows the searches to try. + * Asks devnull for the developers, the organizations and the commits matching the term. An emptied field shows + * the searches to try. */ protected function search(): void { @@ -191,6 +200,7 @@ protected function search(): void if ($term === '') { $this->people = []; + $this->organizations = []; $this->peopleFoundFor = null; $this->commits = []; $this->commitsFoundFor = null; @@ -198,13 +208,15 @@ protected function search(): void return; } - $this->refresh(Client::UserSearchPath, ['q' => ltrim($term, '@')], function (array $json) use ($term): void { + $this->refresh(Client::ProfileSearchPath, ['q' => ltrim($term, '@')], function (array $json) use ($term): void { if ($this->term() !== $term) { return; } - $people = is_array($json['data'] ?? null) ? $json['data'] : []; + $people = is_array($json['users'] ?? null) ? $json['users'] : []; + $organizations = is_array($json['organizations'] ?? null) ? $json['organizations'] : []; $this->people = array_values(array_map(Connection::fromArray(...), array_filter($people, 'is_array'))); + $this->organizations = array_values(array_map(fn (array $organization): User => User::fromArray(['type' => 'organization', ...$organization]), array_filter($organizations, 'is_array'))); $this->peopleFoundFor = $term; }); @@ -236,7 +248,7 @@ protected function revalidate(): void protected function showsContent(): bool { - return $this->people !== [] || $this->commits !== []; + return $this->people !== [] || $this->organizations !== [] || $this->commits !== []; } /** diff --git a/resources/views/native/search.blade.php b/resources/views/native/search.blade.php index 581e42eb..d681ed18 100644 --- a/resources/views/native/search.blade.php +++ b/resources/views/native/search.blade.php @@ -1,7 +1,9 @@ {{-- The web's search: one field for developers or commits. Before you type, the web's searches to try. Then the - developers whose name or handle matches, then the commits nearest in meaning, on a rail of their own as the web's - results page draws them. A term with nothing near it answers in git's voice. + developers whose name or handle matches, then the organizations with a page: the square avatar, name, handle and bio + the web's search shows, laid out like a developer's row, and a tap opens their page. Then the commits nearest in + meaning, on a rail of their own as the web's results page draws them. A term with nothing near it answers in git's + voice. --}} @include('native.partials.error') @@ -40,6 +42,29 @@ @endforeach @endif + @if ($organizations !== []) + + Organizations + + @foreach ($organizations as $organization) + + + + @include('native.partials.avatar', ['user' => $organization, 'size' => 40]) + + + {{ $organization->name }} + {{ $organization->handle() }} + @if ($organization->bio) + {{ $organization->bio }} + @endif + + + + + @endforeach + @endif + @if ($searchesCommits) Most relevant commits @@ -62,7 +87,7 @@ @else @include('native.partials.feed', ['rows' => $rows, 'rowCache' => $rowCache]) @endif - @elseif ($peopleFoundFor === $term && $people === []) + @elseif ($peopleFoundFor === $term && $people === [] && $organizations === []) fatal: nothing matches '{{ $term }}' diff --git a/tests/Feature/Screens/PartialFramesTest.php b/tests/Feature/Screens/PartialFramesTest.php index 9c1516a5..ffb7946a 100644 --- a/tests/Feature/Screens/PartialFramesTest.php +++ b/tests/Feature/Screens/PartialFramesTest.php @@ -422,7 +422,7 @@ function partialFrameInbox(array $groups, ?string $cursor = null): array $found = fn (int $count, string $prefix): array => array_map(fn (int $n): array => commitJson(['id' => "{$prefix}{$n}", 'body' => "{$term} result {$n}"]), range(1, $count)); return match (parse_url($request->url(), PHP_URL_PATH)) { - '/api/v1/search/users' => Http::response(['data' => [userJson(['id' => 'id-ada', 'handle' => 'ada', 'name' => 'Ada', 'is_followed' => false, 'follows_you' => false])]]), + '/api/v1/search/profiles' => Http::response(['users' => [userJson(['id' => 'id-ada', 'handle' => 'ada', 'name' => 'Ada', 'is_followed' => false, 'follows_you' => false])], 'organizations' => []]), // The same commits come back for two terms, in a different order, as a search can find them. '/api/v1/search/commits' => Http::response(['data' => str_starts_with($term, 'php') ? $found(8, 'c') : array_reverse($found(8, 'c'))]), default => Http::response(pageJson([])), diff --git a/tests/Feature/Screens/SearchTest.php b/tests/Feature/Screens/SearchTest.php index 20e3b41b..fd588aa0 100644 --- a/tests/Feature/Screens/SearchTest.php +++ b/tests/Feature/Screens/SearchTest.php @@ -20,21 +20,33 @@ function foundPerson(string $username, array $overrides = []): array } /** - * Answers people searches with $people and commit searches with $commits. Anything else, such as the Log behind - * the search button, gets an empty page. + * An organization as devnull's profile search sends it, which has no id of its own. + * + * @param array $overrides + * @return array + */ +function foundOrganization(string $handle, array $overrides = []): array +{ + return ['handle' => $handle, 'name' => ucfirst($handle), 'avatar_url' => null, 'bio' => null, 'html_url' => "http://devnull.test/@{$handle}", 'lane' => null, 'is_followed' => false, ...$overrides]; +} + +/** + * Answers profile searches with $people and $organizations, and commit searches with $commits. Anything else, such as + * the Log behind the search button, gets an empty page. * * @param list> $people * @param list> $commits * @param array $writes + * @param list> $organizations */ -function serveSearch(array $people = [], array $commits = [], array $writes = []): void +function serveSearch(array $people = [], array $commits = [], array $writes = [], array $organizations = []): void { - serveApi(function (Request $request) use ($people, $commits, $writes) { + serveApi(function (Request $request) use ($people, $commits, $writes, $organizations) { $route = $request->method().' '.parse_url($request->url(), PHP_URL_PATH); return match (true) { isset($writes[$route]) => Http::response($writes[$route][0], $writes[$route][1] ?? 200), - $route === 'GET /api/v1/search/users' => Http::response(['data' => $people]), + $route === 'GET /api/v1/search/profiles' => Http::response(['users' => $people, 'organizations' => $organizations]), $route === 'GET /api/v1/search/commits' => Http::response(['data' => $commits]), default => Http::response(pageJson([])), }; @@ -42,7 +54,7 @@ function serveSearch(array $people = [], array $commits = [], array $writes = [] } /** - * The q each search of $kind ("users" or "commits") was sent with, in order. + * The q each search of $kind ("profiles" or "commits") was sent with, in order. * * @return list */ @@ -110,10 +122,38 @@ function searchedFor(string $kind): array ->and(array_map(fn ($commit): string => $commit->id, $screen->instance()->commits))->toBe(['c2', 'c1']) ->and(dayMarkers($screen))->toBe([]) // A handle is found without its @, and commits by what was typed. - ->and(searchedFor('users'))->toBe(['ada']) + ->and(searchedFor('profiles'))->toBe(['ada']) ->and(searchedFor('commits'))->toBe(['@ada']); }); +it('finds the organizations with a page under the developers, and opens one on the same stack', function () { + serveSearch( + people: [foundPerson('taylor')], + organizations: [foundOrganization('laravel', ['bio' => 'The PHP framework for web artisans.'])], + ); + + $screen = Native::visit('/log/search') + ->set('query', 'la') + ->assertSee('Organizations') + ->assertSee('Laravel') + ->assertSee('@laravel') + ->assertSee('The PHP framework for web artisans.'); + + expect(childRefs($screen, 'search-results'))->toBe(['developers-heading', 'person-taylor', 'organizations-heading', 'organization-laravel']) + ->and(rendersRef($screen, 'follow-laravel'))->toBeFalse(); + + $screen->tap('organization-laravel')->assertNavigatedTo('/log/organizations/laravel'); +}); + +it('says nothing matches only when no developer or organization does', function () { + serveSearch(organizations: [foundOrganization('laravel')]); + + Native::visit('/log/search') + ->set('query', 'la') + ->assertSee('Laravel') + ->assertDontSee("fatal: nothing matches 'la'"); +}); + it('searches commits for a tapped hashtag straight away, without taking focus', function () { serveSearch(commits: [commitJson(['id' => 'c1', 'body' => 'Shipped it #ab with #laravel'])]); @@ -142,7 +182,7 @@ function searchedFor(string $kind): array ->assertSee("fatal: nothing matches 'zz'") ->assertDontSee('Most relevant commits'); - expect(searchedFor('users'))->toBe(['zz']) + expect(searchedFor('profiles'))->toBe(['zz']) ->and(searchedFor('commits'))->toBe([]); }); @@ -161,7 +201,7 @@ function searchedFor(string $kind): array serveApi(function (Request $request) { parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query); - return Http::response(['data' => [foundPerson($query['q'])]]); + return Http::response(['users' => [foundPerson($query['q'])], 'organizations' => []]); }); $screen = Native::visit('/log/search')->set('query', 'ad')->set('query', 'grace');