diff --git a/app/Devnull/BioText.php b/app/Devnull/BioText.php new file mode 100644 index 00000000..e3895c8a --- /dev/null +++ b/app/Devnull/BioText.php @@ -0,0 +1,79 @@ +> + */ + public static function paragraphs(string $text): array + { + return array_map(self::pieces(...), preg_split('/\R/u', trim($text)) ?: []); + } + + /** + * Whether the text tags anyone, and so needs badges rather than one run of text. + */ + public static function hasMentions(string $text): bool + { + return Mention::inText($text) !== []; + } + + /** + * @return list + */ + private static function pieces(string $paragraph): array + { + $pieces = []; + $position = 0; + + foreach (Mention::inText($paragraph) as $mention) { + self::words(mb_substr($paragraph, $position, $mention->offset - $position), $pieces); + $pieces[] = ['text' => $mention->username, 'mention' => $mention->username, 'spaceAfter' => false]; + $position = $mention->offset + $mention->length; + } + + self::words(mb_substr($paragraph, $position), $pieces); + + if ($pieces !== []) { + $pieces[array_key_last($pieces)]['spaceAfter'] = false; + } + + return $pieces; + } + + /** + * Adds the words of $text to $pieces. Whitespace marks the piece before it as followed by a space. + * + * @param list $pieces + */ + private static function words(string $text, array &$pieces): void + { + preg_match_all('/\S+|\s+/u', $text, $matches); + + foreach ($matches[0] as $token) { + if (trim($token) === '') { + if ($pieces !== []) { + $pieces[array_key_last($pieces)]['spaceAfter'] = true; + } + + continue; + } + + $pieces[] = ['text' => $token, 'mention' => null, 'spaceAfter' => false]; + } + } +} diff --git a/app/Devnull/Data/Profile.php b/app/Devnull/Data/Profile.php index 946ccde7..4c989d6d 100644 --- a/app/Devnull/Data/Profile.php +++ b/app/Devnull/Data/Profile.php @@ -4,6 +4,10 @@ final readonly class Profile { + /** + * @param array $bioProfiles the handles their bio and company tag that have a page on devnull, by lowercase handle + * @param array $bioProfileTypes what each of those is, by lowercase handle, when devnull says: user, organization or unclaimed + */ public function __construct( public User $user, public int $commitsCount = 0, @@ -17,13 +21,16 @@ public function __construct( public bool $isMuting = false, public ?Achievements $achievements = null, public ?int $pasteisReceived = null, + public array $bioProfiles = [], + public array $bioProfileTypes = [], ) {} /** * Reads a `GET /users/{username}` response: the user and counts in `data`, * and alongside it whether you follow or mute them, their pinned commit, * their streaks, their activity, their achievements and how many pastéis - * they have been given (null when devnull doesn't say). + * they have been given (null when devnull doesn't say), and which handles + * their bio and company tag have a page on devnull. * * @param array $json */ @@ -47,9 +54,55 @@ public static function fromArray(array $json): self isMuting: ($json['is_muting'] ?? $data['is_muting'] ?? false) === true, achievements: Achievements::fromArray($json['achievements'] ?? $data['achievements'] ?? null), pasteisReceived: is_int($received) ? max(0, $received) : null, + bioProfiles: self::handles($json['bio_profiles'] ?? $data['bio_profiles'] ?? null), + bioProfileTypes: self::handles($json['bio_profile_types'] ?? $data['bio_profile_types'] ?? null), ); } + /** + * Where a handle tagged in their bio or company opens: 'organization' or 'user' for a page on devnull the app has a + * screen for, or null for GitHub. devnull sends which handles have a page (bio_profiles) but not yet what each is, + * so a page of unknown kind opens as a user, whose screen hands over to the organization's when devnull has no + * such user (see Profile::refused()). An unclaimed developer's page has no screen in the app, so that goes to + * GitHub, as do handles with no page at all, as on the web. + */ + public function bioMentionScreen(string $handle): ?string + { + $key = mb_strtolower($handle); + + if (! isset($this->bioProfiles[$key])) { + return null; + } + + return match ($this->bioProfileTypes[$key] ?? 'user') { + 'organization' => 'organization', + 'user' => 'user', + default => null, + }; + } + + /** + * A JSON object of strings by lowercase handle. PHP sends an empty one as [] and Laravel's (object) cast as {}. + * + * @return array + */ + private static function handles(mixed $value): array + { + if (! is_array($value)) { + return []; + } + + $handles = []; + + foreach ($value as $key => $handle) { + if (is_string($key) && is_string($handle) && $handle !== '') { + $handles[mb_strtolower($key)] = $handle; + } + } + + return $handles; + } + public function withFollowing(bool $isFollowing): self { $change = $isFollowing === $this->isFollowing ? 0 : ($isFollowing ? 1 : -1); diff --git a/app/Devnull/Data/User.php b/app/Devnull/Data/User.php index 7a928363..5912abd2 100644 --- a/app/Devnull/Data/User.php +++ b/app/Devnull/Data/User.php @@ -108,6 +108,25 @@ public function handle(): string return '@'.$this->username; } + /** + * Their avatar at $pixels across, as the web's sizedAvatarUrl() asks for it. GitHub sends 460 pixels unless asked + * otherwise, and as large as the picture was uploaded when asked for more. An avatar from anywhere else is left + * as it is. + */ + public function avatarUrlAt(int $pixels): ?string + { + $parts = $this->avatarUrl === null ? false : parse_url($this->avatarUrl); + + if ($parts === false || ($parts['host'] ?? null) !== 'avatars.githubusercontent.com') { + return $this->avatarUrl; + } + + parse_str($parts['query'] ?? '', $query); + $query['s'] = (string) $pixels; + + return ($parts['scheme'] ?? 'https').'://'.$parts['host'].($parts['path'] ?? '').'?'.http_build_query($query); + } + /** * Their GitHub profile. Everyone on devnull signed in with GitHub, so it can be * worked out when the API leaves it out. diff --git a/app/NativeComponents/Organization.php b/app/NativeComponents/Organization.php index c2f3718c..eaeb77f1 100644 --- a/app/NativeComponents/Organization.php +++ b/app/NativeComponents/Organization.php @@ -12,6 +12,7 @@ use App\Devnull\SiteLink; use App\Devnull\Stash; use App\Devnull\Timeline; +use App\Icons\Lucide; use App\NativeComponents\Concerns\DrawsSkeletonFirst; use App\NativeComponents\Concerns\ExpandsThreads; use App\NativeComponents\Concerns\HoldsNewCommits; @@ -22,16 +23,18 @@ use App\NativeComponents\Concerns\TalksToDevnull; use Closure; use Illuminate\View\View; +use Native\Mobile\Edge\Layouts\Builders\NavAction; +use Native\Mobile\Edge\Layouts\Builders\NavBarOptions; use Native\Mobile\Edge\NativeComponent; use Native\Mobile\Facades\Browser; use Native\Mobile\Facades\Share; /** * A GitHub organization's page, as the web shows it at its "@handle": its - * avatar as a rounded square in its lane, Follow and Share, what GitHub says - * about it, how many follow it and its admins, then its Commits and the + * avatar as a rounded square in its lane, Follow, what GitHub says about it, + * how many follow it and its admins, then its Commits and the * commits that mention it, as pages you swipe between, as on a profile (see - * Profile). + * Profile). Share is a button of its own in the nav bar. * * It opens from a commit the organization posted, or a mention of it, beneath * the tab it was tapped in, on the page as it was kept last time, then asks @@ -115,6 +118,20 @@ public function navTitle(): string return $this->organization->organization->name ?? $this->handle(); } + /** + * Share in the nav bar, as its own button, under the share shape each phone uses (Lucide's share on iOS, share-2 + * on Android), as a profile's Share profile has it. It's always there, as the link needs nothing but the handle. + */ + public function navigationOptions(): ?NavBarOptions + { + return NavBarOptions::make()->action( + NavAction::make('share') + ->icon(ios: Lucide::Share->ios(), android: Lucide::Share2->android()) + ->a11yLabel('Share '.($this->organization?->organization->handle() ?? '@'.$this->handle())) + ->press('share'), + ); + } + /** * Pull to refresh. You're at the top, so new commits go straight in. */ @@ -185,7 +202,7 @@ public function toggleFollow(): void } /** - * The web's Share beside Follow: the system share sheet, with the organization's page on the web. + * The web's Share, from the nav bar: the system share sheet, with the organization's page on the web. */ public function share(): void { diff --git a/app/NativeComponents/Profile.php b/app/NativeComponents/Profile.php index d4c1e690..b800b854 100644 --- a/app/NativeComponents/Profile.php +++ b/app/NativeComponents/Profile.php @@ -4,6 +4,7 @@ use App\Devnull\Account; use App\Devnull\Data\Commit; +use App\Devnull\Data\CommitImage; use App\Devnull\Data\Mention; use App\Devnull\Data\Page; use App\Devnull\Data\Profile as ProfileData; @@ -103,6 +104,11 @@ class Profile extends NativeComponent */ public const int PreviewCommits = 3; + /** + * How large the profile picture is fetched for the image viewer, as the web's ProfilePicture asks for it. + */ + public const int ProfilePicturePixels = 1024; + /** * The alerts the safety menu confirms with, as the web words them. Each one's id names the profile too, so an * answer only reaches the profile that asked. @@ -604,19 +610,52 @@ public function openSafety(): void $this->openInTab('/safety'); } - public function openGitHubMention(string $username): void + /** + * An account their bio or company tags, from its badge, as the web links it: its screen here in the app when it + * has a page on devnull, an organization's included, otherwise GitHub (see ProfileData::bioMentionScreen()). Only + * a handle the bio or company does tag opens. + */ + public function openBioMention(string $username): void { $user = $this->profile?->user; foreach (Mention::inText(($user?->bio ?? '')."\n".($user?->company ?? '')) as $mention) { - if ($mention->username === $username) { - Browser::inApp('https://github.com/'.rawurlencode($username)); - - return; + if ($mention->username !== $username) { + continue; } + + match ($this->profile?->bioMentionScreen($username)) { + 'organization' => $this->openOrganization($username), + 'user' => $this->openInTab('/users/'.rawurlencode($username)), + default => Browser::inApp('https://github.com/'.rawurlencode($username)), + }; + + return; } } + /** + * Their profile picture full-screen, as the web opens it from the avatar, at the 1024 pixels the web asks GitHub + * for, in the image viewer commits use (see ShowsImageViewer), where it can be zoomed and saved. + */ + public function viewProfilePicture(): void + { + $user = $this->profile?->user; + $url = $user?->avatarUrlAt(self::ProfilePicturePixels); + + if ($user === null || $url === null) { + return; + } + + $this->showImagesInViewer([new CommitImage( + id: 'avatar-'.$user->id, + url: $url, + width: self::ProfilePicturePixels, + height: self::ProfilePicturePixels, + description: "{$user->name}'s profile picture", + )]); + } + public function openGitHubProfile(): void { $url = $this->profile?->user->gitHubProfileUrl(); diff --git a/resources/views/native/partials/bio-mention.blade.php b/resources/views/native/partials/bio-mention.blade.php new file mode 100644 index 00000000..443e17e8 --- /dev/null +++ b/resources/views/native/partials/bio-mention.blade.php @@ -0,0 +1,15 @@ +{{-- + An account tagged in a bio, as the web's bio badge: its square GitHub icon on the hairline colour (a handle GitHub + doesn't know keeps just the square), then the handle without its "@", in a rounded tag edged with a hairline on + the wash. A tap calls $press with the handle. Scaled to $size, the size of the text around it. +--}} +@php($height = round($size * 1.5)) +@php($icon = round($size * 1.15)) + + + + + + {{ $handle }} + + diff --git a/resources/views/native/partials/bio-text.blade.php b/resources/views/native/partials/bio-text.blade.php new file mode 100644 index 00000000..bbe0e9b7 --- /dev/null +++ b/resources/views/native/partials/bio-text.blade.php @@ -0,0 +1,31 @@ +{{-- + A bio or company synced from GitHub, with each account it tags as a badge, as the web's BioText draws them: the + account's square GitHub icon, then its handle, in a small tag on the wash (see bio-mention). $press names the + screen's method a badge calls with the handle. Text that tags nobody is one run of text, as before. + + EDGE can't put an image in a run of text, so text with tags is a wrapping row of words and badges per paragraph + (see App\Devnull\BioText). $size is the text's size in points, which the badges are scaled to. +--}} +@php($size ??= 15) +@php($press ??= 'openGitHubMention') +@php($maxLines ??= null) +@if (\App\Devnull\BioText::hasMentions($text)) + @php($mentionIndex = 0) + + @foreach (\App\Devnull\BioText::paragraphs($text) as $paragraph) + + @foreach ($paragraph as $piece) + @php($space = $piece['spaceAfter'] ? 'mr-['.round($size * 0.27).']' : '') + @if ($piece['mention'] !== null) + @include('native.partials.bio-mention', ['handle' => $piece['mention'], 'ref' => $ref.'-mention-'.$mentionIndex, 'size' => $size, 'press' => $press, 'space' => $space]) + @php($mentionIndex++) + @else + + @endif + @endforeach + + @endforeach + +@else + {{ $text }} +@endif diff --git a/resources/views/native/partials/github-text.blade.php b/resources/views/native/partials/github-text.blade.php deleted file mode 100644 index 54a224a6..00000000 --- a/resources/views/native/partials/github-text.blade.php +++ /dev/null @@ -1,13 +0,0 @@ -@php($position = 0) - -@foreach (\App\Devnull\Data\Mention::inText($text) as $mention) -@if ($mention->offset > $position) - -@endif - -@php($position = $mention->offset + $mention->length) -@endforeach -@if ($position < mb_strlen($text)) - -@endif - diff --git a/resources/views/native/partials/glass-pill-button.blade.php b/resources/views/native/partials/glass-pill-button.blade.php new file mode 100644 index 00000000..713ed7a8 --- /dev/null +++ b/resources/views/native/partials/glass-pill-button.blade.php @@ -0,0 +1,22 @@ +{{-- + A header's main buttons on a profile or an organization's page: Follow, Unfollow and Remote. A 40pt pill, a step up + from the small theme button they were, on Liquid Glass on iOS as the app's other floating buttons are (see + sheet-bar-button). $filled draws it in your colour, tinted through the glass on iOS, for the one thing to do next, + like Follow; otherwise it's clear glass on iOS and a tonal pill on Android. + + It's a pressable rather than a theme button, which takes no glass or per-button fill. +--}} +@php($filled ??= false) +@php($icon ??= null) +@php($hint ??= null) +@php($a11yLabel ??= $label) +@php($pill = 'rounded-full ios:glass:interactive '.($filled ? 'bg-theme-'.$myColour : 'android:bg-theme-surface-variant')) +@php($ink = $filled ? 'text-theme-on-'.$myColour : 'text-theme-ink') + + + @if ($icon) + + @endif + {{ $label }} + + diff --git a/resources/views/native/partials/organization-header.blade.php b/resources/views/native/partials/organization-header.blade.php index 295a56b4..e2de8a30 100644 --- a/resources/views/native/partials/organization-header.blade.php +++ b/resources/views/native/partials/organization-header.blade.php @@ -1,8 +1,8 @@ {{-- The organization's header, which the pager draws above its tabs (see organization.blade.php), laid out like the - web's: the avatar as a rounded square ringed in its lane, Share and Follow, the name, the handle marked - Organization, what GitHub says about it, how many follow it and its admins. It scrolls up with the tab you're on - until the tabs pin under the nav bar. + web's: the avatar as a rounded square ringed in its lane, Follow, the name, the handle marked Organization, what + GitHub says about it, how many follow it and its admins. Share is in the nav bar. It scrolls up with the tab you're + on until the tabs pin under the nav bar. --}} @php($lane = \App\Devnull\Lane::token($user->lane())) @@ -19,14 +19,11 @@ - - - @if ($organization->isFollowing) - Unfollow - @else - Follow - @endif - + @if ($organization->isFollowing) + @include('native.partials.glass-pill-button', ['ref' => 'follow', 'label' => 'Unfollow', 'press' => 'toggleFollow', 'hint' => 'Unfollows '.$user->handle()]) + @else + @include('native.partials.glass-pill-button', ['ref' => 'follow', 'label' => 'Follow', 'press' => 'toggleFollow', 'filled' => true]) + @endif @@ -41,7 +38,7 @@ @if ($user->bio) - @include('native.partials.github-text', ['text' => $user->bio, 'ref' => 'organization-bio', 'classes' => 'w-full text-[15px] leading-relaxed text-theme-ink', 'maxLines' => null]) + @include('native.partials.bio-text', ['text' => $user->bio, 'ref' => 'organization-bio', 'classes' => 'w-full text-[15px] leading-relaxed text-theme-ink']) @endif {{-- What GitHub says about it, links in Commit Blue, each on its own line, as on a profile. --}} diff --git a/resources/views/native/partials/profile-fact.blade.php b/resources/views/native/partials/profile-fact.blade.php index a012659f..71b4a7f6 100644 --- a/resources/views/native/partials/profile-fact.blade.php +++ b/resources/views/native/partials/profile-fact.blade.php @@ -1,4 +1,7 @@ -{{-- One thing GitHub says about someone, under their bio: a muted icon and text, or a link in Commit Blue when $press is set. --}} +{{-- + One thing GitHub says about someone, under their bio: a muted icon and text, or a link in Commit Blue when $press is + set. With $gitHubMentions, the accounts it tags are badges, as in the bio, which call $mentionPress (see bio-text). +--}} @php($press ??= null) @if ($press) @@ -15,7 +18,7 @@ @if ($gitHubMentions ?? false) - @include('native.partials.github-text', ['text' => $label, 'ref' => 'profile-'.$key.'-text', 'classes' => 'flex-1 text-sm text-theme-ink-muted', 'maxLines' => 1]) + @include('native.partials.bio-text', ['text' => $label, 'ref' => 'profile-'.$key.'-text', 'classes' => 'flex-1 text-sm text-theme-ink-muted', 'maxLines' => 1, 'size' => 14, 'flowClasses' => 'flex-1', 'wordClasses' => 'text-theme-ink-muted', 'press' => $mentionPress ?? 'openGitHubMention']) @else {{ $label }} @endif diff --git a/resources/views/native/partials/profile-header.blade.php b/resources/views/native/partials/profile-header.blade.php index 78c88ffc..abeac50f 100644 --- a/resources/views/native/partials/profile-header.blade.php +++ b/resources/views/native/partials/profile-header.blade.php @@ -1,6 +1,6 @@ {{-- The profile's header, which the pager draws above its tabs (see profile.blade.php): the avatar ringed in their - lane, Follow (or, on your own, Edit on GitHub), who they are, what GitHub says about them, their counts and, in + lane, which opens full-screen, Follow (or, on your own, Edit on GitHub), who they are, what GitHub says about them, their counts and, in pastéis mode, the pastéis they've been given, then their activity, streaks and achievements. It scrolls up with the tab you're on until the tabs pin under the nav bar. --}} @@ -22,6 +22,15 @@ @include('native.partials.live-badge', ['size' => 'lg']) + @elseif ($user->avatarUrl) + {{-- A tap shows the picture full-screen, as the web's does, where it can be zoomed and saved. --}} + + + + @include('native.partials.avatar', ['user' => $user, 'size' => 80]) + + + @else @@ -39,12 +48,12 @@ {{-- The web's Remote, beside Follow, when you can push to them. --}} @if ($canRemote) - Remote + @include('native.partials.glass-pill-button', ['ref' => 'remote', 'label' => 'Remote', 'icon' => \App\Icons\Lucide::MessagesSquare, 'press' => 'openRemote', 'a11yLabel' => 'Remote '.$user->handle()]) @endif @if ($profile->isFollowing) - Unfollow + @include('native.partials.glass-pill-button', ['ref' => 'follow', 'label' => 'Unfollow', 'press' => 'toggleFollow', 'hint' => 'Unfollows '.$user->handle()]) @else - Follow + @include('native.partials.glass-pill-button', ['ref' => 'follow', 'label' => 'Follow', 'press' => 'toggleFollow', 'filled' => true]) @endif @endif @@ -78,7 +87,7 @@ @if ($user->bio) - @include('native.partials.github-text', ['text' => $user->bio, 'ref' => 'profile-bio', 'classes' => 'w-full text-[15px] leading-relaxed text-theme-ink', 'maxLines' => null]) + @include('native.partials.bio-text', ['text' => $user->bio, 'ref' => 'profile-bio', 'classes' => 'w-full text-[15px] leading-relaxed text-theme-ink', 'press' => 'openBioMention']) @endif {{-- @@ -87,7 +96,7 @@ --}} @if ($user->company) - @include('native.partials.profile-fact', ['key' => 'company', 'icon' => \App\Icons\Lucide::Building2, 'label' => $user->company, 'gitHubMentions' => true]) + @include('native.partials.profile-fact', ['key' => 'company', 'icon' => \App\Icons\Lucide::Building2, 'label' => $user->company, 'gitHubMentions' => true, 'mentionPress' => 'openBioMention']) @endif @if ($user->location) @include('native.partials.profile-fact', ['key' => 'location', 'icon' => \App\Icons\Lucide::MapPin, 'label' => $user->location]) diff --git a/tests/Feature/Screens/LaneButtonsTest.php b/tests/Feature/Screens/LaneButtonsTest.php index 3e966d04..29fa415b 100644 --- a/tests/Feature/Screens/LaneButtonsTest.php +++ b/tests/Feature/Screens/LaneButtonsTest.php @@ -59,14 +59,6 @@ function laneLog(): TestableComponent return Native::visit('/log'); }, 'follow-ada'], - 'Follow, on a profile' => [function (): TestableComponent { - serveApi([ - 'GET /api/v1/users/ada' => [['data' => userWithCountsJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada']), 'is_following' => false]], - 'GET /api/v1/users/ada/commits' => [pageJson([])], - ]); - - return Native::visit('/log/users/ada'); - }, 'follow'], 'Follow, in a profile\'s followers' => [function (): TestableComponent { serveApi(['GET /api/v1/users/ada/followers' => [pageJson([userJson(['id' => 'u3', 'handle' => 'grace', 'name' => 'Grace', 'is_followed' => false, 'follows_you' => false])])]]); @@ -193,3 +185,27 @@ function laneLog(): TestableComponent expect(buttonProps(Native::visit('/profile/device', platform: 'ios'), 'unlink-apple')['size'] ?? null)->toBe('lg'); }); + +it('fills Follow on a profile and an organization with your lane, with a label that reads on it', function (string $path, array $api, int $lane) { + signIn(['lane' => $lane]); + serveApi($api); + + $screen = finishRefreshes(Native::visit($path)); + $pill = nodeByRef($screen, 'follow-pill'); + $label = findNode($pill, fn (array $node): bool => ($node['type'] ?? null) === 'text'); + + expect(strtolower($pill['style']['bg_color']))->toBe(strtolower(config("native-ui.theme.light.lane-{$lane}"))) + ->and(strtolower($label['props']['color']))->toBe(strtolower(config("native-ui.theme.light.on-lane-{$lane}"))); +})->with([ + 'a profile' => ['/log/users/ada', [ + 'GET /api/v1/users/ada' => [['data' => userWithCountsJson(['id' => 'u2', 'handle' => 'ada', 'name' => 'Ada']), 'is_following' => false]], + 'GET /api/v1/users/ada/commits' => [pageJson([])], + ]], + 'an organization' => ['/log/organizations/laravel', [ + 'GET /api/v1/organizations/laravel' => [['data' => ['handle' => 'laravel', 'name' => 'Laravel', 'type' => 'organization', 'followers_count' => 1], 'is_following' => false]], + 'GET /api/v1/organizations/laravel/commits' => [pageJson([])], + ]], +])->with([ + 'green, labelled in ink' => 1, + 'rose, labelled in white' => 4, +]); diff --git a/tests/Feature/Screens/OrganizationTest.php b/tests/Feature/Screens/OrganizationTest.php index 2a8fc540..f118bd1f 100644 --- a/tests/Feature/Screens/OrganizationTest.php +++ b/tests/Feature/Screens/OrganizationTest.php @@ -141,14 +141,20 @@ function tapOrganizationTab(TestableComponent $screen, string $tab): TestableCom ->and($screen->instance()->organization->followersCount)->toBe(45); }); -it('shares the organization page and opens its admins', function () { +it('shares the organization page from its own button in the nav bar, and opens its admins', function () { fakeApi([ 'GET /api/v1/organizations/laravel' => [organizationJson()], 'GET /api/v1/organizations/laravel/commits' => [pageJson([])], ]); - finishRefreshes(Native::visit('/log/organizations/laravel')) - ->tap('share') + $screen = finishRefreshes(Native::visit('/log/organizations/laravel', platform: 'ios')); + $actions = findNodes($screen->tree(), fn (array $node): bool => ($node['type'] ?? null) === 'top_bar_action'); + + expect(array_map(fn (array $node): string => $node['props']['id'], $actions))->toBe(['share']) + ->and($actions[0]['props'])->toMatchArray(['icon' => 'custom:share', 'a11y_label' => 'Share @laravel']) + ->and(findNode($screen->tree(), fn (array $node): bool => ($node['ref'] ?? null) === 'share'))->toBeNull(); + + $screen->press('share') ->assertNativeCalled('Share.Url', fn (array $params): bool => $params['url'] === 'http://devnull.test/@laravel') ->tap('admin-nunomaduro') ->assertNavigatedTo('/log/users/nunomaduro'); diff --git a/tests/Feature/Screens/ProfileCacheTest.php b/tests/Feature/Screens/ProfileCacheTest.php index 9820272e..9f1bed47 100644 --- a/tests/Feature/Screens/ProfileCacheTest.php +++ b/tests/Feature/Screens/ProfileCacheTest.php @@ -76,7 +76,7 @@ function serveAda(array $commits, int $followers = 5): void ->assertSee('@ada') ->assertSee('Wrote the first program.') ->assertMissingElement('row', fn (array $node): bool => ($node['ref'] ?? null) === 'counts') - ->assertMissingElement('button', fn (array $node): bool => ($node['ref'] ?? null) === 'follow') + ->assertMissingElement('pressable', fn (array $node): bool => ($node['ref'] ?? null) === 'follow') ->assertElement('row', fn (array $node): bool => ($node['ref'] ?? null) === 'loading-1') ->assertMissingElement('column', fn (array $node): bool => ($node['ref'] ?? null) === 'loading-profile'); @@ -84,7 +84,7 @@ function serveAda(array $commits, int $followers = 5): void finishRefreshes($profile) ->assertElement('row', fn (array $node): bool => ($node['ref'] ?? null) === 'counts') - ->assertElement('button', fn (array $node): bool => ($node['ref'] ?? null) === 'follow') + ->assertElement('pressable', fn (array $node): bool => ($node['ref'] ?? null) === 'follow') ->assertSee('Analytical engine notes'); expect(showsLoading(showingProfile($profile)))->toBeFalse(); diff --git a/tests/Feature/Screens/ProfileStandingTest.php b/tests/Feature/Screens/ProfileStandingTest.php index f09af3f0..ed0312af 100644 --- a/tests/Feature/Screens/ProfileStandingTest.php +++ b/tests/Feature/Screens/ProfileStandingTest.php @@ -98,23 +98,54 @@ function standingJson(array $extra = []): array expect(childRefs($screen, 'sheet-badges'))->toBe(['sheet-badge-init', 'sheet-badge-stdout', 'sheet-badge-max_payload']); })->with(['earned' => [true, '+1 more'], 'none earned' => [false, 'View achievements']]); -it('opens tagged bio and company accounts on GitHub rather than as devnull users', function (string $ref, string $url) { +it('draws the accounts a bio and company tag as badges with their GitHub icon, words between them', function () { $json = standingJson(); - $json['data']['bio'] = 'Builds @laravel with @nunomaduro.'; + $json['data']['bio'] = "Builds @laravel, with @nunomaduro.\nTea first."; $json['data']['company'] = '@nativephp'; fakeApi(['GET /api/v1/users/ada' => [$json], 'GET /api/v1/users/ada/commits' => [pageJson([])]]); - Native::visit('/log/users/ada')->tap($ref) - ->assertNativeCalled('Browser.OpenInApp', fn (array $params): bool => $params['url'] === $url) - ->assertNoNavigation(); + $screen = Native::visit('/log/users/ada'); + $badge = nodeByRef($screen, 'profile-bio-mention-0'); + $icon = findNode($badge, fn (array $node): bool => ($node['type'] ?? null) === 'image'); + $texts = fn (string $ref): array => array_values(array_filter(array_map( + fn (array $node): ?string => $node['props']['text'] ?? null, + findNodes(nodeByRef($screen, $ref), fn (array $node): bool => ($node['type'] ?? null) === 'text'), + ))); + + expect($icon['props']['src'] ?? $icon['props']['url'] ?? null)->toContain('https://github.com/laravel.png') + ->and($texts('profile-bio'))->toBe(['Builds', 'laravel', ',', 'with', 'nunomaduro', '.', 'Tea', 'first.']) + ->and(nodeByRef($screen, 'profile-company-text-mention-0'))->not->toBeNull(); +}); + +it('opens a tagged account that has a page on devnull here in the app, and anything else on GitHub, as the web links them', function (array $types, string $ref, ?string $screenPath, ?string $url) { + $json = standingJson([ + 'bio_profiles' => ['laravel' => 'laravel', 'nunomaduro' => 'nunomaduro', 'taylor' => 'taylor'], + ...($types === [] ? [] : ['bio_profile_types' => $types]), + ]); + $json['data']['bio'] = 'Builds @Laravel with @nunomaduro, @taylor and @nativephp.'; + fakeApi(['GET /api/v1/users/ada' => [$json], 'GET /api/v1/users/ada/commits' => [pageJson([])]]); + + $screen = Native::visit('/log/users/ada')->tap($ref); + + if ($screenPath !== null) { + $screen->assertNavigatedTo($screenPath)->assertNativeNotCalled('Browser.OpenInApp'); + } else { + $screen->assertNoNavigation()->assertNativeCalled('Browser.OpenInApp', fn (array $params): bool => $params['url'] === $url); + } })->with([ - 'bio' => ['profile-bio-mention-7', 'https://github.com/laravel'], - 'company' => ['profile-company-text-mention-0', 'https://github.com/nativephp'], + 'a page of unknown kind opens as a user' => [[], 'profile-bio-mention-0', '/log/users/Laravel', null], + 'an organization' => [['laravel' => 'organization'], 'profile-bio-mention-0', '/log/organizations/Laravel', null], + 'a user' => [['nunomaduro' => 'user'], 'profile-bio-mention-1', '/log/users/nunomaduro', null], + 'an unclaimed developer' => [['taylor' => 'unclaimed'], 'profile-bio-mention-2', null, 'https://github.com/taylor'], + 'no page on devnull' => [[], 'profile-bio-mention-3', null, 'https://github.com/nativephp'], ]); -it('ignores profile mention actions for accounts that are not in the bio or company', function () { - fakeApi(['GET /api/v1/users/ada' => [standingJson()], 'GET /api/v1/users/ada/commits' => [pageJson([])]]); - Native::visit('/log/users/ada')->call('openGitHubMention', 'nobody')->assertNativeNotCalled('Browser.OpenInApp'); +it('ignores tag taps for accounts that are not in the bio or company', function () { + fakeApi(['GET /api/v1/users/ada' => [standingJson(['bio_profiles' => ['nobody' => 'nobody']])], 'GET /api/v1/users/ada/commits' => [pageJson([])]]); + + Native::visit('/log/users/ada')->call('openBioMention', 'nobody') + ->assertNativeNotCalled('Browser.OpenInApp') + ->assertNoNavigation(); }); it('marks a profile you mute, as the web does', function () { diff --git a/tests/Feature/Screens/ProfileTest.php b/tests/Feature/Screens/ProfileTest.php index eea451e5..87f8ab45 100644 --- a/tests/Feature/Screens/ProfileTest.php +++ b/tests/Feature/Screens/ProfileTest.php @@ -1,6 +1,8 @@ [fn (): array => [profileJson()], fn (): array => []], "devnull couldn't send it yet" => [fn (): array => [['message' => 'Server Error'], 500], fn (): array => ['user' => User::fromArray(userJson(['handle' => 'ada', 'name' => 'Ada Lovelace']))]], ]); + +it('opens the profile picture full-screen from the avatar, at the 1024 pixels the web asks GitHub for', function () { + fakeApi([ + 'GET /api/v1/users/ada' => [profileJson(user: ['avatar_url' => 'https://avatars.githubusercontent.com/u/42?v=4'])], + 'GET /api/v1/users/ada/commits' => [pageJson([])], + ]); + + $screen = Native::visit('/log/users/ada')->tap('profile-picture'); + + expect(nodeByRef($screen, 'image-viewer')['props']['visible'])->toBeTrue() + ->and(nodeByRef($screen, 'viewed-image-0')['props'])->toMatchArray([ + 'src' => 'https://avatars.githubusercontent.com/u/42?v=4&s=1024', + 'alt' => "Ada Lovelace's profile picture", + ]); + + $screen->assertNativeNotCalled('Browser.OpenInApp'); +}); + +it('leaves an avatar that is not on GitHub as it is, and a profile with none has nothing to open', function () { + fakeApi([ + 'GET /api/v1/users/ada' => [profileJson(user: ['avatar_url' => 'https://img.test/ada.png'])], + 'GET /api/v1/users/ada/commits' => [pageJson([])], + 'GET /api/v1/users/bob' => [profileJson(user: ['id' => 'u3', 'handle' => 'bob', 'avatar_url' => null])], + 'GET /api/v1/users/bob/commits' => [pageJson([])], + ]); + + $screen = Native::visit('/log/users/ada')->tap('profile-picture'); + + expect(nodeByRef($screen, 'viewed-image-0')['props']['src'])->toBe('https://img.test/ada.png'); + + $bob = Native::visit('/log/users/bob'); + + expect(findNode($bob->tree(), fn (array $node): bool => ($node['ref'] ?? null) === 'profile-picture'))->toBeNull(); + expect($bob->call('viewProfilePicture')->instance()->viewerOpen)->toBeFalse(); +}); + +it('draws Follow in your colour on glass on iOS, and Unfollow on clear glass, both 40pt pills', function (string $platform, bool $glass) { + fakeApi([ + 'GET /api/v1/users/ada' => [profileJson()], + 'GET /api/v1/users/ada/commits' => [pageJson([])], + 'POST /api/v1/users/ada/follow' => [null, 204], + ]); + + $screen = Native::visit('/log/users/ada', platform: $platform); + $pill = nodeByRef($screen, 'follow-pill'); + $mine = config('native-ui.theme.light.'.Lane::token(app(Account::class)->user()?->lane())); + + expect((float) $pill['layout']['height'])->toBe(40.0) + ->and($pill['style']['bg_color'] ?? null)->toBe($mine) + ->and(($pill['props']['glass'] ?? 0) !== 0)->toBe($glass); + + $pill = nodeByRef($screen->tap('follow'), 'follow-pill'); + + expect(($pill['props']['glass'] ?? 0) !== 0)->toBe($glass) + ->and($pill['style']['bg_color'] ?? null)->toBe($glass ? null : config('native-ui.theme.light.surface-variant')); +})->with(['iOS' => ['ios', true], 'Android' => ['android', false]]);